Files
shopdb-flask/scripts/gen_project_map.py
cproudlock 784964c907 Bring the wilm branch into main: endpoints plugin, agent, policies, CSV import
Ports the corporate-GitHub wilm branch onto main, keeping the two branches
separate. Thirteen upstream commits arrive with their authorship intact; the
review fixes and the portability work below are on top.

What arrives:

  endpoints plugin  - a ShopDB agent reporting endpoint state, telemetry,
                      services and interfaces, with a command channel the agent
                      long-polls. Ingest stays on the generic collector route
                      (ADR-006); the agent key is a UI-managed single-scope
                      token whose revocation is an instant kill switch.
  computers policies - Configuration and Business Policies (RMM-style), scope
                      trees validated at write time, actions checked against
                      the endpoints command allowlist. Feature-gated: every
                      route 404s while the setting is off, so enabling it can
                      never half-expose the surface.
  asset CSV import  - templates, a collector script carrying the site path, and
                      a validate-then-commit import. A plugin can own a virtual
                      column plus a post-write row hook, which is how a PC's
                      machine number becomes a controls link without a
                      hand-built relationships sheet.
  agent/            - the Windows agent and its installer.

Contract 0.21.0 -> 0.24.0, additive throughout: register_import_hook (0.22),
mint/list/revoke_service_token (0.23), collector_authorized (0.24). The stability
table had no rows for any of them and now does.

MySQL 5.6 portability. The branch stored three policy columns as sa.JSON, the
only JSON columns in the product, and 5.6 has no JSON type: the migration failed
with a bare syntax error near the word JSON. They are TEXT now, through a
JSONText type decorator, so all seventeen call sites still read and write dicts.
The convention elsewhere is already TEXT (guides.blocksjson,
geenforce.manifestjson).

Review fixes carried on top:

  - six policy GETs were jwt_required(optional=True), which authenticates a
    caller without checking anything; they require computers.view now, and six
    redundant jwt_required decorators are gone
  - the agent installer took a default BaseUrl and CollectorKey, so a mistyped
    run silently pointed an endpoint at the wrong instance; both are mandatory
    and a non-https BaseUrl throws unless -AllowInsecureHttp is passed
  - a committing CSV import wrote no audit row

Documentation: the 34 new routes are described in docs/api-inventory.json, which
is hand-maintained on purpose, and the generated spec now covers 331 paths and
477 operations across 22 surfaces. DEVELOPMENT-SETUP's 1071 troubleshooting row
said "use MySQL 8 for dev" and now gives the actual fix, including the trap that
5.6 downgrades ROW_FORMAT=DYNAMIC to Compact with a warning instead of an error.

Full suite 2123 passed, vitest 260 passed, all seventeen plugin chains migrate
clean on MySQL 5.6 and the naming, docs, parity and read-surface gates are green.
2026-08-28 16:17:17 -04:00

245 lines
9.3 KiB
Python

#!/usr/bin/env python3
"""Generate the PROJECT-MAP wiki page: the facts about this repo that go stale.
Versions, the plugin inventory, every Alembic chain head, the ADR index and the
endpoint count were all written by hand, in several places. Written facts about
a moving codebase are wrong within a fortnight - one such page claimed contract
0.16.0 and 1159 tests while the code was at 0.18.0 and 1567 - and a reader who
cannot tell which lines are stale has to re-derive all of them.
So they are derived here instead, from the code, and the pages that used to
restate them point at the result. Anything in this file that cannot be read from
the repository does not belong in it.
Usage:
venv/bin/python scripts/gen_project_map.py # write the PROJECT-MAP wiki page
venv/bin/python scripts/gen_project_map.py --check # fail if it is out of date
venv/bin/python scripts/gen_project_map.py --stdout # print, write nothing
--check is the CI form: it regenerates in memory and compares, so a plugin added
without regenerating the map fails the build instead of quietly aging.
"""
import argparse
import json
import re
import sys
from pathlib import Path
REPO = Path(__file__).resolve().parents[1]
OUTPUT = REPO / 'docs' / 'PROJECT-MAP.md'
HEADER = """<!-- GENERATED by scripts/gen_project_map.py - do not edit by hand.
Regenerate with: venv/bin/python scripts/gen_project_map.py -->
# Project map
Derived from the code on every regeneration. If something here is wrong, the
code changed and the map was not regenerated - fix it by running the generator,
never by editing this file.
"""
def _read(path):
return path.read_text(encoding='utf-8', errors='replace')
def versions():
"""Product and plugin-contract versions, which are separate series (ADR-007,
ADR-002) and are routinely quoted as if they were one."""
text = _read(REPO / 'shopdb' / '__init__.py')
found = {}
for name in ('__version__', '__contract_version__'):
match = re.search(r"^%s\s*=\s*['\"]([^'\"]+)['\"]" % name, text, re.M)
found[name] = match.group(1) if match else '?'
return found
def chainhead(versionsdir):
"""Head revision of an Alembic chain: the revision nothing else revises.
Read from the files rather than from a database, so the map is generatable
on a checkout with no MySQL and cannot report what one particular server
happens to have applied.
"""
if not versionsdir.is_dir():
return None
revisions, parents = set(), set()
for path in sorted(versionsdir.glob('*.py')):
text = _read(path)
rev = re.search(r"^revision\s*=\s*['\"]([^'\"]+)['\"]", text, re.M)
down = re.search(r"^down_revision\s*=\s*['\"]([^'\"]+)['\"]", text, re.M)
if rev:
revisions.add(rev.group(1))
if down:
parents.add(down.group(1))
heads = sorted(revisions - parents)
if not heads:
return None
# More than one head means the chain has branched, which breaks `upgrade`.
# Reporting it is the point; hiding it behind a [0] is how it stays unnoticed.
return ' + '.join(heads)
def plugins():
rows = []
for manifest in sorted((REPO / 'plugins').glob('*/manifest.json')):
try:
data = json.loads(_read(manifest))
except ValueError as exc:
rows.append({'name': manifest.parent.name, 'version': 'UNREADABLE',
'core': str(exc)[:60], 'head': None})
continue
rows.append({
'name': data.get('name', manifest.parent.name),
'version': data.get('version', '?'),
'core': data.get('core_version', ''),
'head': chainhead(manifest.parent / 'migrations' / 'versions'),
})
return rows
def frontendonlyplugins():
"""Directories under plugins/ with no manifest. They are core frontend
surface that always ships (applications, for one), and they look like
missing plugins to anyone counting directories."""
names = []
for path in sorted((REPO / 'plugins').iterdir()):
# Dot-directories are build scratch (plugins/.staging), not surface.
if path.is_dir() and not (path / 'manifest.json').exists() \
and not path.name.startswith(('__', '.')):
names.append(path.name)
return names
def adrs():
rows = []
for path in sorted((REPO / 'docs' / 'adr').glob('ADR-*.md')):
text = _read(path)
title = next((line.lstrip('# ').strip()
for line in text.splitlines() if line.startswith('# ')),
path.stem)
# Both spellings are in use: '- Status: ACCEPTED' and
# '- **Status:** ACCEPTED'. Matching only one reported every ADR as '?',
# which reads as "nobody decided" rather than "the regex is wrong".
status = re.search(r'^[-*\s]*(?:\*\*)?Status(?:\*\*)?\s*:?\s*(?:\*\*)?\s*(.+)$',
text, re.M | re.I)
clean = re.sub(r'[*_`]', '', status.group(1)).strip() if status else '?'
rows.append({'file': path.name, 'title': title, 'status': clean[:40]})
return rows
def endpointcount():
"""From the generated spec, not from a live app: this script must stay
importable without a database or an app context."""
spec = REPO / 'docs' / 'openapi.json'
if not spec.is_file():
return None
try:
return len(json.loads(_read(spec)).get('paths', {}))
except ValueError:
return None
def testcount():
"""Test FUNCTIONS, counted by reading the files.
Deliberately not `pytest --collect-only`: that needs the venv and the whole
import graph, and a map generator that only runs in a fully installed
environment is one that stops being run. Parametrised cases make the real
collected number higher; this is a floor, and it is labelled as one.
"""
total = 0
for path in (REPO / 'tests').rglob('test_*.py'):
total += len(re.findall(r'^\s*def test_', _read(path), re.M))
return total
def render():
lines = [HEADER]
version = versions()
lines.append('## Versions\n')
lines.append('| series | value | governed by |')
lines.append('|---|---|---|')
lines.append('| product `__version__` | `%s` | ADR-007 |'
% version['__version__'])
lines.append('| plugin contract `__contract_version__` | `%s` | ADR-002 |'
% version['__contract_version__'])
lines.append('')
lines.append('They move independently. A contract bump is not a release.\n')
lines.append('## Alembic chains (ADR-008)\n')
lines.append('Core runs with `flask db upgrade`; every plugin chain runs')
lines.append('with `flask plugin upgrade-all`. Both are needed on a deploy.\n')
lines.append('| chain | head |')
lines.append('|---|---|')
lines.append('| core | `%s` |' % (chainhead(REPO / 'migrations' / 'versions')
or 'none'))
rows = plugins()
for row in rows:
if row['head']:
lines.append('| %s | `%s` |' % (row['name'], row['head']))
lines.append('')
lines.append('## Bundled plugins (%d)\n' % len(rows))
lines.append('| plugin | version | core_version | owns migrations |')
lines.append('|---|---|---|---|')
for row in rows:
lines.append('| %s | %s | %s | %s |' % (
row['name'], row['version'], row['core'] or '-',
'yes' if row['head'] else 'no'))
lines.append('')
extra = frontendonlyplugins()
if extra:
lines.append('Manifest-less directories under `plugins/` are core '
'frontend surface and always ship: %s.\n'
% ', '.join('`%s`' % name for name in extra))
lines.append('## Architecture decisions\n')
lines.append('| ADR | title | status |')
lines.append('|---|---|---|')
for row in adrs():
lines.append('| %s | %s | %s |' % (row['file'], row['title'], row['status']))
lines.append('')
lines.append('## Size\n')
endpoints = endpointcount()
lines.append('- test functions defined: **%d** (parametrised cases collect '
'higher)' % testcount())
lines.append('- documented API paths: **%s** (`docs/openapi.json`, '
'regenerate with `scripts/gen_openapi.py`)'
% ('%d' % endpoints if endpoints is not None else 'unknown'))
lines.append('')
return '\n'.join(lines) + '\n'
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--check', action='store_true',
help='exit 1 if the committed map is out of date')
parser.add_argument('--stdout', action='store_true',
help='print the map instead of writing it')
args = parser.parse_args()
content = render()
if args.stdout:
sys.stdout.write(content)
return 0
if args.check:
current = _read(OUTPUT) if OUTPUT.is_file() else ''
if current != content:
sys.stderr.write(
'the PROJECT-MAP wiki page is out of date.\n'
'Regenerate: venv/bin/python scripts/gen_project_map.py\n')
return 1
print('the PROJECT-MAP wiki page is current')
return 0
OUTPUT.write_text(content, encoding='utf-8')
print('wrote %s' % OUTPUT.relative_to(REPO))
return 0
if __name__ == '__main__':
raise SystemExit(main())