1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-16 10:50:18 +03:00
systemd/tools/update-distro-hash.py
Daan De Meyer 76e14148f6 mkosi: Introduce build image
We want the exitrd image to be built with the latest systemd as well.
As the exitrd image is built as part of mkosi.images, and all subimages
are built before the main image, this implies the packages must be built
as a subimage in mkosi.images/ as well. So we introduce the build image and
move all logic related to building distribution packages there.

This also has the nice side effect of slimming down the main image as the
build dependencies are not installed into the main image anymore. It also
makes sure the packages are built in a "clean" chroot without any of the
other packages which we install in the main image available.

(cherry picked from commit 7205fc7dc31eb2be3075ee6ba23ebe84324aa5cb)
2024-07-16 15:13:42 +02:00

91 lines
2.6 KiB
Python
Executable File

#!/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)
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 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/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:
update_distro(args, distro)