"""Validate the complete app artifact before creating or publishing a Release.""" import hashlib import json from pathlib import Path import re import sys sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'windows')) from update_versions import update_version FEED = 'releases.win-x64.json' INSTALLER = 'Cutdex-win-x64-Setup.exe' PORTABLE = 'Cutdex-win-x64-Portable.zip' AUDIT = 'windows-dependency-audit.json' def app_versions(pubspec): match = re.search(r'^version:\s*(\d+\.\d+\.\d+)\+(\d+)\s*$', Path(pubspec).read_text(encoding='utf-8'), re.M) if not match: raise ValueError('Application version must include a numeric build number') return match[1], update_version(match[1], match[2]) def checksum_lines(files): return ''.join(f'{files[name]} {name}\n' for name in sorted(files)) def validate_artifact(contents, version, windows_version): manifest = json.loads(contents['release-manifest.json']) if (manifest['version'] != version or manifest.get('windowsUpdateVersion') != windows_version or manifest.get('windowsPackageId') != 'Cutdex' or manifest.get('windowsChannel') != 'win-x64'): raise ValueError('Artifact version, identity or channel does not match release') feed = json.loads(contents[FEED]) assets = feed.get('Assets', []) if len(assets) != 1: raise ValueError('Release index must contain exactly one complete full package') asset = assets[0] if (asset.get('PackageId') != 'Cutdex' or asset.get('Version') != windows_version or asset.get('Type') != 'Full'): raise ValueError('Update package identity, version or type does not match release') package = asset.get('FileName', '') if not re.fullmatch(r'[A-Za-z0-9_.-]+\.nupkg', package): raise ValueError('Invalid update package filename') expected = {f'cutdex-{version}-android.apk', INSTALLER, PORTABLE, FEED, AUDIT, package} if set(manifest['files']) != expected or set(contents) != expected | {'SHA256SUMS', 'release-manifest.json'}: raise ValueError('Release requires APK, installer, Portable ZIP, index, full package and audit') for name, digest in manifest['files'].items(): if hashlib.sha256(contents[name]).hexdigest() != digest: raise ValueError(f'Package checksum mismatch: {name}') payload = contents[package] if (asset.get('Size') != len(payload) or str(asset.get('SHA256', '')).lower() != hashlib.sha256(payload).hexdigest() or str(asset.get('SHA1', '')).lower() != hashlib.sha1(payload).hexdigest()): raise ValueError('Update index checksums do not match the full package') if contents['SHA256SUMS'].decode('utf-8') != checksum_lines(manifest['files']): raise ValueError('SHA256SUMS differs from the release manifest') return manifest