Skip to content

Commit

Permalink
Merge branch '90-add_fun_install_ogs_commit_reorganize' into 'master'
Browse files Browse the repository at this point in the history
Resolve "Add function to install ogs and python dependencies"

Closes #90

See merge request ogs/tools/r2ogs6!64
  • Loading branch information
joboog committed May 3, 2024
2 parents 78fe5a3 + 91b41d7 commit a9ec189
Show file tree
Hide file tree
Showing 17 changed files with 1,011 additions and 698 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
.Rhistory
.RData
.Ruserdata
.Renviron
.vscode
config.yml
ci/local_docker_test
inst/doc
inst/extdata/test_tempdirs
Meta
renv/settings.json
r2ogs6.Rcheck
tmp
/doc/
/Meta/
66 changes: 52 additions & 14 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,46 @@

stages:
- test
- production

variables:
# RETICULATE_MINICONDA_PATH: "/root/.local/share/r-miniconda/"
# RETICULATE_PYTHON_ENV: "/root/.local/share/r-miniconda/envs/r-reticulate/"
RENV_CONFIG_EXTERNAL_LIBRARIES: "/usr/local/lib/R/library"
RENV_PATHS_CACHE: "/root/.cache/R/renv/cache" #"$CI_PROJECT_DIR/cache"
RENV_PATHS_LIBRARY: "/root/.cache/R/renv/library" #"$CI_PROJECT_DIR/renv/library"
RUN_PYTHON_CONFIG_TESTS: "false"
R_LIBS: "/usr/local/lib/R/site-library:/usr/local/lib/R/library:$RENV_PATHS_LIBRARY"

.before_script_templ: &before_script_templ
- apt-get update && apt-get upgrade -y
- apt-get install -y git
# Packages to build Python and R packages
- |
apt-get install -y build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev curl libncursesw5-dev xz-utils tk-dev \
libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev \
libharfbuzz-dev libfribidi-dev libcurl4-openssl-dev \
libfreetype6-dev libpng-dev libtiff5-dev libjpeg-dev
- apt-get install -y python3.10-venv python3-pip

test:
image: $CI_REGISTRY_IMAGE:24032023
image: rocker/r-ver:4.2.3
stage: test
tags:
- envinf2
variables:
RETICULATE_MINICONDA_PATH: "/root/.local/share/r-miniconda/"
RETICULATE_PYTHON_ENV: "/root/.local/share/r-miniconda/envs/r-reticulate/"
RENV_CONFIG_EXTERNAL_LIBRARIES: "/usr/local/lib/R/library"
RENV_PATHS_CACHE: "$CI_PROJECT_DIR/cache"
RENV_PATHS_LIBRARY: "$CI_PROJECT_DIR/renv/library"
- docker
before_script:
- R -e "if (!requireNamespace('renv', quietly = TRUE)) install.packages('renv')"
#- R -e 'renv::restore()' does fail while loading Bioconductor packages
- R -e 'renv::install()'
- R -e 'renv::install("bioc::rhdf5")'
- *before_script_templ
- R -e "install.packages('renv', repos = c(CRAN = 'https://cloud.r-project.org'))"
- R -e "renv::restore()"
#- R -e 'renv::status()'
# - R -e 'renv::install()'
# - R -e 'renv::install("bioc::rhdf5")'
script:
- R CMD build . --no-manual
- R CMD check $(ls -1t *.tar.gz | head -n 1) --no-manual
- R -e "devtools::test()"
- R -e "devtools::check(vignettes=FALSE)"
# - R CMD build . --no-manual --no-build-vignettes
# - R CMD check $(ls -1t *.tar.gz | head -n 1) --no-manual --no-build-vignettes
after_script:
- R -e 'covr::package_coverage(type = c("tests", "examples"), quiet = F)'
coverage: '/Coverage: \d+\.\d+\%/'
Expand All @@ -33,6 +53,24 @@ test:
paths:
- "$RENV_PATHS_CACHE"
- "$RENV_PATHS_LIBRARY"
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" || $CI_PIPELINE_SOURCE == "web"'
- if: '$CI_COMMIT_BRANCH == "master"'

test_install:
image: rocker/r-ver:4.2.3
stage: production
tags:
- docker
before_script:
- *before_script_templ
- mkdir ../test_r2ogs6 && cd ../test_r2ogs6 && ls . -ahl
script:
- R -e 'install.packages(c("remotes", "BiocManager"))'
- R -e "remotes::install_local(path=\"$CI_PROJECT_DIR\", dependencies='Imports', repos=BiocManager::repositories())"
- R -e 'reticulate::virtualenv_create(envname="r2ogs6", python="/usr/bin/python3")'
- R -e 'r2ogs6::install_ogs(ogs_version = "6.4.4", envname = "r2ogs6")'
- R -e 'reticulate::py_module_available("ogs")'
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" || $CI_PIPELINE_SOURCE == "web"'
- if: '$CI_COMMIT_BRANCH == "master"'
10 changes: 5 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ Suggests:
knitr,
devtools,
ggplot2,
BiocManager,
rhdf5,
rmarkdown
Imports:
reticulate,
xml2,
tibble,
R6,
Expand All @@ -45,10 +44,11 @@ Imports:
utils,
dplyr,
doParallel,
reticulate,
config,
foreach
RoxygenNote: 7.2.3
foreach,
BiocManager,
rhdf5
RoxygenNote: 7.3.1
VignetteBuilder: knitr
Depends:
R (>= 2.10)
5 changes: 4 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export(OGS6_h5)
export(OGS6_msh)
export(OGS6_pvd)
export(OGS6_vtu)
export(install_ogs)
export(ogs6_call_ogs6)
export(ogs6_download_ogs6)
export(ogs6_export_sim_files)
export(ogs6_generate_benchmark_script)
export(ogs6_generate_benchmark_scripts)
Expand Down Expand Up @@ -71,6 +71,7 @@ export(prj_time_stepping)
export(prj_tl_process)
export(prj_vtkdiff)
export(read_in_prj)
export(set_ogs6_bin_path)
importFrom(R6,R6Class)
importFrom(crayon,green)
importFrom(crayon,red)
Expand All @@ -79,3 +80,5 @@ importFrom(doParallel,registerDoParallel)
importFrom(dplyr,bind_cols)
importFrom(dplyr,bind_rows)
importFrom(foreach,"%dopar%")
importFrom(rhdf5,h5ls)
importFrom(rhdf5,h5read)
97 changes: 97 additions & 0 deletions R/config_ogs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Function based on:
# https://rstudio.github.io/reticulate/articles/python_dependencies.html

#' Install specific version of OpenGeoSys 6 (OGS) along with necessary Python
#' packages `vtk` and `numpy` into a specified Python virtual environment.
#' Note, this does not work with `conda` environments.
#'
#' @param ogs_version The version of OGS to install (default: 6.4.4).
#' @param envname The name of the Python virtual environment (default: r2ogs6).
#' @param ... Rest of the arguments passed to "py_install()"
#' @return None.
#'
#' @export
install_ogs <-
function(...,
ogs_version = "6.4.4",
envname = "r2ogs6"
) {

if (!(reticulate::virtualenv_exists(envname))){
stop(
paste0('The Python virtual environment "', envname,
'" does not exist. You can create it with ',
'reticulate::virtualenv_create("r2ogs6").\n',
'Make sure that this environment is used as the actual ',
'Python configuration; see reticulate::py_config().'
)
)
}

pkgs <- c(paste0("ogs==", ogs_version), "numpy", "vtk")

reticulate::py_install(
packages = pkgs,
envname = envname,
...
)
}


#' Set default OpenGeoSys 6 binary path
#'
#' This function automatically detects and temporarily sets the path to the
#' OpenGeoSys (OGS) binary within the active Python virtual environment. It is
#' typically called after installing OGS using the `install_ogs()` function.
#' For a permanent setting of the OGS binary path, consider to define
#' `r2ogs6.default_ogs6_bin_path` in a `config.yml` file.'
#'
#' @return None, but it sets an option `r2ogs6.default_ogs6_bin_path` to point
#' to the detected OGS binary path.
#'
#' @export
set_ogs6_bin_path <-
function() {
cfg <- reticulate::py_config()

has_python <- reticulate::py_available(initialize = FALSE)
has_numpy <- reticulate::py_numpy_available(initialize = FALSE)
has_vtk <- reticulate::py_module_available("vtk")
has_ogs <- reticulate::py_module_available("ogs")
assertthat::assert_that(
isTRUE(has_python), isTRUE(has_numpy), isTRUE(has_vtk),
isTRUE(has_ogs)
)

# Search for ogs binary in potential paths
if (Sys.info()["sysname"] == "Windows") {
bin_dir <- "Scripts"
ogs_name <- "ogs.exe"
} else {
bin_dir <- "bin"
ogs_name <- "ogs"
}
virtualenv_path <- win_to_linux_path(cfg$virtualenv)
ogs_bin_dir <- file.path(virtualenv_path, bin_dir)
files <- list.files(ogs_bin_dir, full.names = TRUE)
ogs_bin_path <- files[grepl(paste0(ogs_name, "$"), basename(files))]

assertthat::assert_that(
file.exists(ogs_bin_path),
msg = "OGS executable not found."
)
assertthat::assert_that(
basename(ogs_bin_path) %in% c("ogs.exe", "ogs"),
msg = "OGS executable not found."
)
options("r2ogs6.default_ogs6_bin_path" = ogs_bin_path)

message(
paste0(
'The option "r2ogs6.default_ogs6_bin_path" was temporarily ',
'set to "', ogs_bin_path, '" .\n',
'For a permanent setting of "r2ogs6.default_ogs6_bin_path=',
ogs_bin_path, ' define it in a "config.yml" file.'
)
)
}
93 changes: 0 additions & 93 deletions R/download_ogs6.R

This file was deleted.

1 change: 1 addition & 0 deletions R/ogs6_h5.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#' @description Small class to wrap \code{h5} data into the \code{r2ogs6} workflow.
#' @export
#' @importFrom R6 R6Class
#' @importFrom rhdf5 h5ls h5read
OGS6_h5 <- R6::R6Class("OGS6_h5",
public = list(
#' @description This function will be called automatically after a
Expand Down
Loading

0 comments on commit a9ec189

Please sign in to comment.