Skip to content

Commit

Permalink
Make createCondaEnv function + remove internal conda env creation
Browse files Browse the repository at this point in the history
  • Loading branch information
katieb1 committed Apr 16, 2024
1 parent ed607cc commit e135818
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 187 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export(chart)
export(chartId)
export(command)
export(condaFilepath)
export(createCondaEnv)
export(datasheet)
export(datasheetSpatRaster)
export(dateModified)
Expand Down
82 changes: 0 additions & 82 deletions R/createCondaEnv - bad.R

This file was deleted.

82 changes: 42 additions & 40 deletions R/createCondaEnv.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,64 @@
#' @include AAAClassDefinitions.R
NULL

#' Installs Miniconda
#' Create SyncroSim package conda environments
#'
#' This function installs Miniconda to the default installation path
#' within the SyncroSim installation folder. If you already have Conda
#' installed in the non-default location, you can point SyncroSim towards
#' that installation using the \code{\link{condaFilepath}} function.
#' Creates the conda environment for the specified SyncroSim package(s).
#'
#' @param session \code{\link{Session}} object. If \code{NULL} (default),
#' \code{session()} will be used
#'
#' @param pkgs character or list of characters.
#' @param session \code{\link{Session}} object or character (i.e. filepath to a
#' session). If \code{NULL}, \code{session()} will be used
#'
#' @return
#' Invisibly returns \code{TRUE} upon success (i.e.successful
#' install) and \code{FALSE} upon failure.
#' Invisibly returns \code{TRUE} upon success (i.e.successful creation of the
#' conda environment(s)) or \code{FALSE} upon failure.
#'
#' @examples
#' @examples
#' \dontrun{
#' # Install Conda for the default SyncroSim session
#' installConda()
#' # Set up a SyncroSim Session
#' mySession <- session()
#'
#' # Create the conda environment for helloworldConda package
#' condaFilepath(pkgs = "helloworldConda", mySession)
#' }
#'
#' @export
setGeneric("installConda", function(session) standardGeneric("installConda"))
setGeneric("createCondaEnv", function(pkgs, session = NULL) standardGeneric("createCondaEnv"))

#' @rdname installConda
setMethod("installConda", signature(session = "character"), function(session) {
#' @rdname createCondaEnv
setMethod("createCondaEnv", signature(session = "character"), function(pkgs, session) {
return(SyncroSimNotFound(session))
})

#' @rdname installConda
setMethod("installConda", signature(session = "missingOrNULL"), function(session) {
#' @rdname createCondaEnv
setMethod("createCondaEnv", signature(session = "missingOrNULL"), function(pkgs, session) {
session <- .session()
return(installConda(session))
return(createCondaEnv(session))
})

#' @rdname installConda
setMethod("installConda", signature(session = "Session"), function(session) {
#' @rdname createCondaEnv
setMethod("createCondaEnv", signature(session = "Session"), function(pkgs, session) {

success <- FALSE
message("Setting Conda filepath to the default installation.")
args <- list(conda = NULL, install = NULL)

message("Running Conda Installer. Please wait...")
if (is.null(session)){
session <- .session()
}
tt <- command(args, session)

if (tt[1] == "Conda already installed at that location"){
success <- FALSE
tt <- "Conda already installed"
} else if (tt[3] == "Saved") {
success <- TRUE
tt <- paste0("Miniconda successfully installed")
}
message("Creating Conda environments. Please wait...")

message(tt)
# Check if environment needs to be created, create if doesn't exist yet
for (package in pkgs) {
tt <- command(list(conda = NULL, createenv = NULL, pkg = package), session)
if (length(tt) > 1){
if (!grepl("Creating Conda environments", tt[1], fixed = TRUE)){
stop(tt[1])
}
} else {
if (grepl("No Conda installation found", tt, fixed = TRUE)) {
errorMessage = "Conda must be installed to use Conda environments. See ?installConda for details."
} else {
errorMessage = tt
}

message(errorMessage)
return(invisible(TRUE))
}
}

return(invisible(success))
return(invisible(TRUE))
})
34 changes: 0 additions & 34 deletions R/internalHelpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -408,40 +408,6 @@ getProjectSet <- function(ssimObject) {
return(projectSet)
}

# Create package Conda environments
createCondaEnv <- function(libPath, currentPackages, session) {

message("Creating Conda environments. Please wait...")

# Check if environment needs to be created, create if doesn't exist yet
for (package in currentPackages) {
tt <- command(list(conda = NULL, createenv = NULL, pkg = package), session)
if (length(tt) > 1){
if (!grepl("Creating Conda environments", tt[1], fixed = TRUE)){
stop(tt[1])
}
} else {
if (grepl("No Conda installation found", tt, fixed = TRUE)) {
errorMessage = "Conda must be installed to use Conda environments. See ?installConda for details."
tt <- command(list(setprop = NULL,
lib = libPath,
useconda = "no"), session)
} else {
errorMessage = tt
}

message(errorMessage)
return(TRUE)
}
}

tt <- command(list(setprop = NULL,
lib = libPath,
useconda = "yes"), session)

return(TRUE)
}

# make first character of string lower case
camel <- function(x) {
substr(x, 1, 1) <- tolower(substr(x, 1, 1))
Expand Down
4 changes: 0 additions & 4 deletions R/ssimLibrary.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,6 @@ setMethod(
tt <- command(list(setprop = NULL, lib = path, useconda = "no"), session)
} else {
tt <- command(list(setprop = NULL, lib = path, useconda = "yes"), session)
if (useConda == TRUE){
currentPackages <- unique(datasheets$package)
}
createCondaEnv(path, currentPackages, session)
}
}

Expand Down
10 changes: 5 additions & 5 deletions R/useConda.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ NULL

#' Conda configuration of a SsimLibrary
#'
#' Retrieves or sets the Conda configuration of a \code{\link{SsimLibrary}}.
#' Retrieves or sets the Conda configuration of a \code{\link{SsimLibrary}}. Note
#' that in order to use conda environments, you will first need to ensure that
#' the conda environment has been created for a given package. You can create
#' the conda environment for a package using the \code{\link{createCondaEnv}}
#' function.
#'
#' @param ssimObject \code{\link{SsimLibrary}} object
#' @param value logical for whether to use Conda
Expand Down Expand Up @@ -89,12 +93,8 @@ setReplaceMethod(

tt <- command(list(setprop = NULL, lib = .filepath(ssimObject),
useconda = "yes"), .session(ssimObject))

currentPackages <- packages(ssimObject)$name
}

createCondaEnv(.filepath(ssimObject), currentPackages, .session(ssimObject))

return(ssimObject)
}
)
40 changes: 40 additions & 0 deletions man/createCondaEnv.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions man/scenario.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions man/ssimLibrary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion man/useConda.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e135818

Please sign in to comment.