Spaces:
Running
Running
| import os | |
| import threading | |
| import subprocess | |
| import traceback | |
| from datetime import datetime | |
| from rclone_installer import check_and_install_rclone, RCLONE_EXE | |
| # Rclone configuration | |
| REMOTE_NAME = "gdrive" | |
| REMOTE_FOLDER = "Guardians_Workspace/Aizen_Forge" | |
| def _sync_task(filename: str, content: str): | |
| """ | |
| Background worker thread to save the file locally and then sync it to Google Drive using Rclone. | |
| """ | |
| temp_path = None | |
| try: | |
| # 1. Rclone ์ค์น/๊ฒฝ๋ก ํ์ธ | |
| if not os.path.exists(RCLONE_EXE): | |
| return | |
| # 2. ์์ ํ์ผ ์์ฑ (rclone์ ํ์ผ ๋จ์ ์ ์ก์ด ๊ธฐ๋ณธ) | |
| temp_dir = os.path.join(os.path.expanduser("~"), ".jarvis_rclone_temp") | |
| os.makedirs(temp_dir, exist_ok=True) | |
| temp_path = os.path.join(temp_dir, filename) | |
| with open(temp_path, "w", encoding="utf-8") as f: | |
| f.write(content) | |
| # 3. Rclone copyto ์คํ (gdrive:ํด๋/ํ์ผ๋ช ) | |
| # copyto๋ ๋์ ํ์ผ์ด ์์ผ๋ฉด ๋ฎ์ด์์๋๋ค (n8n์ฒ๋ผ ์ค๋ณต ์์ฑ ์ ๋จ) | |
| remote_path = f"{REMOTE_NAME}:{REMOTE_FOLDER}/{filename}" | |
| cmd = [RCLONE_EXE, "copyto", temp_path, remote_path, "--quiet"] | |
| # Subprocess ์คํ (๋น๋๊ธฐ ์ค๋ ๋ ๋ด๋ถ์ด๋ฏ๋ก wait ํด๋ ๋ฉ์ธ ์๋ฒ๋ ์ ๋ฉ์ถค) | |
| result = subprocess.run(cmd, capture_output=True, text=True, encoding='utf-8', errors='replace') | |
| if result.returncode == 0: | |
| print(f"[{datetime.now().strftime('%H:%M:%S')}] โ๏ธ [Rclone] '{filename}' ๋๊ธฐํ ์ฑ๊ณต! -> Google Drive") | |
| else: | |
| print(f"[{datetime.now().strftime('%H:%M:%S')}] โ ๏ธ [Rclone] '{filename}' ๋๊ธฐํ ์คํจ: {result.stderr}") | |
| except Exception as e: | |
| print(f"[{datetime.now().strftime('%H:%M:%S')}] โ [Rclone] '{filename}' ๋๊ธฐํ ์ค ์๋ฌ: {e}") | |
| finally: | |
| # ์์ ํ์ผ ์ญ์ | |
| if temp_path and os.path.exists(temp_path): | |
| try: | |
| os.remove(temp_path) | |
| except: | |
| pass | |
| def sync_to_gdrive(filename: str, content: str): | |
| """ | |
| Spawns a background thread to sync file content to Google Drive via Rclone. | |
| """ | |
| # Exclude files that do not need to be synced | |
| ignored_prefixes = ["JARVIS_TERMINAL", "JARVISRUN_STATUS"] | |
| for prefix in ignored_prefixes: | |
| if filename.startswith(prefix): | |
| return | |
| thread = threading.Thread(target=_sync_task, args=(filename, content)) | |
| thread.daemon = True | |
| thread.start() | |
| if __name__ == "__main__": | |
| # Test execution | |
| print("๐ Testing Rclone Google Drive Sync Module...") | |
| check_and_install_rclone() | |
| test_content = f"# Rclone Test\nLast updated: {datetime.now()}" | |
| sync_to_gdrive("rclone_test.md", test_content) | |
| import time | |
| time.sleep(5) # ์ค๋ ๋ ์๋ฃ ๋๊ธฐ | |
| print("Test finished.") | |