#!/usr/bin/env python3 """Prepare two development apps and serve a signed full update on localhost.""" import argparse from functools import partial from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer from pathlib import Path import plistlib import subprocess import sys sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from project_tools import project_environment from release import sign_app from macos_updates import configure, publish class UpdateHandler(SimpleHTTPRequestHandler): def end_headers(self): self.send_header('Cache-Control', 'no-store') super().end_headers() def list_directory(self, path): self.send_error(404) return None def prepare(source, root, tools, port): repository = Path(__file__).resolve().parents[2] root = root.resolve() environment = project_environment() public = root / 'public' keys = root / 'keys' if (root / 'installed').exists() or public.exists(): raise ValueError('Use a fresh output directory to preserve existing test installs') subprocess.run(['swift', str(repository / 'tool/macos/sparkle_test_key.swift'), str(keys)], check=True) public_key = (keys / 'public.key').read_text() feed = f'http://localhost:{port}/appcast.xml' info = plistlib.loads((source / 'Contents/Info.plist').read_bytes()) if info['CFBundleIdentifier'] != 'app.cutdex.desktop.dev': raise ValueError('Build with --flavor development first') for folder, version in [('installed', '900001'), ('candidate', '900002')]: app = root / folder / 'cutdex.app' app.parent.mkdir(parents=True) subprocess.run(['ditto', str(source), str(app)], check=True) path = app / 'Contents/Info.plist' bundle = plistlib.loads(path.read_bytes()) bundle['CFBundleVersion'] = version path.write_bytes(plistlib.dumps(bundle)) configure(app, feed, public_key, local=True) sign_app(app, environment={**environment, 'CUTDEX_CODESIGN_IDENTITY': '-'}) publish(root / 'candidate/cutdex.app', public, f'http://localhost:{port}', tools.resolve(), keys / 'private.key', local=True) print(f'Open: {root / "installed/cutdex.app"}', flush=True) print('Settings → Check for updates: 900001 → 900002 (full ZIP)', flush=True) return public def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--app', type=Path, help='Built development app') parser.add_argument('--tools', type=Path, help='Sparkle bin directory') parser.add_argument('--output', type=Path, default=Path('.local/update-test')) parser.add_argument('--port', type=int, default=8765) parser.add_argument('--serve-only', action='store_true') args = parser.parse_args() if args.serve_only: public = args.output.resolve() / 'public' else: if not args.app or not args.tools: parser.error('--app and --tools are required unless --serve-only') public = prepare(args.app, args.output, args.tools, args.port) if not (public / 'appcast.xml').is_file(): parser.error('No prepared appcast.xml') # Only public artifacts are served. Private keys live outside this root. server = ThreadingHTTPServer(('127.0.0.1', args.port), partial(UpdateHandler, directory=str(public))) print(f'Serving http://localhost:{args.port}/appcast.xml', flush=True) try: server.serve_forever() except KeyboardInterrupt: pass finally: server.server_close() if __name__ == '__main__': main()