Created
November 6, 2022 13:48
-
-
Save vjcitn/2ec74f495df23bce64e2e6521b92569c to your computer and use it in GitHub Desktop.
process_renv_lock will attempt to clone and check out sources of packages corresponding to specs in renv lockfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
as_renv_entry = function(x) { | |
stopifnot(all(c("Package", "git_url", "git_last_commit") %in% names(x))) | |
class(x) = c("renv_entry", class(x)) | |
x | |
} | |
print.renv_entry = function(x, ...) { | |
cat(sprintf("renv entry for %s %s\n", x$Package, x$git_branch)) | |
} | |
clone_renv_entry = function(x, require_bioc=TRUE, verbose=TRUE, progress=FALSE) { | |
stopifnot(inherits(x, "renv_entry")) | |
if (require_bioc) stopifnot(x$Source == "Bioconductor") | |
if (dir.exists(x$Package)) stop(sprintf("Folder %s already present.\n", x$Package)) | |
rc1 <- git2r::clone(url=x$git_url, local_path=paste0("./", x$Package), branch=x$git_branch, progress=progress) | |
rc2 <- git2r::checkout(x$Package, x$git_last_commit) | |
if (verbose) { | |
dcf = read.dcf(paste0(x$Package, "/", "DESCRIPTION")) | |
message(sprintf(" Got %s of %s", dcf[,"Version"], dcf[,"Package"])) | |
} | |
invisible(c(rc1=rc1, rc2=rc2)) | |
} | |
process_renv_lock = function(lockfile, require_bioc = TRUE, verbose=TRUE) { | |
dat = try(jsonlite::fromJSON(lockfile)) | |
if (inherits(dat, "try-error")) stop(sprintf("could not run fromJSON on lockfile %s", lockfile)) | |
dat = dat$Packages | |
srcs = sapply(dat, function(x) x$Source) # vapply is confounded; character(1)) | |
if (require_bioc) dat = dat[which(srcs=="Bioconductor")] | |
dat = lapply(dat, function(x) try(as_renv_entry(x))) | |
chk = vapply(dat, function(x) inherits(x, "try-error"), logical(1)) | |
if (any(chk)) { | |
print(which(chk)) | |
message("some renv-lock entry fails `as_renv_entry`") | |
dat = dat[-which(chk)] | |
} | |
out = lapply(dat, clone_renv_entry, verbose=verbose) # can fail | |
invisible(out) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hacky approach to acquiring sources of packages corresponding to a renv lock file.