Skip to content

BUG make sure to exclude stuff in conda envs #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 51 additions & 18 deletions conda_forge_conda_plugins/hooks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
import logging
import os
import shutil
import subprocess

from conda.base.context import context
from conda.core.envs_manager import list_all_known_prefixes
from conda.plugins import CondaVirtualPackage, hookimpl

logger = logging.getLogger(__name__)


def _real_abs_path(path):
if path is None:
return None
return os.path.realpath(os.path.abspath(path))


def _find_command_not_in_a_conda_prefix(cmd):
pth = _real_abs_path(shutil.which(cmd))

if (
pth is None
or pth.startswith(_real_abs_path(context.root_prefix))
or pth.startswith(
tuple(_real_abs_path(_pth) for _pth in list_all_known_prefixes())
)
):
return None

return pth


@hookimpl
def conda_virtual_packages():
Expand All @@ -12,15 +39,18 @@ def conda_virtual_packages():
openmpi_version = os.environ["CONDA_OVERRIDE_OPENMPI"]
else:
try:
ret = subprocess.run(
["ompi_info", "--parsable"], capture_output=True, text=True
)
if ret.returncode == 0:
for line in ret.stdout.splitlines():
if line.startswith("ompi:version:full:"):
openmpi_version = line.strip().split(":")[3].strip()
break
except Exception:
ompi_info_pth = _find_command_not_in_a_conda_prefix("ompi_info")
if ompi_info_pth is not None:
ret = subprocess.run(
[ompi_info_pth, "--parsable"], capture_output=True, text=True
)
if ret.returncode == 0:
for line in ret.stdout.splitlines():
if line.startswith("ompi:version:full:"):
openmpi_version = line.strip().split(":")[3].strip()
break
except Exception as e:
logger.error(f"Error while trying to find openmpi version: {e}", exc_info=e)
pass

if openmpi_version:
Expand All @@ -36,15 +66,18 @@ def conda_virtual_packages():
mpich_version = os.environ["CONDA_OVERRIDE_MPICH"]
else:
try:
ret = subprocess.run(
["mpichversion", "--version"], capture_output=True, text=True
)
if ret.returncode == 0:
for line in ret.stdout.splitlines():
if line.startswith("MPICH Version:"):
mpich_version = line.strip().split(":")[1].strip()
break
except Exception:
mpichversion_pth = _find_command_not_in_a_conda_prefix("mpichversion")
if mpichversion_pth is not None:
ret = subprocess.run(
[mpichversion_pth, "--version"], capture_output=True, text=True
)
if ret.returncode == 0:
for line in ret.stdout.splitlines():
if line.startswith("MPICH Version:"):
mpich_version = line.strip().split(":")[1].strip()
break
except Exception as e:
logger.error(f"Error while trying to find openmpi version: {e}", exc_info=e)
pass

if mpich_version:
Expand Down