"""Package audited Windows releases with Velopack, or serve a loopback test feed.""" import argparse from functools import partial from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer import json from pathlib import Path import re import sys from urllib.parse import urljoin, urlparse from urllib.request import url2pathname sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from common.files import file_sha256 from common.process import ROOT, cli, run from check_windows_release import audit from update_versions import numeric_version, update_version SDK_VERSION = '1.2.161' def resolved_deskup(): config = ROOT / '.dart_tool/package_config.json' if not config.is_file(): raise ValueError('Run fvm dart tool/flutter_local.dart pub get before using Windows update tools') packages = json.loads(config.read_text(encoding='utf-8'))['packages'] package = next(item for item in packages if item['name'] == 'deskup') uri = urlparse(urljoin(config.as_uri(), package['rootUri'])) if uri.scheme != 'file' or uri.netloc: raise ValueError('Expected a local resolved deskup package') return Path(url2pathname(uri.path)) def verify_sdk_version(): """Validate the resolved build SDK before saving the app for later packaging.""" cmake = (resolved_deskup() / 'windows/velopack.cmake').read_text(encoding='utf-8') match = re.search(r'set\(DESKUP_VELOPACK_VERSION "([^"]+)"\)', cmake) if not match or match[1] != SDK_VERSION: raise ValueError(f'Resolved deskup must use Velopack SDK {SDK_VERSION}') return SDK_VERSION def vpk_command(path): path = Path(path).resolve() if path.suffix.lower() == '.dll': # Useful with a locally extracted official vpk tool package. return ['dotnet', str(path), '--legacyConsole', '--skip-updates'] return [str(path), '--legacyConsole', '--skip-updates'] def check_tool(command): result = run(command + ['--help'], cwd=Path.cwd(), capture_output=True, text=True, encoding='utf-8', errors='replace', check=True, timeout=30) if not re.search(rf'(?= candidate: raise ValueError('Update version must be greater than every previously published version') def verify_manifest(app): app = app.resolve() manifest = json.loads((app / 'checksums.json').read_text(encoding='utf-8-sig')) actual_files = {file.relative_to(app).as_posix() for file in app.rglob('*') if file.is_file() and file.name != 'checksums.json'} # The processing subtree has its own checksum manifest, which is included # as a file in the outer app manifest. actual_files.update(file.relative_to(app).as_posix() for file in app.rglob('checksums.json') if file != app / 'checksums.json') if set(manifest) != actual_files: raise ValueError('Release files differ from the audited staging manifest') for name, expected in manifest.items(): candidate = app / name file = candidate.resolve() if not file.is_relative_to(app) or candidate.is_symlink(): raise ValueError(f'Invalid manifest path: {name}') if file_sha256(file).lower() != expected.lower(): raise ValueError(f'Release checksum mismatch: {name}') def package_app(app, output, vpk, *, development=False, require_processing=True): app, output = Path(app).resolve(), Path(output).resolve() if output == app or output.is_relative_to(app) or app.is_relative_to(output): raise ValueError('Release output must be outside the staged app') audit(app, require_processing=require_processing) verify_manifest(app) sdk_dll = app / 'velopack_libc.dll' if not sdk_dll.is_file(): raise ValueError('Rebuild the app with the Velopack integration before packaging') command = vpk_command(vpk) check_tool(command) version = file_version(app) package_id = 'Cutdex.Dev' if development else 'Cutdex' # The updater is generic; verify the host flavor through its PE branding. import pefile with pefile.PE(str(app / 'cutdex.exe')) as pe: products = [table.entries.get(b'ProductName', b'').decode('utf-8') for group in pe.FileInfo for block in group if block.Key == b'StringFileInfo' for table in block.StringTable] if ('Cutdex Dev' if development else 'Cutdex') not in products: raise ValueError('Package identity does not match the Flutter build flavor') validate_previous(output, package_id, version) output.mkdir(parents=True, exist_ok=True) pack_release(command, app=app, output=output, package_id=package_id, version=version, title='Cutdex Dev' if development else 'Cutdex') return {'version': version, 'packageId': package_id, 'output': str(output)} def serve(directory, port): directory = directory.resolve() if not directory.is_dir(): raise ValueError('Provide an existing Velopack release directory') with ThreadingHTTPServer(('127.0.0.1', port), partial(SimpleHTTPRequestHandler, directory=str(directory))) as server: print(f'Test update source: http://127.0.0.1:{server.server_port}', flush=True) server.serve_forever() def main(): parser = argparse.ArgumentParser(description=__doc__) commands = parser.add_subparsers(dest='command', required=True) commands.add_parser('verify-sdk', help='Verify resolved native SDK before saving a build') pack = commands.add_parser('pack', help='Package a complete audited release; never uploads') pack.add_argument('--app', type=Path, required=True) pack.add_argument('--output', type=Path, required=True) pack.add_argument('--vpk', type=Path, required=True) pack.add_argument('--development', action='store_true') server = commands.add_parser('serve', help='Serve a test feed on 127.0.0.1 only') server.add_argument('--directory', type=Path, required=True) server.add_argument('--port', type=int, default=8765) args = parser.parse_args() if args.command == 'pack': verify_sdk_version() print(json.dumps(package_app(args.app, args.output, args.vpk, development=args.development))) elif args.command == 'verify-sdk': print(f'Velopack SDK {verify_sdk_version()} verified') else: serve(args.directory, args.port) if __name__ == '__main__': sys.exit(cli(main))