Files
linux-cloud-router/bin/lm-dev-build.py
T

76 lines
3.6 KiB
Python
Executable File

#!/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, the setting LXD_DEV_BUILD_SERIAL (lm_dev.setting, 0 by default), is that of the last
dev build; each build that succeeds writes its own, one higher, into .lm-dev-config/settings.json,
and release.py writes 0 there when it releases. The build's version is the Debian version of the
newest entry of CHANGELOG.md (bin/changelog.py) with ~devN before its revision, 0.41.0 (0.41.0-1)
becoming 0.41.0~dev7-1, so the release of that entry is newer than all its dev builds. ./build.py 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
.lm-dev-config/lxd-project-PROJECT.json, so the script takes no --remote.
"""
import argparse
import json
import subprocess
import sys
import changelog
from lm_dev import PROJECT, ROOT, config_path, lxc_exec, save_setting, setting
CHANGELOG = ROOT / 'CHANGELOG.md'
CONTROL = ROOT / 'debian-package' / 'debian' / 'control'
def main():
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument('--project', default=setting('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 = config_path(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'))
try:
found = changelog.entries(CHANGELOG.read_text(encoding='utf-8'))
package = changelog.control_field(CONTROL.read_text(encoding='utf-8'), 'Source')
except ValueError as error:
sys.exit(f'{CHANGELOG}: {error}')
if not found:
sys.exit(f'{CHANGELOG} has no entry')
number = int(setting('LXD_DEV_BUILD_SERIAL', 0)) + 1
upstream, _, revision = changelog.debian_version(found[0]['version']).rpartition('-')
dev = f'{upstream}~dev{number}-{revision}'
print(f'building {package} {dev}')
if subprocess.run(['./build.py', '--build-arg', f'DEB_VERSION={dev}'], cwd=ROOT).returncode != 0:
sys.exit(f'the build of {dev} failed; the serial stays {number - 1}')
save_setting('LXD_DEV_BUILD_SERIAL', number)
# 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()