"""Assemble installable archives from self-contained dotnet publish output.""" import hashlib import io import os from pathlib import Path import plistlib import struct import sys import tarfile import zipfile DEPLOY = Path(__file__).resolve().parent ROOT = DEPLOY.parent.parent PAYLOAD = Path(os.environ.get('LANUPS_INSTALLER_PAYLOAD', ROOT / 'artifacts' / 'installer-payload-optimized')).resolve() DIST = ROOT / 'artifacts' / 'installers' ICON = DEPLOY.parent / 'src' / 'LanUps.Desktop' / 'Assets' / 'ups.png' def binaries(rid, kind): directory = PAYLOAD / rid / kind executable = directory / f"LanUps.{kind}{'.exe' if rid.startswith('win-') else ''}" if not executable.is_file(): raise FileNotFoundError(executable) for path in sorted(directory.rglob('*')): if path.is_file() and path.suffix.lower() not in {'.pdb', '.dbg'} and not any(part.endswith('.dSYM') for part in path.parts): yield path, path.relative_to(directory).as_posix(), path == executable def tar_bytes(archive, name, data, mode): item = tarfile.TarInfo(name) item.size = len(data) item.mode = mode archive.addfile(item, io.BytesIO(data)) def unix_package(rid): mac = rid.startswith('osx-') path = DIST / f'LanUps-{rid}.tar.gz' with tarfile.open(path, 'w:gz', compresslevel=5) as archive: tar_bytes(archive, 'install.sh', (DEPLOY / 'unix' / 'install.sh').read_bytes().replace(b'\r\n', b'\n'), 0o755) tar_bytes(archive, 'architecture', rid.split('-', 1)[1].encode() + b'\n', 0o644) folder = DEPLOY / ('macos' if mac else 'linux') for sidecar in sorted(folder.iterdir()): tar_bytes(archive, sidecar.name, sidecar.read_bytes(), 0o644) for kind in ('Agent', 'Desktop'): prefix = 'LanUps.app/Contents/MacOS' if mac and kind == 'Desktop' else kind.lower() for source, filename, executable in binaries(rid, kind): item = tarfile.TarInfo(f'{prefix}/{filename}') item.size = source.stat().st_size item.mode = 0o755 if executable else 0o644 with source.open('rb') as stream: archive.addfile(item, stream) if mac: info = dict(CFBundleExecutable='LanUps.Desktop', CFBundleIdentifier='local.lanups.desktop', CFBundleName='LanUps', CFBundleDisplayName='局域网 UPS', CFBundlePackageType='APPL', CFBundleIconFile='ups.icns', CFBundleVersion='1', CFBundleShortVersionString='0.1.0') tar_bytes(archive, 'LanUps.app/Contents/Info.plist', plistlib.dumps(info), 0o644) png = ICON.read_bytes() image = b'icns' + struct.pack('>I', len(png) + 16) + b'ic08' + struct.pack('>I', len(png) + 8) + png tar_bytes(archive, 'LanUps.app/Contents/Resources/ups.icns', image, 0o644) else: tar_bytes(archive, 'ups.png', ICON.read_bytes(), 0o644) return path def windows_package(rid, path=None): path = path or DIST / f'LanUps-{rid}.zip' path.parent.mkdir(parents=True, exist_ok=True) with zipfile.ZipFile(path, 'w', zipfile.ZIP_DEFLATED, compresslevel=5) as archive: for filename in ('Setup.cmd', 'Setup.ps1', 'install.ps1'): script = (DEPLOY / 'windows' / filename).read_text(encoding='utf-8-sig') archive.writestr(filename, script.replace('\n', '\r\n').encode('utf-8-sig' if filename.endswith('.ps1') else 'ascii')) for kind in ('Agent', 'Desktop'): for source, filename, _ in binaries(rid, kind): archive.write(source, f'{kind}/{filename}') return path def main(targets): DIST.mkdir(parents=True, exist_ok=True) router = DIST / 'LanUps-openwrt.tar.gz' with tarfile.open(router, 'w:gz') as archive: for source in sorted((DEPLOY / 'router').iterdir()): tar_bytes(archive, source.name, source.read_bytes().replace(b'\r\n', b'\n'), 0o755) print(router.name, flush=True) for rid in targets: path = windows_package(rid) if rid.startswith('win-') else unix_package(rid) print(path.name, flush=True) archives = sorted([*DIST.glob('LanUps-*.zip'), *DIST.glob('LanUps-*.tar.gz')]) with (DIST / 'SHA256SUMS').open('w', encoding='ascii', newline='\n') as sums: for path in archives: with path.open('rb') as stream: sums.write(f"{hashlib.file_digest(stream, 'sha256').hexdigest()} {path.name}\n") if __name__ == '__main__': main(sys.argv[1:])