inst/extdata/installPackages.R

# Install hazardItems, production instance, prerequisite packages. Should be safe to
# run repeatedly without additional intervention.

# for dev to use CRAN instead of MRAN
args = commandArgs(trailingOnly=TRUE)


# a slight modification of utils::packageDescription(), to read "Imports"
# section of DESCRIPTION file
hazardItemsImports <- function (lib.loc = NULL, encoding = "") {

  if (file.exists(file <- file.path(getwd(), "DESCRIPTION"))) {
    dcf <- read.dcf(file = file)
    if (NROW(dcf) < 1L) 
      stop(gettextf("DESCRIPTION file of package '%s' is corrupt", 
                    "hazardItems"), domain = NA)
    desc <- as.list(dcf[1, ])
  }
  else file <- ""
  
  if (nzchar(file)) {
    enc <- desc[["Encoding"]]
    if (!is.null(enc) && !is.na(encoding)) {
      if (missing(encoding) && Sys.getlocale("LC_CTYPE") == "C") 
        encoding <- "ASCII//TRANSLIT"
      newdesc <- try(lapply(desc, iconv, from = enc, to = encoding))
      if (!inherits(newdesc, "try-error")) 
        desc <- newdesc
      else warning("'DESCRIPTION' file has an 'Encoding' field and re-encoding is not possible", 
                   call. = FALSE)
    }
  }
  
  if ((file == "") || (length(desc$Imports) == 0)) {
    warning(gettextf("DESCRIPTION file of package '%s' is missing or broken", 
                     "repgen"), domain = NA)
    return(NA)
  }

  imports <- strsplit(unlist(desc$Imports), "[\n,]+")
  #get depends too
  depends <- strsplit(unlist(desc$Depends), "[\n,]+")
  #remove the R version specification
  depends <- list(depends[[1]][3])
  #merge them all back together
  imports <- Map(c, imports, depends)
  return(imports[[1]][-1]) # hack to remove "" from results of strsplit() above
}

#convenience function for getting a package version from a repo URL
getVersionOnRepo <- function(pkg, repo) {
  return(available.packages(contriburl = contrib.url(repo))[pkg,]["Version"][[1]])
}
# convenience wrapper function around install.packages()
installPackages <- function(pkgs, lib, repos = getOption("repos")) {
  print(paste("Using repository", repos))
  
  print("")
  
  for (p in pkgs) {
    print(paste("Checking", p, "..."))
    v <- NULL
    tryCatch({
      v <- packageVersion(p) # check to see if package p is already installed
    }, error=function(e){})
    
    if(is.null(v)) {
      print(paste("not installed, installing from repository..."))
      install.packages(p, lib, repos = repos)
    } else {
      print(paste("Found version:", v))
      
      remoteVersion <- NULL
      tryCatch({
        remoteVersion <- getVersionOnRepo(p, repos)
      }, error=function(e){})
      
      if(is.null(remoteVersion)) {
        print("Not found in remote repo, skipping...")
      } else if(v == remoteVersion) {
        print("Up to date with repository version")
      } else {
        print(paste("Does not match version on repository:", remoteVersion))
        print(paste("Installing", p, remoteVersion, "..."))
        install.packages(p, lib, repos = repos)
      }
    }
    print("")
  }
}

lib <- Sys.getenv("R_LIBS")

if (nchar(lib) == 0) {
  stop("Could not get a value for R_LIBS environment variable")
}

pkgs <-
  grep(
    "gsplot", hazardItemsImports(lib.loc = lib),
    value = TRUE, fixed = TRUE, invert = TRUE
  )

# all packages are held back to older, MRAN versions, unless it's dev, then the Dev Jenkins job should use latest and greatest
if (length(args)==0) {
  installPackages(c(pkgs, "devtools"), lib, "http://mran.microsoft.com/snapshot/2017-04-06")  
  } else if (length(args)==1) {
    installPackages(c(pkgs, "devtools"), lib, "http://cran.rstudio.com/")
}
USGS-R/hazardItems documentation built on Aug. 15, 2020, 11:28 a.m.