Find the path to the given executable Parameters ---------- executable: string Value should be: conda, mamba or micromamba
(command)
| 34 | |
| 35 | |
| 36 | def _get_conda_like_executable(command): |
| 37 | """Find the path to the given executable |
| 38 | |
| 39 | Parameters |
| 40 | ---------- |
| 41 | |
| 42 | executable: string |
| 43 | Value should be: conda, mamba or micromamba |
| 44 | """ |
| 45 | # Check for a environment variable bound to the base executable, both conda and mamba |
| 46 | # set these when activating an environment. |
| 47 | base_executable = "CONDA_EXE" |
| 48 | if "mamba" in command.lower(): |
| 49 | base_executable = "MAMBA_EXE" |
| 50 | if base_executable in os.environ: |
| 51 | executable = Path(os.environ[base_executable]) |
| 52 | if executable.is_file(): |
| 53 | return str(executable.resolve()) |
| 54 | |
| 55 | # Check if there is a conda executable in the same directory as the Python executable. |
| 56 | # This is the case within conda's root environment. |
| 57 | executable = Path(sys.executable).parent / command |
| 58 | if executable.is_file(): |
| 59 | return str(executable) |
| 60 | |
| 61 | # Otherwise, attempt to extract the executable from conda history. |
| 62 | # This applies in any conda environment. Parsing this way is error prone because |
| 63 | # different versions of conda and mamba include differing cmd values such as |
| 64 | # `conda`, `conda-script.py`, or `path/to/conda`, here use the raw command provided. |
| 65 | history = Path(sys.prefix, "conda-meta", "history").read_text(encoding="utf-8") |
| 66 | match = re.search( |
| 67 | rf"^#\s*cmd:\s*(?P<command>.*{command})\s[create|install]", |
| 68 | history, |
| 69 | flags=re.MULTILINE, |
| 70 | ) |
| 71 | if match: |
| 72 | return match.groupdict()["command"] |
| 73 | |
| 74 | # Fallback: assume the executable is available on the system path. |
| 75 | return command |
| 76 | |
| 77 | |
| 78 | CONDA_COMMANDS_REQUIRING_PREFIX = { |
no test coverage detected
searching dependent graphs…