|
| 1 | +# Silence streamlit's "missing ScriptRunContext" / "No runtime found" / |
| 2 | +# "Session state does not function" warnings, which fire whenever streamlit- |
| 3 | +# decorated code runs outside an active script run (our CLI, scheduler, server, |
| 4 | +# and any import that touches @st.cache_data). Must run before the first |
| 5 | +# streamlit-using import, so it sits at the top of the module. |
| 6 | +# |
| 7 | +# We replace ``set_log_level`` itself, after seeding it to "error". Streamlit's |
| 8 | +# own ``_update_logger`` callback fires on config parse and would otherwise |
| 9 | +# downgrade us back to "info"; the cap floors any later call at ERROR. |
| 10 | +def _silence_streamlit_logs() -> None: |
| 11 | + import logging as _logging |
| 12 | + |
| 13 | + try: |
| 14 | + from streamlit import logger as _st_logger |
| 15 | + except ImportError: |
| 16 | + return |
| 17 | + |
| 18 | + _original = _st_logger.set_log_level |
| 19 | + _original("error") |
| 20 | + |
| 21 | + def _capped(level): |
| 22 | + if isinstance(level, str): |
| 23 | + try: |
| 24 | + level_num = getattr(_logging, level.upper()) |
| 25 | + except AttributeError: |
| 26 | + _original(level) |
| 27 | + return |
| 28 | + else: |
| 29 | + level_num = level |
| 30 | + _original(max(level_num, _logging.ERROR)) |
| 31 | + |
| 32 | + _st_logger.set_log_level = _capped |
| 33 | + |
| 34 | + |
| 35 | +_silence_streamlit_logs() |
| 36 | + |
| 37 | + |
1 | 38 | import base64 |
2 | 39 | import importlib |
3 | 40 | import logging |
@@ -566,14 +603,21 @@ def generate_secret(length: int = 12) -> str: |
566 | 603 |
|
567 | 604 | # Persist caller-supplied runtime overrides (ports, TLS) so they apply to |
568 | 605 | # subsequent `testgen run-app` invocations. |
569 | | - persisted_env_vars = ("TG_UI_PORT", "TG_API_PORT", "SSL_CERT_FILE", "SSL_KEY_FILE") |
| 606 | + persisted_env_vars = ("TG_UI_PORT", "TG_API_PORT", "TESTGEN_LOG_FILE_PATH", "SSL_CERT_FILE", "SSL_KEY_FILE") |
570 | 607 | persisted_lines = [f"{name}={os.environ[name]}" for name in persisted_env_vars if os.environ.get(name)] |
571 | 608 | if persisted_lines: |
572 | 609 | config_lines.extend(["", "# Runtime overrides from installer", *persisted_lines]) |
573 | 610 |
|
574 | 611 | config_path.write_text("\n".join(config_lines) + "\n") |
575 | 612 | click.echo(f"Config written to {config_path}") |
576 | 613 |
|
| 614 | + # `getenv` resolves env vars before config.env, so a pre-existing |
| 615 | + # TESTGEN_USERNAME / TESTGEN_PASSWORD in the shell would override the |
| 616 | + # CLI-supplied values and get seeded into the DB. Force the CLI args |
| 617 | + # to win for the rest of this process. |
| 618 | + os.environ["TESTGEN_USERNAME"] = username |
| 619 | + os.environ["TESTGEN_PASSWORD"] = password |
| 620 | + |
577 | 621 | # Reload settings — the module was already evaluated at import time |
578 | 622 | # before the config file existed. Reloading re-reads the new file |
579 | 623 | # and re-evaluates all module-level variables. |
@@ -895,6 +939,7 @@ def init_ui(): |
895 | 939 | "run", |
896 | 940 | app_file, |
897 | 941 | "--browser.gatherUsageStats=false", |
| 942 | + f"--logger.level={'debug' if settings.IS_DEBUG else 'error'}", |
898 | 943 | "--client.showErrorDetails=none", |
899 | 944 | "--client.toolbarMode=minimal", |
900 | 945 | "--server.enableStaticServing=true", |
|
0 commit comments