#!/usr/bin/env python3 """Offline HF handoff packaging. Default is a read-only plan; never uploads.""" from __future__ import annotations import argparse import concurrent.futures import fcntl import hashlib import io import json import os from pathlib import Path import re import shutil import sqlite3 import subprocess import tarfile import time ROOT = Path(__file__).resolve().parents[2] PACKAGES = ( "hfd_audio_command_continuous_office_v1", "hfd_control_commands_v1", "hfd_backchannel_commands_v2_short", "hfd_hesitation_pause_v1", "hfd_hesitation_pause_v2", ) THIRD_PARTY_PACKAGE = "hfd_third_party_multi_v5" SKIP_DIRS = {".git", ".venv-psi", "__pycache__", ".pytest_cache", ".cache", ".progress", "logs", "gt_review", "runs", "checkpoints"} CODE_DIRS = ("src", "scripts", "tests", "assets", "backup") CODE_FILES = ("README.md", "pyproject.toml", "uv.lock", "posttrain-full-duplex-audio-command.sh", "infer_render_auto.sh") def files_under(path): """Never follow a link into another dataset, environment or private tree.""" path = Path(path) if path.is_symlink(): raise ValueError(f"Refusing symlink: {path}") if path.is_file(): yield path return if not path.is_dir(): raise FileNotFoundError(path) for directory, dirs, names in os.walk(path, followlinks=False): dirs[:] = sorted(d for d in dirs if d not in SKIP_DIRS) for name in [*dirs, *names]: item = Path(directory) / name if item.is_symlink(): raise ValueError(f"Refusing symlink: {item}") for name in sorted(names): if (name.startswith(".env") or name in {"id_rsa", "id_ed25519"} or name.endswith((".pyc", ".pyo", ".key", ".pem"))): continue yield Path(directory) / name def code_entries(root): roots = [root / p for p in (*CODE_DIRS, *CODE_FILES)] roots.append(root / "doc/hf_audio_motion") for base in roots: for path in files_under(base): if path.suffix.lower() in {'.pt', '.pth', '.safetensors', '.wav', '.mp4', '.pkl', '.sqlite', '.bin'}: continue yield path, "HFD/" + path.relative_to(root).as_posix() def digest(path): h = hashlib.sha256() with path.open("rb") as handle: for block in iter(lambda: handle.read(8 * 1024 * 1024), b""): h.update(block) return h.hexdigest() def safe_code_bytes(path): """Remove historical commented tokens in export only; reject executable secrets.""" value = path.read_bytes() if re.search(rb'-----BEGIN (?:RSA |OPENSSH |EC )?PRIVATE KEY-----', value): raise ValueError(f'Private key detected; not exporting {path}') token = re.compile(rb'(?