FiWiManager/fiwi.py

60 lines
1.6 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""Fi-Wi test framework CLI — maps and SSH env files resolve to this files directory."""
import os
import sys
def _consume_early_config_argv(argv: list[str]) -> list[str]:
"""
Apply ``-c`` / ``--config`` (INI profile or ``*.ini`` path) as ``FIWI_CONFIG``, and drop those
tokens so :mod:`getopt` in :mod:`fiwi.cli` does not treat ``-c`` as unknown.
"""
out: list[str] = []
i = 0
while i < len(argv):
a = argv[i]
if a in ("-c", "--config"):
if i + 1 < len(argv):
os.environ["FIWI_CONFIG"] = argv[i + 1].strip()
i += 2
continue
out.append(a)
i += 1
continue
if a.startswith("--config="):
os.environ["FIWI_CONFIG"] = a.partition("=")[2].strip()
i += 1
continue
out.append(a)
i += 1
return out
if len(sys.argv) > 1:
sys.argv[1:] = _consume_early_config_argv(sys.argv[1:])
import fiwi.paths as _paths
_paths.configure(os.path.dirname(os.path.abspath(__file__)))
def _prefer_line_buffered_stdio() -> None:
"""Flush stdout/stderr after each newline when attached to a pipe (e.g. SSH capture)."""
for stream in (sys.stdout, sys.stderr):
reconf = getattr(stream, "reconfigure", None)
if reconf is None:
continue
try:
reconf(line_buffering=True)
except (OSError, ValueError, AttributeError):
pass
_prefer_line_buffered_stdio()
from fiwi.cli import main
if __name__ == "__main__":
raise SystemExit(main())