#!/usr/bin/env python """Launch the ows-analytics app for an endpoint_diff capture. Same Flask app as ``make dev``, with three deliberate differences that make it safe to drive programmatically and to compare two git checkouts: * ``use_reloader=False`` -- single OS process, so it can be killed cleanly between ``git checkout``s (the debug reloader spawns a child watcher). * ``debug=False`` -- deterministic error pages; an unhandled 500 returns the same plain body on both builds instead of the Werkzeug debugger HTML. * ``REDIS_HOST`` forced empty -- ``analytics.connectors.redis`` then falls back to ``fakeredis``, an in-process cache that lives and dies with this process. Each capture therefore starts cold and no cached value can leak from one build's run into another's. ``application`` is imported *after* the environment is set and the repo root is on ``sys.path``, so it always loads the currently checked-out code. The repo root is normally derived from this file's location. ``run.sh`` / ``resample.sh`` stage a copy of this script into ``_out/`` so it survives a ``git checkout`` of a before-ref that predates this directory, and point the copy back at the repo with ``ENDPOINT_DIFF_ROOT`` -- which takes precedence. """ import os import subprocess import sys ROOT = os.environ.get("ENDPOINT_DIFF_ROOT") or os.path.abspath( os.path.join(os.path.dirname(__file__), "..", "..", "..") ) os.chdir(ROOT) sys.path.insert(0, ROOT) # Force fakeredis. analytics.config calls load_dotenv(override=False), which # will not override a variable that is already present in the environment. os.environ["REDIS_HOST"] = "" os.environ.setdefault("Environment", "dev") HOST = os.environ.get("ENDPOINT_DIFF_HOST", "127.0.0.1") PORT = int(os.environ.get("ENDPOINT_DIFF_PORT", "5000")) from application import app # noqa: E402 (env + sys.path must be set first) if __name__ == "__main__": try: rev = subprocess.check_output( ["git", "rev-parse", "--short", "HEAD"], text=True, cwd=ROOT ).strip() except Exception: rev = "?" print( f"[endpoint_diff serve] git={rev} http://{HOST}:{PORT} " f"debug=off reloader=off fakeredis", flush=True, ) app.run(host=HOST, port=PORT, debug=False, use_reloader=False, threaded=True)