style(scripts): format the scripts and their tests with ruff
Output of `ruff format` and nothing else, in its own commit so the diff is reviewable (#112). The ruff-check hook is skipped for this commit only: its findings are fixed in the next one. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_014uGZTNmHMc85bomfevwUDG
This commit is contained in:
1 parent
557f573df2
commit
db1c1e176f
12 files changed
+1520
-427
No files matched your search
@@ -27,6 +27,7 @@ request (build on main, then the pin follow-up).
|
||||
|
||||
Prints one line. Exits 1 with an `::error::` line on a failure, 2 on bad usage.
|
||||
"""
|
||||
|
||||
import pathlib
|
||||
import re
|
||||
import sys
|
||||
@@ -37,8 +38,12 @@ DEFAULT_BASE = "origin/main"
|
||||
USAGE = "usage: bump_image_versions.py [<base-ref>]\n"
|
||||
INCLUDE_RE = re.compile(r"^\s*include:\s*(?:#.*)?$")
|
||||
ITEM_RE = re.compile(r"^(?P<dash>\s*)-(?P<gap>\s+)(?P<rest>\S.*)$")
|
||||
NAME_KEY_RE = re.compile(r"^(?P<ind>\s*)name:\s*[\"']?(?P<name>[^\"'\s#]+)[\"']?\s*(?:#.*)?$")
|
||||
VERSION_LINE_RE = re.compile(r"^(?P<head>\s*(?:-\s*)?version:\s*)(?P<q>[\"']?)(?P<value>[^\"'\s#]+)(?P=q)(?P<tail>.*)$")
|
||||
NAME_KEY_RE = re.compile(
|
||||
r"^(?P<ind>\s*)name:\s*[\"']?(?P<name>[^\"'\s#]+)[\"']?\s*(?:#.*)?$"
|
||||
)
|
||||
VERSION_LINE_RE = re.compile(
|
||||
r"^(?P<head>\s*(?:-\s*)?version:\s*)(?P<q>[\"']?)(?P<value>[^\"'\s#]+)(?P=q)(?P<tail>.*)$"
|
||||
)
|
||||
|
||||
|
||||
def next_patch(version):
|
||||
@@ -53,7 +58,9 @@ def _entries(lines):
|
||||
list cannot be located unambiguously."""
|
||||
heads = [n for n, line in enumerate(lines) if INCLUDE_RE.match(line.rstrip("\r\n"))]
|
||||
if len(heads) != 1:
|
||||
raise civ.CheckError(f"cannot find exactly one `include:` list in {civ.MATRIX_FILE}")
|
||||
raise civ.CheckError(
|
||||
f"cannot find exactly one `include:` list in {civ.MATRIX_FILE}"
|
||||
)
|
||||
starts, dash_indent = [], None
|
||||
end = len(lines)
|
||||
for n in range(heads[0] + 1, len(lines)):
|
||||
@@ -64,14 +71,18 @@ def _entries(lines):
|
||||
indent = len(body) - len(body.lstrip())
|
||||
if dash_indent is None:
|
||||
if not m:
|
||||
raise civ.CheckError(f"the `include:` list of {civ.MATRIX_FILE} is not a block list of mappings")
|
||||
raise civ.CheckError(
|
||||
f"the `include:` list of {civ.MATRIX_FILE} is not a block list of mappings"
|
||||
)
|
||||
dash_indent = indent
|
||||
if indent < dash_indent:
|
||||
end = n
|
||||
break
|
||||
if indent == dash_indent:
|
||||
if not m:
|
||||
raise civ.CheckError(f"cannot parse the `include:` list of {civ.MATRIX_FILE}")
|
||||
raise civ.CheckError(
|
||||
f"cannot parse the `include:` list of {civ.MATRIX_FILE}"
|
||||
)
|
||||
starts.append(n)
|
||||
if not starts:
|
||||
raise civ.CheckError(f"the `include:` list of {civ.MATRIX_FILE} is empty")
|
||||
@@ -96,8 +107,10 @@ def _entry_lines(lines, start, end):
|
||||
elif VERSION_LINE_RE.match(body):
|
||||
versions.append(n)
|
||||
if len(names) != 1 or len(versions) != 1:
|
||||
raise civ.CheckError(f"cannot parse a matrix entry of {civ.MATRIX_FILE} (line {start + 1}) "
|
||||
"unambiguously: expected one `name:` and one `version:`")
|
||||
raise civ.CheckError(
|
||||
f"cannot parse a matrix entry of {civ.MATRIX_FILE} (line {start + 1}) "
|
||||
"unambiguously: expected one `name:` and one `version:`"
|
||||
)
|
||||
return names[0], versions[0]
|
||||
|
||||
|
||||
@@ -112,13 +125,17 @@ def rewrite(text, new_versions):
|
||||
targets[name] = version_index
|
||||
missing = sorted(set(new_versions) - set(targets))
|
||||
if missing:
|
||||
raise civ.CheckError(f"no `version:` line found for image {', '.join(missing)} in {civ.MATRIX_FILE}")
|
||||
raise civ.CheckError(
|
||||
f"no `version:` line found for image {', '.join(missing)} in {civ.MATRIX_FILE}"
|
||||
)
|
||||
for name, n in targets.items():
|
||||
line = lines[n]
|
||||
body = line.rstrip("\r\n")
|
||||
v = VERSION_LINE_RE.match(body)
|
||||
q = v["q"]
|
||||
lines[n] = f"{v['head']}{q}{new_versions[name]}{q}{v['tail']}" + line[len(body):]
|
||||
lines[n] = (
|
||||
f"{v['head']}{q}{new_versions[name]}{q}{v['tail']}" + line[len(body) :]
|
||||
)
|
||||
return "".join(lines)
|
||||
|
||||
|
||||
@@ -129,7 +146,9 @@ def plan(base_matrix, head_matrix, changed, read_head_file):
|
||||
old = base_matrix.get(name)
|
||||
if old is None:
|
||||
continue # a new image has no earlier version to collide with
|
||||
if civ.parse_version(head_matrix[name]["version"]) > civ.parse_version(old["version"]):
|
||||
if civ.parse_version(head_matrix[name]["version"]) > civ.parse_version(
|
||||
old["version"]
|
||||
):
|
||||
continue # already bumped
|
||||
new_versions[name] = next_patch(old["version"])
|
||||
return new_versions
|
||||
@@ -147,7 +166,9 @@ def run(argv):
|
||||
return None
|
||||
ref = argv[1] if len(argv) == 2 else DEFAULT_BASE
|
||||
if not civ.REF_RE.fullmatch(ref):
|
||||
raise civ.CheckError(f"{ref!r} is not a plain ref name (letters, digits, . _ / -, no leading '-')")
|
||||
raise civ.CheckError(
|
||||
f"{ref!r} is not a plain ref name (letters, digits, . _ / -, no leading '-')"
|
||||
)
|
||||
base, changed = civ.changed_paths(ref, target=None)
|
||||
try:
|
||||
base_matrix = civ.matrix_of(civ.git("show", f"{base}:{civ.MATRIX_FILE}"))
|
||||
@@ -156,17 +177,28 @@ def run(argv):
|
||||
path = pathlib.Path(civ.MATRIX_FILE)
|
||||
text = read_text(path)
|
||||
head_matrix = civ.matrix_of(text)
|
||||
new_versions = plan(base_matrix, head_matrix, changed, lambda p: read_text(pathlib.Path(p)))
|
||||
new_versions = plan(
|
||||
base_matrix, head_matrix, changed, lambda p: read_text(pathlib.Path(p))
|
||||
)
|
||||
if not new_versions:
|
||||
return "image versions: nothing to bump"
|
||||
new_text = rewrite(text, new_versions)
|
||||
expected = {name: new_versions.get(name, entry["version"]) for name, entry in head_matrix.items()}
|
||||
if {name: entry["version"] for name, entry in civ.matrix_of(new_text).items()} != expected:
|
||||
raise civ.CheckError(f"the rewrite of {civ.MATRIX_FILE} would not give the planned versions; edit it by hand")
|
||||
expected = {
|
||||
name: new_versions.get(name, entry["version"])
|
||||
for name, entry in head_matrix.items()
|
||||
}
|
||||
if {
|
||||
name: entry["version"] for name, entry in civ.matrix_of(new_text).items()
|
||||
} != expected:
|
||||
raise civ.CheckError(
|
||||
f"the rewrite of {civ.MATRIX_FILE} would not give the planned versions; edit it by hand"
|
||||
)
|
||||
with open(path, "w", encoding="utf-8", newline="") as handle:
|
||||
handle.write(new_text)
|
||||
return "image versions: bumped " + ", ".join(
|
||||
f"{name} {head_matrix[name]['version']} -> {new}" for name, new in sorted(new_versions.items()))
|
||||
f"{name} {head_matrix[name]['version']} -> {new}"
|
||||
for name, new in sorted(new_versions.items())
|
||||
)
|
||||
|
||||
|
||||
def main(argv):
|
||||
|
||||
Reference in new issue
Block a user