Update deps

This commit is contained in:
Sangye Ince-Johannsen 2025-01-17 22:48:05 +00:00
parent f7fe2ae2a5
commit a69d736903

87
deps
View file

@ -78,24 +78,43 @@ def in_conda_env():
""" """
return "CONDA_DEFAULT_ENV" in os.environ return "CONDA_DEFAULT_ENV" in os.environ
# We'll detect once at runtime (if in a conda env and skip_conda=False):
# we either pick 'mamba' if available, else 'conda' if available, else None
PREFERRED_CONDA_TOOL = None
def detect_conda_tool(skip_conda=False):
"""
Decide which tool to use for conda-based installation:
1) If skip_conda is True or not in a conda env -> return None
2) If mamba is installed, return 'mamba'
3) Else if conda is installed, return 'conda'
4) Else return None
"""
if skip_conda or not in_conda_env():
return None
if which("mamba"):
return "mamba"
elif which("conda"):
return "conda"
return None
def is_package_installed(package, skip_conda=False): def is_package_installed(package, skip_conda=False):
""" """
Checks if 'package' is installed in either mamba/conda (if in conda env and skip_conda=False) or pip. Checks if 'package' is installed with the chosen conda tool or pip.
""" """
if not skip_conda and in_conda_env(): conda_tool = detect_conda_tool(skip_conda)
# Try mamba or conda if conda_tool == "mamba":
if which("mamba"): returncode, stdout, _ = run_command(["mamba", "list"])
returncode, stdout, _ = run_command(["mamba", "list"]) if returncode == 0:
if returncode == 0: pattern = rf"^{re.escape(package)}\s"
pattern = rf"^{re.escape(package)}\s" if re.search(pattern, stdout, re.MULTILINE):
if re.search(pattern, stdout, re.MULTILINE): return True
return True elif conda_tool == "conda":
elif which("conda"): returncode, stdout, _ = run_command(["conda", "list"])
returncode, stdout, _ = run_command(["conda", "list"]) if returncode == 0:
if returncode == 0: pattern = rf"^{re.escape(package)}\s"
pattern = rf"^{re.escape(package)}\s" if re.search(pattern, stdout, re.MULTILINE):
if re.search(pattern, stdout, re.MULTILINE): return True
return True
# Fall back to pip # Fall back to pip
returncode, stdout, _ = run_command(["pip", "list"]) returncode, stdout, _ = run_command(["pip", "list"])
@ -105,31 +124,31 @@ def is_package_installed(package, skip_conda=False):
def install_package(package, skip_conda=False): def install_package(package, skip_conda=False):
""" """
Installs 'package'. Installs 'package'.
1) If skip_conda=False and in conda env -> try mamba or conda 1) Decide once if we can use 'mamba' or 'conda' (if skip_conda=False and in conda env).
2) Fallback to pip 2) Try that conda tool for installation
3) If that fails or not found, fallback to pip
""" """
if is_package_installed(package, skip_conda=skip_conda): if is_package_installed(package, skip_conda=skip_conda):
print(f"Package '{package}' is already installed.") print(f"Package '{package}' is already installed.")
return return
if not skip_conda and in_conda_env(): conda_tool = detect_conda_tool(skip_conda)
# check mamba
if which("mamba"):
print(f"Installing '{package}' with mamba...")
returncode, _, _ = run_command(["mamba", "install", "-y", "-c", "conda-forge", package])
if returncode == 0:
print(f"Successfully installed '{package}' via mamba.")
return
print(f"mamba failed for '{package}', falling back to conda/pip...")
# check conda if conda_tool == "mamba":
if which("conda"): print(f"Installing '{package}' with mamba...")
print(f"Installing '{package}' with conda...") returncode, _, _ = run_command(["mamba", "install", "-y", "-c", "conda-forge", package])
returncode, _, _ = run_command(["conda", "install", "-y", "-c", "conda-forge", package]) if returncode == 0:
if returncode == 0: print(f"Successfully installed '{package}' via mamba.")
print(f"Successfully installed '{package}' via conda.") return
return print(f"mamba failed for '{package}'. Falling back to pip...")
print(f"conda failed for '{package}', falling back to pip...")
elif conda_tool == "conda":
print(f"Installing '{package}' with conda...")
returncode, _, _ = run_command(["conda", "install", "-y", "-c", "conda-forge", package])
if returncode == 0:
print(f"Successfully installed '{package}' via conda.")
return
print(f"conda failed for '{package}'. Falling back to pip...")
# fallback: pip # fallback: pip
print(f"Installing '{package}' with pip...") print(f"Installing '{package}' with pip...")