#!/usr/bin/env python3 """Select installed package contributions by ALPM ownership, without tree copies. Inputs are protected outputs of the isolated builder. This does not authenticate an installation receipt or replace the subsequent artifact consistency checker. """ import hashlib import importlib.util import json from pathlib import Path import stat import sys def capture(checker, lock, installed, cache, source_root): require = checker.require expected_hash = hashlib.sha256(json.dumps(lock, sort_keys=True, separators=(',', ':')).encode()).hexdigest() require(installed['version'] == 0 and installed['resolution_sha256'] == expected_hash, 'Installation resolution mismatch') root = Path(installed['root']) database = Path(installed['database']) for path in (root, database, cache, source_root): require(path.is_absolute() and path.is_dir() and not path.is_symlink() and all(not parent.is_symlink() for parent in path.parents), 'Invalid protected capture input') require(database == root / 'var/lib/pacman', 'Unexpected installation DB location') records = {} local = database / 'local' require(local.is_dir() and not local.is_symlink(), 'Invalid local database') for entry in sorted(local.iterdir()): if entry.name == 'ALPM_DB_VERSION': continue require(stat.S_ISDIR(entry.lstat().st_mode), 'Invalid local package record') desc = entry / 'desc' require(stat.S_ISREG(desc.lstat().st_mode), 'Invalid original desc') fields = checker.alpm_fields(desc.read_bytes()) names = fields.get('%NAME%', []) require(len(names) == 1 and names[0] not in records, 'Duplicate installed package') records[names[0]] = (entry, fields) require(set(records) == {row['name'] for row in lock['packages']}, 'Installed package set differs from resolution') packages = [] for row in lock['packages']: record, fields = records[row['name']] require(fields.get('%VERSION%') == [row['version']] and fields.get('%ARCH%') == [row['architecture']], 'Installed package identity differs from resolution') file_list = record / 'files' require(stat.S_ISREG(file_list.lstat().st_mode), 'Invalid original file list') paths = checker.alpm_fields(file_list.read_bytes()).get('%FILES%', []) sources = {} for runtime in paths: canonical = checker.image_path('/' + runtime.rstrip('/')) require(canonical == '/usr' or (canonical.startswith('/usr/') and canonical != '/usr/local' and not canonical.startswith('/usr/local/')), 'New package lifecycle path requires a named adapter: ' + runtime) # The install root still has runtime /etc. Vendor-default relocation # occurs in the layer output, never in the shared installation tree. relative = canonical.removeprefix('/usr/') if canonical.startswith('/usr/etc/') else canonical.lstrip('/') if canonical == '/usr/etc': relative = 'etc' checker.objects.safe_parent(root, checker.relative(relative)) require((root / relative).exists() or (root / relative).is_symlink(), 'Installed package path is absent: ' + runtime) require(canonical not in sources, 'Aliased package paths require explicit accounting: ' + runtime) sources[canonical] = relative filename = row['filename'] require(filename and Path(filename).name == filename and filename not in ('.', '..'), 'Invalid archive filename') packages.append(dict(name=row['name'], version=row['version'], architecture=row['architecture'], archive=str(cache / filename), archive_sha256=row['archive_sha256'], dependencies=row['dependencies'], root=str(root), record=str(record), path_sources=sources)) return dict(version=0, snapshot_id=lock['snapshot_id'], source_root=str(source_root), packages=packages, requirement_bindings=lock['requirements']) if __name__ == '__main__': if len(sys.argv) != 7: raise SystemExit('Usage: capture-overlay.py COMPOSER_ADAPTER_DIR LOCK INSTALLED CACHE SOURCES NEW_REQUEST') spec = importlib.util.spec_from_file_location('artifact', Path(sys.argv[1]) / 'artifact.py') checker = importlib.util.module_from_spec(spec) spec.loader.exec_module(checker) def read(path): return json.loads(Path(path).read_bytes(), object_pairs_hook=checker.pairs, parse_float=checker.reject_number, parse_constant=checker.reject_number) result = capture(checker, read(sys.argv[2]), read(sys.argv[3]), Path(sys.argv[4]), Path(sys.argv[5])) with Path(sys.argv[6]).open('x') as stream: json.dump(result, stream, sort_keys=True, separators=(',', ':')) stream.write('\n')