scripts/wrong-filename: use Python for speedup + compatibility (#18381)

This commit is contained in:
Sebastiaan Speck
2025-10-07 05:33:54 +02:00
committed by GitHub
parent 23a1536a1d
commit 6c8232fa42
3 changed files with 106 additions and 31 deletions
+2 -2
View File
@@ -19,7 +19,7 @@ This section contains a summary of the scripts available in this directory. For
- [set-more-info-link.py](set-more-info-link.py) is a Python script to generate or update more information links across pages.
- [set-page-title.py](set-page-title.py) is a Python script to update the title across pages.
- [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.
- [wrong-filename.sh](wrong-filename.sh) script checks the consistency between the filenames and the page title.
- [wrong-filename.py](wrong-filename.py) script checks the consistency between the filenames and the page title.
- [update-command.py](update-command.py) is a Python script to update the common contents of a command example across all languages.
## Compatibility
@@ -34,5 +34,5 @@ The table below shows the compatibility of user-executable scripts with differen
| [set-alias-pages.py](set-alias-pages.py) | ✅ | ✅ | ✅ |
| [set-more-info-link.py](set-more-info-link.py) | ✅ | ✅ | ✅ |
| [set-page-title.py](set-page-title.py) | ✅ | ✅ | ✅ |
| [wrong-filename.sh](wrong-filename.sh) | ✅ | | ❌ (WSL ✅)|
| [wrong-filename.py](wrong-filename.py) | ✅ | | |
| [update-command.py](update-command.py) | ✅ | ✅ | ✅ |
+104
View File
@@ -0,0 +1,104 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
import sys
from pathlib import Path
import re
OUTPUT_FILE = Path("inconsistent-filenames.txt")
IGNORE_SET = {
">",
"<",
"<>",
":",
"?",
"|",
"jc.json",
"lid.libuser",
"mc.cli",
"mc.fm",
"pacman d",
"pacman f",
"pacman q",
"pacman r",
"pacman s",
"pacman t",
"pacman u",
"parted",
"print.runmailcap",
"print.win",
"print.zsh",
"python m json.tool",
"rename",
"snap.esa",
"snap.pkg",
}
def normalize(text: str) -> str:
"""
Normalize a string:
- replace '-' with spaces
- lowercase
- collapse multiple spaces into one
- strip leading/trailing whitespace
"""
text = text.replace("-", " ").lower().strip()
text = re.sub(r"\s+", " ", text)
return text
def check_file(path: Path) -> str | None:
"""Check a single markdown file for name/title consistency."""
filename = path.name
# Remove known suffixes
command_file = filename
for suffix in (".md", ".fish", ".js", ".1", ".2", ".3"):
if command_file.endswith(suffix):
command_file = command_file[: -len(suffix)]
command_file = normalize(command_file)
try:
with path.open("r", encoding="utf-8") as f:
firstline = f.readline().strip()
except Exception as exc:
return f"Error reading {path}: {exc}"
if not firstline.startswith("#"):
return f"Inconsistency found in file: {path} has no title"
command_page = normalize(firstline[2:])
# Skip if either filename or title is in the ignore set
if command_file in IGNORE_SET or command_page in IGNORE_SET:
return None
if command_file != command_page:
return (
f"Inconsistency found in file: {path}: "
f"{command_page} should be {command_file}"
)
return None
def main() -> int:
"""Run the filename consistency check."""
base_dirs = [p for p in Path(".").glob("pages*") if p.is_dir()]
files = [f for base in base_dirs for f in base.rglob("*.md")]
# Ensure OUTPUT_FILE is always empty at the start
OUTPUT_FILE.write_text("", encoding="utf-8")
with OUTPUT_FILE.open("a", encoding="utf-8") as out:
for path in files:
result = check_file(path)
if result:
out.write(result + "\n")
return 0
if __name__ == "__main__":
sys.exit(main())
-29
View File
@@ -1,29 +0,0 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
# This script checks consistency between the filenames and the page title.
# Usage: ./scripts/wrong-filename.sh
# Output file for recording inconsistencies
OUTPUT_FILE="inconsistent-filenames.txt"
# Remove existing output file (if any)
rm -f "$OUTPUT_FILE"
touch "$OUTPUT_FILE"
IGNORE_LIST=(">" "<" "<>" ":" "?" "jc.json" "lid.libuser" "mc.cli" "mc.fm" "pacman d" "pacman f" "pacman q" "pacman r" "pacman s" "pacman t" "pacman u" "parted" "print.runmailcap" "print.win" "python m json.tool" "rename" "snap.esa" "snap.pkg")
set -e
# Iterate through all Markdown files in the 'pages' directories
find pages* -name '*.md' -type f | while read -r path; do
# Extract the expected command name from the filename
COMMAND_NAME_FILE=$(basename "$path" | head -c-4 | sed 's/\.fish//' | sed 's/\.js//' | sed 's/\.1//' | sed 's/\.2//' | sed 's/\.3//' | tr '-' ' ' | tr '[:upper:]' '[:lower:]')
# Extract the command name from the first line of the Markdown file
COMMAND_NAME_PAGE=$(head -n1 "$path" | tail -c+3 | sed 's/--//' | tr '-' ' ' | tr '[:upper:]' '[:lower:]')
# Check if there is a mismatch between filename and content command names
if [[ "$COMMAND_NAME_FILE" != "$COMMAND_NAME_PAGE" && ! ${IGNORE_LIST[*]} =~ $COMMAND_NAME_PAGE ]]; then
echo "Inconsistency found in file: $path: $COMMAND_NAME_PAGE should be $COMMAND_NAME_FILE" >> "$OUTPUT_FILE"
fi
done