-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
76 lines (66 loc) · 1.89 KB
/
utils.py
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
# SPDX-License-Identifier: GPL-2.0-or-later
import ctypes
import os
import subprocess
import solvent.constants as constants
def is_admin() -> bool:
if constants.CURRENT_PLATFORM == "Windows":
try:
return ctypes.windll.shell32.IsUserAnAdmin() != 0
except Exception:
return False
else:
return os.getuid() == 0
def install(package: constants.Package) -> None:
if package.version is None:
command = [
constants.PYTHON_EXECUTABLE_LOCATION,
"-m",
"pip",
"install",
"--upgrade",
"--no-warn-script-location",
package.name,
]
else:
command = [
constants.PYTHON_EXECUTABLE_LOCATION,
"-m",
"pip",
"install",
"--upgrade",
"--no-warn-script-location",
f"{package.name}=={package.version}",
]
if package.extra_args is not None:
command.extend(package.extra_args)
subprocess.run(command, check=True, env=constants.ENVIRONMENT_VARIABLES)
def setup() -> None:
# Ensure that pip is installed and up-to-date
spec_output = subprocess.check_output(
[
constants.PYTHON_EXECUTABLE_LOCATION,
"-c",
"from importlib.util import find_spec; print(find_spec('pip'))",
],
env=constants.ENVIRONMENT_VARIABLES,
)
if spec_output == b"None\n":
subprocess.run(
[constants.PYTHON_EXECUTABLE_LOCATION, "-m", "ensurepip"],
check=True,
env=constants.ENVIRONMENT_VARIABLES,
)
pip_upgrade_command = [
constants.PYTHON_EXECUTABLE_LOCATION,
"-m",
"pip",
"install",
"--upgrade",
"pip",
]
subprocess.run(
pip_upgrade_command,
check=True,
env=constants.ENVIRONMENT_VARIABLES,
)