-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch_two_windows.py
More file actions
136 lines (113 loc) · 4.13 KB
/
launch_two_windows.py
File metadata and controls
136 lines (113 loc) · 4.13 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import os
import sys
import subprocess
from pathlib import Path
def is_frozen():
return getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS")
def base_dir() -> Path:
# When frozen by PyInstaller, resources are unpacked into sys._MEIPASS
if is_frozen():
return Path(sys._MEIPASS)
return Path(__file__).resolve().parent
def project_dir_for_channel() -> Path:
"""
Channel files should be written to a stable location.
- If running as .py: project folder
- If running as .exe: folder where the exe lives
"""
if is_frozen():
return Path(sys.executable).resolve().parent
return Path(__file__).resolve().parent
def python_to_run() -> str:
"""
- .py mode: use venv python if present
- .exe mode: use the embedded Python by calling this executable itself with -c is not possible,
so we launch a new copy of the EXE with a special argument.
"""
if is_frozen():
# We'll relaunch the same EXE with args that tell it to run alice/bob
return str(Path(sys.executable).resolve())
# not frozen: use venv python
venv_py = Path(__file__).resolve().parent / "venv" / "Scripts" / "python.exe"
return str(venv_py)
def start_powershell_window(title: str, ps_command: str, cwd: Path):
subprocess.Popen(
[
"powershell",
"-NoExit",
"-ExecutionPolicy", "Bypass",
"-Command",
f'$Host.UI.RawUI.WindowTitle = "{title}"; {ps_command}'
],
cwd=str(cwd),
)
def run_role(role: str):
"""
Runs alice or bob in the current process.
Used when EXE is re-launched with --role alice/bob.
"""
bdir = base_dir()
pdir = project_dir_for_channel()
# Ensure channel dir exists in stable location
(pdir / "channel").mkdir(exist_ok=True)
# Add bundled utils to import path
sys.path.insert(0, str(bdir))
if role == "alice":
alice_path = bdir / "alice.py"
code = alice_path.read_text(encoding="utf-8")
exec(compile(code, str(alice_path), "exec"), {"__name__": "__main__"})
elif role == "bob":
bob_path = bdir / "bob.py"
code = bob_path.read_text(encoding="utf-8")
exec(compile(code, str(bob_path), "exec"), {"__name__": "__main__"})
else:
raise ValueError("Unknown role")
def main_launcher():
bdir = base_dir()
pdir = project_dir_for_channel()
# Stable channel folder lives next to EXE (or project folder in .py mode)
channel_a = pdir / "channel" / "alice_states.npy"
channel_b = pdir / "channel" / "encoding_basis.npy"
py = python_to_run()
if is_frozen():
# In EXE mode, we re-launch the same EXE with --role alice/bob
alice_cmd = f'& "{py}" --role alice'
bob_cmd = f"""
Write-Host "Waiting for Alice channel files..." -ForegroundColor Yellow
while (!(Test-Path "{channel_a}") -or !(Test-Path "{channel_b}")) {{
Start-Sleep -Milliseconds 200
}}
& "{py}" --role bob
Write-Host ""
Write-Host "Bob finished. Press Enter to close." -ForegroundColor Green
[void](Read-Host)
exit
"""
else:
# .py mode: run via venv python and real scripts from project folder
alice_py = pdir / "alice.py"
bob_py = pdir / "bob.py"
alice_cmd = f'Set-Location "{pdir}"; Write-Host "Running Alice..." -ForegroundColor Cyan; & "{py}" "{alice_py}"'
bob_cmd = f"""
Set-Location "{pdir}"
Write-Host "Waiting for Alice channel files..." -ForegroundColor Yellow
while (!(Test-Path "{channel_a}") -or !(Test-Path "{channel_b}")) {{
Start-Sleep -Milliseconds 200
}}
Write-Host "Running Bob..." -ForegroundColor Cyan
& "{py}" "{bob_py}"
Write-Host ""
Write-Host "Bob finished. Press Enter to close." -ForegroundColor Green
[void](Read-Host)
exit
"""
start_powershell_window("BB84 - Alice", alice_cmd, cwd=pdir)
start_powershell_window("BB84 - Bob", bob_cmd, cwd=pdir)
if __name__ == "__main__":
# If running as EXE and invoked with --role alice/bob, run that role.
if "--role" in sys.argv:
idx = sys.argv.index("--role")
role = sys.argv[idx + 1] if idx + 1 < len(sys.argv) else ""
run_role(role)
else:
main_launcher()