2f60825bdf72a8d0c99b81552266c9537e9ee2dc braney Sat Sep 5 08:03:52 2026 -0700 cartTrackVarCatalog: tell an hg.conf name from a cart name. An hg.conf setting and a track-scoped cart variable are built the same way. jksql.c:1325 does safef(cfgName, sizeof cfgName, "%s.excludeDbs", failoverProf->name) and reads the result with cfgOption on the next line, which the scan cannot distinguish from safef(buf, size, "%s.heightPer", track). So excludeDbs sat in cartVarsNotCataloged.txt: correctly, in that it is not a cart variable, but that left the setting described in no registry at all and suppressed in the wrong one. The accessor that reads the buffer back is what tells them apart, so hgConfRead() looks for a cfg* call taking the same identifier within six lines of the safef. Six because the read is normally the next line and a buffer reused later in the function for something else must not excuse an unrelated name; the destination has to be a plain identifier, or we do not know what was filled in. It claims exactly one name in the tree today, and harvestCartVars.py --hgconf prints it with its call site. --reconcile no longer asks about such a name and --update-baseline no longer writes it back, so the baseline is 55 names to 54. The reverse case is now an error rather than a silence: if this catalog ever describes a name the tree reads with a cfg* accessor, --reconcile says so and exits 1, because that is one of the two registries being wrong about what the name is rather than a matter of taste. Verified by planting excludeDbs in the catalog and confirming the report. hgConfCatalog gained the row in the companion commit. refs #37838 #37925 diff --git src/hg/utils/cartTrackVarCatalog/harvestCartVars.py src/hg/utils/cartTrackVarCatalog/harvestCartVars.py index 04d964e0f8c..db7318e968b 100755 --- src/hg/utils/cartTrackVarCatalog/harvestCartVars.py +++ src/hg/utils/cartTrackVarCatalog/harvestCartVars.py @@ -73,30 +73,64 @@ # Extra trees mined for #define values only, not scanned for call sites. MACRO_DIRS = ["inc", "lib", "hg/inc"] # File extensions that mark a harvested name as a filename rather than a cart # variable. Deliberately only the ones the tree builds today, plus tbi beside # bai: every entry here is a name nobody has to classify again, so a guess adds # a way to lose a real cart variable silently and buys nothing. Adding one is # a decision, and --filenames is how to check what it costs. FILE_SUFFIXES = frozenset([ "bai", "bb", "cgm", "eps", "err", "html", "ids", "log", "pdf", "png", "ps", "tbi", "tmp", "txt", "wig", ]) +# How far after the safef to look for the accessor that consumes the buffer. +# Small on purpose: the read is normally the next line, and a buffer reused +# later in the function for something else must not excuse an unrelated name. +HGCONF_WINDOW = 6 + + +def hgConfRead(txt, pos, dest, lineno): + """Is the buffer this call just filled read back as an hg.conf name? + + jksql.c:1325 builds ".excludeDbs" with safef from a failover + profile name and reads it with cfgOption on the very next line. That is + an hg.conf setting, not a cart variable: it belongs to hgConfCatalog's + database-profile suffix family, and hgConfCatalog cannot find it either, + because the name never appears as a literal. + + Nothing about the shape distinguishes the two. What distinguishes them is + which accessor consumes the buffer, so that is what this looks for: a + cfg* call taking the same identifier within the next few lines. dest has + to be a plain identifier; anything else and we do not know what was + filled in. + """ + if not re.fullmatch(r'[A-Za-z_]\w*', dest.strip()): + return False + rx = re.compile(r'\bcfg[A-Za-z0-9]*\s*\(\s*%s\s*[,)]' + % re.escape(dest.strip())) + end = pos + for _ in range(HGCONF_WINDOW): + nl = txt.find("\n", end) + if nl < 0: + break + end = nl + 1 + return rx.search(txt[pos:end]) is not None + + def fileNameLike(var): """Is this harvested name the tail of a filename rather than a cart name? The test is on the trailing dot-separated component, after the leading separator the harvester may or may not have captured, so ".tmp", "_ss.ps" and ".link.bb" all answer yes through the same rule. Two cleverer tests were tried and rejected. The destination buffer's declaration does not decide it: psName and tmpName are char[PATH_LEN] but the .bai and .link.bb sites format into a plain buf and buffer. Nor does the argument being formatted: it is a filename at some sites, a url at others and a table name at a third set, with no shared spelling. The extension is the only part that is actually about the name, which is what this rule asks about, and it is the only part a reader can check. """ @@ -332,45 +366,51 @@ args = split_args(txt[i+1:match_close(txt, i)]) ln = lineno(m.start()) fi = None for k, a in enumerate(args): if a.strip().startswith('"'): fi = k break if fi is None: continue fmt = resolve(args[fi], localconst, macro, conflict) rest = args[fi+1:] mm = re.match(r'^%s([._])(.*)$', fmt or "") if not mm: continue sep, tail = mm.group(1), mm.group(2) + # An hg.conf name and a cart name are built the same way; only the + # accessor that reads the buffer back tells them apart. + conf = (len(args) > 0 + and hgConfRead(txt, match_close(txt, i) + 1, args[0], ln)) + def rec(var, how): + r = dict(var=var, file=rel, line=ln, func=encl[ln], how=how) + if conf: + r["notCart"] = "hgConf" + return r if "%" not in tail and tail: - recs.append(dict(var=sep+tail, file=rel, line=ln, - func=encl[ln], how="fmtlit")) + recs.append(rec(sep+tail, "fmtlit")) elif tail == "%s" and len(rest) >= 2: # "%s.%s", track, SUFFIX -> the suffix is the SECOND vararg v = resolve(rest[1], localconst, macro, conflict) if v and not v.startswith("EXPR:"): - recs.append(dict(var=sep+v, file=rel, line=ln, - func=encl[ln], how="fmt")) + recs.append(rec(sep+v, "fmt")) elif tail == "%s.%s" and len(rest) >= 3: v1 = resolve(rest[1], localconst, macro, conflict) v2 = resolve(rest[2], localconst, macro, conflict) if not v1.startswith("EXPR:") and not v2.startswith("EXPR:"): - recs.append(dict(var=sep+v1+"."+v2, file=rel, line=ln, - func=encl[ln], how="fmt3")) + recs.append(rec(sep+v1+"."+v2, "fmt3")) return recs # --------------------------------------------------------------------------- # entry point for the catalog next door # --------------------------------------------------------------------------- def harvest(dirs=None, quiet=False): """Scan the tree and return the raw records. cartTrackVarCatalog.py --reconcile imports this, so the scan has one definition rather than one here and a second one written out by hand. """ dirs = dirs or [d.strip() for d in DEFAULT_DIRS.split(",") if d.strip()] macro, conflict = build_macros(dirs) @@ -384,30 +424,41 @@ if fn.endswith(".c"): files.append(os.path.join(p, fn)) records = [] for fp in files: records.extend(scan_file(fp, os.path.relpath(fp, ROOT), macro, conflict)) if not quiet: print("scanned %d files in %d dirs, %d macros, %d records" % (len(files), len(dirs), len(macro), len(records)), file=sys.stderr) return records +def hgConfNames(records): + """The harvested names that are hg.conf settings, not cart variables. + + Keyed with the leading separator stripped, the way the catalog compares + them. See hgConfRead() for what makes the call. + """ + return set(r["var"].lstrip("._") for r in records + if r.get("notCart") == "hgConf" and not r["var"].startswith("EXPR:") + and "{" not in r["var"]) + + def resolved(records): """name -> first file:line, for the names the scan resolved to a literal. An EXPR: or {ident} record marks a name built at run time, which is signal for a person reading the harvester output but cannot be compared against a catalog of literal names, so it is dropped here. """ out = {} for r in sorted(records, key=lambda r: (r["file"], r["line"])): var = r["var"] if var.startswith("EXPR:") or "{" in var: continue out.setdefault(var, "%s:%d" % (r["file"], r["line"])) return out @@ -419,70 +470,85 @@ def main(): ap = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("--dirs", default=DEFAULT_DIRS, help="comma-separated dirs under the kent src root to " "scan (default: %s)" % DEFAULT_DIRS) ap.add_argument("--json", metavar="FILE", help="write raw records as JSON") ap.add_argument("--by-func", action="store_true", help="group by file and function") ap.add_argument("--by-var", action="store_true", help="group by variable, listing where each is used") ap.add_argument("--filenames", action="store_true", help="list the harvested names the filename rule claims, " "with the call site, so the rule can be audited") + ap.add_argument("--hgconf", action="store_true", + help="list the harvested names that are hg.conf settings " + "rather than cart variables, with the call site") ap.add_argument("--keep-unresolved", action="store_true", help="include {ident} and EXPR: entries in the groupings") args = ap.parse_args() dirs = [d.strip() for d in args.dirs.split(",") if d.strip()] records = harvest(dirs) def wanted(var): if args.keep_unresolved: return True return not var.startswith("EXPR:") and "{" not in var if args.json: with open(args.json, "w") as f: json.dump(records, f, indent=1) print("wrote %s" % args.json, file=sys.stderr) if args.filenames: hits = {} for r in sorted(records, key=lambda r: (r["file"], r["line"])): if wanted(r["var"]) and fileNameLike(r["var"]): hits.setdefault(r["var"].lstrip("._"), "%s:%d" % (r["file"], r["line"])) print("%d harvested names read as filenames, not cart variables" % len(hits)) for n in sorted(hits): print(" %-16s %s" % (n, hits[n])) + if args.hgconf: + hits = {} + for r in sorted(records, key=lambda r: (r["file"], r["line"])): + if wanted(r["var"]) and r.get("notCart") == "hgConf": + hits.setdefault(r["var"].lstrip("._"), + "%s:%d" % (r["file"], r["line"])) + print("%d harvested names read back with a cfg* accessor, so they are " + "hg.conf\nsettings and not cart variables" % len(hits)) + for n in sorted(hits): + print(" %-16s %s" % (n, hits[n])) + if args.by_func: byfunc = collections.defaultdict(set) for r in records: if wanted(r["var"]): byfunc[(r["file"], r["func"])].add(r["var"]) for k in sorted(byfunc): print("%s %s()" % (k[0], k[1])) print(" " + ", ".join(sorted(byfunc[k]))) if args.by_var: byvar = collections.defaultdict(set) for r in records: if wanted(r["var"]): byvar[r["var"]].add("%s:%d" % (r["file"], r["line"])) for k in sorted(byvar, key=str.lower): print("%-34s %d %s" % (k, len(byvar[k]), " ".join(sorted(byvar[k])[:4]))) - if not (args.json or args.by_func or args.by_var or args.filenames): - print("nothing to do; pass --by-func, --by-var, --filenames or --json", - file=sys.stderr) + if not (args.json or args.by_func or args.by_var or args.filenames + or args.hgconf): + print("nothing to do; pass --by-func, --by-var, --filenames, " + "--hgconf or --json", file=sys.stderr) return 1 return 0 if __name__ == "__main__": sys.exit(main())