#!/usr/bin/python3
"""Shared, offline OBS build driver. Configure/build/postinstall mkosi hooks."""
import hashlib
import json
import os
from pathlib import Path
import re
import shlex
import subprocess
import sys
import tempfile

TOKEN = re.compile(r"[a-z0-9][a-z0-9._+-]*\Z")
PACKAGE = re.compile(r"[A-Za-z0-9][A-Za-z0-9._+-]*\Z")
QUERY = r"%{NAME}\t%{EPOCHNUM}:%{VERSION}-%{RELEASE}\t%{ARCH}\n"


def release_fields(text):
    fields = {}
    for line in text.splitlines():
        if not line or line.startswith('#'):
            continue
        key, sep, value = line.partition('=')
        words = shlex.split(value)
        if not sep or len(words) != 1:
            raise ValueError('Invalid os-release field')
        fields[key] = words[0]
    return fields


def packages(root):
    result = subprocess.run(['rpm', '--root', str(root), '--dbpath',
                             '/usr/lib/sysimage/rpm', '-qa', '--qf', QUERY],
                            check=True, capture_output=True, text=True)
    inventory = {}
    for line in result.stdout.splitlines():
        name, version, arch = line.split('\t')
        inventory[f'{name}.{arch}'] = version
    if not inventory:
        raise ValueError('Empty base RPM inventory')
    return inventory


def discover(directory, architecture):
    entries = []
    seen = set()
    for path in sorted(directory.glob('*.json')):
        entry = json.loads(path.read_text())
        if entry.get('format') != 1:
            raise ValueError(f'Unsupported descriptor: {path}')
        if entry['architecture'] != architecture:
            continue
        for field in ('image', 'version', 'architecture'):
            if not TOKEN.fullmatch(entry[field]):
                raise ValueError(f'Invalid build-root {field}')
        if entry['image'] in seen:
            raise ValueError(f'Multiple installed roots for {entry["image"]}')
        seen.add(entry['image'])
        archive = directory / entry['filename']
        if archive.parent != directory or not archive.is_file() or archive.is_symlink():
            raise ValueError(f'Invalid build-root archive: {archive}')
        with archive.open('rb') as source:
            if hashlib.file_digest(source, 'sha256').hexdigest() != entry['sha256']:
                raise ValueError(f'Build-root checksum mismatch: {archive}')
        if not entry.get('packages') or not entry['release'].get('ID'):
            raise ValueError('Incomplete build-root descriptor')
        check_release(entry['release'], entry)
        entries.append((entry, archive))
    if not entries:
        raise ValueError(f'No installed sysext build roots for {architecture}')
    return entries


def check_release(release, entry):
    for key, value in {'IMAGE_ID': entry['image'], 'IMAGE_VERSION': entry['version'],
                       'SYSEXT_LEVEL': f'{entry["image"]}-{entry["version"]}'}.items():
        if release.get(key) != value:
            raise ValueError(f'Base {key} mismatch: expected {value!r}')
    if release.get('ID') in (None, '', '_any'):
        raise ValueError('Base must have a concrete distribution ID')


def check_packages(baseline, installed):
    changed = [name for name, version in baseline.items() if installed.get(name) != version]
    if changed:
        raise ValueError('Extension replaces/removes inherited RPMs: ' + ', '.join(changed[:20]))


def version_package(name, directory, architecture):
    """Read the selected offline RPM."""
    if not PACKAGE.fullmatch(name):
        raise ValueError('Invalid SYSEXT_VERSION_PACKAGE')
    candidates = sorted(p for p in directory.glob('*.rpm')
                        if p.name == name + '.rpm' or p.name.startswith(name + '-'))
    if not candidates:
        raise ValueError(f'No offline RPM for version package {name}')
    query = r'%{NAME}\t%{VERSION}\t%{RELEASE}\t%{EPOCHNUM}\t%{ARCH}\n'
    result = subprocess.run(['rpm', '-qp', '--qf', query, *map(str, candidates)],
                            check=True, capture_output=True, text=True)
    rpm_arch = {'x86-64': 'x86_64', 'arm64': 'aarch64', 'ppc64-le': 'ppc64le',
                'arm': 'armv7hl'}.get(architecture, architecture)
    found = {}
    for line in result.stdout.splitlines():
        package, version, release, epoch, arch = line.split('\t')
        if package != name or arch not in (rpm_arch, 'noarch'):
            continue
        identity = dict(name=package, version=version, release=release, epoch=epoch,
                        architecture=arch)
        found[tuple(identity.values())] = identity
    if len(found) != 1:
        raise ValueError(f'Expected one selected {name} RPM, found {len(found)}')
    return next(iter(found.values()))


def extension_version(base_version):
    # The OS and every optional sysupdate feature must extract the same @v.
    if not re.fullmatch(r'[0-9][a-z0-9.-]*', base_version):
        raise ValueError('Invalid lockstep OS image version')
    return base_version


def check_primary(primary, installed):
    if primary:
        key = f'{primary["name"]}.{primary["architecture"]}'
        expected = f'{primary["epoch"]}:{primary["version"]}-{primary["release"]}'
        if installed.get(key) != expected:
            raise ValueError('Installed version package differs from the RPM used for naming')


def configure():
    config = json.load(sys.stdin)
    requested = config['Packages']
    if not requested or any(not PACKAGE.fullmatch(p) for p in requested):
        raise ValueError('Specify extension RPM names in Packages=')
    if not TOKEN.fullmatch(config['ImageId']):
        raise ValueError('Invalid extension ImageId')
    config.setdefault('Environment', {})['SYSEXT_PACKAGES'] = ' '.join(requested)
    config['Packages'] = []
    config['BuildPackages'] = []
    json.dump(config, sys.stdout)


def render(entry, archive, prefix, version, requested, epoch):
    # Use a common output prefix so upstream's follow-up signing recipe carries
    # every variant forward, including its os-release and repart definitions.
    return f'''[Config]
MinimumVersion=27
[Include]
Include=mkosi-obs
[Distribution]
Distribution={entry['distribution']}
Release={entry['distribution_release']}
[Output]
Format=sysext
Overlay=yes
CompressOutput=zstd
ImageId={prefix}-{entry['image']}
ImageVersion={version}
Output={prefix}-{entry['image']}_%v_%a
ManifestFormat=
SplitArtifacts=roothash,os-release,repart-definitions
[Build]
WithNetwork=never
Environment=SYSEXT_SCOPE=system
SandboxTrees=/.build.binaries:/.build.binaries
BuildSources=
    .
    /usr/src/packages/OTHER:usr/src/packages/OTHER
[Content]
BaseTrees={archive}
Bootable=no
WithRecommends=no
CleanPackageMetadata=yes
SELinuxRelabel=no
SourceDateEpoch={epoch}
PostInstallationScripts=/usr/libexec/sysext-build
FinalizeScripts=/usr/libexec/sysext-build
Packages={' '.join(requested)}
'''


def postinstall():
    baseline = json.loads((Path(os.environ['SRCDIR']) / 'baseline.json').read_text())
    root = Path(os.environ['BUILDROOT'])
    check_release(release_fields((root / 'usr/lib/os-release').read_text()), baseline)
    installed = packages(root)
    check_packages(baseline['packages'], installed)
    check_primary(baseline.get('primary'), installed)
    for requested in baseline['requested']:
        matching = [p for p in installed if p.rsplit('.', 1)[0] == requested]
        if not matching:
            raise ValueError(f'Requested RPM missing: {requested}')
        # Bundles describe required tools, some of which may already be supplied
        # by a particular desktop base. Overlay=yes excludes those base files.
        if all(p in baseline['packages'] for p in matching):
            print(f'sysext: requested RPM already supplied by base: {requested}', flush=True)
    added = {name: version for name, version in installed.items()
             if name not in baseline['packages']}
    if not added:
        raise ValueError('All requested RPMs are already in the base; no extension is needed')
    (Path(os.environ['SRCDIR']) / 'added-packages.json').write_text(json.dumps(added))
    print('sysext: base identity and inherited RPM versions unchanged', flush=True)


def finalize():
    source = Path(os.environ['SRCDIR'])
    baseline = json.loads((source / 'baseline.json').read_text())
    config = json.loads(Path(os.environ['MKOSI_CONFIG']).read_text())
    release_path = (Path(os.environ['BUILDROOT']) / 'usr/lib/extension-release.d' /
                    f'extension-release.{config["Output"]}')
    release = release_fields(release_path.read_text())
    expected = dict(ID=baseline['release']['ID'],
                    SYSEXT_LEVEL=f'{baseline["image"]}-{baseline["version"]}',
                    SYSEXT_ID=config['ImageId'], SYSEXT_VERSION_ID=config['ImageVersion'],
                    ARCHITECTURE=baseline['architecture'], SYSEXT_SCOPE='system')
    if any(release.get(key) != value for key, value in expected.items()):
        raise ValueError('Generated extension-release does not match the named build')
    raw_name = config['Output'] + '.raw'
    metadata = dict(release, schema_version=1, image_name=raw_name,
                    download_name=raw_name + '.zst', build_version=baseline['build_version'],
                    base_image=dict(id=baseline['image'], version=baseline['version'],
                                    buildroot_sha256=baseline['sha256']),
                    version_package=baseline.get('primary'),
                    packages=json.loads((source / 'added-packages.json').read_text()))
    (Path(os.environ['OUTPUTDIR']) / (config['Output'] + '.json')).write_text(
        json.dumps(metadata, indent=2, sort_keys=True) + '\n')


def collect_signing_requests(archive, allowed, expected, requests):
    """Read the native mkosi-obs archive."""
    archive = Path(archive)
    if not archive.is_file():
        raise ValueError('Native mkosi-obs did not stage a signing archive')
    listing = subprocess.run(['cpio', '--quiet', '-it'], input=archive.read_bytes(),
                             capture_output=True, check=True).stdout.decode('utf-8').splitlines()
    found = set()
    for original in listing:
        name = original.removeprefix('./')
        if name not in allowed or name in found:
            raise ValueError(f'Unexpected or duplicate signing request: {original}')
        found.add(name)
        value = subprocess.run(['cpio', '--quiet', '-i', '--to-stdout', original],
                               input=archive.read_bytes(), capture_output=True, check=True).stdout
        if not re.fullmatch(rb'[0-9a-fA-F]{64,128}\n?', value):
            raise ValueError(f'Invalid staged root hash: {name}')
        if name in requests and requests[name] != value:
            raise ValueError(f'Conflicting staged root hash: {name}')
        requests[name] = value
    if expected not in found:
        raise ValueError(f'Missing signing request for {expected}')


def write_signing_requests(archive, requests, expected):
    """Replace the last nested build's archive with the complete request set."""
    if set(requests) != expected:
        raise ValueError('Signing archive does not contain exactly the expected root hashes')
    with tempfile.TemporaryDirectory(prefix='sysext-signing-requests-') as directory:
        work = Path(directory)
        for name, value in requests.items():
            path = work / name
            path.parent.mkdir(parents=True, exist_ok=True)
            path.write_bytes(value)
        names = b''.join(os.fsencode(name) + b'\0' for name in sorted(requests))
        packed = subprocess.run(['cpio', '--quiet', '-o', '-H', 'newc', '--null'],
                                cwd=work, input=names, capture_output=True, check=True).stdout
        with tempfile.NamedTemporaryFile(dir=Path(archive).parent, prefix='.signing-', delete=False) as target:
            staged = Path(target.name)
            try:
                target.write(packed)
                os.fchmod(target.fileno(), 0o644)
                target.flush()
                os.fsync(target.fileno())
                os.replace(staged, archive)
            finally:
                staged.unlink(missing_ok=True)


def build():
    config = json.loads(Path(os.environ['MKOSI_CONFIG']).read_text())
    requested = os.environ['SYSEXT_PACKAGES'].split()
    prefix, version = config['ImageId'], config['ImageVersion']
    if not TOKEN.fullmatch(prefix) or not TOKEN.fullmatch(version):
        raise ValueError('Invalid extension identity/version')
    if any(not PACKAGE.fullmatch(p) for p in requested):
        raise ValueError('Invalid extension RPM name')
    entries = discover(Path('/usr/share/sysext-buildroots'), config['Architecture'])
    package_name = os.environ.get('SYSEXT_VERSION_PACKAGE')
    if not package_name and len(requested) == 1:
        package_name = requested[0]
    if package_name and package_name not in requested:
        raise ValueError('SYSEXT_VERSION_PACKAGE must be in Packages=')
    primary = version_package(package_name, Path('/.build.binaries'), config['Architecture']) \
        if package_name else None
    output = Path(os.environ['SRCDIR']) / 'usr/src/packages/OTHER'
    epoch = config.get('SourceDateEpoch')
    if epoch is None:
        import time
        epoch = int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))
    expected_requests = {
        f'roothashes/{prefix}-{entry["image"]}_{extension_version(entry["version"])}_{config["Architecture"]}.roothash'
        for entry, _ in entries
    }
    if len(expected_requests) != len(entries):
        raise ValueError('Duplicate extension signing request names')
    requests = {}
    for entry, archive in entries:
        child_version = extension_version(entry['version'])
        print(f'sysext: building {prefix} for {entry["image"]} {entry["version"]}', flush=True)
        with tempfile.TemporaryDirectory(prefix='sysext-build-') as directory:
            work = Path(directory)
            (work / 'baseline.json').write_text(json.dumps(dict(
                entry, requested=requested, primary=primary, build_version=version)))
            (work / 'mkosi.conf').write_text(render(entry, archive, prefix, child_version, requested, epoch))
            subprocess.run(['mkosi', '--directory=' + directory,
                            '--output-dir=' + str(output), 'build'], check=True)
        expected = f'roothashes/{prefix}-{entry["image"]}_{child_version}_{config["Architecture"]}.roothash'
        collect_signing_requests(output / 'hashes.cpio.rsasign', expected_requests, expected, requests)
    write_signing_requests(output / 'hashes.cpio.rsasign', requests, expected_requests)
    recipe = output / 'mkosi.conf'
    if not recipe.is_file() or not (output / 'hashes.cpio.rsasign').is_file():
        raise ValueError('Native mkosi-obs did not stage the signing continuation')
    text, count = re.subn(r'^ImageId=.*$', 'ImageId=' + prefix, recipe.read_text(), flags=re.M)
    if count != 1:
        raise ValueError('Unexpected native signing recipe')
    recipe.write_text(text)
    (output / f'{prefix}.buildroots.json').write_text(json.dumps(
        [dict(image=e['image'], version=e['version'], sha256=e['sha256']) for e, _ in entries], indent=2) + '\n')


if __name__ == '__main__':
    try:
        if sys.argv[1:] == ['final']:
            postinstall()
        elif os.environ.get('SCRIPT') == '/work/finalize':
            finalize()
        elif 'MKOSI_CONFIG' in os.environ and 'BUILDROOT' in os.environ:
            build()
        else:
            configure()
    except (ValueError, KeyError, OSError, subprocess.CalledProcessError) as error:
        sys.exit(f'sysext-build: {error}')
