53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Build the standalone host updater using only the Python standard library."""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import os
|
|
from pathlib import Path
|
|
import tempfile
|
|
import zipfile
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
SOURCES = {
|
|
"updater.py": "deploy/web/updater.py",
|
|
"ranch_web/__init__.py": "ranch_web/__init__.py",
|
|
"ranch_web/updates.py": "ranch_web/updates.py",
|
|
}
|
|
ENTRYPOINT = b"from updater import main\nraise SystemExit(main())\n"
|
|
|
|
|
|
def build(output):
|
|
output = Path(output)
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
descriptor, temporary = tempfile.mkstemp(prefix=".updater-", dir=output.parent)
|
|
try:
|
|
# Fixed order/timestamps make the asset reproducible for a given source.
|
|
with os.fdopen(descriptor, "wb") as stream, zipfile.ZipFile(stream, "w", compression=zipfile.ZIP_DEFLATED) as archive:
|
|
contents = {"__main__.py": ENTRYPOINT}
|
|
contents.update({name: (ROOT / source).read_bytes() for name, source in SOURCES.items()})
|
|
for name, content in contents.items():
|
|
compile(content, name, "exec")
|
|
info = zipfile.ZipInfo(name, date_time=(2020, 1, 1, 0, 0, 0))
|
|
info.compress_type = zipfile.ZIP_DEFLATED
|
|
info.external_attr = 0o100644 << 16
|
|
archive.writestr(info, content)
|
|
os.replace(temporary, output)
|
|
finally:
|
|
if os.path.exists(temporary):
|
|
os.unlink(temporary)
|
|
return output
|
|
|
|
|
|
def main(argv=None):
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--output", required=True, type=Path, metavar="PATH", help="destination updater.pyz release asset")
|
|
args = parser.parse_args(argv)
|
|
print(build(args.output))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|