diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/core/conda.c | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/src/lib/core/conda.c b/src/lib/core/conda.c index fa3052a..435af35 100644 --- a/src/lib/core/conda.c +++ b/src/lib/core/conda.c @@ -207,6 +207,24 @@ int conda_exec(const char *args) { return system(command); } +static int conda_prepend_bin(const char *root) { + const char *system_path_old = getenv("PATH"); + char conda_bin[PATH_MAX] = {0}; + + snprintf(conda_bin, sizeof(conda_bin) - 1, "%s/bin:%s/condabin", root, root); + + if (!strstr(system_path_old, conda_bin)) { + // conda_bin is not present in PATH. Add it to the head. + char system_path_new[STASIS_BUFSIZ]; + sprintf(system_path_new, "%s:%s", conda_bin, system_path_old); + if (setenv("PATH", system_path_new, 1) < 0) { + SYSERROR("Unable to prepend to PATH: %s", conda_bin); + return -1; + } + } + return 0; +} + int conda_activate(const char *root, const char *env_name) { FILE *fp = NULL; const char *init_script_conda = "/etc/profile.d/conda.sh"; @@ -248,9 +266,24 @@ int conda_activate(const char *root, const char *env_name) { return -1; } + if (conda_prepend_bin(root)) { + remove(logfile); + return -1; + } + // Fully activate conda and record its effect on the runtime environment char command[PATH_MAX * 3]; - snprintf(command, sizeof(command) - 1, "set -a; source %s; source %s; conda activate %s &>/dev/null; env -0", path_conda, path_mamba, env_name); + const char *conda_shlvl = getenv("CONDA_SHLVL"); + if (conda_shlvl == NULL || strcmp(conda_shlvl, "0") == 0) { + // First-run initialization + snprintf(command, sizeof(command) - 1, "source %s; source %s; conda activate %s &>/dev/null; env -0", path_conda, path_mamba, env_name); + } else { + // Conda is already available and configured. + // Make calls directly to conda using conda's base interpreter. + // The shell functions generated by sourcing path_conda and path_mamba are extremely inconsistent + // in this environment. DO NOT USE THEM. + snprintf(command, sizeof(command) - 1, "$CONDA_PYTHON_EXE $CONDA_EXE activate %s &>/dev/null; env -0", env_name); + } int retval = shell(&proc, command); if (retval) { // it didn't work; drop out for cleanup |