|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import argparse |
| 5 | +import hashlib |
| 6 | +import os |
| 7 | +import shutil |
| 8 | +import sys |
| 9 | +import tarfile |
| 10 | +import zipfile |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | + |
| 14 | +def sha256_file(path: Path) -> str: |
| 15 | + digest = hashlib.sha256() |
| 16 | + with path.open("rb") as f: |
| 17 | + for chunk in iter(lambda: f.read(1024 * 1024), b""): |
| 18 | + digest.update(chunk) |
| 19 | + return digest.hexdigest() |
| 20 | + |
| 21 | + |
| 22 | +def should_zip(target: str, bin_path: Path) -> bool: |
| 23 | + if bin_path.suffix.lower() == ".exe": |
| 24 | + return True |
| 25 | + t = target.lower() |
| 26 | + return ("windows" in t) or t.endswith("msvc") |
| 27 | + |
| 28 | + |
| 29 | +def create_tar_gz(archive_path: Path, stage_dir: Path) -> None: |
| 30 | + with tarfile.open(archive_path, "w:gz") as tf: |
| 31 | + tf.add(stage_dir, arcname=stage_dir.name) |
| 32 | + |
| 33 | + |
| 34 | +def create_zip(archive_path: Path, stage_dir: Path) -> None: |
| 35 | + with zipfile.ZipFile(archive_path, "w", compression=zipfile.ZIP_DEFLATED) as zf: |
| 36 | + for p in stage_dir.rglob("*"): |
| 37 | + if p.is_dir(): |
| 38 | + continue |
| 39 | + zf.write(p, arcname=str(p.relative_to(stage_dir.parent))) |
| 40 | + |
| 41 | + |
| 42 | +def main(argv: list[str]) -> int: |
| 43 | + parser = argparse.ArgumentParser(description="Package a rexos release archive + sha256 file.") |
| 44 | + parser.add_argument("--version", required=True, help="Version string, e.g. v0.1.0") |
| 45 | + parser.add_argument("--target", required=True, help="Target triple or label, e.g. x86_64-unknown-linux-gnu") |
| 46 | + parser.add_argument("--bin", required=True, help="Path to built rexos binary (rexos or rexos.exe)") |
| 47 | + parser.add_argument("--out-dir", default="dist", help="Output directory (default: dist)") |
| 48 | + parser.add_argument( |
| 49 | + "--include", |
| 50 | + action="append", |
| 51 | + default=[], |
| 52 | + help="Optional file to include in the archive (repeatable)", |
| 53 | + ) |
| 54 | + args = parser.parse_args(argv) |
| 55 | + |
| 56 | + repo_root = Path.cwd() |
| 57 | + out_dir = (repo_root / args.out_dir).resolve() |
| 58 | + out_dir.mkdir(parents=True, exist_ok=True) |
| 59 | + |
| 60 | + bin_path = (repo_root / args.bin).resolve() |
| 61 | + if not bin_path.exists(): |
| 62 | + print(f"error: --bin does not exist: {bin_path}", file=sys.stderr) |
| 63 | + return 2 |
| 64 | + |
| 65 | + if not bin_path.is_file(): |
| 66 | + print(f"error: --bin is not a file: {bin_path}", file=sys.stderr) |
| 67 | + return 2 |
| 68 | + |
| 69 | + base_name = f"rexos-{args.version}-{args.target}" |
| 70 | + stage_dir = out_dir / base_name |
| 71 | + if stage_dir.exists(): |
| 72 | + shutil.rmtree(stage_dir) |
| 73 | + stage_dir.mkdir(parents=True, exist_ok=True) |
| 74 | + |
| 75 | + # Copy binary (keep its original filename; on Windows it's usually rexos.exe). |
| 76 | + dest_bin = stage_dir / bin_path.name |
| 77 | + shutil.copy2(bin_path, dest_bin) |
| 78 | + if dest_bin.suffix.lower() != ".exe": |
| 79 | + os.chmod(dest_bin, 0o755) |
| 80 | + |
| 81 | + # Default include set if present. |
| 82 | + default_includes = ["README.md", "LICENSE", "LICENSE.txt"] |
| 83 | + includes: list[Path] = [] |
| 84 | + for rel in default_includes: |
| 85 | + p = (repo_root / rel) |
| 86 | + if p.exists() and p.is_file(): |
| 87 | + includes.append(p) |
| 88 | + for user_inc in args.include: |
| 89 | + p = (repo_root / user_inc) |
| 90 | + if p.exists() and p.is_file(): |
| 91 | + includes.append(p) |
| 92 | + |
| 93 | + seen: set[Path] = set() |
| 94 | + for p in includes: |
| 95 | + rp = p.resolve() |
| 96 | + if rp in seen: |
| 97 | + continue |
| 98 | + seen.add(rp) |
| 99 | + shutil.copy2(rp, stage_dir / p.name) |
| 100 | + |
| 101 | + if should_zip(args.target, bin_path): |
| 102 | + archive_path = out_dir / f"{base_name}.zip" |
| 103 | + create_zip(archive_path, stage_dir) |
| 104 | + else: |
| 105 | + archive_path = out_dir / f"{base_name}.tar.gz" |
| 106 | + create_tar_gz(archive_path, stage_dir) |
| 107 | + |
| 108 | + digest = sha256_file(archive_path) |
| 109 | + sha_path = archive_path.with_suffix(archive_path.suffix + ".sha256") |
| 110 | + sha_path.write_text(f"{digest} {archive_path.name}\n", encoding="utf-8") |
| 111 | + |
| 112 | + print(str(archive_path)) |
| 113 | + print(str(sha_path)) |
| 114 | + return 0 |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + raise SystemExit(main(sys.argv[1:])) |
| 119 | + |
0 commit comments