test(installer): remove the installer-only translations checker
This commit is contained in:
1 file changed
-122
@@ -1,122 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
"""Check the FWS installer translations file.
|
|
||||||
|
|
||||||
Errors (exit status 1):
|
|
||||||
- the file is not valid TOML;
|
|
||||||
- a message has no "en" text (the fallback of every language);
|
|
||||||
- a language code does not look like an Anaconda one (fr, pt_BR, sr@latin);
|
|
||||||
- a translation does not use exactly the same {placeholders} as "en";
|
|
||||||
- the code asks for a key that the file does not define.
|
|
||||||
Warnings: keys defined but never used.
|
|
||||||
The summary lists how many messages each language translates.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
import pathlib
|
|
||||||
import re
|
|
||||||
import string
|
|
||||||
import sys
|
|
||||||
import tomllib
|
|
||||||
|
|
||||||
REPO = pathlib.Path(__file__).resolve().parent.parent
|
|
||||||
DEFAULT_FILE = REPO / "configs/baseline/airootfs/usr/local/share/fws/installer-translations.toml"
|
|
||||||
CODE_ROOTS = [REPO / "configs/baseline/airootfs", REPO / "configs/releng/airootfs"]
|
|
||||||
|
|
||||||
LANGUAGE_CODE = re.compile(r"^[a-z]{2,3}(_[A-Z]{2})?(@[a-z]+)?$")
|
|
||||||
# t("key") in Python; in shell, the "fws:key" form that hands a key over to the
|
|
||||||
# installer's progress screen, and calls to the fws-i18n command.
|
|
||||||
PY_USAGE = re.compile(r"""\bt\(\s*["']([a-z0-9_]+(?:\.[a-z0-9_]+)+)["']""")
|
|
||||||
SHELL_USAGE = re.compile(r"""(?:\bfws:|\bfws-i18n\s+)([a-z0-9_]+(?:\.[a-z0-9_]+)+)\b""")
|
|
||||||
|
|
||||||
|
|
||||||
def flatten(table, prefix=""):
|
|
||||||
texts = {name: value for name, value in table.items() if not isinstance(value, dict)}
|
|
||||||
if texts and prefix:
|
|
||||||
yield prefix, texts
|
|
||||||
for name, value in table.items():
|
|
||||||
if isinstance(value, dict):
|
|
||||||
yield from flatten(value, f"{prefix}.{name}" if prefix else name)
|
|
||||||
|
|
||||||
|
|
||||||
def placeholders(text):
|
|
||||||
return {field for _, field, _, _ in string.Formatter().parse(text) if field is not None}
|
|
||||||
|
|
||||||
|
|
||||||
def used_keys():
|
|
||||||
keys = {}
|
|
||||||
for root in CODE_ROOTS:
|
|
||||||
for path in root.rglob("*"):
|
|
||||||
if not path.is_file() or "__pycache__" in path.parts:
|
|
||||||
continue
|
|
||||||
if path.suffix not in (".py", ".sh", ".ks", "") and path.name != ".xinitrc":
|
|
||||||
continue
|
|
||||||
try:
|
|
||||||
text = path.read_text(encoding="utf-8")
|
|
||||||
except (UnicodeDecodeError, OSError):
|
|
||||||
continue
|
|
||||||
for pattern in (PY_USAGE, SHELL_USAGE):
|
|
||||||
for match in pattern.finditer(text):
|
|
||||||
keys.setdefault(match.group(1), path.relative_to(REPO))
|
|
||||||
return keys
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
|
||||||
parser.add_argument("--file", type=pathlib.Path, default=DEFAULT_FILE)
|
|
||||||
parser.add_argument("--skip-usage", action="store_true",
|
|
||||||
help="do not compare the keys with the code (checking a fragment)")
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
errors, warnings = [], []
|
|
||||||
try:
|
|
||||||
data = tomllib.loads(args.file.read_text(encoding="utf-8"))
|
|
||||||
except (OSError, tomllib.TOMLDecodeError) as exc:
|
|
||||||
print(f"ERROR: {args.file}: {exc}", file=sys.stderr)
|
|
||||||
return 1
|
|
||||||
|
|
||||||
messages = dict(flatten(data))
|
|
||||||
coverage = {}
|
|
||||||
for key, texts in messages.items():
|
|
||||||
reference = texts.get("en")
|
|
||||||
if not isinstance(reference, str) or not reference:
|
|
||||||
errors.append(f"{key}: no \"en\" text")
|
|
||||||
continue
|
|
||||||
expected = placeholders(reference)
|
|
||||||
for language, text in texts.items():
|
|
||||||
if not LANGUAGE_CODE.match(language):
|
|
||||||
errors.append(f"{key}: unexpected language code {language!r}")
|
|
||||||
continue
|
|
||||||
if not isinstance(text, str) or not text:
|
|
||||||
errors.append(f"{key}.{language}: empty or not a string")
|
|
||||||
continue
|
|
||||||
try:
|
|
||||||
found = placeholders(text)
|
|
||||||
except ValueError as exc:
|
|
||||||
errors.append(f"{key}.{language}: {exc}")
|
|
||||||
continue
|
|
||||||
if found != expected:
|
|
||||||
errors.append(f"{key}.{language}: placeholders {sorted(found)} "
|
|
||||||
f"instead of {sorted(expected)}")
|
|
||||||
coverage[language] = coverage.get(language, 0) + 1
|
|
||||||
|
|
||||||
if not args.skip_usage:
|
|
||||||
used = used_keys()
|
|
||||||
for key, where in sorted(used.items()):
|
|
||||||
if key not in messages:
|
|
||||||
errors.append(f"{key}: used in {where} but not defined")
|
|
||||||
for key in sorted(set(messages) - set(used)):
|
|
||||||
warnings.append(f"{key}: defined but never used")
|
|
||||||
|
|
||||||
for line in warnings:
|
|
||||||
print(f"warning: {line}", file=sys.stderr)
|
|
||||||
for line in errors:
|
|
||||||
print(f"ERROR: {line}", file=sys.stderr)
|
|
||||||
total = len(messages)
|
|
||||||
summary = ", ".join(f"{lang} {count}/{total}" for lang, count in
|
|
||||||
sorted(coverage.items(), key=lambda item: (-item[1], item[0])))
|
|
||||||
print(f"{total} messages — {summary or 'no translation'}")
|
|
||||||
return 1 if errors else 0
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
sys.exit(main())
|
|
||||||
Reference in new issue
Block a user