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:
Defame1297andClaude Sonnet 5.5 committed 2026-11-14 13:25:58 +00:00
1 parent 557f573df2
commit db1c1e176f
12 files changed
+1520 -427

No files matched your search

+90 -22
View File
@@ -36,6 +36,7 @@ added line fails: a pin change always has both, and the line diff is taken with
Exit codes: 0 ok or skipped, 1 a violation or a failure to run, 2 bad usage.
"""
import os
import re
import sys
@@ -48,12 +49,21 @@ PIN = re.compile(rf"(git\.rkdr\.net/mortis/[a-z0-9._-]+):{TAG}@sha256:[0-9a-f]{{
REQ_LINE = re.compile(r"^([A-Za-z0-9][A-Za-z0-9._-]*)==[0-9][0-9A-Za-z.+!_-]*$")
REV_LINE = re.compile(r"^\s*rev:\s*\S+\s*$")
VERSION_LINE = re.compile(
r"""^\s*(?:-\s*)?version:\s*["']?(?P<v>[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?)["']?\s*(?:#.*)?$""")
FROM_LINE = re.compile(rf"^(FROM\s+(?:--\S+\s+)*)([^\s:@]+)(?::{TAG})?@sha256:[0-9a-f]{{64}}(\s+AS\s+\S+)?\s*$")
r"""^\s*(?:-\s*)?version:\s*["']?(?P<v>[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?)["']?\s*(?:#.*)?$"""
)
FROM_LINE = re.compile(
rf"^(FROM\s+(?:--\S+\s+)*)([^\s:@]+)(?::{TAG})?@sha256:[0-9a-f]{{64}}(\s+AS\s+\S+)?\s*$"
)
UV_TOKEN = re.compile(r"\buv==[0-9][0-9A-Za-z.+!_-]*")
UBUNTU_LINE = re.compile(rf"^(\s*job_image:\s*ubuntu)(?::{TAG})?@sha256:[0-9a-f]{{64}}\s*$")
UBUNTU_LINE = re.compile(
rf"^(\s*job_image:\s*ubuntu)(?::{TAG})?@sha256:[0-9a-f]{{64}}\s*$"
)
DOCKERFILE = re.compile(r"images/[^/]+/Dockerfile")
CONSUMERS = {".gitea/workflows/lint.yml", ".gitea/workflows/build.yml", ".gitea/workflows/molecule.yml"}
CONSUMERS = {
".gitea/workflows/lint.yml",
".gitea/workflows/build.yml",
".gitea/workflows/molecule.yml",
}
def normalized(lines, pattern_sub):
@@ -111,15 +121,32 @@ def reduce_pin(line):
def file_problem(path, removed, added):
"""One-line reason why the change to `path` is not allowed for a bot, or None."""
if path == "requirements-dev.txt":
return same_shape(removed, added, reduce_requirement, "a pinned `name==version` line of the same package")
return same_shape(
removed,
added,
reduce_requirement,
"a pinned `name==version` line of the same package",
)
if path == ".pre-commit-config.yaml":
return same_shape(removed, added, reduce_rev, "a `rev:` line")
if DOCKERFILE.fullmatch(path):
return same_shape(removed, added, reduce_dockerfile, "the tag or digest of a FROM line or a `uv==` pin")
return same_shape(
removed,
added,
reduce_dockerfile,
"the tag or digest of a FROM line or a `uv==` pin",
)
if path == civ.MATRIX_FILE:
return same_shape(removed, added, reduce_image_yml, "a matrix `version:` or a `job_image:` ubuntu pin")
return same_shape(
removed,
added,
reduce_image_yml,
"a matrix `version:` or a `job_image:` ubuntu pin",
)
if path in CONSUMERS:
return same_shape(removed, added, reduce_pin, "an image pin of the mortis registry")
return same_shape(
removed, added, reduce_pin, "an image pin of the mortis registry"
)
return "is not a file a bot pull request may change"
@@ -173,7 +200,11 @@ def version_problem(removed, added):
return "moves or deletes a line (the changed lines must stay where they are)"
for (unused, old), (unused, new) in zip(removed, added):
old_m, new_m = VERSION_LINE.match(old), VERSION_LINE.match(new)
if old_m and new_m and civ.parse_version(new_m["v"]) <= civ.parse_version(old_m["v"]):
if (
old_m
and new_m
and civ.parse_version(new_m["v"]) <= civ.parse_version(old_m["v"])
):
return f"does not raise a version ({old_m['v']} -> {new_m['v']})"
return None
@@ -183,18 +214,26 @@ def check(entries, patch_of):
problems = []
for old_mode, new_mode, status, path in entries:
if status != "M" or old_mode != new_mode or new_mode != "100644":
problems.append(f"{path}: a bot pull request may not add, delete, rename or change the mode of a file")
problems.append(
f"{path}: a bot pull request may not add, delete, rename or change the mode of a file"
)
continue
patch = patch_of(path)
if BINARY.search(patch):
problems.append(f"{path}: git treats the change as binary, which cannot be checked line by line")
problems.append(
f"{path}: git treats the change as binary, which cannot be checked line by line"
)
continue
removed, added = parse_numbered(patch)
if not removed or not added:
problems.append(f"{path}: a pin change removes and adds at least one line; this diff has "
f"{len(removed)} removed and {len(added)} added (binary or empty change?)")
problems.append(
f"{path}: a pin change removes and adds at least one line; this diff has "
f"{len(removed)} removed and {len(added)} added (binary or empty change?)"
)
continue
reason = file_problem(path, [line for unused, line in removed], [line for unused, line in added])
reason = file_problem(
path, [line for unused, line in removed], [line for unused, line in added]
)
if not reason and path == civ.MATRIX_FILE:
reason = version_problem(removed, added)
if reason:
@@ -208,20 +247,49 @@ def run(argv):
return None
head_ref, base_ref = argv[1], argv[2]
if not head_ref and os.environ.get("EVENT_NAME") == "pull_request":
raise civ.CheckError("the head ref of a pull request is empty: cannot tell whether it is a bot "
"pull request, so failing closed")
raise civ.CheckError(
"the head ref of a pull request is empty: cannot tell whether it is a bot "
"pull request, so failing closed"
)
if not head_ref.startswith(PREFIX):
return [], "not a renovate/ branch, skipped"
if not civ.REF_RE.fullmatch(base_ref):
raise civ.CheckError(f"{base_ref!r} is not a plain ref name (letters, digits, . _ / -, no leading '-')")
raise civ.CheckError(
f"{base_ref!r} is not a plain ref name (letters, digits, . _ / -, no leading '-')"
)
base = civ.git("merge-base", "--end-of-options", base_ref, "HEAD").strip()
raw = civ.git("-c", "core.quotePath=false", "diff", "--raw", "-z", "--no-renames", "--abbrev=40",
"--end-of-options", base, "HEAD", raw=True)
raw = civ.git(
"-c",
"core.quotePath=false",
"diff",
"--raw",
"-z",
"--no-renames",
"--abbrev=40",
"--end-of-options",
base,
"HEAD",
raw=True,
)
def patch_of(path):
return civ.git("--literal-pathspecs", "-c", "core.quotePath=false", "diff", "-U0", "--no-renames",
"--no-color", "--text", "--no-ext-diff", "--no-textconv",
"--end-of-options", base, "HEAD", "--", path)
return civ.git(
"--literal-pathspecs",
"-c",
"core.quotePath=false",
"diff",
"-U0",
"--no-renames",
"--no-color",
"--text",
"--no-ext-diff",
"--no-textconv",
"--end-of-options",
base,
"HEAD",
"--",
path,
)
entries = parse_raw(raw)
return check(entries, patch_of), f"{len(entries)} file(s) within the bot allowance"