import os from pathlib import Path import shutil import tempfile import urllib.request import zipfile name = os.environ["APP_NAME"] version = os.environ["APP_VERSION"] plain_version = version.removeprefix("v") url = f"https://github.com/storytold/{name}/releases/download/{version}/{name}-web-{plain_version}.zip" with tempfile.TemporaryDirectory() as tmp: archive = Path(tmp) / "release.zip" urllib.request.urlretrieve(url, archive) unpacked = Path(tmp) / "unpacked" with zipfile.ZipFile(archive) as bundle: for member in bundle.infolist(): path = Path(member.filename) if path.is_absolute() or ".." in path.parts or (member.external_attr >> 16) & 0o170000 == 0o120000: raise ValueError(f"unsafe archive entry: {path}") bundle.extractall(unpacked) roots = [path.parent for path in unpacked.glob("*/index.html")] if (unpacked / "index.html").exists(): roots = [unpacked] if len(roots) != 1: raise ValueError("expected exactly one web root") target = Path("/srv/web") target.mkdir(parents=True) for source in roots[0].iterdir(): destination = target / source.name if source.is_dir(): shutil.copytree(source, destination) else: shutil.copy2(source, destination)