-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch_swampy.py
More file actions
165 lines (137 loc) · 4.94 KB
/
Copy pathlaunch_swampy.py
File metadata and controls
165 lines (137 loc) · 4.94 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""
Swampy entry point.
Run this from the repo root:
python launch_swampy.py [options]
It checks for updates first, then delegates to app/launch_swampy.py.
"""
import os
import runpy
import sys
_REPO_ROOT = os.path.dirname(os.path.abspath(__file__))
_APP_DIR = os.path.join(_REPO_ROOT, "app")
# Make repo root importable (for updater) and app/ importable (for all
# the app modules: gui_swampy, sambuca, output_calculation, …)
for _p in (_REPO_ROOT, _APP_DIR):
if _p not in sys.path:
sys.path.insert(0, _p)
import auth
import updater
def _show_messagebox(method_name, title, message):
try:
import tkinter as tk
from tkinter import messagebox
except Exception:
print(f"{title}\n\n{message}", file=sys.stderr)
return False if method_name == "askyesno" else None
root = None
try:
root = tk.Tk()
root.withdraw()
try:
root.attributes("-topmost", True)
except Exception:
pass
return getattr(messagebox, method_name)(title, message, parent=root)
except Exception:
print(f"{title}\n\n{message}", file=sys.stderr)
return False if method_name == "askyesno" else None
finally:
if root is not None:
try:
root.destroy()
except Exception:
pass
def _describe_current_env(current_env_name):
if current_env_name:
return current_env_name
return "no active Conda environment"
def _build_wrong_env_message(current_env_name):
return (
f"Swampy must be launched from the Conda environment '{updater.CONDA_ENV_NAME}'.\n\n"
f"Current environment: {_describe_current_env(current_env_name)}\n\n"
f"Activate the correct environment, then relaunch Swampy:\n"
f" conda activate {updater.CONDA_ENV_NAME}\n"
" python launch_swampy.py"
)
def _build_legacy_env_delete_prompt(current_env_name):
message = _build_wrong_env_message(current_env_name)
if current_env_name == updater.LEGACY_CONDA_ENV_NAME:
return (
f"{message}\n\n"
f"The legacy Swampy environment '{updater.LEGACY_CONDA_ENV_NAME}' is still active.\n"
"Swampy can try to delete it now, but Conda may refuse because that environment is in use.\n\n"
f"Do you want Swampy to try to delete '{updater.LEGACY_CONDA_ENV_NAME}' now?"
)
return (
f"{message}\n\n"
f"The legacy Swampy environment '{updater.LEGACY_CONDA_ENV_NAME}' is still installed.\n\n"
f"Do you want Swampy to delete '{updater.LEGACY_CONDA_ENV_NAME}' now?"
)
def ensure_expected_conda_env():
current_env_name = updater.get_current_conda_env_name()
if current_env_name == updater.CONDA_ENV_NAME:
return True
warning_message = _build_wrong_env_message(current_env_name)
try:
legacy_env_exists = updater.conda_env_exists(updater.LEGACY_CONDA_ENV_NAME)
except Exception as exc:
_show_messagebox(
"showwarning",
"Wrong Conda Environment",
(
f"{warning_message}\n\n"
f"Swampy could not inspect whether '{updater.LEGACY_CONDA_ENV_NAME}' is still installed.\n"
f"{exc}"
),
)
return False
if not legacy_env_exists:
_show_messagebox("showwarning", "Wrong Conda Environment", warning_message)
return False
wants_delete = _show_messagebox(
"askyesno",
"Wrong Conda Environment",
_build_legacy_env_delete_prompt(current_env_name),
)
if wants_delete:
try:
updater.remove_conda_env(updater.LEGACY_CONDA_ENV_NAME)
except Exception as exc:
_show_messagebox(
"showerror",
"Cannot Delete Legacy Environment",
(
f"{warning_message}\n\n"
f"Swampy could not delete '{updater.LEGACY_CONDA_ENV_NAME}'.\n"
f"{exc}"
),
)
else:
_show_messagebox(
"showinfo",
"Legacy Environment Removed",
(
f"Removed '{updater.LEGACY_CONDA_ENV_NAME}'.\n\n"
f"Activate '{updater.CONDA_ENV_NAME}' and run Swampy again."
),
)
return False
def main():
# Always run from the repo root so that relative paths inside the app
# (Data/, Output/, …) resolve correctly.
os.chdir(_REPO_ROOT)
if not auth.ensure_app_authorized(_REPO_ROOT):
return 1
# Update check — runs before any app code is loaded.
if not updater.check_and_prompt():
# Updater relaunched the process after applying the update.
return 0
if not ensure_expected_conda_env():
return 1
runpy.run_path(
os.path.join(_APP_DIR, "launch_swampy.py"),
run_name="__main__",
)
return 0
if __name__ == "__main__":
sys.exit(main())