|
1 | 1 | #!/usr/bin/env python3
|
2 |
| - |
3 |
| -import os |
4 |
| -import re |
5 |
| -import sys |
6 |
| - |
7 |
| -DEPRECATED_TERMS = [ |
| 2 | +# .ci/scripts/check_deprecated_terms.py |
| 3 | + |
| 4 | +import os, re, sys, argparse |
| 5 | + |
| 6 | +# ---- Term sets ---- |
| 7 | +# Block these when you're on the *2.x* branch (i.e., forbid legacy 1.x names): |
| 8 | +TERMS_1X = [ |
8 | 9 | "provision-configs",
|
| 10 | + "provision-config-instances", |
| 11 | + "results-publishing", |
9 | 12 | "results-publisher",
|
10 | 13 | "load-worker-coordinator-hosts",
|
11 | 14 | "execute-test",
|
12 | 15 | ]
|
13 |
| - |
14 |
| -SKIP_DIRS = [".git", "venv", "__pycache__", ".pytest_cache"] |
| 16 | + |
| 17 | +# Block these when you're on the *1.x* branch (i.e., forbid 2.x names): |
| 18 | +TERMS_2X = [ |
| 19 | + "cluster-configs", |
| 20 | + "reporting", |
| 21 | + "worker-hosts", |
| 22 | + "run-test", |
| 23 | + "test-run", |
| 24 | +] |
| 25 | + |
| 26 | +SKIP_DIRS = {".git", "venv", "__pycache__", ".pytest_cache", ".ci", "tests"} |
15 | 27 | VALID_EXTENSIONS = (".py", ".yml", ".yaml", ".md", ".sh", ".json", ".txt")
|
16 |
| - |
17 |
| -VARIANT_PATTERNS = [] |
18 |
| - |
19 |
| -def generate_variants(term): |
| 28 | + |
| 29 | +SUPRESS_MARKERS = { |
| 30 | + "block-1x": "check-deprecated-terms-disable-1x", |
| 31 | + "block-2x": "check-deprecated-terms-disable-2x" |
| 32 | +} |
| 33 | + |
| 34 | +def generate_variants(term: str) -> set[str]: |
20 | 35 | base = term.replace("-", " ").replace("_", " ")
|
21 | 36 | words = base.split()
|
22 | 37 | variants = set()
|
23 |
| - |
24 |
| - # kebab-case, snake_case, PascalCase, camelCase |
| 38 | + # kebab, snake, Pascal, camel |
25 | 39 | variants.add("-".join(words))
|
26 | 40 | variants.add("_".join(words))
|
27 |
| - variants.add("".join([w.capitalize() for w in words])) # PascalCase |
| 41 | + variants.add("".join([w.capitalize() for w in words])) # PascalCase |
28 | 42 | variants.add(words[0] + "".join([w.capitalize() for w in words[1:]])) # camelCase
|
29 |
| - |
30 |
| - # Word order flip for 2-word terms |
31 |
| - if len(words) == 2: |
| 43 | + |
| 44 | + # Optional: flip order for 2-word terms, but avoid silly "-ip" flips creating noise |
| 45 | + if len(words) == 2 and not words[1].lower() == "ip": |
32 | 46 | variants.add("-".join(words[::-1]))
|
33 | 47 | variants.add("_".join(words[::-1]))
|
34 | 48 | variants.add(words[1] + words[0].capitalize()) # camelCase reverse
|
35 |
| - |
36 | 49 | return variants
|
37 |
| - |
38 |
| -for term in DEPRECATED_TERMS: |
39 |
| - for variant in generate_variants(term): |
40 |
| - VARIANT_PATTERNS.append(re.compile(re.escape(variant), re.IGNORECASE)) |
41 |
| - |
42 |
| -def should_check_file(filename): |
43 |
| - return filename.endswith(VALID_EXTENSIONS) |
44 |
| - |
45 |
| -def main(): |
46 |
| - error_found = False |
47 |
| - |
| 50 | + |
| 51 | +def build_patterns(terms: list[str]) -> list[re.Pattern]: |
| 52 | + pats = [] |
| 53 | + for t in terms: |
| 54 | + for v in generate_variants(t): |
| 55 | + pats.append(re.compile(re.escape(v), re.IGNORECASE)) |
| 56 | + return pats |
| 57 | + |
| 58 | +def should_check_file(path: str) -> bool: |
| 59 | + return path.endswith(VALID_EXTENSIONS) |
| 60 | + |
| 61 | +def walk_and_check(patterns: list[re.Pattern], mode: str) -> int: |
| 62 | + error_found = 0 |
| 63 | + suppress_marker = SUPRESS_MARKERS.get(mode) |
48 | 64 | for root, _, files in os.walk("."):
|
49 |
| - if any(skip in root for skip in SKIP_DIRS): |
| 65 | + if any(skip in root.split(os.sep) for skip in SKIP_DIRS): |
50 | 66 | continue
|
51 |
| - |
52 | 67 | for f in files:
|
53 | 68 | full_path = os.path.join(root, f)
|
54 | 69 | if not should_check_file(full_path):
|
55 | 70 | continue
|
56 |
| - |
57 | 71 | try:
|
58 |
| - with open(full_path, "r", encoding="utf-8") as file: |
59 |
| - for i, line in enumerate(file, 1): |
60 |
| - for pattern in VARIANT_PATTERNS: |
61 |
| - if pattern.search(line): |
62 |
| - print(f"[Deprecated Term] {full_path}:{i}: {line.strip()}") |
63 |
| - error_found = True |
| 72 | + with open(full_path, "r", encoding="utf-8") as fh: |
| 73 | + previous_line = "" |
| 74 | + for i, line in enumerate(fh, 1): |
| 75 | + if suppress_marker in previous_line or suppress_marker in line: |
| 76 | + previous_line = line |
| 77 | + continue |
| 78 | + for patt in patterns: |
| 79 | + if patt.search(line): |
| 80 | + print(f"[Forbidden Term] {full_path}:{i}: {line.strip()}") |
| 81 | + error_found = 1 |
| 82 | + break |
64 | 83 | except Exception as e:
|
65 | 84 | print(f"[Warning] Skipped file {full_path}: {e}")
|
66 |
| - |
67 |
| - if error_found: |
68 |
| - print("\n❌ Deprecated terms found. Please remove or rename them.") |
| 85 | + return error_found |
| 86 | + |
| 87 | +def main(): |
| 88 | + p = argparse.ArgumentParser(description="Check forbidden term set by mode or env.") |
| 89 | + p.add_argument("--mode", choices=["block-1x", "block-2x"], default=os.getenv("OSB_TERM_MODE")) |
| 90 | + args = p.parse_args() |
| 91 | + |
| 92 | + mode = args.mode |
| 93 | + if not mode: |
| 94 | + print("No mode provided (use --mode block-1x | block-2x or set OSB_TERM_MODE). Exiting 0.") |
| 95 | + sys.exit(0) |
| 96 | + |
| 97 | + if mode == "block-1x": |
| 98 | + terms = TERMS_1X |
| 99 | + banner = "❌ 1.x terms found in 2.x branch. Replace with 2.x names." |
| 100 | + else: |
| 101 | + terms = TERMS_2X |
| 102 | + banner = "❌ 2.x terms found in 1.x branch. Replace with 1.x names." |
| 103 | + |
| 104 | + patterns = build_patterns(terms) |
| 105 | + failed = walk_and_check(patterns, mode) |
| 106 | + if failed: |
| 107 | + print("\n" + banner) |
69 | 108 | sys.exit(1)
|
70 |
| - |
71 |
| - print("✅ No deprecated terms found.") |
| 109 | + print("✅ No forbidden terms found for", mode) |
72 | 110 | sys.exit(0)
|
73 |
| - |
| 111 | + |
74 | 112 | if __name__ == "__main__":
|
75 | 113 | main()
|
0 commit comments