Skip to content

Commit 6c8232f

Browse files
scripts/wrong-filename: use Python for speedup + compatibility (#18381)
1 parent 23a1536 commit 6c8232f

File tree

3 files changed

+106
-31
lines changed

3 files changed

+106
-31
lines changed

scripts/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This section contains a summary of the scripts available in this directory. For
1919
- [set-more-info-link.py](set-more-info-link.py) is a Python script to generate or update more information links across pages.
2020
- [set-page-title.py](set-page-title.py) is a Python script to update the title across pages.
2121
- [test.sh](test.sh) script runs some basic tests on every PR/commit to ensure the pages are valid and the code is formatted correctly.
22-
- [wrong-filename.sh](wrong-filename.sh) script checks the consistency between the filenames and the page title.
22+
- [wrong-filename.py](wrong-filename.py) script checks the consistency between the filenames and the page title.
2323
- [update-command.py](update-command.py) is a Python script to update the common contents of a command example across all languages.
2424

2525
## Compatibility
@@ -34,5 +34,5 @@ The table below shows the compatibility of user-executable scripts with differen
3434
| [set-alias-pages.py](set-alias-pages.py) ||||
3535
| [set-more-info-link.py](set-more-info-link.py) ||||
3636
| [set-page-title.py](set-page-title.py) ||||
37-
| [wrong-filename.sh](wrong-filename.sh) || | ❌ (WSL ✅)|
37+
| [wrong-filename.py](wrong-filename.py) || | |
3838
| [update-command.py](update-command.py) ||||

scripts/wrong-filename.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env python3
2+
# SPDX-License-Identifier: MIT
3+
4+
import sys
5+
from pathlib import Path
6+
import re
7+
8+
OUTPUT_FILE = Path("inconsistent-filenames.txt")
9+
10+
IGNORE_SET = {
11+
">",
12+
"<",
13+
"<>",
14+
":",
15+
"?",
16+
"|",
17+
"jc.json",
18+
"lid.libuser",
19+
"mc.cli",
20+
"mc.fm",
21+
"pacman d",
22+
"pacman f",
23+
"pacman q",
24+
"pacman r",
25+
"pacman s",
26+
"pacman t",
27+
"pacman u",
28+
"parted",
29+
"print.runmailcap",
30+
"print.win",
31+
"print.zsh",
32+
"python m json.tool",
33+
"rename",
34+
"snap.esa",
35+
"snap.pkg",
36+
}
37+
38+
39+
def normalize(text: str) -> str:
40+
"""
41+
Normalize a string:
42+
- replace '-' with spaces
43+
- lowercase
44+
- collapse multiple spaces into one
45+
- strip leading/trailing whitespace
46+
"""
47+
text = text.replace("-", " ").lower().strip()
48+
text = re.sub(r"\s+", " ", text)
49+
return text
50+
51+
52+
def check_file(path: Path) -> str | None:
53+
"""Check a single markdown file for name/title consistency."""
54+
filename = path.name
55+
56+
# Remove known suffixes
57+
command_file = filename
58+
for suffix in (".md", ".fish", ".js", ".1", ".2", ".3"):
59+
if command_file.endswith(suffix):
60+
command_file = command_file[: -len(suffix)]
61+
command_file = normalize(command_file)
62+
63+
try:
64+
with path.open("r", encoding="utf-8") as f:
65+
firstline = f.readline().strip()
66+
except Exception as exc:
67+
return f"Error reading {path}: {exc}"
68+
69+
if not firstline.startswith("#"):
70+
return f"Inconsistency found in file: {path} has no title"
71+
72+
command_page = normalize(firstline[2:])
73+
74+
# Skip if either filename or title is in the ignore set
75+
if command_file in IGNORE_SET or command_page in IGNORE_SET:
76+
return None
77+
78+
if command_file != command_page:
79+
return (
80+
f"Inconsistency found in file: {path}: "
81+
f"{command_page} should be {command_file}"
82+
)
83+
return None
84+
85+
86+
def main() -> int:
87+
"""Run the filename consistency check."""
88+
base_dirs = [p for p in Path(".").glob("pages*") if p.is_dir()]
89+
files = [f for base in base_dirs for f in base.rglob("*.md")]
90+
91+
# Ensure OUTPUT_FILE is always empty at the start
92+
OUTPUT_FILE.write_text("", encoding="utf-8")
93+
94+
with OUTPUT_FILE.open("a", encoding="utf-8") as out:
95+
for path in files:
96+
result = check_file(path)
97+
if result:
98+
out.write(result + "\n")
99+
100+
return 0
101+
102+
103+
if __name__ == "__main__":
104+
sys.exit(main())

scripts/wrong-filename.sh

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)