| @@ -0,0 +1,162 @@ |
| 1 | + |
| 2 | +"""Unit (d) audit: does this tree carry the per-file notices MODIFICATIONS.md records? |
| 3 | + |
| 4 | +Reads MODIFICATIONS.md's table and every Lean file's provenance notice under PoincareLib/, |
| 5 | +then compares them on (upstream repo, upstream source path) pairs. |
| 6 | +Run from the repository root: python3 a6/audit.py . |
| 7 | +""" |
| 8 | +import os, re, sys, collections |
| 9 | + |
| 10 | +ROOT = sys.argv[1] if len(sys.argv) > 1 else '.' |
| 11 | +OUT = [] |
| 12 | + |
| 13 | + |
| 14 | +def say(s=''): |
| 15 | + OUT.append(s) |
| 16 | + |
| 17 | + |
| 18 | +def slug(url): |
| 19 | + m = re.match(r'https://github\.com/([^/]+/[^/]+)', url) |
| 20 | + return m.group(1) if m else url |
| 21 | + |
| 22 | + |
| 23 | +def src_path(url): |
| 24 | + m = re.match(r'https://github\.com/[^/]+/[^/]+/(?:blob|tree)/[0-9a-fA-F]+/(.+)$', url) |
| 25 | + return m.group(1) if m else None |
| 26 | + |
| 27 | + |
| 28 | +def read_notices(): |
| 29 | + """(repo, path) pairs each in-tree file's notice declares, in the order written.""" |
| 30 | + out = {} |
| 31 | + for base, _, files in os.walk(os.path.join(ROOT, 'PoincareLib')): |
| 32 | + for f in sorted(files): |
| 33 | + if not f.endswith('.lean'): |
| 34 | + continue |
| 35 | + p = os.path.join(base, f) |
| 36 | + with open(p, encoding='utf-8', errors='replace') as fh: |
| 37 | + txt = fh.read(8000) |
| 38 | + if 'Provenance and modification notice' not in txt: |
| 39 | + continue |
| 40 | + end = txt.find('-/') |
| 41 | + blk = txt[:end if end > 0 else len(txt)] |
| 42 | + lines = [l.strip() for l in blk.splitlines()] |
| 43 | + |
| 44 | + pairs, repo, awaiting = [], None, False |
| 45 | + for l in lines: |
| 46 | + m = re.match(r'^Source:\s*(\S+)$', l) |
| 47 | + if m: |
| 48 | + repo, awaiting = slug(m.group(1)), True |
| 49 | + continue |
| 50 | + m = re.match(r'^Additional provenance:\s*(\S+)$', l) |
| 51 | + if m: |
| 52 | + repo, awaiting = slug(m.group(1)), True |
| 53 | + continue |
| 54 | + if re.match(r'^[A-Za-z0-9_/.\-]+\.lean$', l): |
| 55 | + if repo: |
| 56 | + pairs.append((repo, l)) |
| 57 | + awaiting = False |
| 58 | + continue |
| 59 | + if l: |
| 60 | + awaiting = False |
| 61 | + out[os.path.relpath(p, ROOT)] = { |
| 62 | + 'pairs': pairs, |
| 63 | + 'copyright': len(re.findall(r'^Copyright', blk, re.M)), |
| 64 | + 'license': bool(re.search(r'^License:\s*Apache-2\.0', blk, re.M)), |
| 65 | + 'dangling': [x for x in ('PoincareConjecture/provenance', |
| 66 | + 'PoincareConjecture/MODIFICATIONS.md') |
| 67 | + if x in blk], |
| 68 | + } |
| 69 | + return out |
| 70 | + |
| 71 | + |
| 72 | +def read_table(): |
| 73 | + rows = [] |
| 74 | + with open(os.path.join(ROOT, 'MODIFICATIONS.md'), encoding='utf-8') as fh: |
| 75 | + for line in fh: |
| 76 | + if not line.startswith('| ['): |
| 77 | + continue |
| 78 | + cells = line.split('|') |
| 79 | + local = re.match(r'\s*\[([^\]]+)\]', cells[1]).group(1) |
| 80 | + srcs = [(slug(u), src_path(u)) |
| 81 | + for _, u in re.findall(r'\[([^\]]+)\]\((https://[^)]+)\)', cells[2])] |
| 82 | + rows.append({'local': local, 'srcs': srcs, |
| 83 | + 'mod': cells[3].strip() if len(cells) > 3 else ''}) |
| 84 | + return rows |
| 85 | + |
| 86 | + |
| 87 | +notices = read_notices() |
| 88 | +rows = read_table() |
| 89 | + |
| 90 | +say('== what the tree holds ==') |
| 91 | +say(f'Lean files under PoincareLib/: {sum(len([f for f in fs if f.endswith(".lean")]) for _, _, fs in os.walk(os.path.join(ROOT, "PoincareLib")))}') |
| 92 | +say(f'files carrying a "Provenance and modification notice": {len(notices)}') |
| 93 | +say('') |
| 94 | + |
| 95 | +say('== what MODIFICATIONS.md records ==') |
| 96 | +say(f'table rows: {len(rows)} distinct local paths named: {len(set(r["local"] for r in rows))}') |
| 97 | +say(f'rows naming 2+ upstream sources: {sum(1 for r in rows if len(r["srcs"]) > 1)}') |
| 98 | +say(f'local paths named by the table that still exist at that path on main: ' |
| 99 | + f'{sum(1 for r in rows if os.path.exists(os.path.join(ROOT, r["local"])))}') |
| 100 | +say('') |
| 101 | + |
| 102 | +tab = set() |
| 103 | +for r in rows: |
| 104 | + tab.update(s for s in r['srcs'] if s[0]) |
| 105 | +not_pairs = collections.Counter() |
| 106 | +for n in notices.values(): |
| 107 | + for pr in n['pairs']: |
| 108 | + not_pairs[pr] += 1 |
| 109 | + |
| 110 | +say('== (repo, upstream path) pairs ==') |
| 111 | +say(f'distinct in the table: {len(tab)}') |
| 112 | +say(f'distinct in notices: {len(not_pairs)}') |
| 113 | +only_tab = sorted(tab - set(not_pairs)) |
| 114 | +only_not = sorted(set(not_pairs) - tab) |
| 115 | +say(f'in the table, no notice states it: {len(only_tab)}') |
| 116 | +for x in only_tab: |
| 117 | + say(f' TABLE-ONLY {x[0]} :: {x[1]}') |
| 118 | +say(f'in notices, not in the table: {len(only_not)}') |
| 119 | +for x in only_not: |
| 120 | + say(f' NOTICE-ONLY {x[0]} :: {x[1]}') |
| 121 | +say('') |
| 122 | + |
| 123 | +say('== row by row ==') |
| 124 | +by_src = collections.defaultdict(list) |
| 125 | +for rel, n in notices.items(): |
| 126 | + for pr in n['pairs']: |
| 127 | + by_src[pr].append(rel) |
| 128 | +tally = collections.Counter() |
| 129 | +gaps = [] |
| 130 | +for r in rows: |
| 131 | + want = [s for s in r['srcs'] if s[0]] |
| 132 | + hit = set() |
| 133 | + for s in want: |
| 134 | + hit.update(by_src.get(s, [])) |
| 135 | + if hit: |
| 136 | + st = 'at least one notice names a source of this row' |
| 137 | + else: |
| 138 | + st = 'NO notice in the tree names any source of this row' |
| 139 | + gaps.append(r['local']) |
| 140 | + tally[st] += 1 |
| 141 | +for k, v in tally.most_common(): |
| 142 | + say(f' {v:3d} {k}') |
| 143 | +for g in gaps: |
| 144 | + say(f' GAP {g}') |
| 145 | +say('') |
| 146 | + |
| 147 | +say('== notice text ==') |
| 148 | +say(f'notices with no Copyright line: {sum(1 for n in notices.values() if n["copyright"] == 0)}') |
| 149 | +say(f'notices that do not say "License: Apache-2.0": {sum(1 for n in notices.values() if not n["license"])}') |
| 150 | +say(f'notices pointing at PoincareConjecture/provenance/ or PoincareConjecture/MODIFICATIONS.md ' |
| 151 | + f'(neither exists in this tree): {sum(1 for n in notices.values() if n["dangling"])}') |
| 152 | +say('') |
| 153 | + |
| 154 | +say('== numbers stated in the prose, against the table ==') |
| 155 | +say('prose: 110 source adaptations inspected; 106 of them matched exactly; ' |
| 156 | + '4 inspected individually; "All 124 edited Lean files".') |
| 157 | +say(f'table rows: {len(rows)} (vs 110 inspected, vs 106 exact, vs 124 edited)') |
| 158 | +say(f'notices in the tree: {len(notices)}') |
| 159 | + |
| 160 | +with open(os.path.join('/mnt/work', 'a6', 'audit_out.txt'), 'w') as fh: |
| 161 | + fh.write('\n'.join(OUT) + '\n') |
| 162 | +print('\n'.join(OUT)) |