Last active
August 29, 2015 14:02
-
-
Save kevinushey/9ca8aa4b08021fff06fd to your computer and use it in GitHub Desktop.
Tracking the API of a CRAN package over time
This file contains 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
if (!require(httr)) { | |
devtools::install_github("httr") | |
library(httr) | |
} | |
get_archived_packages <- function(package, repo, where) { | |
archive <- file.path(repo, "Archive", package) | |
response <- httr::GET(archive) | |
if (response$status_code == "404") | |
stop("Could not access page at '", archive, "'.") | |
content <- httr::content(response, "text") | |
matches <- gregexpr("<a href=\"(.*?)\">", content, perl = TRUE) | |
starts <- as.integer(matches[[1]]) | |
ends <- starts + attr(matches[[1]], "match.length") | |
links <- substring(content, starts, ends) | |
tarballs <- unlist(lapply(strsplit(links, '"', fixed = TRUE), "[[", 2)) | |
tarballs <- grep(package, tarballs, value = TRUE) | |
links <- file.path(archive, tarballs) | |
lapply(links, function(link) { | |
download.file(link, destfile = file.path(where, basename(link))) | |
}) | |
} | |
API <- function(package, | |
repo = contrib.url(getOption("repos"), type = "source"), | |
where = file.path( | |
normalizePath(tempdir(), winslash = "/"), | |
paste(package, "API", sep = "-") | |
) | |
) { | |
unloadNamespace(package) | |
dir.create(where, recursive = TRUE, showWarnings = FALSE) | |
get_archived_packages(package, repo, where) | |
tarballs <- list.files(where, full.names = TRUE) | |
templib <- path.expand(file.path("~", ".devtools", package, "library")) | |
dir.create(templib, recursive = TRUE, showWarnings = FALSE) | |
api <- lapply(tarballs, function(tarball) { | |
install.packages(tarball, lib = templib, repos = NULL, quiet = TRUE, type = "source") | |
library(package, lib.loc = templib, character.only = TRUE) | |
exports <- getNamespaceExports(package) | |
result <- lapply(exports, function(export) { | |
formals(export) | |
}) | |
names(result) <- exports | |
unloadNamespace(package) | |
result | |
}) | |
names(api) <- gsub("\\.tar\\.gz$*", "", basename(tarballs)) | |
class(api) <- "API" | |
api | |
} | |
history <- function(api) { | |
all_names <- sort(unique(unlist(lapply(api, names)))) | |
result <- do.call(rbind, lapply(api, function(x) { | |
all_names %in% names(x) | |
})) | |
colnames(result) <- all_names | |
result | |
} | |
devtools_api <- API("devtools") | |
history(devtools_api) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment