"""Extract the EzServer cabinet records from a 7-Zip InstallShield overlay.""" from pathlib import Path import shutil import sys CABINETS = {b"data1.hdr", b"data1.cab", b"data2.cab"} def read_field(stream): value = bytearray() for _ in range(1024): byte = stream.read(1) if byte == b"\0": return bytes(value) if not byte: break value.extend(byte) raise ValueError("Incomplete InstallShield record") def extract(overlay, output): missing = CABINETS.copy() with overlay.open("rb") as stream: while missing: name = read_field(stream) read_field(stream) # Path on the installation media. read_field(stream) # File version. size = int(read_field(stream)) end = stream.tell() + size if size < 0 or end > overlay.stat().st_size: raise ValueError("Invalid InstallShield record size") if name in missing: with (output / name.decode("ascii")).open("wb") as destination: remaining = size while remaining: chunk = stream.read(min(remaining, 1024 * 1024)) if not chunk: raise ValueError("Truncated cabinet") destination.write(chunk) remaining -= len(chunk) missing.remove(name) stream.seek(end) def stage_web_service(extracted, runtime): source = extracted / "www" / "ezwebserver" / "src" destination = runtime / "www" / "ezwebserver" / "src" for folder in ("application", "public", "vendor"): shutil.copytree( source / folder, destination / folder, ignore=shutil.ignore_patterns( "*.exe", "*.dll", "*.bat", "*.cmd", "*.msi", "dbconfig.ini", "config_base.ini" ), ) for name in ("application/cache/sessions", "application/logs"): (destination / name).mkdir(parents=True, exist_ok=True) shutil.copy2(extracted / "webapps" / "index.html", runtime / "index.html") if __name__ == "__main__": if sys.argv[1] == "cabinet": extract(Path(sys.argv[2]), Path(sys.argv[3])) elif sys.argv[1] == "stage": stage_web_service(Path(sys.argv[2]), Path(sys.argv[3])) else: raise SystemExit("Unknown extraction operation")