Generalize Auto-Package Installation Error Handling

This commit is contained in:
TSC
2023-09-09 13:19:21 -05:00
committed by GitHub
parent e1d302b5f5
commit c8f21356e8

View File

@@ -489,33 +489,38 @@ def install_packages(my_dir):
with open(os.path.join(my_dir, 'requirements.txt'), 'r') as f:
required_packages = [line.strip() for line in f if line.strip()]
try:
installed_packages = packages(embedded_python_exe if use_embedded else None, versions=False)
for pkg in required_packages:
if pkg not in installed_packages:
print(f"\033[32mEfficiency Nodes:\033[0m Installing required package '{pkg}'...", end='', flush=True)
try:
printout = f"Installing required package '{pkg}'..."
print(f"{message('Efficiency Nodes:')} {printout}", end='', flush=True)
if use_embedded: # Targeted installation
subprocess.check_call(['pip', 'install', pkg, '--target=' + target_dir, '--no-warn-script-location',
'--disable-pip-version-check'], stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
'--disable-pip-version-check'], stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE)
else: # Untargeted installation
subprocess.check_call(['pip', 'install', pkg], stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
print(f"\r\033[32mEfficiency Nodes:\033[0m Installing required package '{pkg}'... Installed!", flush=True)
print(f"\r{message('Efficiency Nodes:')} {printout}{success('Installed!')}", flush=True)
except subprocess.CalledProcessError as e: # Failed installation
base_message = f"\r\033[31mEfficiency Nodes Error:\033[0m Failed to install python package '{pkg}'. "
if e.stderr:
error_message = e.stderr.decode()
print(base_message + f"Error message: {error_message}")
else:
print(base_message + "\nPlease check your permissions, network connectivity, or try a manual installation.")
except Exception as e: # This catches all exceptions derived from the base Exception class
print_general_error_message()
def packages(python_exe=None, versions=False):
# Get packages of the active or embedded Python environment
try:
if python_exe:
return [(r.decode().split('==')[0] if not versions else r.decode()) for r in
subprocess.check_output([python_exe, '-m', 'pip', 'freeze']).split()]
else:
return [(r.split('==')[0] if not versions else r) for r in subprocess.getoutput('pip freeze').splitlines()]
except subprocess.CalledProcessError as e:
raise e # re-raise the error to handle it outside
def print_general_error_message():
print(
f"\r{message('Efficiency Nodes:')} An unexpected error occurred during the package installation process. {error('Failed!')}")
print(warning("Please try manually installing the required packages from the requirements.txt file."))
# Install missing packages
install_packages(my_dir)