Skip to content

Commit e7fc2b9

Browse files
committed
move venv launch function to utils
1 parent feeb93a commit e7fc2b9

4 files changed

Lines changed: 39 additions & 42 deletions

File tree

CI.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
from Messages import ITEM_MESSAGES, IMPORTANT_ITEM_MESSAGES, MISC_MESSAGES
1919
from SettingsList import SettingInfos, logic_tricks, validate_settings
2020
import Unittest as Tests
21-
from Utils import data_path
22-
from Gui import ensure_venv
21+
from Utils import data_path, ensure_venv
2322

2423

2524
def error(msg: str, can_fix: bool) -> None:

Gui.py

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,12 @@
1010
input("Press enter to exit...")
1111
sys.exit(1)
1212

13-
import os
14-
import platform
1513
import shutil
1614
import subprocess
17-
import venv
1815
import webbrowser
1916

2017
from SettingsToJson import create_settings_list_json
21-
from Utils import local_path, data_path, compare_version, VersionError
22-
23-
24-
VENV_DIR = local_path(".venv")
25-
PYTHON_BIN = os.path.abspath(os.path.join(VENV_DIR, "bin", "python3"))
26-
if platform.system() == 'Windows':
27-
PYTHON_BIN = os.path.abspath(os.path.join(VENV_DIR, "Scripts", "python.exe"))
28-
REQUIREMENTS = local_path("requirements.txt")
29-
30-
31-
def ensure_venv():
32-
# If venv doesn’t exist, create it
33-
if not os.path.exists(PYTHON_BIN):
34-
print("Creating virtual environment...")
35-
venv.create(VENV_DIR, with_pip=True)
36-
requirements_not_met = False
37-
if '--no-pip' not in sys.argv:
38-
# Running pip twice lets us both capture output cleanly to see if
39-
# we need to reload the venv and send output live to the user on
40-
# actual install.
41-
print("Checking for required python dependencies")
42-
req_check = subprocess.run([PYTHON_BIN, "-m", "pip", "install", "-r", REQUIREMENTS, "--dry-run"], capture_output=True, text=True)
43-
if req_check.returncode != 0:
44-
raise ImportError(f"pip failed to verify required dependencies:\n{req_check.stderr}")
45-
requirements_not_met = "collecting " in req_check.stdout.lower()
46-
if requirements_not_met:
47-
print("Installing missing python dependencies")
48-
req_check = subprocess.run([PYTHON_BIN, "-m", "pip", "install", "-r", REQUIREMENTS], capture_output=True, text=True)
49-
if req_check.returncode != 0:
50-
raise ImportError(f"pip failed to install required dependencies:\n{req_check.stderr}")
51-
52-
# If we're not already running inside the venv, restart with it
53-
if sys.executable != PYTHON_BIN or requirements_not_met:
54-
print('Re-launching in virtual environment')
55-
subprocess.check_call([PYTHON_BIN] + sys.argv + ['--no-pip'])
56-
sys.exit(0)
18+
from Utils import local_path, data_path, compare_version, ensure_venv, VersionError
5719

5820

5921
def gui_main() -> None:

OoTRandomizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import os
1010
import time
1111

12-
from Gui import ensure_venv
12+
from Utils import ensure_venv
1313

1414
def start() -> None:
1515
ensure_venv()

Utils.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import json
44
import logging
55
import os
6+
import platform
67
import re
78
import subprocess
89
import sys
910
import urllib.request
11+
import venv
1012
from collections.abc import Sequence
1113
from itertools import chain, combinations
1214
from typing import AnyStr, Optional, Any
@@ -235,3 +237,37 @@ def powerset(iterable):
235237
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
236238
s = list(iterable)
237239
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
240+
241+
242+
def ensure_venv():
243+
VENV_DIR = local_path(".venv")
244+
PYTHON_BIN = os.path.abspath(os.path.join(VENV_DIR, "bin", "python3"))
245+
if platform.system() == 'Windows':
246+
PYTHON_BIN = os.path.abspath(os.path.join(VENV_DIR, "Scripts", "python.exe"))
247+
REQUIREMENTS = local_path("requirements.txt")
248+
249+
# If venv doesn’t exist, create it
250+
if not os.path.exists(PYTHON_BIN):
251+
print("Creating virtual environment...")
252+
venv.create(VENV_DIR, with_pip=True)
253+
requirements_not_met = False
254+
if '--no-pip' not in sys.argv:
255+
# Running pip twice lets us both capture output cleanly to see if
256+
# we need to reload the venv and send output live to the user on
257+
# actual install.
258+
print("Checking for required python dependencies")
259+
req_check = subprocess.run([PYTHON_BIN, "-m", "pip", "install", "-r", REQUIREMENTS, "--dry-run"], capture_output=True, text=True)
260+
if req_check.returncode != 0:
261+
raise ImportError(f"pip failed to verify required dependencies:\n{req_check.stderr}")
262+
requirements_not_met = "collecting " in req_check.stdout.lower()
263+
if requirements_not_met:
264+
print("Installing missing python dependencies")
265+
req_check = subprocess.run([PYTHON_BIN, "-m", "pip", "install", "-r", REQUIREMENTS], capture_output=True, text=True)
266+
if req_check.returncode != 0:
267+
raise ImportError(f"pip failed to install required dependencies:\n{req_check.stderr}")
268+
269+
# If we're not already running inside the venv, restart with it
270+
if sys.executable != PYTHON_BIN or requirements_not_met:
271+
print('Re-launching in virtual environment')
272+
subprocess.check_call([PYTHON_BIN] + sys.argv + ['--no-pip'])
273+
sys.exit(0)

0 commit comments

Comments
 (0)