From 016c1e120b79dd4c1e16e4f9af1e87a7dbe7c8c7 Mon Sep 17 00:00:00 2001 From: Yao Chi Date: Fri, 20 Sep 2024 09:43:28 +0800 Subject: [PATCH] check and update requirements automatically (#145) * check and update requirements automatically * remove depency numpy --- prestartup_script.py | 45 ++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 - 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 prestartup_script.py diff --git a/prestartup_script.py b/prestartup_script.py new file mode 100644 index 00000000..2c5f879b --- /dev/null +++ b/prestartup_script.py @@ -0,0 +1,45 @@ +import os +import subprocess +import sys +from importlib.metadata import distributions + +from packaging.requirements import Requirement +from packaging.version import Version + + +def install_dependencies(): + current_path = os.path.abspath(os.path.dirname(__file__)) + with open(os.path.join(current_path, "requirements.txt")) as f: + required_packages = [ + line.strip() + for line in f.readlines() + if line.strip() and not line.strip().startswith("#") + ] + + installed_packages = { + dist.metadata["Name"]: Version(dist.version) for dist in distributions() + } + + for package in required_packages: + try: + requirement = Requirement(package) + installed_version = installed_packages.get(requirement.name) + if not installed_version or not requirement.specifier.contains( + installed_version + ): + try: + print(f"\033[92m[BizyAir]\033[0m Try to install depency {package}") + subprocess.check_call( + [sys.executable, "-m", "pip", "install", package] + ) + except subprocess.CalledProcessError as e: + print(f"\033[91m[BizyAir]\033[0m Failed to install {package}: {e}") + continue + except Exception as e: + print( + f"\033[91m[BizyAir]\033[0m Failed to parse requirement {package}: {e}" + ) + continue + + +install_dependencies() diff --git a/requirements.txt b/requirements.txt index 3f73b9b8..b2f73295 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ -numpy<2.0 oss2 crcmod requests