#!/usr/bin/env python3 """Publish the already-built web bundle to the local Pipelines installation.""" import hashlib import json from pathlib import Path import shutil import sys import time def main(): source = Path(__file__).resolve().parents[1] / "build/web-next" if not all((source / name).is_file() for name in ("index.html", "main.dart.js", "flutter_bootstrap.js")): raise ValueError("No complete web build at app/build/web-next") manifest = Path.home() / ".local/share/pipelines/pipelines-install.json" target = Path(json.loads(manifest.read_text())["web"]["root"]) if not target.is_dir(): raise ValueError(f"Installed web directory is missing: {target}") stamp = str(time.time_ns()) staging = target.with_name(target.name + ".next-" + stamp) rollback = target.with_name(target.name + ".before-web-update-" + stamp) try: shutil.copytree(source, staging) # Preserve downloads when an older installation stores them in the root. for name in ("updates", "downloads"): if (target / name).is_dir() and not (staging / name).exists(): shutil.copytree(target / name, staging / name) target.rename(rollback) try: staging.rename(target) except OSError: rollback.rename(target) raise finally: if staging.exists(): shutil.rmtree(staging) digest = hashlib.sha256((target / "main.dart.js").read_bytes()).hexdigest() print(f"Published: {target}\nRollback: {rollback}\nJS SHA256: {digest}") print("Reload Pipelines in your browser. No server restart is needed.") if __name__ == "__main__": try: main() except (ValueError, OSError, KeyError) as error: sys.exit(str(error))