-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add support for conda environments #35
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,96 @@ | ||
| # Globals ----------------------------------------------------------------- | ||
| acro_venv <- "r-acro" | ||
| acro_pkg <- "acro==0.4.11" | ||
| ch <- "conda-forge" | ||
|
|
||
|
|
||
| # Internal helper: resolve Python executable | ||
| ## nocov start | ||
| get_python <- function() { | ||
| python <- Sys.which("python3") | ||
| if (python == "") { | ||
| python <- Sys.which("python") | ||
| if (python == "") { | ||
| stop("Python not found in PATH. Please ensure Python is installed.") | ||
| } | ||
| } | ||
| return(python) | ||
| } | ||
| ## nocov end | ||
|
|
||
|
|
||
| # Internal helper: install ACRO in a Conda environment | ||
| ## nocov start | ||
| install_conda <- function(envname) { # nocov | ||
| if (!reticulate::condaenv_exists(envname = envname, conda = "auto")) { | ||
| reticulate::conda_create(envname = envname, python_version = "3.12", channel = ch) | ||
| reticulate::conda_install(envname = envname, packages = acro_pkg, channel = ch) | ||
| } | ||
| } | ||
| ## nocov end | ||
|
|
||
|
|
||
| # Internal helper: install ACRO in a Python virtual environment | ||
| install_venv <- function(envname = acro_venv) { | ||
| if (!reticulate::virtualenv_exists(envname)) { | ||
| python <- get_python() | ||
|
|
||
| reticulate::virtualenv_create( | ||
| envname = envname, | ||
| python = python, | ||
| force = TRUE, | ||
| packages = NULL | ||
| ) | ||
|
|
||
| reticulate::py_install(acro_pkg, envname = envname) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| # Internal helper: resolve whether Conda should be used | ||
| get_use_conda <- function(use_conda = NULL) { | ||
| if (is.null(use_conda)) { | ||
| use_conda <- tolower(Sys.getenv("ACRO_USE_CONDA")) %in% c("1", "true", "yes") | ||
| } | ||
| use_conda <- isTRUE(use_conda) # default FALSE | ||
|
|
||
| if (use_conda && is.null(reticulate::conda_binary())) { # nocov | ||
| stop("Conda requested but no conda installation found", call. = FALSE) # nocov | ||
| } | ||
|
|
||
| return(use_conda) | ||
| } | ||
|
|
||
|
|
||
| #' Initialise an ACRO object | ||
| #' | ||
| #' @param config Name of a yaml configuration file with safe parameters. | ||
| #' @param suppress Whether to automatically apply suppression. | ||
| #' @param envname Name of the Python environment to use. | ||
| #' @param use_conda Whether to use a Conda environment. | ||
| #' If `NULL`, looks for environment variable `ACRO_USE_CONDA`, | ||
| #' defaults to `FALSE` if unset. | ||
| #' | ||
| #' @return No return value, called for side effects | ||
| #' @return Invisibly returns the ACRO object, which is used internally. | ||
| #' @export | ||
| acro_init <- function(config = "default", suppress = FALSE, envname = acro_venv, use_conda = NULL) { | ||
| # define the environment | ||
| use_conda <- get_use_conda(use_conda) | ||
|
|
||
| acro_init <- function(suppress = FALSE) { | ||
| create_virtualenv() | ||
| # initialise the environment | ||
| if (!reticulate::py_available(initialize = FALSE)) { | ||
| if (use_conda) { # nocov | ||
| install_conda(envname) # nocov | ||
| reticulate::use_condaenv(envname, required = TRUE) # nocov | ||
| } else { | ||
| install_venv(envname) | ||
| reticulate::use_virtualenv(envname, required = TRUE) | ||
| } | ||
| } | ||
|
|
||
| # import the acro package and instantiate an object | ||
| acro <- reticulate::import("acro", delay_load = TRUE) | ||
| acroEnv$ac <- acro$ACRO(suppress = suppress) | ||
| acroEnv$ac <- acro$ACRO(config = config, suppress = suppress) | ||
|
|
||
| invisible(acroEnv$ac) | ||
| } |
This file was deleted.
rpreen marked this conversation as resolved.
Show resolved
Hide resolved
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
This file was deleted.
This file was deleted.
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we have a test for install_conda ?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's time consuming to create a single runner that will test both Conda and venv just so the code coverage can be contained. The CI has dedicated runners for Conda now which execute the tests in that environment (except for one specific test that tests installation in a venv that I kept because it was originally there --- I wonder if it's even necessary since if that fails then none of the tests would pass.)
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds good. |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.