-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_requirements.py
66 lines (54 loc) · 2.05 KB
/
install_requirements.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
# -*- coding: utf-8 -*-
import platform
import re
import subprocess as sb
try:
import tomlkit
except ImportError:
sb.call("pip install tomlkit==0.11.6", shell=True)
import tomlkit
OS = platform.system().lower()
REQUIREMENTS_FILE = "requirements.txt"
POETRY_FILE = "pyproject.toml"
EXCEPTIONS = {"python"}
PYTHON36_VERSIONS = {
"opencv-python": "4.3.0.38",
"rich": "12.6.0"
}
def write_to_file(content, mode="w"):
with open(REQUIREMENTS_FILE, mode) as f:
f.write(content)
def get_version_for_old_python(package):
return PYTHON36_VERSIONS.get(package.lower(), "")
def get_dependency_version(package, version_info, is_old_python):
if isinstance(version_info, dict):
if "git" in version_info:
return f"git+{version_info['git']}@{version_info.get('branch', 'main')}"
if package.lower() == "pywin32":
return re.sub(r"[*^]", "", version_info.get("version", ""))
elif is_old_python:
return get_version_for_old_python(package)
else:
return re.sub(r"[*^]", "", version_info)
return ""
def generate_requirements():
is_old_python = int(platform.python_version_tuple()[1]) < 9 # Python < 3.9
with open(POETRY_FILE, "r") as f:
poetry_data = tomlkit.parse(f.read())
dependencies = poetry_data.get("tool", {}).get("poetry", {}).get("dependencies", {})
for package, version_info in dependencies.items():
if package.lower() in EXCEPTIONS:
continue
version = get_dependency_version(package, version_info, is_old_python)
if package.lower() == "pywin32" and OS != "windows":
continue
write_to_file(f"{package}=={version}\n" if version else f"{package}\n", "a")
def upgrade_pip():
sb.call("python -m pip install --upgrade pip", shell=True)
def install_requirements():
sb.call(f"pip install -r {REQUIREMENTS_FILE}", shell=True)
if __name__ == "__main__":
upgrade_pip()
write_to_file("# -*- coding: utf-8 -*-\n", "w") # Очистка файла
generate_requirements()
install_requirements()