|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import hashlib |
| 4 | +import re |
| 5 | +import shutil |
| 6 | +import subprocess |
| 7 | +import zipfile |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | + |
| 11 | +FIXED_TIMESTAMP = (2020, 1, 1, 0, 0, 0) |
| 12 | +EXCLUDED_PARTS = {".git", ".github", ".venv", "__pycache__", "tests"} |
| 13 | + |
| 14 | + |
| 15 | +def repository_root() -> Path: |
| 16 | + return Path(__file__).resolve().parents[1] |
| 17 | + |
| 18 | + |
| 19 | +def normalized_version(root: Path, requested: str | None) -> str: |
| 20 | + version = requested |
| 21 | + if not version: |
| 22 | + process = subprocess.run( |
| 23 | + ["git", "describe", "--tags", "--always", "--dirty"], |
| 24 | + cwd=root, |
| 25 | + text=True, |
| 26 | + capture_output=True, |
| 27 | + check=True, |
| 28 | + ) |
| 29 | + version = process.stdout.strip() |
| 30 | + version = version.removeprefix("v") |
| 31 | + if not re.fullmatch(r"[0-9A-Za-z][0-9A-Za-z._-]*", version): |
| 32 | + raise ValueError(f"invalid release version: {version!r}") |
| 33 | + return version |
| 34 | + |
| 35 | + |
| 36 | +def included(path: Path, root: Path) -> bool: |
| 37 | + relative = path.relative_to(root) |
| 38 | + return not any(part in EXCLUDED_PARTS for part in relative.parts) |
| 39 | + |
| 40 | + |
| 41 | +def write_zip(archive: Path, root: Path, files: list[Path]) -> None: |
| 42 | + with zipfile.ZipFile(archive, "w", compression=zipfile.ZIP_DEFLATED) as output: |
| 43 | + for path in sorted(files, key=lambda item: item.as_posix()): |
| 44 | + relative = path.relative_to(root).as_posix() |
| 45 | + info = zipfile.ZipInfo(relative, FIXED_TIMESTAMP) |
| 46 | + info.compress_type = zipfile.ZIP_DEFLATED |
| 47 | + info.external_attr = 0o100644 << 16 |
| 48 | + output.writestr(info, path.read_bytes()) |
| 49 | + |
| 50 | + |
| 51 | +def build_archives(root: Path, output_directory: Path, version: str) -> list[Path]: |
| 52 | + if output_directory.exists(): |
| 53 | + shutil.rmtree(output_directory) |
| 54 | + output_directory.mkdir(parents=True) |
| 55 | + |
| 56 | + skill_directories = sorted( |
| 57 | + path for path in root.iterdir() if path.is_dir() and (path / "SKILL.md").is_file() |
| 58 | + ) |
| 59 | + runtime_files = [ |
| 60 | + path for path in root.iterdir() if path.is_file() and included(path, root) |
| 61 | + ] |
| 62 | + for directory in [root / "docs", *skill_directories]: |
| 63 | + if directory.is_dir(): |
| 64 | + runtime_files.extend( |
| 65 | + path |
| 66 | + for path in directory.rglob("*") |
| 67 | + if path.is_file() and included(path, root) |
| 68 | + ) |
| 69 | + |
| 70 | + archives: list[Path] = [] |
| 71 | + bundle = output_directory / f"embeddedskills-{version}.zip" |
| 72 | + write_zip(bundle, root, runtime_files) |
| 73 | + archives.append(bundle) |
| 74 | + |
| 75 | + for skill_directory in skill_directories: |
| 76 | + files = [root / "LICENSE"] |
| 77 | + files.extend( |
| 78 | + path |
| 79 | + for path in skill_directory.rglob("*") |
| 80 | + if path.is_file() and included(path, root) |
| 81 | + ) |
| 82 | + archive = output_directory / f"embeddedskills-{skill_directory.name}-{version}.zip" |
| 83 | + write_zip(archive, root, files) |
| 84 | + archives.append(archive) |
| 85 | + return archives |
| 86 | + |
| 87 | + |
| 88 | +def write_checksums(output_directory: Path, archives: list[Path]) -> Path: |
| 89 | + checksum_file = output_directory / "SHA256SUMS" |
| 90 | + lines = [ |
| 91 | + f"{hashlib.sha256(path.read_bytes()).hexdigest()} {path.name}" |
| 92 | + for path in sorted(archives) |
| 93 | + ] |
| 94 | + checksum_file.write_text("\n".join(lines) + "\n", encoding="utf-8", newline="\n") |
| 95 | + return checksum_file |
| 96 | + |
| 97 | + |
| 98 | +def main() -> int: |
| 99 | + parser = argparse.ArgumentParser(description="Build deterministic release archives") |
| 100 | + parser.add_argument("--root", type=Path, default=repository_root()) |
| 101 | + parser.add_argument("--output", type=Path) |
| 102 | + parser.add_argument("--version") |
| 103 | + args = parser.parse_args() |
| 104 | + |
| 105 | + root = args.root.resolve() |
| 106 | + output = (args.output or root / "dist").resolve() |
| 107 | + version = normalized_version(root, args.version) |
| 108 | + archives = build_archives(root, output, version) |
| 109 | + checksum_file = write_checksums(output, archives) |
| 110 | + for path in [*archives, checksum_file]: |
| 111 | + print(path) |
| 112 | + return 0 |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + raise SystemExit(main()) |
0 commit comments