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

+46 -12
View File
@@ -39,6 +39,7 @@ Exit codes: 0 ok (an empty selection from an all-ignored diff is ok), 1 no scena
all, or a changed role has none, 2 bad usage. Notices go to stderr as `::notice::` lines, with
control characters in paths escaped so a crafted file name cannot inject a log command.
"""
import fnmatch
import os
import subprocess
@@ -86,7 +87,10 @@ IGNORED = (
def printable(text):
"""`text` with control characters (a newline in a file name) escaped, for a log line."""
return "".join(c if c.isprintable() else c.encode("unicode_escape").decode("ascii") for c in text)
return "".join(
c if c.isprintable() else c.encode("unicode_escape").decode("ascii")
for c in text
)
def discover(root="."):
@@ -95,7 +99,9 @@ def discover(root="."):
ext = os.path.join(root, "extensions", "molecule")
if os.path.isdir(ext):
for name in sorted(os.listdir(ext)):
if name.isprintable() and os.path.isfile(os.path.join(ext, name, "molecule.yml")):
if name.isprintable() and os.path.isfile(
os.path.join(ext, name, "molecule.yml")
):
found.append(f"extensions/molecule/{name}")
roles = os.path.join(root, "roles")
if os.path.isdir(roles):
@@ -104,7 +110,11 @@ def discover(root="."):
if not os.path.isdir(mol):
continue
for name in sorted(os.listdir(mol)):
if role.isprintable() and name.isprintable() and os.path.isfile(os.path.join(mol, name, "molecule.yml")):
if (
role.isprintable()
and name.isprintable()
and os.path.isfile(os.path.join(mol, name, "molecule.yml"))
):
found.append(f"roles/{role}/molecule/{name}")
return found
@@ -127,11 +137,18 @@ def select(all_scenarios, changed, root="."):
roles.add(parts[1])
continue
return list(all_scenarios), f"{printable(path)} can affect every scenario"
chosen = [s for s in all_scenarios if s.startswith("roles/") and s.split("/")[1] in roles]
chosen = [
s for s in all_scenarios if s.startswith("roles/") and s.split("/")[1] in roles
]
covered = {s.split("/")[1] for s in chosen}
bare = sorted(r for r in roles - covered if os.path.isdir(os.path.join(root, "roles", r)))
bare = sorted(
r for r in roles - covered if os.path.isdir(os.path.join(root, "roles", r))
)
if bare:
raise ValueError("changed role without a molecule scenario: " + ", ".join(printable(r) for r in bare))
raise ValueError(
"changed role without a molecule scenario: "
+ ", ".join(printable(r) for r in bare)
)
if roles:
reason = "only roles changed: " + ", ".join(printable(r) for r in sorted(roles))
else:
@@ -142,8 +159,17 @@ def select(all_scenarios, changed, root="."):
def changed_paths(base_ref):
"""Paths changed in base_ref...HEAD; a rename counts as a delete plus an add."""
out = subprocess.run(
["git", "diff", "--name-only", "--no-renames", "-z", f"{base_ref}...HEAD", "--"],
check=True, capture_output=True,
[
"git",
"diff",
"--name-only",
"--no-renames",
"-z",
f"{base_ref}...HEAD",
"--",
],
check=True,
capture_output=True,
).stdout
return [os.fsdecode(p) for p in out.split(b"\0") if p]
@@ -159,14 +185,19 @@ def main(argv, environ=None):
print("::error::no molecule scenario found", file=sys.stderr)
return 1
if environ.get("EVENT_NAME") != "pull_request" or not base_ref:
print(f"::notice::Running all {len(scenarios)} scenarios (not a pull request)", file=sys.stderr)
print(
f"::notice::Running all {len(scenarios)} scenarios (not a pull request)",
file=sys.stderr,
)
chosen = scenarios
else:
try:
changed = changed_paths(base_ref)
except (subprocess.CalledProcessError, OSError, UnicodeError) as exc:
print(f"::warning::cannot diff against {printable(base_ref)} ({printable(str(exc))}); running all scenarios",
file=sys.stderr)
print(
f"::warning::cannot diff against {printable(base_ref)} ({printable(str(exc))}); running all scenarios",
file=sys.stderr,
)
chosen = scenarios
else:
try:
@@ -174,7 +205,10 @@ def main(argv, environ=None):
except ValueError as exc:
print(f"::error::{exc}", file=sys.stderr)
return 1
print(f"::notice::Running {len(chosen)} of {len(scenarios)} scenarios: {reason}", file=sys.stderr)
print(
f"::notice::Running {len(chosen)} of {len(scenarios)} scenarios: {reason}",
file=sys.stderr,
)
for scenario in chosen:
print(scenario)
return 0