-
Notifications
You must be signed in to change notification settings - Fork 1
/
requirements_install.py
145 lines (126 loc) · 4.51 KB
/
requirements_install.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
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
import importlib
import os
import platform
import re
import subprocess
from typing import List, Union
package_list: List[str] = []
def generate_requirements():
global package_list
base_requirements = "requirements_base.txt"
windows_specific = [
"pywin32",
"wmi",
"PySide6==6.*",
"qtawesome==1.*",
# "nuitka==2.*",
"nuitka @ https://github.com/Nuitka/Nuitka/archive/develop.zip",
"pyinstaller",
"pyqtgraph",
"pyqtdarktheme",
"psutil",
"pynput",
]
linux_specific = ["uwsgi", "gunicorn"]
lines = []
with open(base_requirements, "r", encoding="utf-8") as base_file:
lines.extend(base_file.readlines())
if os.path.exists("requirements_additional.txt"):
with open("requirements_additional.txt", "r", encoding="utf-8") as base_file:
lines.extend(base_file.readlines())
if platform.system() == "Windows":
lines.extend([f"{package}\n" for package in windows_specific])
else:
lines.extend([f"{package}\n" for package in linux_specific])
# Remove empty lines and duplicates
unique_lines = list(dict.fromkeys([line for line in lines if line.strip()]))
package_list = unique_lines
# Write to requirements.txt
with open("requirements.txt", "w", encoding="utf-8") as req_file:
req_file.writelines(unique_lines)
# Add a new line at the end of the file
with open("requirements.txt", "a", encoding="utf-8") as req_file:
req_file.write("\n")
return True
def is_package_installed(package_name):
try:
importlib.import_module(package_name)
return True
except ImportError:
return False
def install_package(name: Union[str, List[str]], install_args=[]):
if isinstance(name, str):
print(f"installing {name}")
else:
print(f"installing local package \"{' '.join(name)}\"")
index_urls = [
"https://pypi.org/simple",
"https://mirrors.sustech.edu.cn/pypi/simple/",
"https://mirrors.sustech.edu.cn/pypi/web/simple",
"https://pypi.tuna.tsinghua.edu.cn/simple/",
"https://mirrors.bfsu.edu.cn/pypi/web/simple/",
"https://mirrors.aliyun.com/pypi/simple/",
"https://mirrors.cloud.tencent.com/pypi/simple/",
"https://repo.huaweicloud.com/repository/pypi/simple/",
"https://mirror.nju.edu.cn/pypi/web/simple/",
]
for index_url in index_urls:
try:
if isinstance(name, str):
subprocess.check_call(
[
"pip",
"install",
name,
f"--index-url={index_url}",
]
+ install_args
)
else:
# param name is list
subprocess.check_call(
["pip", "install"]
+ name
+ [f"--index-url={index_url}"]
+ install_args
)
break
except Exception:
print(f"fail install {name} from {index_url}")
def install_requirements():
global package_list
if not package_list:
generate_requirements()
subprocess.check_call(
["python", "-m", "pip", "install", "--upgrade", "pip", "setuptools", "wheel"]
)
if not is_package_installed("socks"):
install_package("socks", ["--use-pep517"])
for pkg in package_list:
name = re.sub(r"\s+", " ", pkg.strip()).strip()
if name.startswith("#") or not name:
# skip comment block & empty package name
continue
if name.startswith("-e"):
# always install local package
name = name.replace("-e ", "").strip()
install_package(["-e", name])
elif "@ http" in name:
# install `packagename @ url-zip`
pkgname, url = name.split(" @ ")
if not is_package_installed(pkgname.strip()):
install_package(name)
elif " --" in name:
_args = [item for item in name.split(" ") if item]
print(_args)
install_package(_args)
elif not is_package_installed(pkg):
install_package(name)
# try:
# subprocess.check_call(["pip", "install", "-r", "requirements.txt"])
# subprocess.check_call(["pip", "install", "socks", "--use-pep517"])
# except Exception:
# pass
if __name__ == "__main__":
generate_requirements()
install_requirements()