Skip to content

Instantly share code, notes, and snippets.

@sv99
Forked from bearloga/upgrade_packages.R
Last active May 14, 2017 10:20
Show Gist options
  • Select an option

  • Save sv99/756558f7a971b1075c4007e62d8052e8 to your computer and use it in GitHub Desktop.

Select an option

Save sv99/756558f7a971b1075c4007e62d8052e8 to your computer and use it in GitHub Desktop.

Revisions

  1. sv99 revised this gist May 14, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion upgrade_packages.R
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ if (Sys.info()["sysname"] == "Linux") {
    # site installer
    # pkg_paths <- file.path(dir("~/Library/R", full.names = TRUE), "library")
    # for brew install homebrew/science/r
    pkg_paths <- file.path(dir("/usr/local/lib/R", full.names = TRUE), "library")
    pkg_paths <- file.path(dir("/usr/local/lib/R", full.names = TRUE), "site-library")
    }

    # Pick the latest one that is not the current R version's library:
  2. sv99 revised this gist May 14, 2017. 1 changed file with 4 additions and 8 deletions.
    12 changes: 4 additions & 8 deletions upgrade_packages.R
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,3 @@
    # WMF only:
    if (file.exists("/etc/wikimedia-cluster")) {
    message('Detected that this script is being run on a WMF machine ("', Sys.info()["nodename"], '"). Setting proxies...')
    Sys.setenv("http_proxy" = "http://webproxy.eqiad.wmnet:8080")
    Sys.setenv("https_proxy" = "http://webproxy.eqiad.wmnet:8080")
    }

    # General use:
    message("Checking for a personal library...")
    if (!dir.exists(Sys.getenv("R_LIBS_USER"))) {
    @@ -28,7 +21,10 @@ if (! "devtools" %in% installed.packages()[, "Package"]) {
    if (Sys.info()["sysname"] == "Linux") {
    pkg_paths <- dir(paste0("~/R/", R.version$platform, "-library"), full.names = TRUE)
    } else if (Sys.info()["sysname"] == "Darwin") {
    pkg_paths <- file.path(dir("~/Library/R", full.names = TRUE), "library")
    # site installer
    # pkg_paths <- file.path(dir("~/Library/R", full.names = TRUE), "library")
    # for brew install homebrew/science/r
    pkg_paths <- file.path(dir("/usr/local/lib/R", full.names = TRUE), "library")
    }

    # Pick the latest one that is not the current R version's library:
  3. @bearloga bearloga revised this gist Nov 4, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion upgrade_packages.R
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ if (! "devtools" %in% installed.packages()[, "Package"]) {

    # First, we locate the various personal R libraries the user has:
    if (Sys.info()["sysname"] == "Linux") {
    pkg_paths <- dir("~/R/x86_64-pc-linux-gnu-library", full.names = TRUE)
    pkg_paths <- dir(paste0("~/R/", R.version$platform, "-library"), full.names = TRUE)
    } else if (Sys.info()["sysname"] == "Darwin") {
    pkg_paths <- file.path(dir("~/Library/R", full.names = TRUE), "library")
    }
  4. @bearloga bearloga revised this gist Nov 4, 2016. No changes.
  5. @bearloga bearloga revised this gist Nov 4, 2016. No changes.
  6. @bearloga bearloga revised this gist Nov 4, 2016. No changes.
  7. @bearloga bearloga revised this gist Nov 4, 2016. 1 changed file with 12 additions and 3 deletions.
    15 changes: 12 additions & 3 deletions upgrade_packages.R
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,11 @@
    message("Setting proxies...")
    Sys.setenv("http_proxy" = "http://webproxy.eqiad.wmnet:8080")
    Sys.setenv("https_proxy" = "http://webproxy.eqiad.wmnet:8080")
    # WMF only:
    if (file.exists("/etc/wikimedia-cluster")) {
    message('Detected that this script is being run on a WMF machine ("', Sys.info()["nodename"], '"). Setting proxies...')
    Sys.setenv("http_proxy" = "http://webproxy.eqiad.wmnet:8080")
    Sys.setenv("https_proxy" = "http://webproxy.eqiad.wmnet:8080")
    }

    # General use:
    message("Checking for a personal library...")
    if (!dir.exists(Sys.getenv("R_LIBS_USER"))) {
    warning("Personal library not found, creating one...")
    @@ -10,6 +15,7 @@ if (!dir.exists(Sys.getenv("R_LIBS_USER"))) {
    } else {
    message("Personal library found.")
    }

    message("Installing devtools for re-installing from GitHub, etc....")
    install.packages("devtools", repos = "https://cran.rstudio.com/")
    # ^ also ensures that we have a personal library for this version of R
    @@ -24,13 +30,15 @@ if (Sys.info()["sysname"] == "Linux") {
    } else if (Sys.info()["sysname"] == "Darwin") {
    pkg_paths <- file.path(dir("~/Library/R", full.names = TRUE), "library")
    }

    # Pick the latest one that is not the current R version's library:
    pkg_path <- tail(setdiff(pkg_paths, .libPaths()), 1)
    if (length(pkg_path) == 0) {
    stop("Nothing to do.")
    } else {
    message("Found library from previous R installation: ", pkg_path)
    }

    # List of installed packages in the user's personal library:
    installed_pkgs <- dir(pkg_path)
    if (length(installed_pkgs) == 0) {
    @@ -72,6 +80,7 @@ pkgs_info <- do.call(rbind, lapply(installed_pkgs, function(installed_pkg) {
    stringsAsFactors = FALSE
    ))
    }))

    # Re-install packages:
    if (sum(pkgs_info$source == "cran") > 0) {
    message("Re-installing ", sum(pkgs_info$source == "cran"), " R packages from CRAN...")
  8. @bearloga bearloga revised this gist Nov 4, 2016. No changes.
  9. @bearloga bearloga created this gist Nov 4, 2016.
    90 changes: 90 additions & 0 deletions upgrade_packages.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    message("Setting proxies...")
    Sys.setenv("http_proxy" = "http://webproxy.eqiad.wmnet:8080")
    Sys.setenv("https_proxy" = "http://webproxy.eqiad.wmnet:8080")
    message("Checking for a personal library...")
    if (!dir.exists(Sys.getenv("R_LIBS_USER"))) {
    warning("Personal library not found, creating one...")
    dir.create(Sys.getenv("R_LIBS_USER"), recursive = TRUE)
    message("Registering newly created personal library...")
    .libPaths(Sys.getenv("R_LIBS_USER"))
    } else {
    message("Personal library found.")
    }
    message("Installing devtools for re-installing from GitHub, etc....")
    install.packages("devtools", repos = "https://cran.rstudio.com/")
    # ^ also ensures that we have a personal library for this version of R

    if (! "devtools" %in% installed.packages()[, "Package"]) {
    stop("devtools is/was not installed!")
    }

    # First, we locate the various personal R libraries the user has:
    if (Sys.info()["sysname"] == "Linux") {
    pkg_paths <- dir("~/R/x86_64-pc-linux-gnu-library", full.names = TRUE)
    } else if (Sys.info()["sysname"] == "Darwin") {
    pkg_paths <- file.path(dir("~/Library/R", full.names = TRUE), "library")
    }
    # Pick the latest one that is not the current R version's library:
    pkg_path <- tail(setdiff(pkg_paths, .libPaths()), 1)
    if (length(pkg_path) == 0) {
    stop("Nothing to do.")
    } else {
    message("Found library from previous R installation: ", pkg_path)
    }
    # List of installed packages in the user's personal library:
    installed_pkgs <- dir(pkg_path)
    if (length(installed_pkgs) == 0) {
    stop("Did not find any packages from previous R installation.")
    }
    message("Found ", length(installed_pkgs), " packages from previous R installation.")

    # A helper function for extracting a parameter
    # from an installed package's DESCRIPTION file
    extract_param <- function(DESCRIPTION, param) {
    return(sub(paste0(param, ": "), "", DESCRIPTION[grepl(param, DESCRIPTION)], fixed = TRUE))
    }

    message("Extracting metadata about packages from previous R installation...")
    pkgs_info <- do.call(rbind, lapply(installed_pkgs, function(installed_pkg) {
    # message('Checking how "', installed_pkg, '" was installed...')
    pkg_description <- readLines(file.path(find.package(installed_pkg, lib.loc = pkg_path), "DESCRIPTION"))
    if (any(grepl("Repository: CRAN", pkg_description))) {
    # message('"', installed_pkg, '" was installed from CRAN.')
    pkg_source <- "cran"; pkg_url <- NA
    } else if (any(grepl("RemoteType", pkg_description))) {
    # message('"', installed_pkg, '" was installed from a remote source like GitHub.')
    pkg_source <- extract_param(pkg_description, "RemoteType")
    if (pkg_source == "github") {
    pkg_url <- paste(extract_param(pkg_description, "GithubUsername"), extract_param(pkg_description, "GithubRepo"), sep = "/")
    } else if (pkg_source == "git") {
    pkg_url <- extract_param(pkg_description, "RemoteUrl")
    } else {
    pkg_source <- "other remote"; pkg_url <- NA
    }
    } else {
    # message('"', installed_pkg, '" was installed from a local source.')
    pkg_source <- "local"; pkg_url <- NA
    }
    return(data.frame(
    package = installed_pkg,
    source = pkg_source,
    url = pkg_url,
    stringsAsFactors = FALSE
    ))
    }))
    # Re-install packages:
    if (sum(pkgs_info$source == "cran") > 0) {
    message("Re-installing ", sum(pkgs_info$source == "cran"), " R packages from CRAN...")
    install.packages(pkgs_info$package[pkgs_info$source == "cran"], repos = "https://cran.rstudio.com/")
    }
    if (sum(pkgs_info$source == "github") > 0) {
    message("The following ", sum(pkgs_info$source == "github"), " packages will be re-installed from GitHub: ", paste(pkgs_info$package[pkgs_info$source == "github"], collapse = ", "))
    devtools::install_github(pkgs_info$url[pkgs_info$source == "github"])
    }
    if (sum(pkgs_info$source == "git") > 0) {
    message("The following ", sum(pkgs_info$source == "git"), " packages will be re-installed from Git repos: ", paste(pkgs_info$package[pkgs_info$source == "git"], collapse = ", "))
    devtools::install_git(pkgs_info$url[pkgs_info$source == "git"])
    }
    if (sum(pkgs_info$source %in% c("other remote", "local")) > 0) {
    message("The following ", sum(pkgs_info$source %in% c("other remote", "local")), " packages will need to be manually re-installed (sorry): ", paste(pkgs_info$package[pkgs_info$source %in% c("other remote", "local")], collapse = ", "))
    }