| 1 |
|
| 2 |
# given the path to a Python installation managed by conda, attempt to |
| 3 |
# find the conda installation + executable used to create it |
| 4 |
renv_conda_find <- function(python) { |
| 5 |
|
| 6 |
tryCatch( |
| 7 |
renv_conda_find_impl(python), |
| 8 |
error = function(e) { |
| 9 |
warning(e) |
| 10 |
"" |
| 11 |
} |
| 12 |
) |
| 13 |
|
| 14 |
} |
| 15 |
|
| 16 |
renv_conda_find_impl <- function(python) { |
| 17 |
|
| 18 |
# read the conda environment's history to try to find conda |
| 19 |
base <- dirname(python) |
| 20 |
if (!renv_platform_windows()) |
| 21 |
base <- dirname(base) |
| 22 |
|
| 23 |
history <- file.path(base, "conda-meta/history") |
| 24 |
if (!file.exists(history)) |
| 25 |
return("") |
| 26 |
|
| 27 |
contents <- readLines(history, n = 2L, warn = FALSE) |
| 28 |
if (length(contents) < 2) |
| 29 |
return("") |
| 30 |
|
| 31 |
line <- substring(contents[2L], 8L) |
| 32 |
index <- regexpr(" ", line, fixed = TRUE) |
| 33 |
if (index == -1L) |
| 34 |
return("") |
| 35 |
|
| 36 |
conda <- substring(line, 1L, index - 1L) |
| 37 |
if (renv_platform_windows()) |
| 38 |
conda <- file.path(dirname(conda), "conda.exe") |
| 39 |
|
| 40 |
# prefer condabin if it exists |
| 41 |
condabin <- file.path(dirname(conda), "../condabin", basename(conda)) |
| 42 |
if (file.exists(condabin)) |
| 43 |
conda <- condabin |
| 44 |
|
| 45 |
# bail if conda wasn't found |
| 46 |
if (!file.exists(conda)) |
| 47 |
return("") |
| 48 |
|
| 49 |
renv_path_canonicalize(conda) |
| 50 |
|
| 51 |
} |