]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fetch-mkosi: apply "ruff format" and "ruff check --fix"
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 19 Feb 2026 17:09:19 +0000 (02:09 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 17 May 2026 17:44:47 +0000 (02:44 +0900)
This also makes CONFIG is closed when not necessary.

tools/fetch-mkosi.py

index 6e6440d65ef9c7d090300fc043f5b76818f6bd8f..481f607d40fcfe59a7937b83d8ff51825159d0b5 100755 (executable)
@@ -7,9 +7,9 @@ With -u, if changed, commit the latest hash.
 """
 
 import argparse
+import re
 import shlex
 import subprocess
-import re
 from pathlib import Path
 
 URL = 'https://github.com/systemd/mkosi'
@@ -17,6 +17,7 @@ BRANCH = 'main'  # We only want to ever use commits on upstream 'main' branch
 CONFIG = Path('mkosi/mkosi.conf')
 WORKFLOWS = [Path('.github/workflows') / f for f in ['mkosi.yml', 'coverage.yml', 'linter.yml']]
 
+
 def parse_args():
     p = argparse.ArgumentParser(
         description=__doc__,
@@ -26,59 +27,72 @@ def parse_args():
         type=Path,
     )
     p.add_argument(
-        '--update', '-u',
+        '--update',
+        '-u',
         action='store_true',
         default=False,
     )
     return p.parse_args()
 
+
 def read_config():
     print(f'Reading {CONFIG}…')
-    matches = [m.group(1)
-               for line in open(CONFIG)
-               if (m := re.match('^MinimumVersion=commit:([a-z0-9]{40})$',
-                                 line.strip()))]
+    c = CONFIG.read_text()
+    matches = [
+        m.group(1) for m in re.finditer('^\s*MinimumVersion=commit:([a-z0-9]{40})\s*$', c, re.MULTILINE)
+    ]
     assert len(matches) == 1
     return matches[0]
 
+
 def commit_file(files: list[Path], commit: str, changes: str):
-    message = '\n'.join((
-        f'mkosi: update mkosi ref to {commit}',
-        '',
-        changes))
+    message = '\n'.join((f'mkosi: update mkosi ref to {commit}', '', changes))
 
     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)
 
+
 def checkout_mkosi(args):
     if args.dir.exists():
         print(f'{args.dir} already exists.')
         return
 
     cmd = [
-        'git', 'clone', URL,
+        'git',
+        'clone',
+        URL,
         f'--branch={BRANCH}',
         args.dir.as_posix(),
     ]
-    print(f"+ {shlex.join(cmd)}")
+    print(f'+ {shlex.join(cmd)}')
     subprocess.check_call(cmd)
 
+
 def update_mkosi(args):
     old_commit = read_config()
 
     cmd = ['git', '-C', args.dir.as_posix(), 'rev-parse', f'refs/remotes/origin/{BRANCH}']
-    print(f"+ {shlex.join(cmd)}")
+    print(f'+ {shlex.join(cmd)}')
     new_commit = subprocess.check_output(cmd, text=True).strip()
 
     if old_commit == new_commit:
         print(f'mkosi: commit {new_commit!s} is still fresh')
         return
 
-    cmd = ['git', '-C', args.dir.as_posix(), 'log', '--graph', '--no-merges',
-           '--pretty=oneline', '--no-decorate', '--abbrev-commit', '--abbrev=10',
-           f'{old_commit}..{new_commit}']
-    print(f"+ {shlex.join(cmd)}")
+    cmd = [
+        'git',
+        '-C', args.dir.as_posix(),
+        'log',
+        '--graph',
+        '--no-merges',
+        '--pretty=oneline',
+        '--no-decorate',
+        '--abbrev-commit',
+        '--abbrev=10',
+        f'{old_commit}..{new_commit}',
+    ]  # fmt: skip
+    print(f'+ {shlex.join(cmd)}')
     changes = subprocess.check_output(cmd, text=True).strip()
 
     for f in [CONFIG, *WORKFLOWS]:
@@ -91,6 +105,7 @@ def update_mkosi(args):
 
     commit_file([CONFIG, *WORKFLOWS], new_commit, changes)
 
+
 if __name__ == '__main__':
     args = parse_args()
     checkout_mkosi(args)