gst-code-collection / tools /background_evostream_migration.py
SitongGong's picture
merge code dirs 1031 (part 28)
0e2032e verified
Raw History Blame Contribute Delete
2.45 kB
#!/usr/bin/env python3
"""Finish the EvoStream sanitized text migration independently of Codex."""
from __future__ import annotations
import logging
import re
import subprocess
import sys
from pathlib import Path
from huggingface_hub import HfApi
ROOT = Path("/mnt/tokyo-ai/gst")
SOURCE = ROOT / "EvoStream"
STAGE = Path("/tmp/EvoStream-public")
LOG = ROOT / "migration_evostream.log"
def configure_logging() -> None:
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(message)s",
handlers=[logging.FileHandler(LOG, encoding="utf-8"), logging.StreamHandler()],
)
def verify_snapshot() -> None:
patterns = {
"secret": re.compile(
r"hf_[A-Za-z0-9]{20,}|sk-[A-Za-z0-9_-]{20,}|AKIA[0-9A-Z]{16}|"
r"AIza[0-9A-Za-z_-]{30,}|-----BEGIN [A-Z ]*PRIVATE KEY-----"
),
"email": re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}"),
"name": re.compile(r"\bSitong[ _.-]*Gong\b", re.I),
}
checked = 0
for path in STAGE.rglob("*"):
if not path.is_file() or ".cache" in path.parts:
continue
try:
text = path.read_text("utf-8")
except (OSError, UnicodeDecodeError):
continue
checked += 1
for label, pattern in patterns.items():
if pattern.search(text):
raise RuntimeError(f"Residual {label} pattern in {path.relative_to(STAGE)}")
logging.info("Verification passed for %d files", checked)
def main() -> None:
configure_logging()
logging.info("Starting persistent EvoStream migration")
subprocess.run(
[
sys.executable,
str(ROOT / "tools/prepare_hf_public_snapshot.py"),
str(SOURCE),
str(STAGE),
],
check=True,
cwd=ROOT,
)
verify_snapshot()
logging.info("Uploading %s to SitongGong/EvoStream", STAGE)
api = HfApi()
api.upload_large_folder(
repo_id="SitongGong/EvoStream",
repo_type="dataset",
folder_path=STAGE,
num_workers=8,
print_report=True,
print_report_every=60,
)
info = api.repo_info("SitongGong/EvoStream", repo_type="dataset")
logging.info("MIGRATION_COMPLETE remote_files=%d", len(info.siblings))
if __name__ == "__main__":
try:
main()
except Exception:
logging.exception("MIGRATION_FAILED")
raise