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
+85
-18
@@ -35,6 +35,7 @@ ignored by the build.
|
||||
When the Collection gains a plugin type or a directory consumers should get, add it to
|
||||
ALLOWED_TOP in the same change.
|
||||
"""
|
||||
|
||||
import fnmatch
|
||||
import re
|
||||
import sys
|
||||
@@ -42,21 +43,62 @@ import tarfile
|
||||
import unicodedata
|
||||
|
||||
ALLOWED_TOP = {
|
||||
"CHANGELOG.rst", "COPYING", "FILES.json", "LICENSES", "MANIFEST.json", "NOTICE", "README.md",
|
||||
"REUSE.toml", "changelogs", "docs", "meta", "plugins", "roles", "tests",
|
||||
"CHANGELOG.rst",
|
||||
"COPYING",
|
||||
"FILES.json",
|
||||
"LICENSES",
|
||||
"MANIFEST.json",
|
||||
"NOTICE",
|
||||
"README.md",
|
||||
"REUSE.toml",
|
||||
"changelogs",
|
||||
"docs",
|
||||
"meta",
|
||||
"plugins",
|
||||
"roles",
|
||||
"tests",
|
||||
}
|
||||
# Every Collection archive built by ansible-galaxy holds these.
|
||||
REQUIRED = {
|
||||
"MANIFEST.json", "FILES.json", "COPYING", "README.md", "meta/runtime.yml", "changelogs/changelog.yaml",
|
||||
"MANIFEST.json",
|
||||
"FILES.json",
|
||||
"COPYING",
|
||||
"README.md",
|
||||
"meta/runtime.yml",
|
||||
"changelogs/changelog.yaml",
|
||||
}
|
||||
# Checked on every member, whatever its top-level directory.
|
||||
FORBIDDEN = [
|
||||
"*/molecule/*", "*/molecule", "tests/scripts*", "*.pyc", "*__pycache__*", "*.tar.gz",
|
||||
"*.pem", "*.key", "*vault*", "*/.git/*", "*.env", "*importer_result.json",
|
||||
"*id_rsa*", "*id_ed25519*", "*.envrc", "*/.ssh", "*/.ssh/*", "*.env.*", "*/.env*", ".env*",
|
||||
"*/molecule/*",
|
||||
"*/molecule",
|
||||
"tests/scripts*",
|
||||
"*.pyc",
|
||||
"*__pycache__*",
|
||||
"*.tar.gz",
|
||||
"*.pem",
|
||||
"*.key",
|
||||
"*vault*",
|
||||
"*/.git/*",
|
||||
"*.env",
|
||||
"*importer_result.json",
|
||||
"*id_rsa*",
|
||||
"*id_ed25519*",
|
||||
"*.envrc",
|
||||
"*/.ssh",
|
||||
"*/.ssh/*",
|
||||
"*.env.*",
|
||||
"*/.env*",
|
||||
".env*",
|
||||
]
|
||||
# Names Windows treats as devices, with or without an extension (and with trailing spaces).
|
||||
WINDOWS_RESERVED = {"con", "prn", "aux", "nul", *(f"com{i}" for i in range(1, 10)), *(f"lpt{i}" for i in range(1, 10))}
|
||||
WINDOWS_RESERVED = {
|
||||
"con",
|
||||
"prn",
|
||||
"aux",
|
||||
"nul",
|
||||
*(f"com{i}" for i in range(1, 10)),
|
||||
*(f"lpt{i}" for i in range(1, 10)),
|
||||
}
|
||||
MAX_COMPONENT_BYTES = 255
|
||||
|
||||
|
||||
@@ -74,7 +116,9 @@ def fold(text):
|
||||
def component_problems(path, part):
|
||||
"""Names that mean something else once extracted on Windows or macOS."""
|
||||
if part.endswith((" ", ".")):
|
||||
return [f"{path}: component {part!r} ends in a space or a dot (dropped on Windows)"]
|
||||
return [
|
||||
f"{path}: component {part!r} ends in a space or a dot (dropped on Windows)"
|
||||
]
|
||||
if fold(part).split(".")[0].rstrip(" ") in WINDOWS_RESERVED:
|
||||
return [f"{path}: component {part!r} is a reserved Windows device name"]
|
||||
if "::" in part:
|
||||
@@ -92,8 +136,12 @@ def path_problems(name):
|
||||
return [f"{name!r}: absolute path"]
|
||||
if "\\" in name:
|
||||
return [f"{name!r}: backslash in the path"]
|
||||
odd = [c for c in name if unicodedata.category(c) in ("Cc", "Cf", "Cs", "Co", "Cn", "Zl", "Zp")
|
||||
or (unicodedata.category(c) == "Zs" and c != " ")]
|
||||
odd = [
|
||||
c
|
||||
for c in name
|
||||
if unicodedata.category(c) in ("Cc", "Cf", "Cs", "Co", "Cn", "Zl", "Zp")
|
||||
or (unicodedata.category(c) == "Zs" and c != " ")
|
||||
]
|
||||
if odd:
|
||||
return [f"{name!r}: control or invisible character in the path"]
|
||||
path = normalize(name)
|
||||
@@ -110,7 +158,9 @@ def path_problems(name):
|
||||
if found:
|
||||
return found
|
||||
if parts[0] not in ALLOWED_TOP:
|
||||
return [f"{path}: top-level entry '{parts[0]}' is not allowed in the Collection archive"]
|
||||
return [
|
||||
f"{path}: top-level entry '{parts[0]}' is not allowed in the Collection archive"
|
||||
]
|
||||
if any(fnmatch.fnmatchcase("/".join(folded), pat) for pat in FORBIDDEN):
|
||||
return [f"{path}: matches a forbidden pattern"]
|
||||
return []
|
||||
@@ -133,9 +183,20 @@ def member_problems(members):
|
||||
found = []
|
||||
for member in sorted(members, key=lambda m: m.name):
|
||||
if not (member.isreg() or member.isdir()):
|
||||
kind = ("symlink" if member.issym() else "hardlink" if member.islnk()
|
||||
else "device" if member.isdev() else "fifo" if member.isfifo() else "special file")
|
||||
found.append(f"{member.name}: {kind} is not allowed in the Collection archive")
|
||||
kind = (
|
||||
"symlink"
|
||||
if member.issym()
|
||||
else "hardlink"
|
||||
if member.islnk()
|
||||
else "device"
|
||||
if member.isdev()
|
||||
else "fifo"
|
||||
if member.isfifo()
|
||||
else "special file"
|
||||
)
|
||||
found.append(
|
||||
f"{member.name}: {kind} is not allowed in the Collection archive"
|
||||
)
|
||||
found += path_problems(member.name)
|
||||
return found
|
||||
|
||||
@@ -149,8 +210,10 @@ def archive_problems(members):
|
||||
keys = [fold(n) for n in names]
|
||||
for key in sorted({k for k in keys if keys.count(k) > 1}):
|
||||
same = sorted({n for n, k in zip(names, keys) if k == key})
|
||||
found.append(f"{' / '.join(same)}: appears more than once in the archive (names are compared "
|
||||
"ignoring case)")
|
||||
found.append(
|
||||
f"{' / '.join(same)}: appears more than once in the archive (names are compared "
|
||||
"ignoring case)"
|
||||
)
|
||||
file_names = {fold(n): n for n, m in zip(names, members) if not m.isdir()}
|
||||
for name in sorted(set(names)):
|
||||
parts = name.split("/")
|
||||
@@ -160,7 +223,9 @@ def archive_problems(members):
|
||||
if fold(prefix) in file_names and message not in found:
|
||||
found.append(message)
|
||||
present = {normalize(m.name) for m in members if m.isreg()}
|
||||
found += [f"{name}: required file is missing" for name in sorted(REQUIRED - present)]
|
||||
found += [
|
||||
f"{name}: required file is missing" for name in sorted(REQUIRED - present)
|
||||
]
|
||||
return found
|
||||
|
||||
|
||||
@@ -176,7 +241,9 @@ def main(argv):
|
||||
return 1
|
||||
bad = archive_problems(members)
|
||||
if bad:
|
||||
sys.stderr.write("Unexpected files in the Collection archive:\n " + "\n ".join(bad) + "\n")
|
||||
sys.stderr.write(
|
||||
"Unexpected files in the Collection archive:\n " + "\n ".join(bad) + "\n"
|
||||
)
|
||||
return 1
|
||||
print(f"{argv[1]}: {len(members)} entries, all allowed")
|
||||
return 0
|
||||
|
||||
Reference in new issue
Block a user