fetch-distro: apply "ruff format"

This commit is contained in:
Yu Watanabe
2026-05-18 02:44:47 +09:00
parent 0df17e665b
commit 6c2bf5330b
+47 -28
View File
@@ -12,6 +12,7 @@ import shlex
import subprocess import subprocess
from pathlib import Path from pathlib import Path
def parse_args(): def parse_args():
p = argparse.ArgumentParser( p = argparse.ArgumentParser(
description=__doc__, description=__doc__,
@@ -27,34 +28,35 @@ def parse_args():
default=True, default=True,
) )
p.add_argument( p.add_argument(
'--update', '-u', '--update',
'-u',
action='store_true', action='store_true',
default=False, default=False,
) )
p.add_argument('--profile') p.add_argument('--profile')
return p.parse_args() return p.parse_args()
def read_config(distro: str): def read_config(distro: str):
cmd = ['mkosi', '--json', '-d', distro, '-f', 'summary'] cmd = ['mkosi', '--json', '-d', distro, '-f', 'summary']
if args.profile: if args.profile:
cmd += ['--profile', args.profile] cmd += ['--profile', args.profile]
print(f"+ {shlex.join(cmd)}") print(f'+ {shlex.join(cmd)}')
text = subprocess.check_output(cmd, text=True) text = subprocess.check_output(cmd, text=True)
data = json.loads(text) data = json.loads(text)
images = {image["Image"]: image for image in data["Images"]} images = {image['Image']: image for image in data['Images']}
return images["build"] return images['build']
def commit_file(distro: str, files: list[Path], commit: str, changes: str): def commit_file(distro: str, files: list[Path], commit: str, changes: str):
message = '\n'.join(( message = '\n'.join((f'mkosi: update {distro} commit reference to {commit}', '', changes))
f'mkosi: update {distro} commit reference to {commit}',
'',
changes))
cmd = ['git', 'commit', '-m', message, *(str(file) for file in files)] cmd = ['git', 'commit', '-m', message, *(str(file) for file in files)]
print(f"+ {shlex.join(cmd)}") print(f'+ {shlex.join(cmd)}')
subprocess.check_call(cmd) subprocess.check_call(cmd)
def checkout_distro(args, distro: str, config: dict): def checkout_distro(args, distro: str, config: dict):
url = config['Environment']['GIT_URL'] url = config['Environment']['GIT_URL']
branch = config['Environment']['GIT_BRANCH'] branch = config['Environment']['GIT_BRANCH']
@@ -74,28 +76,30 @@ def checkout_distro(args, distro: str, config: dict):
reference = ['--reference-if-able=.'] if distro == 'debian' else [] reference = ['--reference-if-able=.'] if distro == 'debian' else []
cmd = [ cmd = [
'git', 'clone', url, 'git',
'clone',
url,
f'--branch={branch}', f'--branch={branch}',
*sparse, *sparse,
dest.as_posix(), dest.as_posix(),
*reference, *reference,
] ]
print(f"+ {shlex.join(cmd)}") print(f'+ {shlex.join(cmd)}')
subprocess.check_call(cmd) subprocess.check_call(cmd)
# Sparse checkout if the package is in a subdirectory # Sparse checkout if the package is in a subdirectory
if subdir is not None: if subdir is not None:
cmd = ['git', '-C', f'pkg/{pkg_subdir}', 'sparse-checkout', 'set', cmd = ['git', '-C', f'pkg/{pkg_subdir}', 'sparse-checkout', 'set', '--no-cone', f'{subdir}']
'--no-cone', f'{subdir}'] print(f'+ {shlex.join(cmd)}')
print(f"+ {shlex.join(cmd)}")
subprocess.check_call(cmd) subprocess.check_call(cmd)
cmd = ['git', '-C', f'pkg/{pkg_subdir}', 'checkout', 'HEAD'] cmd = ['git', '-C', f'pkg/{pkg_subdir}', 'checkout', 'HEAD']
print(f"+ {shlex.join(cmd)}") print(f'+ {shlex.join(cmd)}')
subprocess.check_call(cmd) subprocess.check_call(cmd)
args.fetch = False # no need to fetch if we just cloned args.fetch = False # no need to fetch if we just cloned
def update_distro(args, distro: str, config: dict): def update_distro(args, distro: str, config: dict):
branch = config['Environment']['GIT_BRANCH'] branch = config['Environment']['GIT_BRANCH']
subdir = config['Environment'].get('GIT_SUBDIR') subdir = config['Environment'].get('GIT_SUBDIR')
@@ -103,32 +107,46 @@ def update_distro(args, distro: str, config: dict):
pkg_subdir = config['Environment']['PKG_SUBDIR'] pkg_subdir = config['Environment']['PKG_SUBDIR']
if args.fetch: if args.fetch:
cmd = ['git', '-C', f'pkg/{pkg_subdir}', 'fetch', 'origin', '-v', cmd = [
f'{branch}:remotes/origin/{branch}'] 'git',
print(f"+ {shlex.join(cmd)}") '-C', f'pkg/{pkg_subdir}',
'fetch',
'origin',
'-v',
f'{branch}:remotes/origin/{branch}',
] # fmt: skip
print(f'+ {shlex.join(cmd)}')
subprocess.check_call(cmd) subprocess.check_call(cmd)
cmd = ['git', '-C', f'pkg/{pkg_subdir}', 'switch', branch] cmd = ['git', '-C', f'pkg/{pkg_subdir}', 'switch', branch]
print(f"+ {shlex.join(cmd)}") print(f'+ {shlex.join(cmd)}')
subprocess.check_call(cmd) subprocess.check_call(cmd)
cmd = ['git', '-C', f'pkg/{pkg_subdir}', 'log', '-n1', '--format=%H', cmd = ['git', '-C', f'pkg/{pkg_subdir}', 'log', '-n1', '--format=%H', f'refs/remotes/origin/{branch}']
f'refs/remotes/origin/{branch}']
if subdir is not None: if subdir is not None:
cmd += [f'{subdir}'] cmd += [f'{subdir}']
print(f"+ {shlex.join(cmd)}") print(f'+ {shlex.join(cmd)}')
new_commit = subprocess.check_output(cmd, text=True).strip() new_commit = subprocess.check_output(cmd, text=True).strip()
if old_commit == new_commit: if old_commit == new_commit:
print(f'{pkg_subdir}: commit {new_commit!s} is still fresh') print(f'{pkg_subdir}: commit {new_commit!s} is still fresh')
return return
cmd = ['git', '-C', f'pkg/{pkg_subdir}', 'log', '--graph', '--no-merges', cmd = [
'--pretty=oneline', '--no-decorate', '--abbrev-commit', '--abbrev=10', 'git',
f'{old_commit}..{new_commit}'] '-C', f'pkg/{pkg_subdir}',
'log',
'--graph',
'--no-merges',
'--pretty=oneline',
'--no-decorate',
'--abbrev-commit',
'--abbrev=10',
f'{old_commit}..{new_commit}',
] # fmt: skip
if subdir is not None: if subdir is not None:
cmd += [f'{subdir}'] cmd += [f'{subdir}']
print(f"+ {shlex.join(cmd)}") print(f'+ {shlex.join(cmd)}')
changes = subprocess.check_output(cmd, text=True).strip() changes = subprocess.check_output(cmd, text=True).strip()
conf_dir = Path('mkosi/mkosi.pkgenv/mkosi.conf.d') conf_dir = Path('mkosi/mkosi.pkgenv/mkosi.conf.d')
@@ -142,8 +160,8 @@ def update_distro(args, distro: str, config: dict):
file.write_text(new) file.write_text(new)
tocommit = [file] tocommit = [file]
if distro == "fedora": if distro == 'fedora':
packit = Path(".packit.yml") packit = Path('.packit.yml')
s = packit.read_text() s = packit.read_text()
assert old_commit in s assert old_commit in s
new = s.replace(old_commit, new_commit) new = s.replace(old_commit, new_commit)
@@ -155,6 +173,7 @@ def update_distro(args, distro: str, config: dict):
else: else:
raise ValueError(f'{distro}: hash {new_commit} not found under {conf_dir}') raise ValueError(f'{distro}: hash {new_commit} not found under {conf_dir}')
if __name__ == '__main__': if __name__ == '__main__':
args = parse_args() args = parse_args()