-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
101 lines (88 loc) · 3.61 KB
/
Copy pathmain.py
File metadata and controls
101 lines (88 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
import sys
def _ensure_windows_cli_stdio() -> None:
"""GUI-subsystem binaries need usable stdio for CLI flags on Windows."""
if sys.platform != "win32":
return
if len(sys.argv) < 2 or not str(sys.argv[1]).startswith("--"):
return
# If parent already redirected pipes (CI / Cursor helper), keep them.
if sys.stdout is not None and sys.stderr is not None:
try:
sys.stdout.fileno()
sys.stderr.fileno()
return
except Exception:
pass
try:
import ctypes
import io
kernel32 = ctypes.windll.kernel32 # type: ignore[attr-defined]
if kernel32.GetConsoleWindow() == 0:
kernel32.AttachConsole(0xFFFFFFFF) # ATTACH_PARENT_PROCESS
try:
sys.stdout = io.TextIOWrapper(
open("CONOUT$", "wb", buffering=0), encoding="utf-8", errors="replace"
)
sys.stderr = io.TextIOWrapper(
open("CONOUT$", "wb", buffering=0), encoding="utf-8", errors="replace"
)
except OSError:
# Fallback for fully headless launches.
if sys.stdout is None:
sys.stdout = open(os.devnull, "w", encoding="utf-8")
if sys.stderr is None:
sys.stderr = open(os.devnull, "w", encoding="utf-8")
except Exception:
pass
def main():
_ensure_windows_cli_stdio()
if len(sys.argv) >= 2 and sys.argv[1] in {"--print-provider-env", "--provider-env"}:
from pi_manager.provider_env import main as provider_env_main
return provider_env_main(sys.argv[2:])
if len(sys.argv) >= 2 and sys.argv[1] == "--config-mutate":
import json
from pi_manager.config_broker import mutate_file
from pi_manager.provider_env import _emit
output_path = ""
if len(sys.argv) == 5 and sys.argv[3] == "--output":
output_path = sys.argv[4]
elif len(sys.argv) != 3:
result = {"ok": False, "error": "request file is required"}
print(json.dumps(result))
return 2
result = mutate_file(sys.argv[2])
encoded = json.dumps(result, ensure_ascii=False)
if output_path:
try:
# Same hardened write as provider-env responses (pre-created
# file only, no symlink following); stdout below remains the
# fallback channel the extension already reads.
_emit(result, output_path)
except (ValueError, OSError):
pass
print(encoded)
return 0 if result.get("ok") else 2
# Helper subcommands above are the extension's hot path and must not
# rewrite the registry on every call; publish it when the app itself runs.
from pi_manager.helper_registry import register_current_helper_best_effort
register_current_helper_best_effort()
if len(sys.argv) >= 2 and sys.argv[1] in {"--self-check", "--smoke-test"}:
from pi_manager.extras import APP_VERSION
from pi_manager.resources import self_check
errors = self_check()
if errors:
for line in errors:
print(f"FAIL: {line}", file=sys.stderr)
print("self-check: FAILED", file=sys.stderr)
return 1
print("self-check: OK")
print(f"version={APP_VERSION}")
print(f"frozen={bool(getattr(sys, 'frozen', False))}")
print(f"executable={sys.executable}")
print(f"platform={sys.platform}")
return 0
from pi_manager.ui import run_app
return run_app()
if __name__ == "__main__":
raise SystemExit(main())