Skip to content

Commit

Permalink
Merge pull request #78 from chainsawriot/fix67
Browse files Browse the repository at this point in the history
Fix67
  • Loading branch information
chainsawriot authored Feb 24, 2023
2 parents 39eb5b7 + c200863 commit 4f748d8
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 28 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: rang
Title: Reconstructing Reproducible R Computational Environments with Ease
Version: 0.1.1
Version: 0.1.2
Authors@R:
c(person("Chung-hong", "Chan", , "chainsawtiney@gmail.com", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-6232-7530")),
Expand Down
61 changes: 38 additions & 23 deletions R/installation.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,16 @@
.group_sysreqs <- function(rang) {
must_do_cmd <- "apt-get update -qq && apt-get install -y libpcre3-dev zlib1g-dev pkg-config"
if (length(rang$sysreqs) == 0) {
must_do_cmd <- paste(must_do_cmd, "libcurl4-openssl-dev")
return(must_do_cmd)
}
if (isFALSE(.is_ppa_in_sysreqs(rang))) {
cmds <- rang$sysreqs
prefix <- ""
cmd <- .group_apt_cmds(cmds, fix_libgit2 = TRUE)
if (!grepl("libcurl4-gnutls-dev", cmd)) {
must_do_cmd <- paste(must_do_cmd, "libcurl4-openssl-dev")
}
} else {
cmds <- setdiff(rang$sysreqs, c("apt-get install -y software-properties-common", "apt-get update"))
ppa_lines <- c("apt-get install -y software-properties-common",
Expand Down Expand Up @@ -249,7 +253,7 @@
dockerfile_content[(rang_line + 1):length(dockerfile_content)])
}

.generate_pre310_dockerfile_content <- function(r_version, lib, sysreqs_cmd, cache, debian_version = "lenny") {
.generate_debian_eol_dockerfile_content<- function(r_version, lib, sysreqs_cmd, cache, debian_version = "lenny") {
dockerfile_content <- c(
paste0("FROM debian/eol:", debian_version),
"ENV TZ UTC",
Expand All @@ -268,6 +272,23 @@
return(dockerfile_content)
}

.generate_rocker_dockerfile_content <- function(r_version, lib, sysreqs_cmd, cache, image) {
dockerfile_content <- c("", "", "COPY rang.R ./rang.R", "RUN Rscript rang.R", "CMD [\"R\"]")
if (!is.na(lib)) {
dockerfile_content[4] <- paste0("RUN mkdir ", lib, " && Rscript rang.R")
}
dockerfile_content[1] <- paste0("FROM rocker/", image, ":", r_version)
dockerfile_content[2] <- paste("RUN", sysreqs_cmd)
if (image == "rstudio") {
dockerfile_content[5] <- "EXPOSE 8787"
dockerfile_content[6] <- "CMD [\"/init\"]"
}
if (isTRUE(cache)) {
dockerfile_content <- .insert_cache_dir(dockerfile_content)
}
return(dockerfile_content)
}

.generate_docker_readme <- function(output_dir,image) {
file.create(file.path(output_dir,"README"))
con <- file(file.path(output_dir,"README"), open="w")
Expand Down Expand Up @@ -341,10 +362,9 @@ export_rang <- function(rang, path, rang_as_comment = TRUE, verbose = TRUE, lib
cat(paste0("lib <- \"", as.character(lib), "\"\n"), file = con)
}
cat(paste0("cran_mirror <- \"", cran_mirror, "\"\n"), file = con)
if(!is.null(rang$bioc_version)){
if(!is.null(rang$bioc_version)) {
cat(paste0("bioc_mirror <- \"", "https://bioconductor.org/packages/",rang$bioc_version,"/", "\"\n"), file = con)
}

writeLines(readLines(system.file("footer.R", package = "rang")), con = con)
if (isTRUE(rang_as_comment)) {
.write_rang_as_comment(rang = rang, con = con, path = path, verbose = verbose,
Expand All @@ -364,6 +384,8 @@ export_rang <- function(rang, path, rang_as_comment = TRUE, verbose = TRUE, lib
#' @param image character, which versioned Rocker image to use. Can only be "r-ver", "rstudio", "tidyverse", "verse", "geospatial"
#' This applies only to R version <= 3.1
#' @param cache logical, whether to cache the packages now. Please note that the system requirements are not cached. For query with non-CRAN packages, this option is strongly recommended. For R version < 3.1, this must be TRUE if there is any non-CRAN packages.
#' @param no_rocker logical, whether to skip using Rocker images even when an appropriate version is available. Please keep this as `TRUE` unless you know what you are doing
#' @param debian_version, when Rocker images are not used, which EOL version of Debian to use. Can only be "lenny", "etch", "squeeze", "wheezy", "jessie", "stretch". Please keep this as default "lenny" unless you know what you are doing
#' @param ... arguments to be passed to `dockerize`
#' @return `output_dir`, invisibly
#' @inheritParams export_rang
Expand All @@ -384,7 +406,9 @@ export_rang <- function(rang, path, rang_as_comment = TRUE, verbose = TRUE, lib
dockerize <- function(rang, output_dir, materials_dir = NULL, image = c("r-ver", "rstudio", "tidyverse", "verse", "geospatial"),
rang_as_comment = TRUE, cache = FALSE, verbose = TRUE, lib = NA,
cran_mirror = "https://cran.r-project.org/", check_cran_mirror = TRUE,
bioc_mirror = "https://bioconductor.org/packages/") {
bioc_mirror = "https://bioconductor.org/packages/",
no_rocker = FALSE,
debian_version = c("lenny", "etch", "squeeze", "wheezy", "jessie", "stretch")) {
if (length(rang$ranglets) == 0) {
warning("Nothing to dockerize.")
return(invisible(NULL))
Expand All @@ -409,6 +433,7 @@ dockerize <- function(rang, output_dir, materials_dir = NULL, image = c("r-ver",
stop("Non-CRAN packages must be cached for this R version: ", rang$r_version, ". Please set `cache` = TRUE.", call. = FALSE)
}
image <- match.arg(image)
debian_version <- match.arg(debian_version)
sysreqs_cmd <- .group_sysreqs(rang)
if (!dir.exists(output_dir)) {
dir.create(output_dir)
Expand All @@ -420,26 +445,18 @@ dockerize <- function(rang, output_dir, materials_dir = NULL, image = c("r-ver",
if (isTRUE(cache)) {
.cache_pkgs(rang, output_dir, cran_mirror, bioc_mirror, verbose)
}
if (utils::compareVersion(rang$r_version, "3.1") == -1) {
if (utils::compareVersion(rang$r_version, "3.1") == -1 || isTRUE(no_rocker)) {
file.copy(system.file("compile_r.sh", package = "rang"), file.path(output_dir, "compile_r.sh"),
overwrite = TRUE)
dockerfile_content <- .generate_pre310_dockerfile_content(r_version = rang$r_version,
sysreqs_cmd = sysreqs_cmd, lib = lib,
cache = cache)

dockerfile_content <- .generate_debian_eol_dockerfile_content(r_version = rang$r_version,
sysreqs_cmd = sysreqs_cmd, lib = lib,
cache = cache,
debian_version = debian_version)
} else {
dockerfile_content <- c("", "", "COPY rang.R ./rang.R", "RUN Rscript rang.R", "CMD [\"R\"]")
if (!is.na(lib)) {
dockerfile_content[4] <- paste0("RUN mkdir ", lib, " && Rscript rang.R")
}
dockerfile_content[1] <- paste0("FROM rocker/", image, ":", rang$r_version)
dockerfile_content[2] <- paste("RUN", sysreqs_cmd)
if (image == "rstudio") {
dockerfile_content[5] <- "EXPOSE 8787"
dockerfile_content[6] <- "CMD [\"/init\"]"
}
if (isTRUE(cache)) {
dockerfile_content <- .insert_cache_dir(dockerfile_content)
}
dockerfile_content <- .generate_rocker_dockerfile_content(r_version = rang$r_version,
sysreqs_cmd = sysreqs_cmd, lib = lib,
cache = cache, image = image)
}
if (!(is.null(materials_dir))) {
materials_subdir_in_output_dir <- file.path(output_dir, "materials")
Expand All @@ -452,9 +469,7 @@ dockerize <- function(rang, output_dir, materials_dir = NULL, image = c("r-ver",
dockerfile_content <- .insert_materials_dir(dockerfile_content)
}
writeLines(dockerfile_content, file.path(output_dir, "Dockerfile"))

.generate_docker_readme(output_dir = output_dir,image = image)

invisible(output_dir)
}

Expand Down
5 changes: 4 additions & 1 deletion inst/compile_r.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ if echo "$1" | grep -Eq '^1' ; then
elif echo "$1" | grep -Eq '^2' ; then
DOWNLOAD_DIR="R-2"
TARFILE="R-$1.tar.gz"
else
elif echo "$1" | grep -Eq '^3' ; then
DOWNLOAD_DIR="R-3"
TARFILE="R-$1.tar.gz"
else
DOWNLOAD_DIR="R-4"
TARFILE="R-$1.tar.gz"
fi

wget "http://cran.r-project.org/src/base/$DOWNLOAD_DIR/$TARFILE"
Expand Down
8 changes: 7 additions & 1 deletion man/dockerize.Rd

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

24 changes: 22 additions & 2 deletions tests/testthat/test_dockerize.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ test_that("Docker R < 2.1", {

test_that(".group_sysreqs and issue #21", {
graph <- readRDS("../testdata/graph.RDS")
expect_equal(.group_sysreqs(graph), "apt-get update -qq && apt-get install -y libpcre3-dev zlib1g-dev pkg-config && apt-get install -y default-jdk libgsl0-dev libicu-dev libpng-dev libxml2-dev make python3 zlib1g-dev liblzma-dev libpcre3-dev libbz2-dev && R CMD javareconf")
expect_equal(.group_sysreqs(graph), "apt-get update -qq && apt-get install -y libpcre3-dev zlib1g-dev pkg-config libcurl4-openssl-dev && apt-get install -y default-jdk libgsl0-dev libicu-dev libpng-dev libxml2-dev make python3 zlib1g-dev liblzma-dev libpcre3-dev libbz2-dev && R CMD javareconf")
graph <- readRDS("../testdata/rang_ok.RDS")
expect_equal(.group_sysreqs(graph), "apt-get update -qq && apt-get install -y libpcre3-dev zlib1g-dev pkg-config")
expect_equal(.group_sysreqs(graph), "apt-get update -qq && apt-get install -y libpcre3-dev zlib1g-dev pkg-config libcurl4-openssl-dev")
graph <- readRDS("../testdata/issue21.RDS")
expected_output <- "apt-get update -qq && apt-get install -y libpcre3-dev zlib1g-dev pkg-config && apt-get install -y software-properties-common && add-apt-repository -y ppa:cran/libgit2 && apt-get update && apt-get install -y cmake git libcurl4-openssl-dev libfontconfig1-dev libfreetype6-dev libfribidi-dev libgit2-dev libgsl0-dev libharfbuzz-dev libicu-dev libjpeg-dev libpng-dev libssh2-1-dev libssl-dev libtiff-dev libxml2-dev make pandoc pari-gp zlib1g-dev"
expect_warning(output <- .group_sysreqs(graph))
Expand Down Expand Up @@ -217,3 +217,23 @@ test_that("dockerize with bioc #58", {
x <- readLines(file.path(temp_dir, "rang.R"))
expect_true(any(grepl("bioc_mirror",x)))
})

test_that("no_rocker #67", {
rang_ok <- readRDS("../testdata/rang_ok.RDS")
temp_dir <- .generate_temp_dir()
dockerize(rang = rang_ok, output_dir = temp_dir) ## no_rocker = FALSE
expect_false(file.exists(file.path(temp_dir, "compile_r.sh")))
expect_false(any(readLines(file.path(temp_dir, "Dockerfile")) == "FROM debian/eol:lenny"))
temp_dir <- .generate_temp_dir()
dockerize(rang = rang_ok, output_dir = temp_dir, no_rocker = TRUE) ## debian_version = lenny
expect_true(file.exists(file.path(temp_dir, "compile_r.sh")))
expect_true(any(readLines(file.path(temp_dir, "Dockerfile")) == "FROM debian/eol:lenny"))
temp_dir <- .generate_temp_dir()
dockerize(rang = rang_ok, output_dir = temp_dir, no_rocker = TRUE,
debian_version = "jessie")
expect_true(file.exists(file.path(temp_dir, "compile_r.sh")))
expect_true(any(readLines(file.path(temp_dir, "Dockerfile")) == "FROM debian/eol:jessie"))
temp_dir <- .generate_temp_dir()
expect_error(dockerize(rang = rang_ok, output_dir = temp_dir, no_rocker = TRUE,
debian_version = "3.11"))
})

0 comments on commit 4f748d8

Please sign in to comment.