"""Upload the Docker Space from the lobby PC through HTTP(S)_PROXY. Do not save the HF access token into any project files. """ from __future__ import annotations import json import os from pathlib import Path import secrets import time from huggingface_hub import HfApi root = Path(__file__).resolve().parents[1] token = os.getenv("HF_TOKEN","").strip() if not token: raise SystemExit("HF_TOKEN environment variable is required.") api = HfApi(token=token) username = api.whoami()["name"] repo = os.getenv("HF_SPACE_REPO",f"{username}/ahp-development-workstation") if not repo.startswith(username + "/"): raise SystemExit("Account mismatch. Refusing to upload to an unrelated account.") # Keep credentials OUTSIDE the Space folder so re-upload cannot leak them. creds_path = root.parent / "HF-AhpDevSpace-20261012-credentials.txt" creds = {} if creds_path.is_file(): for line in creds_path.read_text(encoding="utf-8").splitlines(): if ": " in line: k,v = line.split(": ",1) creds[k] = v ide_pass = os.getenv("HF_IDE_PASSWORD") or creds.get("IDE password") or secrets.token_urlsafe(30) mcp_key = os.getenv("HF_MCP_KEY") or creds.get("MCP API Key") or secrets.token_urlsafe(40) creds_path.write_text("Space: https://huggingface.co/spaces/" + repo + "\nIDE password: " + ide_pass + "\nMCP API Key: " + mcp_key + "\n",encoding="utf-8") print("Account verified:",username,flush=True) print("Creating Docker Space repository:",repo,flush=True) api.create_repo(repo_id=repo,repo_type="space",space_sdk="docker",private=False,exist_ok=True) api.add_space_secret(repo,"CODE_SERVER_PASSWORD",ide_pass) api.add_space_secret(repo,"MCP_API_KEY",mcp_key) print("Space secrets configured; credentials stored on lobby PC outside project directory.",flush=True) info=api.upload_folder( repo_id=repo,repo_type="space",folder_path=str(root), commit_message="Install complete Linux VS Code + AHP MCP development workstation 2026-10-12", ignore_patterns=["*.log","*.pdb","__pycache__/*","*.pyc","*.bak","*.tmp"]) print("PUBLISHED",f"https://huggingface.co/spaces/{repo}",flush=True) print("COMMIT",str(info),flush=True) try: state=api.get_space_runtime(repo) print("SPACE_STAGE",state.stage,flush=True) except Exception as exc: print("SPACE_STAGE_PENDING",type(exc).__name__,str(exc)[:240],flush=True)