unused_strings/find.py

58 lines
1.2 KiB
Python
Raw Normal View History

2024-12-09 02:57:36 +01:00
#!/usr/bin/env python3
import os
import sys
import json
import subprocess
if len(sys.argv) != 2 or sys.argv[1].startswith("-"):
print(f"Usage: {sys.argv[0]} <path to mapcomplete repo>", file=sys.stderr)
sys.exit(1)
os.chdir(sys.argv[1])
if subprocess.run(["rg", "--version"], check=False, stdout=subprocess.DEVNULL).returncode != 0:
# Regular grep doesn't suffice, we rely on ripgrep's functionality to honour .gitignore
print("rg (ripgrep) is not found in PATH or not functional", file=sys.stderr)
sys.exit(1)
with open("langs/en.json", "r", encoding="utf-8") as fh:
translations = json.load(fh)
def walk(obj, prefix):
for k, v in obj.items():
if isinstance(v, str):
yield (f"{prefix}{k}", k)
else:
yield from walk(v, f"{prefix}{k}.")
for fqkey, key in walk(translations, ""):
print(f" Checking {key}", file=sys.stderr)
p = subprocess.run(
[
"rg",
"--no-config",
"--iglob=!langs",
"--fixed-strings", "--max-count=1",
"--", key
],
check=False, stdout=subprocess.DEVNULL
)
if p.returncode == 0:
# String is used
pass
elif p.returncode == 1:
# String is not used
print(fqkey)
else:
# grep encountered an error
print(f"Error while checking for {key}", file=sys.stderr)