Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
48 lines
2.1 KiB
Python
48 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""List the production functions over the lawbook 301 limits, longest first (STRA-338 helper for the size work).
|
|
|
|
Usage: scripts/limits-offenders.py [--top N] [--min-lines 31] PATH_PREFIX...
|
|
Each line: lines complexity nesting wide-lines params file:start function-key
|
|
"""
|
|
import argparse
|
|
import pathlib
|
|
import sys
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
|
|
|
|
from limits import files, policy # noqa: E402
|
|
|
|
|
|
def over_any(fn):
|
|
return fn.fn_lines > 30 or fn.complexity > 10 or fn.nesting > 4 or fn.width > 0 or fn.params > 5
|
|
|
|
|
|
def main(argv):
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("prefixes", nargs="+")
|
|
parser.add_argument("--top", type=int, default=0)
|
|
parser.add_argument("--min-lines", type=int, default=31)
|
|
parser.add_argument("--over", action="store_true", help="every function over ANY limit (lines, complexity, nesting, width, params)")
|
|
parser.add_argument("--params", action="store_true", help="only the functions with more than 5 parameters, most first")
|
|
args = parser.parse_args(argv)
|
|
root = pathlib.Path(__file__).resolve().parent.parent
|
|
pol = policy.load(root)
|
|
rows = []
|
|
for prefix in args.prefixes:
|
|
for path in sorted((root / prefix).rglob("*.rs")):
|
|
rel = path.relative_to(root).as_posix()
|
|
if "/target/" in rel or policy.is_test_path(rel):
|
|
continue
|
|
fns, _ = files.scan_file(root, pol, rel)
|
|
keep = over_any if args.over else (lambda fn: fn.fn_lines >= args.min_lines)
|
|
keep = (lambda fn: fn.params > 5) if args.params else keep
|
|
rows += [(fn, rel) for fn in fns if not fn.test and keep(fn)]
|
|
rows.sort(key=(lambda row: -row[0].params) if args.params else (lambda row: -row[0].fn_lines))
|
|
for fn, rel in rows[: args.top or None]:
|
|
print(f"{fn.fn_lines:5} {fn.complexity:4} {fn.nesting:3} {fn.width:4} {fn.params:3} {rel}:{fn.start} {fn.key}")
|
|
print(f"# {len(rows)} functions", file=sys.stderr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main(sys.argv[1:])
|