#!/usr/bin/env python3 """Guarded PHM-Vibench publisher for Hugging Face and ModelScope. Default usage is a no-network plan. The execute command requires explicit confirmation and strips proxy/mirror environment variables before importing SDKs. """ from __future__ import annotations import argparse import os import sys from pathlib import Path from phm_vibench_manifest import ( ARCHIVED_FILES, FILE_SIZES, HF_ENDPOINT, HF_REVISION, LOCAL_ONLY_PATHS, MODELSCOPE_ENDPOINT, MODELSCOPE_REVISION, PLATFORM_TARGET_CHOICES, PUBLISHED_FILES, REPO_ID, enforce_no_network_redirects, expand_platform_targets, format_bytes, total_size, ) REPO_ROOT = Path(__file__).resolve().parents[1] CONFIRM_TOKEN = "NO_PROXY_UPLOAD" def selected_files(args: argparse.Namespace) -> list[str]: files = args.file or PUBLISHED_FILES unknown = [path for path in files if path not in PUBLISHED_FILES] if unknown: raise SystemExit("Refusing files outside published manifest: " + ", ".join(unknown)) return files def local_validate(files: list[str]) -> None: for rel in files: path = REPO_ROOT / rel if not path.exists(): raise SystemExit(f"Missing published file: {rel}") actual = path.stat().st_size expected = FILE_SIZES[rel] if actual != expected: raise SystemExit(f"Size mismatch for {rel}: actual={actual} expected={expected}") def apply_proxy_guard(targets: list[str]) -> None: """Per-platform proxy policy for the execute path (see enforce_network_policy). If Hugging Face is a target, PRESERVE the user's proxy/HF_ENDPOINT (HF is often only reachable through a proxy in some regions); ModelScope, if also targeted, will use the proxy too (still functional). If only ModelScope is targeted, strip the proxy so the domestic endpoint is reached directly. """ if "hf" in targets: proxied = [k for k in ("HTTPS_PROXY", "HTTP_PROXY", "ALL_PROXY") if os.environ.get(k)] if proxied: print("HF target: preserving proxy env (%s) — HF often requires a proxy" % ", ".join(proxied)) else: print("HF target: no proxy env set (direct)") if os.environ.get("HF_ENDPOINT"): print("HF target: respecting HF_ENDPOINT=%s" % os.environ["HF_ENDPOINT"]) return removed = enforce_no_network_redirects() if removed: print("Disabled network redirect env vars:", ", ".join(removed)) os.environ["NO_PROXY"] = "*" os.environ["no_proxy"] = "*" def print_plan(files: list[str], targets: list[str], *, delete_archived: bool) -> None: print("plan only: no network request, upload, download, or delete is performed") print("repo:", REPO_ID) print("files:", len(files), format_bytes(total_size(files))) for target in targets: revision = HF_REVISION if target == "hf" else MODELSCOPE_REVISION endpoint = HF_ENDPOINT if target == "hf" else MODELSCOPE_ENDPOINT print(f"[{target}] revision={revision} endpoint={endpoint}") for rel in files: print(f" upload {rel:<36} {format_bytes(FILE_SIZES[rel])}") if delete_archived: print(" delete archived:", ", ".join(ARCHIVED_FILES)) print(" never publish:", ", ".join(LOCAL_ONLY_PATHS)) def upload_hf(files: list[str], *, commit_prefix: str, delete_archived: bool) -> None: token = os.environ.get("HF_TOKEN") if not token: raise SystemExit("HF_TOKEN is required for Hugging Face execute") # HF preserves the user's proxy/HF_ENDPOINT (see apply_proxy_guard); do NOT force no-proxy. from huggingface_hub import HfApi endpoint = os.environ.get("HF_ENDPOINT") or HF_ENDPOINT api = HfApi(endpoint=endpoint, token=token) for rel in files: api.upload_file( path_or_fileobj=str(REPO_ROOT / rel), path_in_repo=rel, repo_id=REPO_ID, repo_type="dataset", revision=HF_REVISION, commit_message=f"{commit_prefix}: upload {rel}", ) print("HF uploaded", rel) if delete_archived: for rel in ARCHIVED_FILES: api.delete_file( path_in_repo=rel, repo_id=REPO_ID, repo_type="dataset", revision=HF_REVISION, commit_message=f"{commit_prefix}: remove legacy {rel}", ) print("HF removed legacy", rel) def upload_modelscope(files: list[str], *, commit_prefix: str, delete_archived: bool) -> None: from modelscope.hub.api import HubApi api = HubApi() token = os.environ.get("MODELSCOPE_API_TOKEN") if token: api.login(token) for rel in files: api.upload_file( path_or_fileobj=str(REPO_ROOT / rel), path_in_repo=rel, repo_id=REPO_ID, repo_type="dataset", commit_message=f"{commit_prefix}: upload {rel}", ) print("ModelScope uploaded", rel) if delete_archived: for rel in ARCHIVED_FILES: api.delete_oss_dataset_object( object_name=rel, dataset_name="PHM-Vibench", namespace="PHMbench", revision=MODELSCOPE_REVISION, ) print("ModelScope removed legacy", rel) def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description=__doc__) sub = parser.add_subparsers(dest="cmd", required=True) plan = sub.add_parser("plan", help="Print publish plan without network access") plan.add_argument("--platform", choices=PLATFORM_TARGET_CHOICES, default="all") plan.add_argument("--file", action="append", help="Published file path to include; may repeat") plan.add_argument("--skip-delete-archived", action="store_true") execute = sub.add_parser("execute", help="Upload/delete after explicit no-proxy confirmation") execute.add_argument("--platform", choices=PLATFORM_TARGET_CHOICES, required=True) execute.add_argument("--file", action="append", help="Published file path to upload; may repeat") execute.add_argument("--skip-delete-archived", action="store_true") execute.add_argument("--yes", action="store_true", help="Required for any network write") execute.add_argument("--confirm", help=f"Must be exactly {CONFIRM_TOKEN}") execute.add_argument("--commit-prefix", default="PHM-Vibench sync") return parser.parse_args() def main() -> int: args = parse_args() files = selected_files(args) local_validate(files) targets = expand_platform_targets(args.platform) delete_archived = not args.skip_delete_archived if args.cmd == "plan": print_plan(files, targets, delete_archived=delete_archived) return 0 if not args.yes or args.confirm != CONFIRM_TOKEN: raise SystemExit(f"execute requires --yes --confirm {CONFIRM_TOKEN}") apply_proxy_guard(targets) print_plan(files, targets, delete_archived=delete_archived) print("execute: starting network writes") for target in targets: if target == "hf": upload_hf(files, commit_prefix=args.commit_prefix, delete_archived=delete_archived) else: upload_modelscope(files, commit_prefix=args.commit_prefix, delete_archived=delete_archived) return 0 if __name__ == "__main__": sys.exit(main())