Skip to content

Instantly share code, notes, and snippets.

@BenWiederhake
Created April 30, 2024 16:34
Show Gist options
  • Save BenWiederhake/f43c386c957825b6109325c39a351cac to your computer and use it in GitHub Desktop.
Save BenWiederhake/f43c386c957825b6109325c39a351cac to your computer and use it in GitHub Desktop.
Find unnecessary spellcheck exclusions in uutils; run this from the top-level directory
#!/usr/bin/env python3
import re
import subprocess
RE_SPELLCHECK_DISABLE = re.compile("spell-checker:disable-\w+")
CACHED_FILES_STRING = None
def check_right_directory():
try:
with open("src/bin/coreutils.rs", "r") as fp:
pass
except FileNotFoundError as e:
print("Started in the wrong directory? cd to $(git root) before invoking.")
raise e
def does_it_spellcheck():
global CACHED_FILES_STRING
if CACHED_FILES_STRING is None:
result = subprocess.run(
["git", "ls-files", ":!:tests/fixtures"],
check=True,
capture_output=True,
text=True,
)
CACHED_FILES_STRING = result.stdout
result = subprocess.run(
[
"cspell",
"--config",
".vscode/cSpell.json",
"--cache",
"--no-progress",
"--file-list",
"stdin",
],
input=CACHED_FILES_STRING,
capture_output=True,
text=True,
)
return result.returncode == 0
def list_locations():
result = subprocess.run(
["git", "grep", "-Frn", "spell-checker:disable-"],
check=True,
capture_output=True,
text=True,
)
locations = []
for match_line in result.stdout.split("\n"):
if not match_line:
continue
# 'match_line' could be something like:
# src/uu/tail/src/parse.rs:35:/// tail -\[NUM\]\[bcl\]\[f\] and tail +\[NUM\]\[bcl\]\[f\] // spell-checker:disable-line
parts = match_line.split(":", 2)
filepath = parts[0]
linenumber = int(parts[1])
locations.append((filepath, linenumber))
return locations
def try_replacing(location):
filepath, linenumber = location
line_index = linenumber - 1
del linenumber
# TODO: Avoid opening and reading the same file over and over again.
with open(filepath, "r") as fp:
lines_orig = fp.readlines()
lines_modified = lines_orig.copy()
lines_modified[line_index] = RE_SPELLCHECK_DISABLE.sub(
"FIXME-REPLACED ", lines_modified[line_index]
)
assert lines_modified[line_index] != lines_orig[line_index], (
filepath,
linenumber,
lines_orig[line_index],
)
with open(filepath, "w") as fp:
fp.write("".join(lines_modified))
if does_it_spellcheck():
return 1
else:
with open(filepath, "w") as fp:
fp.write("".join(lines_orig))
return 0
def run():
check_right_directory()
assert does_it_spellcheck(), "Fix spellcheck issues first!"
locations = list_locations() # git grep -Frn 'spell-checker:disable-'
print(f"Checking {len(locations)} occurrences ...")
matches = 0
for loc in locations:
matches += try_replacing(loc)
print(
f"Found {len(locations)} occurrences of spell-checker disabling, replaced {matches} of them."
)
if __name__ == "__main__":
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment