#!/usr/bin/env python3 """ Syncs BedWars1058 shop.db to/from a HuggingFace Dataset repo. Usage: python3 sync_db.py download # Pull shop.db from HF python3 sync_db.py upload # Push shop.db to HF """ import sys import os import shutil from pathlib import Path from huggingface_hub import HfApi, hf_hub_download HF_REPO_ID = "eaglercraft/db" HF_REPO_TYPE = "dataset" HF_TOKEN = os.environ.get("HF_TOKEN", "") DB_LOCAL_PATH = "/opt/server/backend/plugins/BedWars1058/Cache/shop.db" DB_REPO_PATH = "shop.db" def download(): """Download shop.db from HuggingFace repo to local path.""" if not HF_TOKEN: print("[sync_db] WARNING: HF_TOKEN not set, skipping download") return False try: api = HfApi(token=HF_TOKEN) try: repo_files = api.list_repo_files( repo_id=HF_REPO_ID, repo_type=HF_REPO_TYPE, token=HF_TOKEN ) if DB_REPO_PATH not in repo_files: print(f"[sync_db] {DB_REPO_PATH} not found in repo, starting fresh") return False except Exception as e: print(f"[sync_db] Could not list repo files: {e}") return False downloaded_path = hf_hub_download( repo_id=HF_REPO_ID, filename=DB_REPO_PATH, repo_type=HF_REPO_TYPE, token=HF_TOKEN, local_dir="/tmp/hf_download" ) os.makedirs(os.path.dirname(DB_LOCAL_PATH), exist_ok=True) shutil.copy2(downloaded_path, DB_LOCAL_PATH) file_size = os.path.getsize(DB_LOCAL_PATH) print(f"[sync_db] Downloaded shop.db ({file_size} bytes)") return True except Exception as e: print(f"[sync_db] Download failed: {e}") return False def upload(): """Upload local shop.db to HuggingFace repo.""" if not HF_TOKEN: print("[sync_db] WARNING: HF_TOKEN not set, skipping upload") return False if not os.path.exists(DB_LOCAL_PATH): print(f"[sync_db] {DB_LOCAL_PATH} does not exist, skipping upload") return False try: tmp_copy = "/tmp/shop_upload.db" shutil.copy2(DB_LOCAL_PATH, tmp_copy) api = HfApi(token=HF_TOKEN) try: api.create_repo( repo_id=HF_REPO_ID, repo_type=HF_REPO_TYPE, exist_ok=True, private=True ) except Exception: pass file_size = os.path.getsize(tmp_copy) api.upload_file( path_or_fileobj=tmp_copy, path_in_repo=DB_REPO_PATH, repo_id=HF_REPO_ID, repo_type=HF_REPO_TYPE, token=HF_TOKEN, commit_message=f"Sync shop.db ({file_size} bytes)" ) os.remove(tmp_copy) print(f"[sync_db] Uploaded shop.db ({file_size} bytes)") return True except Exception as e: print(f"[sync_db] Upload failed: {e}") return False if __name__ == "__main__": if len(sys.argv) < 2: print("Usage: python3 sync_db.py [download|upload]") sys.exit(1) action = sys.argv[1].lower() if action == "download": download() elif action == "upload": upload() else: print(f"Unknown action: {action}") sys.exit(1)