Skip to content

Commit

Permalink
use internalised renv version.
Browse files Browse the repository at this point in the history
* fix issue #56 and #64 .
- feat: use of {memoise} to cache call to `pak::pkg_system_requirements`
- fix : dont depend anymore  to {renv} use an internalised {renv} version (1.0.3) 
- fix : remove `renv:::lockfile` and use `lockfile_read` instead
- feat: Added `renv_version` parameter to `dock_from_renv` to be able to fix the renv version to use during `renv::restore()`
  • Loading branch information
VincentGuyader authored Nov 5, 2023
1 parent 2984727 commit adcaaf1
Show file tree
Hide file tree
Showing 13 changed files with 34,451 additions and 211 deletions.
7 changes: 3 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: dockerfiler
Title: Easy Dockerfile Creation from R
Version: 0.2.1.9003
Version: 0.2.1.9004
Authors@R: c(
person("Colin", "Fay", , "[email protected]", role = c("cre", "aut"),
comment = c(ORCID = "0000-0001-7343-1846")),
Expand All @@ -24,20 +24,19 @@ Imports:
fs (>= 1.5.0),
glue (>= 1.4.2),
jsonlite (>= 1.7.2),
memoise,
pak (>= 0.2.0),
pkgbuild (>= 1.2.0),
R6 (>= 2.5.0),
remotes (>= 2.2.0),
renv (>= 0.12.0),
usethis (>= 2.0.1),
utils
Suggests:
knitr (>= 1.31),
rmarkdown (>= 2.6),
testthat (>= 3.0.0),
withr
VignetteBuilder:
knitr
VignetteBuilder: knitr
Config/fusen/version: 0.5.2.9000
Config/testthat/edition: 3
Encoding: UTF-8
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(docker_ignore_add)
export(get_sysreqs)
export(parse_dockerfile)
export(r)
export(renv)
importFrom(R6,R6Class)
importFrom(attempt,map_try_catch)
importFrom(attempt,warn_if_not)
Expand All @@ -21,6 +22,7 @@ importFrom(fs,file_temp)
importFrom(fs,path)
importFrom(glue,glue)
importFrom(jsonlite,fromJSON)
importFrom(memoise,memoise)
importFrom(pak,pkg_system_requirements)
importFrom(pkgbuild,build)
importFrom(remotes,dev_package_deps)
Expand Down
17 changes: 13 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
# dockerfile 0.2.0 to 0.3.0
# dockerfile 0.2.x to 0.3.0

- fix: graceful failing in case no internet
- feat: use of {memoise} to cache call to `pak::pkg_system_requirements`

- fix : dont depend anymore to {renv} use an internalised {renv} version (1.0.3)

- fix : remove `renv:::lockfile` and use `lockfile_read` instead

- feat: Added `dock_from_renv()`, to create a Dockerfile from a renv.lock file (@JosiahParry, @statnmap)

- feat: Added `parse_dockerfile()`, to Create a Dockerfile object from a Dockerfile (@JosiahParry)
- feat: Added `parse_dockerfile()`, to Create a Dockerfile object from a Dockerfile file (@JosiahParry)

- feat: Added `renv_version` parameter to `dock_from_renv` to be able to fix the renv version to use during `renv::restore()` (@campbead)

- feat: Added `fix_renv_version` boolean parameter to `dock_from_renv` to be able to fix the renv version to use during `renv::restore()`

# dockerfile 0.2.0

- fix: graceful failing in case no internet

- fix: the dedicated `compact_sysreqs` function allow to deal with 'complex' sysreqs, such as chromimum installation

Expand Down
52 changes: 35 additions & 17 deletions R/dock_from_renv.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ available_distros <- c(
"centos8"
)

#' @importFrom memoise memoise
pkg_system_requirements_mem <- memoise::memoise(
pak::pkg_system_requirements
)


#' Create a Dockerfile from an `renv.lock` file
#'
#' @param lockfile Path to an `renv.lock` file to use as an input..
Expand All @@ -24,8 +30,9 @@ available_distros <- c(
#' @param repos character. The URL(s) of the repositories to use for `options("repos")`.
#' @param extra_sysreqs character vector. Extra debian system requirements.
#' Will be installed with apt-get install.
#' @param fix_renv_version boolean. If `TRUE` the version of renv in the lockfile
#' will be used for the `renv::restore()` command
#' @param renv_version character. The {renv} version to use in the generated Dockerfile.
#' By default, it is set to the version specified in the `renv.lock` file.
#' If the `renv.lock` file does not specify a {renv} version, the version of {renv} bundled with {dockerfiler}, specifically `dockerfiler::renv$the$metadata$version`, will be used. If you set it to NULL, the latest available version of {renv} will be used.
#' @importFrom utils getFromNamespace
#' @return A R6 object of class `Dockerfile`.
#' @details
Expand Down Expand Up @@ -58,17 +65,15 @@ dock_from_renv <- function(
repos = c(CRAN = "https://cran.rstudio.com/"),
expand = FALSE,
extra_sysreqs = NULL,
fix_renv_version = FALSE
renv_version
) {
distro <- match.arg(distro, available_distros)

lock <- getFromNamespace("lockfile", "renv")(lockfile)

# lock$repos(CRAN = repos)
lockfile <- basename(lockfile)
try(dockerfiler::renv$initialize(),silent=TRUE)
lock <- dockerfiler::renv$lockfile_read(file = lockfile) # using vendored renv
# https://rstudio.github.io/renv/reference/vendor.html?q=vendor#null

# start the dockerfile
R_major_minor <- lock$data()$R$Version
R_major_minor <- lock$R$Version
dock <- Dockerfile$new(
FROM = gen_base_image(
distro = distro,
Expand All @@ -79,7 +84,18 @@ dock_from_renv <- function(
)

# get renv version
renv_version <- lock$data()$Packages$renv$Version

if (missing(renv_version)) {
if (!is.null(lock$Packages$renv$Version)) {
renv_version <- lock$Packages$renv$Version
} else {
renv_version <- dockerfiler::renv$the$metadata$version
}
}

message("renv version = ",
ifelse(!is.null(renv_version),renv_version,"the must up to date in the repos")
)

distro_args <- switch(
distro,
Expand Down Expand Up @@ -139,7 +155,7 @@ dock_from_renv <- function(
jammy = "rm -rf /var/lib/apt/lists/*"
)

pkgs <- names(lock$data()$Packages)
pkgs <- names(lock$Packages)

if (sysreqs) {

Expand Down Expand Up @@ -167,11 +183,12 @@ dock_from_renv <- function(
}
)


pkg_sysreqs <- attempt::map_try_catch(
pkg_os,
function(x) {
do.call(
pak::pkg_system_requirements,
pkg_system_requirements_mem,
x
)
},
Expand Down Expand Up @@ -244,13 +261,13 @@ dock_from_renv <- function(
)
)

# check if fix_renv_version is true
if (fix_renv_version){

if (!is.null(renv_version)){
dock$RUN("R -e 'install.packages(\"remotes\")'")
install_renv_string <- paste0(
"R -e 'remotes::install_version(\"renv\", version = ",
"R -e 'remotes::install_version(\"renv\", version = \"",
renv_version,
")'"
"\")'"
)
dock$RUN(install_renv_string)

Expand All @@ -259,8 +276,9 @@ dock_from_renv <- function(
}

dock$COPY(basename(lockfile), "renv.lock")
dock$RUN(r(renv::restore()))
dock$RUN("R -e 'renv::restore()'")

dock
}


91 changes: 91 additions & 0 deletions R/renv.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

#
# renv 1.0.3 [rstudio/renv#e49d9be]: A dependency management toolkit for R.
# Generated using `renv:::vendor()` at 2023-11-05 11:48:15.
#


#' Internalised {renv}
#'
#' https://rstudio.github.io/renv/reference/vendor.html?q=vendor
#'
#' @export
#' @name renv
#' @alias renv
#' @docType data
renv <- new.env(parent = new.env())

renv$initialize <- function() {

# set up renv + imports environments
attr(renv, "name") <- "embedded:renv"
attr(parent.env(renv), "name") <- "imports:renv"

# get imports
imports <- list(
tools = c(
"file_ext",
"pskill",
"psnice",
"write_PACKAGES"
),
utils = c(
"Rprof",
"URLencode",
"adist",
"available.packages",
"browseURL",
"citation",
"contrib.url",
"download.file",
"download.packages",
"file.edit",
"getCRANmirrors",
"head",
"help",
"install.packages",
"installed.packages",
"modifyList",
"old.packages",
"packageDescription",
"packageVersion",
"read.table",
"remove.packages",
"sessionInfo",
"str",
"summaryRprof",
"tail",
"tar",
"toBibtex",
"untar",
"unzip",
"update.packages",
"zip"
)
)

# load the imports required by renv
for (package in names(imports)) {
namespace <- asNamespace(package)
functions <- imports[[package]]
list2env(mget(functions, envir = namespace), envir = parent.env(renv))
}

# source renv into the aforementioned environment
script <- system.file("vendor/renv.R", package = .packageName)
sys.source(script, envir = renv)

# initialize metadata
renv$the$metadata <- list(
embedded = TRUE,
version = structure("1.0.3", sha = "e49d9be9528e0ff73b673f97382731c140013474")
)

# run our load / attach hooks so internal state is initialized
renv$renv_zzz_load()

# remove our initialize method when we're done
rm(list = "initialize", envir = renv)

}
# renv$initialize()
2 changes: 1 addition & 1 deletion dev/config_fusen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ flat_dock_from_renv.Rmd:
vignette_name: Dockerfile from renv.lock
open_vignette: false
check: false
document: true
document: false
overwrite: 'yes'
Loading

0 comments on commit adcaaf1

Please sign in to comment.