"""Stage a full-Hub update or lightweight metadata archive; never upload.""" from __future__ import annotations import argparse import gzip import json import shutil import tarfile from pathlib import Path from kitti_c2l.release import ( DATA_FILES, RELEASE_VERSION, sha256, validate_hub_manifest, validate_release, ) RELEASE_FILES = ( "README.md", "LICENSE", "LICENSE-DATA", "CITATION.cff", "configs/sequence_mapping.yaml", "docs/PROTOCOL.md", "docs/BASELINES.md", "docs/ASSESSMENT.md", "docs/RELEASING.md", "docs/THIRD_PARTY.md", "release/v1/APPLE-MOBILEVIT-LICENSE.txt", "release/v1/UNICAL-LICENSE.txt", "release/v1/protocol.json", "release/v1/provenance.json", "release/v1/baselines_manifest.json", "release/v1/verification.json", "release/v1/manifest.json", "release/v1/CITATION.cff", "release/v1/assessment_results.json", "release/v1/HF_DATASET_CARD.md", "release/v1/hub_manifest.json", "release/v1/hub_payload_verification.json", "release/v1/hub_loading_verification.json", "release/v1/full_release_verification.json", *(f"data/{name}" for name in DATA_FILES), ) def write_checksums(directory: Path) -> None: files = sorted( path for path in directory.rglob("*") if path.is_file() and path.name != "SHA256SUMS" ) (directory / "SHA256SUMS").write_text( "".join(f"{sha256(path)} {path.relative_to(directory).as_posix()}\n" for path in files) ) def archive_directory(directory: Path) -> Path: archive = directory.with_name(directory.name + ".tar.gz") with ( archive.open("xb") as raw, gzip.GzipFile(filename="", fileobj=raw, mode="wb", mtime=0) as compressed, ): with tarfile.open(fileobj=compressed, mode="w") as tar: for path in sorted(directory.rglob("*")): if not path.is_file(): continue info = tar.gettarinfo( str(path), arcname=f"{directory.name}/{path.relative_to(directory).as_posix()}" ) info.uid = info.gid = info.mtime = 0 info.uname = info.gname = "" info.mode = 0o644 with path.open("rb") as stream: tar.addfile(info, stream) return archive def prepare( root: Path, output: Path, kitti_calib_root: Path | None = None, *, profile: str = "metadata" ) -> Path: if profile not in {"metadata", "full-update"}: raise ValueError(f"Unknown release profile: {profile}") validate_release(root, kitti_calib_root=kitti_calib_root) hub = validate_hub_manifest(root) if profile == "full-update" else None if output.exists() or output.with_name(output.name + ".tar.gz").exists(): raise FileExistsError(f"Refusing to overwrite release output: {output}") # Resolve every source before creating output: no recursive working-directory upload. for relative in RELEASE_FILES: source = root / relative if not source.is_file() or source.is_symlink(): raise ValueError(f"Missing or symlinked release source: {relative}") output.mkdir(parents=True) for relative in RELEASE_FILES: destination = output / relative destination.parent.mkdir(parents=True, exist_ok=True) shutil.copyfile(root / relative, destination) # This command stages the frozen v1.0 profile, even when the source # repository's current citation advances to an additive dataset release. shutil.copyfile(root / "release/v1/CITATION.cff", output / "CITATION.cff") if hub is not None: shutil.copyfile(root / "release/v1/HF_DATASET_CARD.md", output / "README.md") (output / "release-profile.json").write_text( json.dumps( { "profile": profile, "target_repo": hub["repo_id"], "base_revision": hub["revision"], "requires_existing_frame_shards": True, "delete_remote_files": False, "note": "Update package only; the 50 existing frame shards stay on the Hub. No upload is performed.", }, indent=2, ) + "\n" ) write_checksums(output) return archive_directory(output) def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--root", type=Path, default=Path(__file__).resolve().parents[1]) parser.add_argument("--profile", choices=["full-update", "metadata"], default="full-update") parser.add_argument("--output", type=Path) parser.add_argument("--kitti-calib-root", type=Path) args = parser.parse_args() output = args.output or Path(f"dist/kitti-c2l-{args.profile}-v{RELEASE_VERSION}") archive = prepare( args.root.resolve(), output.resolve(), args.kitti_calib_root, profile=args.profile ) print(f"Prepared {args.profile}: {output}\nArchive: {archive}\nSHA-256: {sha256(archive)}") if __name__ == "__main__": main()