Skip to content

Instantly share code, notes, and snippets.

@slopp
Created September 12, 2019 22:47
Show Gist options
  • Select an option

  • Save slopp/654dd90daccae1a2c3d69925d701c7f6 to your computer and use it in GitHub Desktop.

Select an option

Save slopp/654dd90daccae1a2c3d69925d701c7f6 to your computer and use it in GitHub Desktop.
Automatic workaround for adding BioCsoft packages to RSPM local source
# --- BioC RSPM Installer ----
# This R script is designed to be run by a user on the RStudio Package
# Manager server with access to the rspm command line utility
#
# The script loops through the R packages in the BioCsoft repository
# and adds them to a RSPM source called bioc_version
#
# To run this script, call Rscript add_bioc.R "3.9"
# Parse Inputs
args <- commandArgs(trailingOnly = TRUE)
if(length(args) > 1)
stop("More than one argument found, but only expecting a BioC release version. Example: Rscript add_bioc.R '3.9'")
if(!grepl("^\\d\\.\\d$", args))
stop(sprintf("Could not determine BioC release version from argument %s, expecting something like 3.9", args))
bioc_release <- args
# Check rspm availability
rspm <- function(args, verbose=FALSE) {
tryCatch({
res <- system2(command = "/opt/rstudio-pm/bin/rspm", args = args, stdout = TRUE, stderr = TRUE)
}, error = function(e){
stop(sprintf("Could not run rspm CLI using /opt/rstudio-pm/bin/rspm! Error: %s", e$message))
})
if (verbose)
print(res)
res
}
rspm('--help')
# Determine packages to add
print(sprintf("Getting packages for BioCsoft version %s", bioc_release))
repo <- sprintf("https://bioconductor.org/packages/%s/bioc", bioc_release)
pkgs <- available.packages(repos = repo)
print(sprintf("Found %d packages to add", nrow(pkgs)))
# Create directory
if(dir.exists("./bioc_pkgs")) {
print("Using cache ./bioc_pkgs")
} else {
print("Creating cache ./bioc_pkgs")
success <- dir.create("./bioc_pkgs")
if (!success)
stop("Failed to create cache at ./bioc_pkgs")
}
# Download packages
for(i in 1:nrow(pkgs)) {
fname <- sprintf("./bioc_pkgs/%s_%s.tar.gz", pkgs[i,"Package"], pkgs[i,"Version"])
url <- sprintf("%s/%s_%s.tar.gz", pkgs[i, "Repository"], pkgs[i,"Package"], pkgs[i,"Version"])
if(!file.exists(fname)) {
download.file(url, destfile = fname)
}
if(!(i %% 10))
print(sprintf("Downloaded %d of %d", i, nrow(pkgs)))
}
# Add source
print(sprintf('Adding local source named bioc_%s', bioc_release))
rspm(sprintf('create source --name=bioc_%s',bioc_release))
print('Adding packages to source...')
for(i in 1:nrow(pkgs)){
fname <- sprintf("./bioc_pkgs/%s_%s.tar.gz", pkgs[i,"Package"], pkgs[i,"Version"])
rspm(sprintf('add --source=bioc_%s --path=%s', bioc_release, fname))
if(!(i %% 10))
print(sprintf("Downloaded %d of %d", i, nrow(pkgs)))
}
rspm(sprintf('list packages --source=bioc_%s', bioc_release))
print('Done!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment