|
| 1 | +"""Generate the offline NeqSim API manifest consumed by ``neqsim.discovery``. |
| 2 | +
|
| 3 | +Runs once at release time. It reflects the whole Java API (via ``neqsim.discovery`` |
| 4 | +live reflection) and writes ``src/neqsim/data/neqsim_api.json`` containing every |
| 5 | +class with its constructors and public method names. Shipping that file makes |
| 6 | +``discovery.list_classes`` / ``find_classes`` / ``describe`` work instantly and |
| 7 | +JVM-free for end users, and it can drive a docs site. |
| 8 | +
|
| 9 | +Usage |
| 10 | +----- |
| 11 | + python scripts/generate_api_manifest.py [--out PATH] [--packages neqsim ...] |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import argparse |
| 17 | +import gzip |
| 18 | +import json |
| 19 | +import sys |
| 20 | +from datetime import datetime, timezone |
| 21 | +from pathlib import Path |
| 22 | +from typing import Dict, List |
| 23 | + |
| 24 | +_REPO_ROOT = Path(__file__).resolve().parent.parent |
| 25 | +_SRC = _REPO_ROOT / "src" |
| 26 | +if _SRC.is_dir(): |
| 27 | + sys.path.insert(0, str(_SRC)) |
| 28 | + |
| 29 | +from neqsim import discovery # noqa: E402 |
| 30 | + |
| 31 | + |
| 32 | +def _class_entry(fqcn: str) -> Dict[str, List[str]]: |
| 33 | + """Build the manifest entry (constructors + methods) for one class. |
| 34 | +
|
| 35 | + Args: |
| 36 | + fqcn: Fully-qualified Java class name. |
| 37 | +
|
| 38 | + Returns: |
| 39 | + A dict with ``constructors`` and ``methods`` lists (possibly empty). |
| 40 | + """ |
| 41 | + constructors: List[str] = [] |
| 42 | + methods: List[str] = [] |
| 43 | + try: |
| 44 | + java_class = discovery.get_class(fqcn).class_ |
| 45 | + constructors = sorted( |
| 46 | + discovery._simplify_signature(str(c)) for c in java_class.getConstructors() |
| 47 | + ) |
| 48 | + method_names = set() |
| 49 | + for m in java_class.getMethods(): |
| 50 | + name = str(m.getName()) |
| 51 | + if name.isidentifier(): |
| 52 | + method_names.add(name) |
| 53 | + methods = sorted(method_names) |
| 54 | + except Exception: |
| 55 | + pass |
| 56 | + return {"constructors": constructors, "methods": methods} |
| 57 | + |
| 58 | + |
| 59 | +def _detect_version() -> str: |
| 60 | + """Return the NeqSim jar version if derivable, else ``"unknown"``.""" |
| 61 | + try: |
| 62 | + for entry in discovery._classpath_entries(): |
| 63 | + name = Path(entry).name |
| 64 | + if name.startswith("neqsim-") and name.endswith(".jar"): |
| 65 | + return name[len("neqsim-") : -len(".jar")] |
| 66 | + except Exception: |
| 67 | + pass |
| 68 | + return "unknown" |
| 69 | + |
| 70 | + |
| 71 | +def generate(out_path: Path, packages: List[str]) -> int: |
| 72 | + """Generate the manifest for the requested packages. |
| 73 | +
|
| 74 | + Args: |
| 75 | + out_path: Destination JSON file. |
| 76 | + packages: Package prefixes to include. |
| 77 | +
|
| 78 | + Returns: |
| 79 | + The number of classes written. |
| 80 | + """ |
| 81 | + class_names: List[str] = [] |
| 82 | + for prefix in packages: |
| 83 | + class_names.extend(discovery.list_classes(prefix, recursive=True)) |
| 84 | + class_names = sorted(set(class_names)) |
| 85 | + |
| 86 | + classes: Dict[str, Dict[str, List[str]]] = {} |
| 87 | + for fqcn in class_names: |
| 88 | + classes[fqcn] = _class_entry(fqcn) |
| 89 | + |
| 90 | + manifest = { |
| 91 | + "schema": "neqsim-api-manifest/1.0", |
| 92 | + "version": _detect_version(), |
| 93 | + "generated": datetime.now(timezone.utc).isoformat(), |
| 94 | + "class_count": len(classes), |
| 95 | + "classes": classes, |
| 96 | + } |
| 97 | + |
| 98 | + out_path.parent.mkdir(parents=True, exist_ok=True) |
| 99 | + payload = json.dumps(manifest, indent=1, sort_keys=True) |
| 100 | + if out_path.suffix == ".gz": |
| 101 | + with gzip.open(out_path, "wt", encoding="utf-8") as handle: |
| 102 | + handle.write(payload) |
| 103 | + else: |
| 104 | + with open(out_path, "w", encoding="utf-8") as handle: |
| 105 | + handle.write(payload) |
| 106 | + return len(classes) |
| 107 | + |
| 108 | + |
| 109 | +def main(argv: List[str] | None = None) -> int: |
| 110 | + """CLI entry point. |
| 111 | +
|
| 112 | + Args: |
| 113 | + argv: Optional argument list (defaults to ``sys.argv``). |
| 114 | +
|
| 115 | + Returns: |
| 116 | + Process exit code. |
| 117 | + """ |
| 118 | + parser = argparse.ArgumentParser(description=__doc__) |
| 119 | + parser.add_argument( |
| 120 | + "--out", |
| 121 | + default=str(_SRC / "neqsim" / "data" / "neqsim_api.json.gz"), |
| 122 | + help="Output path for the manifest JSON (use .gz to gzip).", |
| 123 | + ) |
| 124 | + parser.add_argument( |
| 125 | + "--packages", |
| 126 | + nargs="*", |
| 127 | + default=["neqsim"], |
| 128 | + help="Package prefixes to include (default: the whole neqsim tree).", |
| 129 | + ) |
| 130 | + args = parser.parse_args(argv) |
| 131 | + |
| 132 | + count = generate(Path(args.out), args.packages) |
| 133 | + print(f"Wrote manifest with {count} classes to {args.out}") |
| 134 | + return 0 |
| 135 | + |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + raise SystemExit(main()) |
0 commit comments