Upload install.py
Browse files- install.py +45 -0
install.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pathlib
|
| 2 |
+
import re
|
| 3 |
+
import subprocess
|
| 4 |
+
import sys
|
| 5 |
+
import traceback
|
| 6 |
+
import pkg_resources
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def install_package(package):
|
| 10 |
+
try:
|
| 11 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
| 12 |
+
except subprocess.CalledProcessError as e:
|
| 13 |
+
print(f"Error installing package {package}: {e}", file=sys.stderr)
|
| 14 |
+
return False
|
| 15 |
+
return True
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def is_package_installed(package_name, comparison=None, required_version=None):
|
| 19 |
+
try:
|
| 20 |
+
installed_version = pkg_resources.get_distribution(package_name).version
|
| 21 |
+
if comparison and required_version:
|
| 22 |
+
return eval(f'"{installed_version}" {comparison}= "{required_version}"')
|
| 23 |
+
return True
|
| 24 |
+
except:
|
| 25 |
+
return False
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def main():
|
| 29 |
+
req_file = pathlib.Path(__file__).resolve().parent / "requirements.txt"
|
| 30 |
+
req_re = re.compile(r'^([^=<>~]*)\s*(?:([=<>~])=\s*([^=<>~]*))?$')
|
| 31 |
+
|
| 32 |
+
with open(req_file) as file:
|
| 33 |
+
for package in file:
|
| 34 |
+
package = package.strip()
|
| 35 |
+
match = req_re.match(package)
|
| 36 |
+
if not match:
|
| 37 |
+
print(f"Invalid package format: {package}", file=sys.stderr)
|
| 38 |
+
continue
|
| 39 |
+
|
| 40 |
+
package_name, comparison, required_version = match.groups()
|
| 41 |
+
if not is_package_installed(package_name, comparison, required_version):
|
| 42 |
+
install_package(package)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
main()
|