52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Build the dependency-free GovOPlaN deployer as one executable zipapp."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
from hashlib import sha256
|
|
from pathlib import Path
|
|
import zipapp
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parent
|
|
DEFAULT_OUTPUT = ROOT.parent.parent / "runtime" / "deployment" / "govoplan-deploy.pyz"
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--output", type=Path, default=DEFAULT_OUTPUT)
|
|
args = parser.parse_args(argv)
|
|
|
|
output = args.output.expanduser().resolve()
|
|
if output == ROOT or ROOT in output.parents:
|
|
raise SystemExit("output must be outside the deployment source directory")
|
|
output.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
temporary = output.with_name(f".{output.name}.tmp")
|
|
if temporary.exists():
|
|
temporary.unlink()
|
|
zipapp.create_archive(
|
|
ROOT,
|
|
target=temporary,
|
|
interpreter="/usr/bin/env python3",
|
|
compressed=True,
|
|
filter=_include_source,
|
|
)
|
|
temporary.chmod(0o755)
|
|
temporary.replace(output)
|
|
digest = sha256(output.read_bytes()).hexdigest()
|
|
print(f"{digest} {output}")
|
|
return 0
|
|
|
|
|
|
def _include_source(path: Path) -> bool:
|
|
return (
|
|
"__pycache__" not in path.parts
|
|
and path.suffix not in {".pyc", ".pyo"}
|
|
and path.name != "build-deployer-zipapp.py"
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|