From 8612d3d858fa108e5732a586d4e2d0227ae34422 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 16 Sep 2024 20:20:20 +0200 Subject: Merging upstream version 256.4. Signed-off-by: Daniel Baumann --- tools/fetch-distro.py | 126 ++++++++++++++++++++++++++++++++++++++++++++ tools/update-distro-hash.py | 89 ------------------------------- tools/vcs-tag.sh | 17 ++++++ 3 files changed, 143 insertions(+), 89 deletions(-) create mode 100755 tools/fetch-distro.py delete mode 100755 tools/update-distro-hash.py create mode 100755 tools/vcs-tag.sh (limited to 'tools') diff --git a/tools/fetch-distro.py b/tools/fetch-distro.py new file mode 100755 index 0000000..9fc5b1b --- /dev/null +++ b/tools/fetch-distro.py @@ -0,0 +1,126 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: LGPL-2.1-or-later + +""" +Check out pkg/{distribution}. +With -u, fetch commits, and if changed, commit the latest hash. +""" + +import argparse +import json +import shlex +import subprocess +from pathlib import Path + +def parse_args(): + p = argparse.ArgumentParser( + description=__doc__, + ) + p.add_argument( + 'distribution', + nargs='+', + ) + p.add_argument( + '--no-fetch', + dest='fetch', + action='store_false', + default=True, + ) + p.add_argument( + '--update', '-u', + action='store_true', + default=False, + ) + return p.parse_args() + +def read_config(distro: str): + cmd = ['mkosi', '--json', '-d', distro, 'summary'] + print(f"+ {shlex.join(cmd)}") + text = subprocess.check_output(cmd, text=True) + + data = json.loads(text) + images = {image["Image"]: image for image in data["Images"]} + return images["build"] + +def commit_file(distro: str, file: Path, commit: str, changes: str): + message = '\n'.join(( + f'mkosi: update {distro} commit reference', + '', + changes)) + + cmd = ['git', 'commit', '-m', message, str(file)] + print(f"+ {shlex.join(cmd)}") + subprocess.check_call(cmd) + +def checkout_distro(args, distro: str, config: dict): + dest = Path(f'pkg/{distro}') + if dest.exists(): + print(f'{dest} already exists.') + return + + url = config['Environment']['GIT_URL'] + branch = config['Environment']['GIT_BRANCH'] + + # Only debian uses source-git for now… + reference = [f'--reference-if-able=.'] if distro == 'debian' else [] + + cmd = [ + 'git', 'clone', url, + f'--branch={branch}', + dest.as_posix(), + *reference, + ] + print(f"+ {shlex.join(cmd)}") + subprocess.check_call(cmd) + + args.fetch = False # no need to fetch if we just cloned + +def update_distro(args, distro: str, config: dict): + branch = config['Environment']['GIT_BRANCH'] + old_commit = config['Environment']['GIT_COMMIT'] + + cmd = ['git', '-C', f'pkg/{distro}', 'switch', branch] + print(f"+ {shlex.join(cmd)}") + subprocess.check_call(cmd) + + cmd = ['git', '-C', f'pkg/{distro}', 'fetch', 'origin', '-v', + f'{branch}:remotes/origin/{branch}'] + print(f"+ {shlex.join(cmd)}") + subprocess.check_call(cmd) + + cmd = ['git', '-C', f'pkg/{distro}', 'rev-parse', f'refs/remotes/origin/{branch}'] + print(f"+ {shlex.join(cmd)}") + new_commit = subprocess.check_output(cmd, text=True).strip() + + if old_commit == new_commit: + print(f'{distro}: commit {new_commit!s} is still fresh') + return + + cmd = ['git', '-C', f'pkg/{distro}', 'log', '--graph', + '--pretty=oneline', '--no-decorate', '--abbrev-commit', '--abbrev=10', + f'{old_commit}..{new_commit}'] + print(f"+ {shlex.join(cmd)}") + changes = subprocess.check_output(cmd, text=True).strip() + + conf_dir = Path('mkosi.images/build/mkosi.conf.d') + files = conf_dir.glob('*/*.conf') + for file in files: + s = file.read_text() + if old_commit in s: + print(f'{distro}: {file}: found old hash, updating…') + new = s.replace(old_commit, new_commit) + assert new != s + file.write_text(new) + commit_file(distro, file, new_commit, changes) + break + else: + raise ValueError(f'{distro}: hash {new_commit} not found under {conf_dir}') + +if __name__ == '__main__': + args = parse_args() + + for distro in args.distribution: + config = read_config(distro) + checkout_distro(args, distro, config) + if args.update: + update_distro(args, distro, config) diff --git a/tools/update-distro-hash.py b/tools/update-distro-hash.py deleted file mode 100755 index 16ed2e7..0000000 --- a/tools/update-distro-hash.py +++ /dev/null @@ -1,89 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: LGPL-2.1-or-later - -""" -Fetch commits for pkg/{distribution} and, if changed, commit the latest hash. -""" - -import argparse -import json -import shlex -import subprocess -from pathlib import Path - -def parse_args(): - p = argparse.ArgumentParser( - description=__doc__, - ) - p.add_argument( - 'distribution', - nargs='+', - ) - p.add_argument( - '--no-fetch', - dest='fetch', - action='store_false', - default=True, - ) - return p.parse_args() - -def read_config(distro: str): - cmd = ['mkosi', '--json', '-d', distro, 'summary'] - print(f"+ {shlex.join(cmd)}") - text = subprocess.check_output(cmd, text=True) - - data = json.loads(text) - return data['Images'][-1] - -def commit_file(distro: str, file: Path, commit: str, changes: str): - message = '\n'.join(( - f'mkosi: update {distro} commit reference', - '', - changes)) - - cmd = ['git', 'commit', '-m', message, str(file)] - print(f"+ {shlex.join(cmd)}") - subprocess.check_call(cmd) - -def update_distro(args, distro: str): - cmd = ['git', '-C', f'pkg/{distro}', 'fetch'] - print(f"+ {shlex.join(cmd)}") - subprocess.check_call(cmd) - - config = read_config(distro) - - branch = config['Environment']['GIT_BRANCH'] - old_commit = config['Environment']['GIT_COMMIT'] - - cmd = ['git', '-C', f'pkg/{distro}', 'rev-parse', f'refs/remotes/origin/{branch}'] - print(f"+ {shlex.join(cmd)}") - new_commit = subprocess.check_output(cmd, text=True).strip() - - if old_commit == new_commit: - print(f'{distro}: commit {new_commit!s} is still fresh') - return - - cmd = ['git', '-C', f'pkg/{distro}', 'log', '--graph', - '--pretty=oneline', '--no-decorate', '--abbrev-commit', '--abbrev=10', - f'{old_commit}..{new_commit}'] - print(f"+ {shlex.join(cmd)}") - changes = subprocess.check_output(cmd, text=True).strip() - - conf_dir = Path('mkosi.images/system/mkosi.conf.d') - files = conf_dir.glob('*/*.conf') - for file in files: - s = file.read_text() - if old_commit in s: - print(f'{distro}: {file}: found old hash, updating…') - new = s.replace(old_commit, new_commit) - assert new != s - file.write_text(new) - commit_file(distro, file, new_commit, changes) - break - else: - raise ValueError(f'{distro}: hash {new_commit} not found under {conf_dir}') - -if __name__ == '__main__': - args = parse_args() - for distro in args.distribution: - update_distro(args, distro) diff --git a/tools/vcs-tag.sh b/tools/vcs-tag.sh new file mode 100755 index 0000000..5da39cc --- /dev/null +++ b/tools/vcs-tag.sh @@ -0,0 +1,17 @@ +#!/bin/bash +# SPDX-License-Identifier: LGPL-2.1-or-later +set -e + +MODE="$1" + +if ! [[ -d .git ]] || git describe --tags --exact-match &>/dev/null; then + exit 0 +fi + +if [[ "$MODE" == "developer" ]]; then + DIRTY="--dirty=^" +else + DIRTY="" +fi + +echo "-g$(git describe --abbrev=7 --match="" --always $DIRTY)" -- cgit v1.2.3