check-db-naming.py (5056B)
1 #!/usr/bin/env python3 2 """Enforce the database layer naming convention. 3 4 Rules checked (see the commit series "database naming cleanup"): 5 6 1. every public function's name starts with the name of the file it lives in 7 2. the file's own name uses a prefix from the closed set, or is exempt 8 3. iterate_ iff the function takes a caller-supplied callback; get_ iff not 9 4. a .h exists beside every .c, and a .sql (if any) shares the name 10 5. every prepared statement is named after the function preparing it 11 6. no prepared statement name is used in two files 12 7. no numeric suffixes and no repeated library prefix 13 14 Run from the top of the source tree; exits non-zero on any violation. 15 """ 16 import re 17 import sys 18 import pathlib 19 import collections 20 21 LAYERS = [ 22 ("src/exchangedb", "src/include/exchange-database", "TALER_EXCHANGEDB_"), 23 ("src/auditordb", "src/include/auditor-database", "TALER_AUDITORDB_"), 24 ] 25 26 # Prefixes that describe what the caller gets. 27 PREFIXES = ("get_", "get_count_", "get_exists_", "iterate_", 28 "insert_", "update_", "update_to_", "delete_", "do_") 29 30 # Not CRUD: transaction control, DDL, events, memory, session plumbing, 31 # cancellable handle APIs, shard lifecycle, and multi-function helper modules. 32 EXEMPT = { 33 "start", "start_read_only", "start_read_committed", 34 "start_deferred_wire_out", "commit", "rollback", "preflight", "disconnect", 35 "create_tables", "drop_tables", "inject_auditor_triggers", "gc", 36 "event_listen", "event_listen_cancel", "event_notify", 37 "free_coin_transaction_list", "free_reserve_history", 38 "compute_shard", "enable_rules", "disable_rules", "begin_rule_update", 39 "begin_shard", "begin_revolving_shard", "complete_shard", "abort_shard", 40 "release_revolving_shard", 41 "account_history", "exchangedb_accounts", "exchangedb_transactions", 42 } 43 44 # Files that are not part of the API surface at all. 45 SKIP = {"pg", "template", "helper", "bench_db"} 46 47 PREP = re.compile(r'PREPARE\s*\(\s*\w+\s*,\s*"([A-Za-z0-9_]+)"', re.S) 48 49 errors = [] 50 51 52 def check_layer(impl_dir, hdr_dir, prefix): 53 impl, hdr = pathlib.Path(impl_dir), pathlib.Path(hdr_dir) 54 if not impl.is_dir(): 55 return 56 decl = re.compile(r"^" + re.escape(prefix) + r"(\w+)\s*\((.*?)\)\s*;", 57 re.S | re.M) 58 stmt_owner = {} 59 60 for c in sorted(impl.glob("*.c")): 61 stem = c.stem 62 if stem in SKIP or stem.startswith("test_"): 63 continue 64 65 # 2. prefix from the closed set, or exempt 66 if stem not in EXEMPT and not stem.startswith(PREFIXES): 67 errors.append(f"{c}: '{stem}' uses no approved prefix") 68 69 # 7. numeric suffix 70 if re.search(r"\d$", stem): 71 errors.append(f"{c}: '{stem}' ends in a digit") 72 73 # 4. matching header 74 h = hdr / (stem + ".h") 75 if stem not in EXEMPT and not h.exists(): 76 errors.append(f"{c}: no matching header {h}") 77 78 text = c.read_text(errors="replace") 79 80 # 5. prepared statements named after the function 81 for s in dict.fromkeys(PREP.findall(text)): 82 if s != stem and not s.startswith(stem + "_"): 83 errors.append( 84 f"{c}: prepared statement '{s}' is not named after '{stem}'") 85 # 6. cross-file uniqueness 86 if s in stmt_owner and stmt_owner[s] != str(c): 87 errors.append( 88 f"{c}: prepared statement '{s}' also prepared in " 89 f"{stmt_owner[s]}") 90 stmt_owner[s] = str(c) 91 92 if not h.exists(): 93 continue 94 htext = re.sub(r"/\*.*?\*/", "", h.read_text(errors="replace"), 95 flags=re.S) 96 for name, params in decl.findall(htext): 97 # 1. declared names belong to their file. Exempt entries are the 98 # documented multi-function modules, so this does not apply there. 99 if (stem not in EXEMPT 100 and name != stem and not name.startswith(stem + "_")): 101 errors.append( 102 f"{h}: declares '{name}', which does not belong to " 103 f"'{stem}'") 104 # 3. iterate_ iff callback 105 has_cb = bool(re.search(r"\w*(Callback|Iterator)\s+\w+", params)) 106 if name.startswith("iterate_") and not has_cb: 107 errors.append(f"{h}: '{name}' is iterate_ but takes no callback") 108 if name.startswith("get_") and has_cb: 109 errors.append(f"{h}: '{name}' takes a callback; use iterate_") 110 111 # 7. repeated library prefix anywhere in the tree 112 for f in list(impl.glob("*.[ch]")) + list(hdr.glob("*.h")): 113 if "unc-backup" in f.name: 114 continue 115 if re.search(r"\b" + re.escape(prefix.split("_")[0]) + r"_" + 116 re.escape(prefix), f.read_text(errors="replace")): 117 errors.append(f"{f}: repeated '{prefix}' prefix") 118 119 120 for a in LAYERS: 121 check_layer(*a) 122 123 for e in errors: 124 print("ERROR:", e) 125 print(f"\ndb-naming: {len(errors)} violation(s)") 126 sys.exit(1 if errors else 0)