-
Notifications
You must be signed in to change notification settings - Fork 16
/
install.py
38 lines (31 loc) · 1.65 KB
/
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
import launch
import os
import pkg_resources
from typing import Tuple, Optional
req_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "requirements.txt")
def comparable_version(version: str) -> Tuple:
return tuple(version.split('.'))
def get_installed_version(package: str) -> Optional[str]:
try:
return pkg_resources.get_distribution(package).version
except Exception:
return None
with open(req_file) as file:
for package in file:
try:
package = package.strip()
if '==' in package:
package_name, package_version = package.split('==')
installed_version = get_installed_version(package_name)
if installed_version != package_version:
launch.run_pip(f"install -U {package}", f"prompt-morph requirement: changing {package_name} version from {installed_version} to {package_version}")
elif '>=' in package:
package_name, package_version = package.split('>=')
installed_version = get_installed_version(package_name)
if not installed_version or comparable_version(installed_version) < comparable_version(package_version):
launch.run_pip(f"install -U {package}", f"prompt-morph requirement: changing {package_name} version from {installed_version} to {package_version}")
elif not launch.is_installed(package):
launch.run_pip(f"install {package}", f"prompt-morph requirement: {package}")
except Exception as e:
print(e)
print(f'Warning: Failed to install {package}, some preprocessors may not work.')