"""Package Android and audited Velopack Windows installation/update artifacts.""" import argparse import hashlib import json from pathlib import Path import shutil import sys import tempfile import zipfile from release_artifacts import AUDIT, FEED, INSTALLER, PORTABLE, METADATA, app_versions, checksum_lines, validate_artifact ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(ROOT / 'tool')) from common.files import file_sha256 from common.process import cli def validate_apk(apk): with zipfile.ZipFile(apk) as archive: names = archive.namelist() for abi in ('arm64-v8a', 'x86_64'): if f'lib/{abi}/libflutter.so' not in names: raise ValueError(f'APK missing Flutter ABI: {abi}') if any(name.startswith('lib/armeabi-v7a/') for name in names): raise ValueError('APK must use supported 64-bit ABIs only') def package_windows(windows, vpk, windows_version): sys.path.insert(0, str(ROOT / 'tool/windows')) from check_windows_release import audit from windows_updates import package_app with tempfile.TemporaryDirectory(prefix='cutdex-release-') as directory: temporary = Path(directory) staged, releases = temporary / 'app', temporary / 'releases' shutil.copytree(windows, staged, ignore=shutil.ignore_patterns('processing', '*.pdb', '*.lib', '*.exp', 'dartjni.dll')) report = audit(staged, require_processing=False) checksums = {} for path in sorted(staged.rglob('*')): if path.is_file(): checksums[path.relative_to(staged).as_posix()] = file_sha256(path) (staged / 'checksums.json').write_text(json.dumps(checksums), encoding='utf-8') result = package_app(staged, releases, vpk, require_processing=False) if result['version'] != windows_version: raise ValueError('Built Windows version differs from pubspec') contents = {AUDIT: (json.dumps(report, indent=2) + '\n').encode()} for path in releases.iterdir(): if path.name in {FEED, INSTALLER, PORTABLE} or path.suffix == '.nupkg': contents[path.name] = path.read_bytes() return contents def assemble(contents, version, windows_version): files = {name: hashlib.sha256(body).hexdigest() for name, body in contents.items()} manifest = {'version': version, 'files': files, 'windowsUpdateVersion': windows_version, 'windowsPackageId': 'Cutdex', 'windowsChannel': 'win-x64', 'androidSigning': 'debug', 'processingWorkersBundled': False} contents['SHA256SUMS'] = checksum_lines(files).encode() contents['release-manifest.json'] = (json.dumps(manifest, indent=2) + '\n').encode() validate_artifact(contents, version, windows_version) return contents def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--platform', choices=['all', 'windows', 'assemble'], default='all') parser.add_argument('--vpk', type=Path, help='Pinned vpk executable or DLL') parser.add_argument('--windows-packages', type=Path, help='Previously packaged Windows artifact directory') args = parser.parse_args() if args.platform != 'assemble' and args.vpk is None: parser.error('--vpk is required when packaging Windows') if args.platform == 'assemble' and args.windows_packages is None: parser.error('--windows-packages is required when assembling artifacts') version, windows_version = app_versions(ROOT / 'pubspec.yaml') output = ROOT / 'dist' if output.exists() and any(output.iterdir()): raise ValueError('dist must be empty; use a clean release workspace') metadata = ROOT / '.build/release-metadata' if args.platform != 'windows' and metadata.exists() and any(metadata.iterdir()): raise ValueError('Release metadata output must be empty') apk = ROOT / 'build/app/outputs/flutter-apk/app-production-release.apk' windows = ROOT / 'build/windows/x64/production/runner/Release' if args.platform != 'windows': validate_apk(apk) if args.platform == 'assemble': contents = {path.name: path.read_bytes() for path in args.windows_packages.iterdir() if path.is_file()} if any(path.is_dir() for path in args.windows_packages.iterdir()): raise ValueError('Windows packages must be a flat artifact directory') if f'cutdex-{version}-android.apk' in contents: raise ValueError('Unexpected Android package in Windows artifact') else: if not (windows / 'cutdex.exe').is_file(): raise ValueError('Production Windows build is required') contents = package_windows(windows, args.vpk, windows_version) if args.platform != 'windows': contents[f'cutdex-{version}-android.apk'] = apk.read_bytes() assemble(contents, version, windows_version) output.mkdir(exist_ok=True) for name, body in contents.items(): (output / name).write_bytes(body) if args.platform != 'windows': metadata.mkdir(parents=True, exist_ok=True) for name in METADATA: (metadata / name).write_bytes(contents[name]) print(json.dumps({'platform': args.platform, 'version': version, 'files': sorted(contents)}, indent=2)) if __name__ == '__main__': sys.exit(cli(main))