#!/usr/bin/env python3 """Build the next numbered dev build and publish it to a test project's repository: lm-dev-build.py. The serial N in .dev-build.serial of the repository is that of the last dev build; each build that succeeds writes its own, one higher, and release.py writes 0 when it releases. The build's version is the newest changelog entry's with ~devN before its Debian revision, 0.41.0-1 becoming 0.41.0~dev7-1, so the release of that entry is newer than all its dev builds. ./build.sh builds it with CONTAINER_RUNTIME and DOCKER_CONTEXT from the environment; the build is moved to out/dev/, copied into the repository container of the project --project (LXD_PROJECT, lm-dev by default) made by lm-dev-lxd-project.py, and published there. The remote is the project's, read from tmp/lxd-project-PROJECT.json, so the script takes no --remote. """ import argparse import json import os import re import subprocess import sys from lm_dev import PROJECT, ROOT, lxc_exec SERIAL = ROOT / '.dev-build.serial' CHANGELOG = ROOT / 'debian-package' / 'debian' / 'changelog' def main(): parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument('--project', default=os.environ.get('LXD_PROJECT', PROJECT), help=f'the test project made by lm-dev-lxd-project.py, whose remote is used (LXD_PROJECT, ' f'default {PROJECT})') args = parser.parse_args() state_file = ROOT / 'tmp' / f'lxd-project-{args.project}.json' if not state_file.is_file(): sys.exit(f'no {state_file}; create the project with lm-dev-lxd-project.py create') state = json.loads(state_file.read_text(encoding='utf-8')) # The newest entry's first line is "package (version) distribution; urgency=...". match = re.match(r'(\S+) \(([^)]+)\)', CHANGELOG.read_text(encoding='utf-8')) if not match: sys.exit(f'no version in the first line of {CHANGELOG}') package, version = match.groups() number = int(SERIAL.read_text(encoding='utf-8').strip()) + 1 if SERIAL.is_file() else 1 upstream, dash, revision = version.rpartition('-') dev = f'{upstream}~dev{number}-{revision}' if dash else f'{version}~dev{number}' print(f'building {package} {dev}') if subprocess.run(['./build.sh', '--build-arg', f'DEB_VERSION={dev}'], cwd=ROOT).returncode != 0: sys.exit(f'the build of {dev} failed; the serial stays {number - 1}') SERIAL.write_text(f'{number}\n', encoding='utf-8') # The dev build leaves out/ to out/dev/, so out/ keeps the release builds. target = ROOT / 'out' / 'dev' target.mkdir(exist_ok=True) for artifact in (ROOT / 'out').glob(f'{package}_{dev}_*'): artifact.replace(target / artifact.name) deb = target / f'{package}_{dev}_all.deb' if not deb.is_file(): sys.exit(f'the build made no {deb.name}') pool = f'{state["remote"]}:repo/var/www/html/pool/main/{deb.name}' pushed = subprocess.run(['lxc', 'file', 'push', str(deb), pool, '--mode', '0644', '--project', state['project']], capture_output=True, text=True) if pushed.returncode != 0: sys.exit(f'could not copy {deb.name} into the repository: {pushed.stderr.strip()}') published = lxc_exec(state['remote'], state['project'], 'repo', 'linux-manager-repo-publish') if published.returncode != 0: sys.exit(f'the repository was not published: {published.stderr.strip()}') print(f'{package} {dev} is published at {state["repository"]} (resolute main)') if __name__ == '__main__': main()