opendta-pyodide 0.0.0-stage → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,472 @@
1
+ """opendta-pyodide: a host bridge to OpenDTA (Stata 14 commands and Mata) in Pyodide.
2
+
3
+ The host (for example a Stata do-file interpreter in JavaScript) owns the
4
+ dataset, macros and scalars. For each mata block or mata: line it sends them here; this module runs
5
+ the code against a stand-in for OpenDTA's Session and returns the log and what
6
+ the code changed. The Mata engine (functions, globals, classes) lives until
7
+ the host asks for a reset.
8
+ """
9
+
10
+ import json
11
+ import math
12
+
13
+ import numpy as np
14
+
15
+ # Pyodide's NumPy reports LAPACK failure through floating-point flags, which
16
+ # WebAssembly lacks, so cholesky, inv and solve return NaN instead of raising.
17
+ # OpenDTA catches LinAlgError in about 95 places, so restore the error.
18
+ if not getattr(np.linalg, "_mata_pyodide_shim", False):
19
+ def _wrap(fn):
20
+ def w(a, *args, **kw):
21
+ r = fn(a, *args, **kw)
22
+ arr = r[0] if isinstance(r, tuple) else r
23
+ if np.isnan(np.asarray(arr, dtype=np.float64)).any() and not np.isnan(np.asarray(a, dtype=np.float64)).any():
24
+ raise np.linalg.LinAlgError(fn.__name__ + ": matrix is singular or not positive definite")
25
+ return r
26
+ w.__name__ = fn.__name__
27
+ return w
28
+ for _n in ("cholesky", "inv", "solve"):
29
+ setattr(np.linalg, _n, _wrap(getattr(np.linalg, _n)))
30
+ np.linalg._mata_pyodide_shim = True
31
+
32
+ # Pyodide is 32-bit, so NumPy's index type (intp) is int32, and np.repeat refuses
33
+ # int64 repeat counts under its 'safe' casting rule. OpenDTA builds counts as
34
+ # int64 (expand, contract, fillin), so narrow them here; counts above 2**31 are
35
+ # out of range for a 32-bit array anyway.
36
+ if np.dtype(np.intp).itemsize < 8 and not getattr(np, "_opendta_repeat_shim", False):
37
+ _np_repeat = np.repeat
38
+
39
+ def _repeat(a, repeats, axis=None):
40
+ r = np.asarray(repeats)
41
+ if r.dtype.kind in "iu" and r.dtype.itemsize > np.dtype(np.intp).itemsize:
42
+ repeats = r.astype(np.intp)
43
+ return _np_repeat(a, repeats, axis=axis)
44
+
45
+ np.repeat = _repeat
46
+ np._opendta_repeat_shim = True
47
+
48
+ from opendta.core import formats as _formats
49
+ from opendta.core.dataset import Dataset, Variable, str_type_for
50
+ from opendta.core.errors import StataError
51
+ from opendta.core.missing import SYSMISS
52
+ from opendta.core.output import Output
53
+ from opendta.lang.lexer import split_commands
54
+ from opendta.lang.macros import MacroStore
55
+
56
+ _formats.set_superscript(False)
57
+ LINESIZE = 80
58
+
59
+
60
+ class _HostStata:
61
+ """stata("cmd") runs cmd in the host. The host is
62
+ asynchronous, so the call waits through Pyodide's run_sync, which needs
63
+ WebAssembly stack switching (JSPI). Mata's changes so far are sent first,
64
+ and the dataset, macros and scalars come back after the command."""
65
+
66
+ def execute(self, cmd):
67
+ s = _session
68
+ try:
69
+ from pyodide.ffi import can_run_sync, run_sync
70
+ except ImportError:
71
+ can_run_sync = None
72
+ if s.host is None or can_run_sync is None or not can_run_sync():
73
+ raise StataError(3000, "stata() needs WebAssembly stack switching (JSPI), which this browser lacks")
74
+ state = _state()
75
+ state["cmd"] = cmd
76
+ reply = run_sync(s.host(json.dumps(state)))
77
+ meta = json.loads(reply.json)
78
+ cols = [c.to_py() if hasattr(c, "to_py") else c for c in reply.columns]
79
+ for line in meta["log"]:
80
+ s.output.write(line + "\n")
81
+ _load(meta, cols)
82
+ if meta.get("error"):
83
+ raise StataError(int(meta["error"]["code"]), meta["error"]["message"])
84
+
85
+
86
+ class _Session:
87
+ """The slice of OpenDTA's Session that the Mata package reads."""
88
+
89
+ def __init__(self):
90
+ self.data = Dataset()
91
+ self.output = Output()
92
+ self.output.linesize = lambda: LINESIZE
93
+ self.macros = MacroStore()
94
+ self.scalars = {}
95
+ self.matrices = _MATRICES
96
+ self.r = {}
97
+ self.e = {}
98
+ self.sret = {}
99
+ self.scopes = []
100
+ self.rc = 0
101
+ self.interp = _HostStata()
102
+ self.host = None
103
+ self.evaluate = None
104
+ self.settings = {"linesize": str(LINESIZE), "level": "95", "varabbrev": "on",
105
+ "hints": "off", "dp": "period"}
106
+ self._mata = None
107
+ self.dirty = False
108
+ self.log = []
109
+ self.output.add_listener(lambda text, style: self.log.append(text))
110
+
111
+ @property
112
+ def mata(self):
113
+ if self._mata is None:
114
+ from opendta.mata.interp import MataEngine
115
+ self._mata = MataEngine(self)
116
+ return self._mata
117
+
118
+ @property
119
+ def nobs(self):
120
+ return self.data.nobs
121
+
122
+ def notify_state(self):
123
+ self.dirty = True
124
+
125
+ def eval(self, text):
126
+ """A Stata scalar expression, evaluated by the host (matrix command)."""
127
+ try:
128
+ from pyodide.ffi import can_run_sync, run_sync
129
+ except ImportError:
130
+ can_run_sync = None
131
+ if self.evaluate is None or can_run_sync is None or not can_run_sync():
132
+ raise StataError(3000, "this matrix expression needs WebAssembly stack switching (JSPI)")
133
+ out = json.loads(run_sync(self.evaluate(text)))
134
+ if out.get("error"):
135
+ raise StataError(int(out["error"]["code"]), out["error"]["message"])
136
+ v = out.get("value")
137
+ return SYSMISS if v is None else v
138
+
139
+ def expand_varlist(self, text):
140
+ from opendta.core.varlist import expand
141
+ return expand(self.data, text, abbrev=self.settings.get("varabbrev", "on") == "on")
142
+
143
+ def set_rc(self, rc):
144
+ self.rc = int(rc)
145
+
146
+ def report_error(self, e, *, show_rc=True):
147
+ self.output.error(e.message, e.rc, show_rc=show_rc)
148
+
149
+ def creturn(self, name):
150
+ values = {"pi": math.pi, "N": float(self.data.nobs), "k": float(self.data.nvars),
151
+ "rc": float(self.rc), "linesize": float(LINESIZE),
152
+ "maxdouble": 8.98846567431158e307, "mindouble": -8.98846567431158e307,
153
+ "epsdouble": 2.220446049250313e-16, "smallestdouble": 2.2250738585072014e-308,
154
+ "stata_version": 14.0, "version": 14.0, "os": "Unix",
155
+ "machine_type": "WebAssembly", "level": 95.0, "dp": "period"}
156
+ return values.get(name, "")
157
+
158
+
159
+ _session = None
160
+ _engine = None
161
+ # Stata matrices, shared by the Mata session and the command session, so a
162
+ # matrix made by one (matrix M = ..., st_matrix("M", X)) is visible to the other.
163
+ _MATRICES = {}
164
+
165
+
166
+ def reset():
167
+ """Start over: a new Mata session, no command session yet, no matrices."""
168
+ global _session, _engine
169
+ _MATRICES.clear()
170
+ _session = _Session()
171
+ _engine = None
172
+
173
+
174
+ def _variables(specs, columns):
175
+ """Variables from the host columns: numbers as float64 with Stata's missing."""
176
+ out = []
177
+ for (name, kind, sqltype), col in zip(specs, columns):
178
+ if kind == "str":
179
+ vals = np.array([str(x) for x in col], dtype=object)
180
+ longest = max((len(x.encode("utf-8")) for x in vals), default=1)
181
+ out.append(Variable(name, str_type_for(longest), vals))
182
+ continue
183
+ if isinstance(col, list):
184
+ a = np.array([np.nan if x is None else x for x in col], dtype=np.float64)
185
+ else:
186
+ a = np.asarray(col, dtype=np.float64).copy()
187
+ a[np.isnan(a)] = SYSMISS
188
+ t = sqltype.upper()
189
+ fmt = ""
190
+ if kind == "date":
191
+ vt, fmt = "long", "%td"
192
+ elif t in ("TINYINT", "BOOLEAN"):
193
+ vt = "byte"
194
+ elif t in ("SMALLINT", "UTINYINT"):
195
+ vt = "int"
196
+ elif t in ("INTEGER", "USMALLINT"):
197
+ vt = "long"
198
+ elif t == "FLOAT":
199
+ vt = "float"
200
+ else:
201
+ vt = "double"
202
+ out.append(Variable(name, vt, a, fmt))
203
+ return out
204
+
205
+
206
+ def _export(before, nobs_before, sess=None):
207
+ ds = (sess or _session).data
208
+ names_before = [b[0] for b in before]
209
+ names_now = [v.name for v in ds.vars]
210
+ full = ds.nobs != nobs_before or any(n not in names_now for n in names_before) \
211
+ or names_now[:len(names_before)] != names_before
212
+ by_name = {b[0]: b for b in before}
213
+ changed = []
214
+ for v in ds.vars:
215
+ old = by_name.get(v.name)
216
+ if not full and old is not None and old[1] == v.is_string:
217
+ if v.is_string and list(old[2]) == list(v.data):
218
+ continue
219
+ if not v.is_string and np.array_equal(old[2], v.data):
220
+ continue
221
+ changed.append(v)
222
+ cols = []
223
+ for v in (ds.vars if full else changed):
224
+ if v.is_string:
225
+ cols.append({"name": v.name, "kind": "str", "values": [str(x) for x in v.data]})
226
+ else:
227
+ a = np.asarray(v.data, dtype=np.float64)
228
+ vals = [None if x >= SYSMISS or x != x else x for x in a.tolist()]
229
+ kind = "date" if v.fmt.startswith("%td") else "num"
230
+ cols.append({"name": v.name, "kind": kind,
231
+ "integer": v.vtype in ("byte", "int", "long"), "values": vals})
232
+ return {"full": full, "nobs": int(ds.nobs), "order": names_now, "columns": cols}
233
+
234
+
235
+ def _chunks(source):
236
+ def code(ln):
237
+ # In Mata a star at the start of a line is not a comment (*p = 1).
238
+ if ln.kind == "comment" and ln.echo_lines and ln.echo_lines[0].lstrip().startswith("*"):
239
+ return "\n".join(ln.echo_lines)
240
+ return ln.text if ln.kind == "cmd" else ""
241
+ return [(code(ln), ln.echo_lines) for ln in split_commands(source)]
242
+
243
+
244
+ def _plain(d):
245
+ out = {}
246
+ for k, v in d.items():
247
+ if isinstance(v, (bool, int, float, np.floating, np.integer)):
248
+ x = float(v)
249
+ out[k] = None if x >= SYSMISS or x != x else x
250
+ elif isinstance(v, str):
251
+ out[k] = v
252
+ return out
253
+
254
+
255
+ _SUBCOMMANDS = ("clear", "describe", "d", "drop", "set", "mlib", "mosave", "which", "memory",
256
+ "query", "rename")
257
+
258
+
259
+ def _load(meta, columns, sess=None):
260
+ """Dataset, macros, scalars, results and generator state from the host."""
261
+ s = sess or _session
262
+ ds = Dataset()
263
+ ds.nobs = int(meta["nobs"])
264
+ ds.vars.extend(_variables(meta["vars"], columns))
265
+ s.data = ds
266
+ s.dirty = False
267
+ s.macros = MacroStore()
268
+ s.macros.locals.update(meta.get("locals", {}))
269
+ s.macros.globals.update(meta.get("globals", {}))
270
+ s.scalars = {k: (SYSMISS if v is None else v) for k, v in meta.get("scalars", {}).items()}
271
+ s.r = {k: (SYSMISS if v is None else v) for k, v in meta.get("r", {}).items()}
272
+ s.e = {k: (SYSMISS if v is None else v) for k, v in meta.get("e", {}).items()}
273
+ if meta.get("rng"):
274
+ from opendta.core import rng as _rng
275
+ _rng.RNG.set_state(meta["rng"])
276
+ em = meta.get("ematrix")
277
+ if em:
278
+ from opendta.commands.matrix import Matrix
279
+ names = list(em["names"])
280
+ s.e["b"] = Matrix(np.array([em["b"]], dtype=np.float64), ["y1"], names)
281
+ s.e["V"] = Matrix(np.array(em["V"], dtype=np.float64), names, names)
282
+ s.before = [(v.name, v.is_string, v.data.copy()) for v in ds.vars]
283
+ s.nobs_before = ds.nobs
284
+ s.r_before, s.e_before = dict(s.r), dict(s.e)
285
+
286
+
287
+ def _state(sess=None):
288
+ """What changed since the last load, plus macros, scalars and results."""
289
+ from opendta.core import rng as _rng
290
+ s = sess or _session
291
+ changed = s.dirty or s.data.nobs != s.nobs_before or getattr(s, "always_export", False)
292
+ return {
293
+ "data": _export(s.before, s.nobs_before, s) if changed else None,
294
+ "locals": dict(s.macros.locals),
295
+ "globals": dict(s.macros.globals),
296
+ "scalars": _plain(s.scalars),
297
+ "r": _plain(s.r) if _plain(s.r) != _plain(s.r_before) else None,
298
+ "e": _plain(s.e) if _plain(s.e) != _plain(s.e_before) else None,
299
+ "rng": _rng.RNG.state(),
300
+ }
301
+
302
+
303
+ def run(meta, columns, host=None, evaluate=None):
304
+ global LINESIZE
305
+ if _session is None or meta.get("reset"):
306
+ reset()
307
+ s = _session
308
+ s.host = host
309
+ s.evaluate = evaluate
310
+ LINESIZE = int(meta.get("linesize", 80))
311
+ s.settings["linesize"] = str(LINESIZE)
312
+ _load(meta, columns)
313
+ s.log = []
314
+ error = None
315
+ from opendta.mata.interp import MataLeave
316
+ try:
317
+ if meta["kind"] == "matrix":
318
+ from opendta.commands.matrix import cmd_matrix
319
+ cmd_matrix(s, meta["code"])
320
+ elif meta["kind"] == "line":
321
+ text = meta["code"].strip()
322
+ first = text.split()[0] if text else ""
323
+ if first in _SUBCOMMANDS:
324
+ s.mata.subcommand(text)
325
+ elif text:
326
+ s.mata.run_statement(text)
327
+ else:
328
+ title = " mata (type end to exit) "
329
+ s.output.write("-" * 49 + title + "-" * max(0, LINESIZE - 49 - len(title)) + "\n")
330
+ try:
331
+ s.mata.run_block(_chunks(meta["code"]), echo=True,
332
+ stop_on_error=bool(meta["colon"]))
333
+ except StataError as e:
334
+ e.message = (e.message + "\n" if e.message else "") + "-" * LINESIZE
335
+ raise
336
+ s.output.write(": end\n", "command")
337
+ s.output.write("-" * LINESIZE + "\n")
338
+ except MataLeave:
339
+ pass
340
+ except StataError as e:
341
+ error = {"code": e.rc, "message": e.message}
342
+ except Exception as e: # an interpreter fault becomes a Stata-style error
343
+ error = {"code": 3000, "message": "Mata internal error: " + type(e).__name__ + ": " + str(e)}
344
+ out = _state()
345
+ out["log"] = "".join(s.log)
346
+ out["error"] = error
347
+ s.host = None
348
+ s.evaluate = None
349
+ return out
350
+
351
+
352
+ # -- Stata commands on OpenDTA's own Session ----------------------------------
353
+
354
+ def _new_engine():
355
+ import os
356
+ os.environ.setdefault("MPLBACKEND", "agg")
357
+ from opendta.session import Session
358
+ e = Session()
359
+ e.settings["hints"] = "off"
360
+ e.always_export = True
361
+ e.dirty = False
362
+ e.log = []
363
+ e.graphs = []
364
+ e.output.add_listener(lambda text, style: e.log.append(text))
365
+ e.ui_hooks["graph"] = lambda g: e.graphs.append(_svg(g))
366
+ e.matrices = _MATRICES
367
+ return e
368
+
369
+
370
+ def _svg(g):
371
+ import io
372
+ from opendta.graphics.render import export
373
+ buf = io.BytesIO()
374
+ export(g, buf, "svg")
375
+ return buf.getvalue().decode("utf-8")
376
+
377
+
378
+ def _ematrix(s):
379
+ from opendta.commands.matrix import Matrix
380
+ b, V = s.e.get("b"), s.e.get("V")
381
+ if not isinstance(b, Matrix) or not isinstance(V, Matrix):
382
+ return None
383
+ return {"names": list(b.colnames), "b": [float(x) for x in np.asarray(b.data)[0]],
384
+ "V": [[float(x) for x in row] for row in np.asarray(V.data)]}
385
+
386
+
387
+ def _carry(prev, ds):
388
+ """Keep what the host does not hold from the engine's previous dataset:
389
+ variable and value labels, notes, formats, characteristics (where OpenDTA
390
+ keeps tsset, xtset, stset, svyset and mi settings) and the sort order."""
391
+ import copy
392
+ if prev is None or not prev.vars:
393
+ return
394
+ old = {v.name: v for v in prev.vars}
395
+ names = set()
396
+ for v in ds.vars:
397
+ names.add(v.name)
398
+ o = old.get(v.name)
399
+ if o is None or o.is_string != v.is_string:
400
+ continue
401
+ v.label, v.value_label, v.notes = o.label, o.value_label, list(o.notes)
402
+ if not v.fmt.startswith("%t"):
403
+ v.fmt = o.fmt
404
+ ds.label, ds.notes = prev.label, list(prev.notes)
405
+ ds.value_labels = copy.deepcopy(prev.value_labels)
406
+ ds.chars = {k: dict(c) for k, c in prev.chars.items() if k == "_dta" or k in names}
407
+ same = ds.nobs == prev.nobs and all(
408
+ k in old and k in names and (
409
+ list(old[k].data) == list(ds.get(k).data) if old[k].is_string
410
+ else np.array_equal(np.asarray(old[k].data), np.asarray(ds.get(k).data))
411
+ )
412
+ for k in prev.sortlist
413
+ )
414
+ if same:
415
+ ds.sortlist = list(prev.sortlist)
416
+
417
+
418
+ def run_command(meta, columns):
419
+ """Run Stata command lines on OpenDTA's Session, starting from the host's state."""
420
+ global _engine, LINESIZE
421
+ if _session is None or meta.get("reset"):
422
+ reset()
423
+ if _engine is None:
424
+ _engine = _new_engine()
425
+ s = _engine
426
+ LINESIZE = int(meta.get("linesize", 80))
427
+ s.settings["linesize"] = str(LINESIZE)
428
+ if meta.get("keepData"):
429
+ # No host dataset: the engine keeps its own data, macros and results.
430
+ s.before = [(v.name, v.is_string, v.data.copy()) for v in s.data.vars]
431
+ s.nobs_before = s.data.nobs
432
+ s.r_before, s.e_before = dict(s.r), dict(s.e)
433
+ else:
434
+ globals_before = dict(s.macros.globals)
435
+ prev_data, prev_e = s.data, s.e
436
+ _load(meta, columns, s)
437
+ for k, v in globals_before.items():
438
+ s.macros.globals.setdefault(k, v)
439
+ # The host hands e() back as plain values. When they are what this engine
440
+ # returned last time, keep the engine's own e() (matrices included) and its
441
+ # estimation sample, so postestimation (margins, predict, test) still works.
442
+ _carry(prev_data, s.data)
443
+ if not meta.get("reset") and prev_e and _plain(prev_e) == _plain(s.e):
444
+ s.e = prev_e
445
+ s.e_before = dict(prev_e)
446
+ es = getattr(prev_data, "esample", None)
447
+ if es is not None and len(es) == s.data.nobs:
448
+ s.data.esample = es.copy()
449
+ s.log = []
450
+ s.graphs = []
451
+ error = None
452
+ try:
453
+ s.interp.run_lines(split_commands(meta["code"]), echo=bool(meta.get("echo", False)))
454
+ except StataError as e:
455
+ s.report_error(e)
456
+ error = {"code": e.rc, "message": e.message}
457
+ except Exception as e: # an engine fault becomes a Stata-style error
458
+ error = {"code": 3000, "message": "OpenDTA internal error: " + type(e).__name__ + ": " + str(e)}
459
+ out = _state(s)
460
+ out["ematrix"] = _ematrix(s)
461
+ out["graphs"] = list(s.graphs)
462
+ out["log"] = "".join(s.log)
463
+ out["error"] = error
464
+ return out
465
+
466
+
467
+ def run_json(meta_json, columns, host=None, evaluate=None):
468
+ cols = [c.to_py() if hasattr(c, "to_py") else c for c in columns]
469
+ meta = json.loads(meta_json)
470
+ if meta.get("kind") == "command":
471
+ return json.dumps(run_command(meta, cols))
472
+ return json.dumps(run(meta, cols, host, evaluate))
package/package.json CHANGED
@@ -1,6 +1,53 @@
1
1
  {
2
2
  "name": "opendta-pyodide",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.3",
4
+ "description": "Run Stata 14 commands, graphs and Mata in the browser or Node on Pyodide, using OpenDTA's open-source engine.",
5
+ "type": "module",
6
+ "main": "./index.mjs",
7
+ "types": "./index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./index.d.ts",
11
+ "default": "./index.mjs"
12
+ },
13
+ "./opendta.json": "./opendta.json",
14
+ "./opendta_bridge.py": "./opendta_bridge.py"
15
+ },
16
+ "files": [
17
+ "index.mjs",
18
+ "index.d.ts",
19
+ "opendta.json",
20
+ "opendta_bridge.py",
21
+ "scripts/",
22
+ "LICENSE",
23
+ "LICENSE-OpenDTA"
24
+ ],
25
+ "scripts": {
26
+ "test": "node --experimental-wasm-jspi --test --test-concurrency=1 test/*.test.mjs"
27
+ },
28
+ "peerDependencies": {
29
+ "pyodide": ">=0.27.0 <0.28.0"
30
+ },
31
+ "devDependencies": {
32
+ "pyodide": "0.27.7"
33
+ },
34
+ "keywords": [
35
+ "stata",
36
+ "mata",
37
+ "do-file",
38
+ "pyodide",
39
+ "webassembly",
40
+ "statistics",
41
+ "econometrics",
42
+ "opendta"
43
+ ],
44
+ "license": "MIT",
45
+ "repository": {
46
+ "type": "git",
47
+ "url": "git+https://github.com/NakliTechie/opendta-pyodide.git"
48
+ },
49
+ "homepage": "https://github.com/NakliTechie/opendta-pyodide#readme",
50
+ "engines": {
51
+ "node": ">=22"
52
+ }
53
+ }
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env python3
2
+ """Bundle OpenDTA (MIT) for Pyodide.
3
+
4
+ Copies every Python module of an OpenDTA checkout at the pinned commit into one
5
+ JSON bundle, opendta.json, which installOpenDTA() writes into Pyodide's file
6
+ system. Left out, because they need a desktop or a network: the Qt interface
7
+ (gui/), the updater, the command-line entry points, the help files and the ICD
8
+ code tables.
9
+
10
+ python3 -I scripts/bundle-opendta.py /path/to/opendta
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ import json
16
+ import subprocess
17
+ import sys
18
+ from pathlib import Path
19
+
20
+ PIN = "29a3b28"
21
+ SOURCE = "https://github.com/yaskepalhares/opendta"
22
+ ROOT = Path(__file__).resolve().parents[1]
23
+ OUT = ROOT / "opendta.json"
24
+ SKIP_DIRS = {"gui", "help", "__pycache__"}
25
+ SKIP_FILES = {"updater.py", "cli.py", "__main__.py"}
26
+
27
+
28
+ def main() -> int:
29
+ checkout = Path(sys.argv[1]).resolve()
30
+ head = subprocess.run(["git", "-C", str(checkout), "rev-parse", "--short=7", "HEAD"],
31
+ capture_output=True, text=True, check=True).stdout.strip()
32
+ if head != PIN:
33
+ print(f"OpenDTA checkout is at {head}; check out {PIN} first", file=sys.stderr)
34
+ return 1
35
+ src = checkout / "src"
36
+ files: dict[str, str] = {}
37
+ for path in sorted((src / "opendta").rglob("*.py")):
38
+ rel = path.relative_to(src)
39
+ if SKIP_DIRS.intersection(rel.parts[1:-1]) or (len(rel.parts) == 2 and rel.name in SKIP_FILES):
40
+ continue
41
+ files[rel.as_posix()] = path.read_text()
42
+ bundle = {
43
+ "source": SOURCE,
44
+ "commit": PIN,
45
+ "licence": (checkout / "LICENSE").read_text(),
46
+ "patched": [],
47
+ "files": files,
48
+ }
49
+ OUT.write_text(json.dumps(bundle, ensure_ascii=False, sort_keys=True, separators=(",", ":")))
50
+ lines = sum(t.count("\n") for t in files.values())
51
+ print(f"{OUT.name}: {len(files)} modules, {lines} lines, {OUT.stat().st_size} bytes")
52
+ return 0
53
+
54
+
55
+ if __name__ == "__main__":
56
+ raise SystemExit(main())