Skip to content

Commit

Permalink
Combine functions for finding conda executable in conda.R
Browse files Browse the repository at this point in the history
  • Loading branch information
tl-hbk committed Sep 4, 2024
1 parent 27d6e73 commit 14d7327
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 54 deletions.
54 changes: 51 additions & 3 deletions R/conda.R
Original file line number Diff line number Diff line change
Expand Up @@ -1106,9 +1106,26 @@ get_python_conda_info <- function(python) {
else
file.path(root, "bin/conda")
} else {
# not base env, parse conda-meta history to find the conda binary
# that created it
conda <- python_info_condaenv_find(root)
# not base env
conda <- find_conda()
if (is.null(conda)) {
if (is_windows()) {
# in Anaconda base env, conda.exe lives under Scripts
exe <- file.path(root, "Scripts", "conda.exe")
if (file.exists(exe))
conda <- exe

# in ArcGIS env, conda.exe lives in a parent directory
exe <- file.path(root, "../..", "Scripts", "conda.exe")
exe <- normalizePath(exe, winslash = "/", mustWork = FALSE)
if (file.exists(exe))
conda <- exe
}
if (is.null(conda)) {
# parse conda-meta history to find the conda binary that created it
conda <- conda_history_find(root)
}
}
}

conda <- normalizePath(conda, winslash = "/", mustWork = FALSE)
Expand All @@ -1123,6 +1140,37 @@ get_python_conda_info <- function(python) {
}


conda_history_find <- function(path) {
exe <- if (is_windows()) "conda.exe" else "conda"

# read history file
histpath <- file.path(path, "conda-meta/history")
if (!file.exists(histpath))
return(NULL)

history <- readLines(histpath, warn = FALSE)

# look for cmd line
pattern <- "^[[:space:]]*#[[:space:]]*cmd:[[:space:]]*"
lines <- grep(pattern, history, value = TRUE)
if (length(lines) == 0)
return(NULL)

# get path to conda script used
script <- sub(
"^#\\s+cmd: (.+?)\\s+(env\\s+create|create|rename)\\s+.*",
"\\1", lines[[1]], perl = TRUE
)
# on Windows, a wrapper script is recorded in the history,
# so instead attempt to find the real conda binary
if(is_windows())
conda <- file.path(dirname(script), exe)
else
conda <- script
normalizePath(conda, winslash = "/", mustWork = FALSE)

}

conda_prefix <- function(conda = "auto") {
conda <- normalizePath(conda_binary(conda),
winslash = "/", mustWork = TRUE)
Expand Down
52 changes: 1 addition & 51 deletions R/python-tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ python_info_condaenv <- function(path) {
python <- file.path(path, suffix)

# find path to conda associated with this env
conda <- python_info_condaenv_find(path)
conda <- get_python_conda_info(python)$conda

list(
python = python,
Expand All @@ -155,56 +155,6 @@ python_info_condaenv <- function(path) {

}

python_info_condaenv_find <- function(path) {

# first, check if we have a condabin
exe <- if (is_windows()) "conda.exe" else "conda"
conda <- file.path(path, "condabin", exe)
if (file.exists(conda))
return(conda)

if (is_windows()) {
# in Anaconda base env, conda.exe lives under Scripts
conda <- file.path(path, "Scripts", exe)
if (file.exists(conda))
return(conda)

# in ArcGIS env, conda.exe lives in a parent directory
conda <- file.path(path, "../..", "Scripts", exe)
conda <- normalizePath(conda, winslash = "/", mustWork = FALSE)
if (file.exists(conda))
return(conda)

}

# read history file
histpath <- file.path(path, "conda-meta/history")
if (!file.exists(histpath))
return(NULL)

history <- readLines(histpath, warn = FALSE)

# look for cmd line
pattern <- "^[[:space:]]*#[[:space:]]*cmd:[[:space:]]*"
lines <- grep(pattern, history, value = TRUE)
if (length(lines) == 0)
return(NULL)

# get path to conda script used
script <- sub(
"^#\\s+cmd: (.+?)\\s+(env\\s+create|create|rename)\\s+.*",
"\\1", lines[[1]], perl = TRUE
)
# on Windows, a wrapper script is recorded in the history,
# so instead attempt to find the real conda binary
if(is_windows())
conda <- file.path(dirname(script), exe)
else
conda <- script
normalizePath(conda, winslash = "/", mustWork = FALSE)

}

python_info_system <- function(path, python) {

list(
Expand Down

0 comments on commit 14d7327

Please sign in to comment.