#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-or-later """Stage the reviewed software catalog as a read-only app resource, never media. Ordinary source builds get an empty schema if the private catalog is absent. Release packaging passes --require-full and fails rather than shipping it empty. The explicit source content profile generates an empty schema without reading maintainer inputs; normal remains the default. """ import argparse from contextlib import closing import hashlib import json import os from pathlib import Path import re import shutil import sqlite3 import tempfile import struct import subprocess import zipfile import sys from urllib.parse import urlsplit from concurrent.futures import ThreadPoolExecutor ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(ROOT / 'tools')) from build_content import add_content_profile_argument, atomic_empty_database, content_profile as resolve_content_profile SCHEMA = Path(__file__).with_name('app-schema.sql') DEFAULT_SOURCE = ROOT / 'media/software-library-full/prepared/app-catalog.sqlite' DEFAULT_ARTWORK_CACHE = ROOT / 'media/software-library-full/cache' MAX_BYTES = 256 * 1024 * 1024 PRIVATE = re.compile(r'(?:file://|/Users/|/home/|/private/|(?II', data[16:24]) return 0 < width <= 640 and 0 < height <= 640 def screenshot_member(archive): """Read one bounded image, never extract archive-supplied paths to disk.""" with zipfile.ZipFile(archive) as images: members = images.infolist() if len(members) > 256: raise ValueError('Artwork archive has too many members') candidates = [m for m in members if not m.is_dir() and Path(m.filename).suffix.lower() in ('.png', '.jpg', '.jpeg', '.gif', '.webp') and not any(part.startswith('.') or part == '__MACOSX' for part in Path(m.filename).parts) and not (m.flag_bits & 1) and 0 < m.file_size <= 8 * 1024 * 1024 and (m.external_attr >> 16) & 0o170000 != 0o120000] def rank(member): stem = Path(member.filename).stem.lower() preferred = {'cover': 0, 'title': 1, 'intro': 2} return (preferred.get(stem, 3), member.filename.casefold(), member.filename) if not candidates: return None member = min(candidates, key=rank) with images.open(member) as stream: data = stream.read(8 * 1024 * 1024 + 1) if len(data) != member.file_size or len(data) > 8 * 1024 * 1024: raise ValueError('Artwork archive image exceeds its declared size') return data def package_artwork(catalog, cache, destination): """Generate maintainer thumbnails from previously verified local art. The content-addressed crawl cache is optional for a developer checkout. No network requests, original artwork or documents are copied into the app. """ cache, destination = Path(cache), Path(destination) if destination.is_symlink(): raise ValueError('Artwork destination must not be a symlink') with closing(sqlite3.connect(Path(catalog).resolve().as_uri() + '?mode=ro', uri=True)) as db: rows = db.execute("SELECT DISTINCT a.source_url,a.sha256,a.byte_size FROM asset a JOIN release_asset ra ON ra.asset_id=a.id WHERE a.kind IN ('replacement-label','screenshot') ORDER BY a.id").fetchall() destination.mkdir(parents=True, exist_ok=True) def convert(row): url, sha, size = row parts = urlsplit(url) suffix = Path(parts.path).suffix.lower() if (parts.scheme != 'https' or parts.hostname != 'colorcomputerarchive.com' or not parts.path.startswith('/repo/') or suffix not in ('.png', '.jpg', '.jpeg', '.gif', '.webp', '.zip') or not sha or not re.fullmatch('[a-f0-9]{64}', sha)): return None source = cache / (sha + suffix) if not source.is_file() or source.is_symlink() or not 0 < source.stat().st_size <= 8 * 1024 * 1024: return None original = source.read_bytes() if len(original) != size or hashlib.sha256(original).hexdigest() != sha: raise ValueError('Cached artwork does not match its catalog fingerprint') name = artwork_key(url, sha) + '.png' target = destination / name # A generated output is already bounded; its input identity is in its name. if target.is_file() and not target.is_symlink() and png_thumbnail(target): return name # ZIP assets retain their original URL/fingerprint and attribution. Only # a single selected image becomes a thumbnail; no archive is shipped. image_data = screenshot_member(source) if suffix == '.zip' else None if suffix == '.zip' and image_data is None: return None fd, temporary = tempfile.mkstemp(prefix='.artwork-', suffix='.png', dir=destination) os.close(fd) temporary = Path(temporary) try: with tempfile.TemporaryDirectory(prefix='vcc-artwork-') as scratch: input_path = source if image_data is not None: input_path = Path(scratch) / 'image' input_path.write_bytes(image_data) subprocess.run(['/usr/bin/sips', '-s', 'format', 'png', '-Z', '640', str(input_path), '--out', str(temporary)], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) if not png_thumbnail(temporary): raise ValueError('Generated artwork thumbnail is not a bounded PNG') os.chmod(temporary, 0o444) os.replace(temporary, target) finally: temporary.unlink(missing_ok=True) return name with ThreadPoolExecutor(max_workers=4) as workers: names = {name for name in workers.map(convert, rows) if name} # Clear only our generated PNG namespace after a successful complete pass. for path in destination.iterdir(): if re.fullmatch(r'[a-f0-9]{64}\.png', path.name) and path.name not in names and not path.is_symlink(): path.unlink() return len(names) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--source', type=Path) add_content_profile_argument(parser) parser.add_argument('--output', type=Path) parser.add_argument('--check-only', action='store_true', help='Validate input without staging a resource') parser.add_argument('--require-full', action='store_true') args = parser.parse_args() if not args.check_only and args.output is None: parser.error('--output is required unless --check-only is used') try: profile = resolve_content_profile(args.content_profile) if profile == 'source': if args.require_full: raise ValueError('--require-full cannot be combined with the source content profile') if args.check_only: with tempfile.TemporaryDirectory(prefix='vcc-source-catalog-check-') as temporary: result = source_catalog(Path(temporary) / 'SoftwareLibrary/catalog.sqlite') else: result = source_catalog(args.output) else: source = args.source if args.source is not None else DEFAULT_SOURCE result = validate(source, args.require_full) if args.check_only else package(source, args.output, args.require_full) if not args.check_only: artwork = args.output.parent / 'Artwork' if artwork.is_symlink(): raise ValueError('Artwork destination must not be a symlink') if artwork.is_dir(): for path in artwork.iterdir(): if re.fullmatch(r'[a-f0-9]{64}\.png', path.name) and not path.is_symlink(): path.unlink() result['thumbnails'] = 0 except (ValueError, OSError, sqlite3.DatabaseError, subprocess.CalledProcessError) as error: parser.exit(1, 'error: Software Library catalog: ' + str(error) + '\n') print('Software Library catalog: ' + json.dumps(result, sort_keys=True)) if __name__ == '__main__': main()