Files
2026-11-19 01:44:15 +02:00

125 lines
6.4 KiB
Python

#!/usr/bin/env python3
"""Write docs/TOOLS.md: every operator tool in ops/ with its purpose and what it needs.
The purpose is the first paragraph of the tool's own docstring; the needs are
the non-standard Python modules it imports. Nothing is executed or imported.
python3 ops/tools_index.py # rewrite docs/TOOLS.md
python3 ops/tools_index.py --check # exit 1 when docs/TOOLS.md is out of date
"""
import ast
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
OUTPUT = ROOT / 'docs' / 'TOOLS.md'
# (heading, one-line description, file-name prefixes). First match wins.
SECTIONS = [
('Running and driving the local test client',
'Start, stop and operate the owned test client and the local reference servers.',
['start_', 'run_', 'restart_', 'client_', 'local_', 'owned_', 'launcher_', 'forever_', 'active_']),
('In-client stack: build, install and observe',
'Build the Windows/Wine modules that run inside the client, install them with journals, and read their state.',
['native_', 'nohelper_', 'bootstrap_', 'build_', 'codec_', 'dx12_', 'diagnostics_']),
('Windows builds and the build VM', 'Cross-build and virtual-machine helpers for the Windows side.',
['windows_']),
('Engine observation', 'Read-only helpers used for assessments of how the original client behaves.',
['engine_', 'rea_']),
('Source-side data', 'Read the Octo/Turtle client archives and the local source server data.',
['octo_', 'turtle_', 'source_', 'classic_']),
('Items', 'Convert source items, their appearances, models and materials into native data.', ['item_']),
('Spells, auras, talents and class data', 'Convert and audit spell-related data and class systems.',
['spell_', 'aura_', 'talent_', 'class_', 'warrior_', 'character_', 'companion_']),
('Maps, models and creatures', 'Convert terrain, buildings, models and creature displays.',
['map_', 'adt_', 'wmo_', 'm2_', 'creature_', 'wdc5_', 'wdt_']),
('Quests', 'Quest log, objectives and map markers.', ['quest_']),
('Packaging, installation and release',
'Assemble content packages, publish them into the client data store, install and verify.',
['casc_', 'content_', 'install_', 'prepare_', 'export_', 'verify_', 'published_', 'publish_',
'linux_', 'development_', 'tools_']),
]
OTHER = ('Other tools', 'Everything that does not fit a group above.')
# Import name -> what to install.
PACKAGES = {'PIL': 'Pillow', 'Xlib': 'python-xlib', 'gi': 'PyGObject', 'pefile': 'pefile',
'capstone': 'capstone', 'evdev': 'evdev'}
def describe(path, local_modules):
tree = ast.parse(path.read_text(encoding='utf-8'))
doc = (ast.get_docstring(tree) or '').strip()
summary = ' '.join(doc.split('\n\n')[0].split()) if doc else '(no description)'
if len(summary) > 240:
summary = summary[:237].rsplit(' ', 1)[0] + '...'
needs, has_arguments = set(), False
for node in ast.walk(tree):
modules = []
if isinstance(node, ast.Import):
modules = [alias.name.split('.')[0] for alias in node.names]
elif isinstance(node, ast.ImportFrom) and node.module and node.level == 0:
modules = [node.module.split('.')[0]]
for module in modules:
if module == 'argparse':
has_arguments = True
if module not in sys.stdlib_module_names and module not in local_modules and module != 'ops':
needs.add(PACKAGES.get(module, module))
return summary.replace('|', '\\|'), sorted(needs), has_arguments
def render():
tools = sorted((ROOT / 'ops').glob('*.py'))
local_modules = {path.stem for path in tools}
grouped = {heading: [] for heading, _, _ in SECTIONS}
grouped[OTHER[0]] = []
all_needs = {}
for path in tools:
summary, needs, has_arguments = describe(path, local_modules)
heading = next((heading for heading, _, prefixes in SECTIONS
if any(path.name.startswith(prefix) for prefix in prefixes)), OTHER[0])
grouped[heading].append((path.name, summary, needs, has_arguments))
for need in needs:
all_needs.setdefault(need, []).append(path.name)
lines = ['# Operator tools', '',
f'All {len(tools)} Python tools under `ops/`, grouped by what they are for. Generated by',
'`ops/tools_index.py` from each tool\'s own description; run it again after adding a tool.', '',
'How to read the table: **Purpose** is the tool\'s own first paragraph. **Needs** lists Python',
'modules outside the standard library. A tool marked `--help` takes command-line arguments',
'and explains them itself: `python3 ops/<tool> --help`. Tools without that mark are either',
'libraries used by other tools or take fixed positional arguments described in their source.', '',
'## What the tools need overall', '',
'- Python 3 (developed and run on 3.12 to 3.14). Most tools use only the standard library.']
for need in sorted(all_needs):
lines.append(f'- `{need}` — used by {len(all_needs[need])} tools.')
lines += ['- Your own copies of the native client and of the source client data; nothing here contains them.',
'- For tools that drive a running client: Linux with X11 or XWayland, Wine, and the programs',
' named in [ONE_CLICK_LAUNCHER.md](ONE_CLICK_LAUNCHER.md) under "Linux prerequisites".', '']
for heading, text in [(heading, text) for heading, text, _ in SECTIONS] + [OTHER]:
rows = grouped[heading]
if not rows:
continue
lines += [f'## {heading}', '', text, '', '| Tool | Purpose | Needs |', '|---|---|---|']
for name, summary, needs, has_arguments in rows:
extra = ', '.join(f'`{need}`' for need in needs)
if has_arguments:
extra = (extra + '; ' if extra else '') + '`--help`'
lines.append(f'| `{name}` | {summary} | {extra} |')
lines.append('')
return '\n'.join(lines)
def main():
text = render()
if '--check' in sys.argv[1:]:
current = OUTPUT.read_text(encoding='utf-8') if OUTPUT.exists() else ''
if current != text:
print('docs/TOOLS.md is out of date; run python3 ops/tools_index.py')
return 1
return 0
OUTPUT.write_text(text, encoding='utf-8')
print(f'wrote {OUTPUT.relative_to(ROOT)}')
return 0
if __name__ == '__main__':
sys.exit(main())