shadowbrain / scripts /tools /gdrive_sync.py
taemin1980's picture
๐Ÿ”ฑ Imperial Deployment: Shadow Brain Core ignition
349a7de verified
Raw
History Blame Contribute Delete
3 kB
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.")