Files
simtrace-analyser/simtrace_analyser/__main__.py
T
catarrh 25aa87ba1e Packaging: the wheel carries the PWA, so a release install serves the UI (v2.7.10)
- pyproject declared build-backend = "setuptools.backends._legacy:_Backend",
  which does not exist: `python -m build`, `pip install .` and even
  `pip install -e .` died with "Backend ... is not available".  It is
  setuptools.build_meta now.
- the wheel and sdist carried only the Python packages, and _default_web_dir()
  resolved <site-packages>/../frontend, so an installed server started fine but
  answered every static request with 404.  setup.py's build_py now copies the
  checkout's frontend/ into simtrace_analyser/web/ inside the build (the PWA's
  tests excluded), and _default_web_dir() prefers that packaged copy, falling
  back to <repo>/frontend for source and editable runs.  The source directory
  is resolved from setup.py (so an sdist build works) and a missing frontend/
  fails the build loudly instead of shipping a package without its UI.
- MANIFEST.in grafts frontend/ (with the READMEs, the udev rule and the four
  launch scripts) into the sdist, so a wheel built from the sdist has the PWA.
- README (EN/RU): an install-from-a-release section (pipx/pip with the wheel,
  the direct release URL for pip) and the --web-dir default documented as the
  bundled PWA, or <repo>/frontend from source.
- tests/test_packaging.py pins the packaged-first resolution (including the
  empty-directory fallback), the source checkout default, the real build
  backend, the manifest graft and setup.py's bundling.
- verified: a fresh venv install from the wheel and from the sdist serve
  GET / (200, byte-identical index.html, PWA v2.7.10) out of
  site-packages/simtrace_analyser/web; an editable install still resolves
  <repo>/frontend.  547 Python + 80 Node tests.
2026-10-20 23:06:33 +03:00

98 lines
3.9 KiB
Python

"""simtrace-analyser-server — CLI entry point.
Usage:
simtrace-analyser-server --capture gsmtap [--gsmtap-port 4729] [--port 8081]
simtrace-analyser-server --capture direct [--port 8081]
"""
import argparse
import os
import sys
from http.server import HTTPServer
from .server import RequestHandler
from .database import Database, DEFAULT_DB_PATH
from .capture import CaptureManager, GsmtapListener, DirectSniffer
from .version import __version__
def _pick_web_dir(package_dir, repo_root):
"""The installed package's bundled PWA first, then the source checkout."""
packaged = os.path.join(package_dir, 'web')
if os.path.isfile(os.path.join(packaged, 'index.html')):
return packaged
return os.path.join(repo_root, 'frontend')
def _default_web_dir():
# A wheel/sdist install carries simtrace_analyser/web (copied there by
# setup.py); an editable install and a plain source run fall through to
# <repo>/frontend. __file__ is <repo>/simtrace_analyser/__main__.py.
pkg_dir = os.path.dirname(os.path.abspath(__file__))
return _pick_web_dir(pkg_dir, os.path.dirname(pkg_dir))
def main():
p = argparse.ArgumentParser(
prog='simtrace-analyser-server',
description='HTTP API + PWA server for APDU trace capture and analysis')
p.add_argument('--host', default='127.0.0.1',
help='HTTP bind address (default: 127.0.0.1)')
p.add_argument('--port', type=int, default=8081,
help='HTTP server port (default: 8081)')
p.add_argument('--db', default=DEFAULT_DB_PATH,
help=f'SQLite database path (default: {DEFAULT_DB_PATH})')
p.add_argument('--capture', choices=['gsmtap', 'direct', 'disabled'], default='gsmtap',
help='Capture mode: gsmtap (listen UDP 4729), direct (SIMtrace2 USB), or disabled (no capture)')
p.add_argument('--gsmtap-port', type=int, default=4729,
help='UDP port for GSMTAP listener (default: 4729)')
p.add_argument('--log-interval', type=float, default=60.0,
help='Seconds between capture liveness heartbeat logs '
'(default: 60.0)')
p.add_argument('--log-requests', action='store_true', default=False,
help='Log every HTTP request (off by default)')
p.add_argument('--web-dir', default=_default_web_dir(), metavar='PATH',
help='Directory with the simtrace-analyser PWA static files to serve '
'(default: the PWA bundled in the installed package, '
'or <repo>/frontend in a source checkout)')
args = p.parse_args()
db = Database(args.db)
capture = None
if args.capture == 'gsmtap':
backend = GsmtapListener(bind_port=args.gsmtap_port)
capture = CaptureManager(backend, db, log_interval=args.log_interval)
elif args.capture == 'direct':
backend = DirectSniffer()
capture = CaptureManager(backend, db, log_interval=args.log_interval)
RequestHandler.db = db
RequestHandler.capture = capture
RequestHandler.capture_mode = args.capture
RequestHandler.log_requests = args.log_requests
server = HTTPServer((args.host, args.port), RequestHandler)
server.web_dir = args.web_dir
print(f'simtrace-analyser-server v{__version__} — http://{args.host}:{args.port}', file=sys.stderr)
print(f' capture mode: {args.capture}', file=sys.stderr)
if args.capture == 'gsmtap':
print(f' GSMTAP port: {args.gsmtap_port}', file=sys.stderr)
print(f' database: {args.db}', file=sys.stderr)
print(f' PWA served at http://{args.host}:{args.port}/', file=sys.stderr)
print(file=sys.stderr)
try:
server.serve_forever()
except KeyboardInterrupt:
pass
finally:
if capture and capture.active:
capture.stop_session()
server.server_close()
if __name__ == '__main__':
main()