Skip to content

Commit

Permalink
Support all version, R-release, R-oldrel separately
Browse files Browse the repository at this point in the history
Remove magrittr dependency, add XML dependency.
  • Loading branch information
gaborcsardi committed Nov 24, 2014
1 parent e39c19f commit 4783625
Show file tree
Hide file tree
Showing 9 changed files with 915 additions and 93 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ License: MIT + file LICENSE
URL: https://github.com/metacran/rversions
BugReports: https://github.com/metacran/rversions/issues
Imports:
magrittr,
RCurl
RCurl,
XML
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
all: README.md

README.md: README.Rmd
Rscript -e "library(knitr); knit('$<', output = '$@', quiet = TRUE)"
Rscript -e "library(knitr); knit('$<', output = '$@', quiet = TRUE)" || \
rm "$@"
8 changes: 7 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Generated by roxygen2 (4.0.2): do not edit by hand

export(r_oldrel)
export(r_release)
export(r_versions)
importFrom(RCurl,getURLContent)
importFrom(magrittr,"%>%")
importFrom(XML,xmlChildren)
importFrom(XML,xmlParse)
importFrom(XML,xmlRoot)
importFrom(XML,xmlValue)
importFrom(XML,xpathApply)
144 changes: 79 additions & 65 deletions R/rversions.R
Original file line number Diff line number Diff line change
@@ -1,85 +1,99 @@

#' Query which versions R-release and R-oldrel refer to
#' Query R's past and present versions
#'
#' R version numbers consist of three numbers (since version 1.4.1):
#' major, minor and patch.
#'
#' We use the latest (in terms of versions numbers) tag in the
#' R SVN repository to determine R-release.
#'
#' For R-oldrel, the latest version of the previous minor version
#' is used, again, based on the SVN repo.
#' We extract the version numbers from the tags in the SVN repository.
#'
#' @return A named character vector with entries \code{release} and
#' \code{oldrel}.
#' @param dots Whether to use dots instead of dashes in the version
#' number.
#' @return A list, where each entry is an R version and has two
#' entries: \sQuote{version} is the version number itself,
#' \sQuote{date} is the exact date and time when this R version was
#' released.
#'
#' @export
#' @importFrom RCurl getURLContent
#' @importFrom magrittr %>%
#' @importFrom XML xmlParse xmlRoot xmlChildren xpathApply xmlValue
#' @examples
#' r_versions()

r_versions <- function() {

## Get the tag info from SVN
tags <- getURLContent(
"http://svn.r-project.org/R/tags/",
customrequest = "PROPFIND",
httpheader=c("Depth"="1")
) %>%
get_tags()

## Extract versions numbers from tags and sort them
versions <- tags %>%
sub(pattern = "/R/tags/R-([^/]+)/", replacement = "\\1") %>%
grep(pattern = "^[0-9]+-[0-9]+(-[0-9]+|)$", value = TRUE) %>%
sort_tags()

## Relase is easy, most recent
release <- tail(versions, 1)

## Oldrel is latest from the previous minor
## (Careful with factors, they are ordered by default!)
oldrel <- versions %>%
sub(pattern = "-[0-9]+$", replacement = "") %>%
factor(levels = unique(.)) %>%
tapply(X = versions, FUN = tail, 1) %>%
tail(2) %>%
head(1) %>%
unname()

c(release = release, oldrel = oldrel) %>%
gsub(pattern = "-", replacement = ".")
}
r_versions <- function(dots = TRUE) {

props<- getURLContent("http://svn.r-project.org/R/tags/",
customrequest = "PROPFIND",
httpheader=c("Depth"="1") )
props <- xmlParse(props)
props <- xmlRoot(props)
props <- xmlChildren(props)
props <- lapply(props, get_props)
props <- unname(props)

tags <- sapply(props, get_tags)
tags <- sub("^.*/tags/R-([-0-9]+).*$", "\\1", tags)

dates <- sapply(props, get_dates)

get_tags <- function(xml) {
xml2 <- xml %>%
gsub(pattern = "<D:propstat>(.*?)</D:propstat>", replacement = "")
versions <- grepl("^[0-9]+-[0-9]+(-[0-9]+|)$", tags)
tags <- tags[versions]
dates <- dates[versions]

matches <- xml2 %>%
gregexpr(pattern = "<D:href>[^<]+</D:href>")
if (dots) tags <- gsub('-', '.', tags)

regmatches(xml2, matches)[[1]] %>%
sub(pattern = "<D:href>", replacement = "") %>%
sub(pattern = "</D:href>", replacement = "")
versions <- mapply(FUN = list, version = tags, date = dates,
SIMPLIFY = FALSE)
versions <- versions[order(package_version(tags))]

versions
}

## Split a version number to major, minor, patch
split_versions <- function(x) {
x %>%
sub(pattern = "^([0-9]+-[0-9]+)$", replacement = "\\1-0") %>%
strsplit(split = "-") %>%
sapply(as.numeric)

#' Version number of R-release
#'
#' The latest tag in the SVN repository (in terms of version numbers,
#' not dates).
#'
#' @inheritParams r_versions
#'
#' @export
#' @examples
#' r_release()

r_release <- function(dots = TRUE) {
tail(r_versions(dots), 1)[[1]]
}

## Sort version numbers
sort_tags <- function(x) {
x_order <- x %>%
split_versions() %>%
apply(1, list) %>%
lapply("[[", 1) %>%
do.call(what = order)
x [x_order]

#' Version number of R-oldrel
#'
#' R-oldrel is the latest version of the previous minor version.
#' We extract version numbers from the R SVN repository tags.
#'
#' @inheritParams r_versions
#'
#' @export
#' @examples
#' r_oldrel()

r_oldrel <- function(dots = TRUE) {

versions <- r_versions(dots)

version_strs <- package_version(names(versions))

major <- version_strs$major
minor <- version_strs$minor
major_minor <- paste(major, sep = ".", minor)

latest <- tail(major_minor, 1)
tail(versions[ major_minor != latest ], 1)[[1]]
}

. <- "Looking for job offers"

get_props <- function(x) unname(x[["propstat"]][["prop"]])


get_tags <- function(x) unname(xmlValue(x[["getetag"]]))


get_dates <- function(x) unname(xmlValue(x[["creationdate"]]))
15 changes: 12 additions & 3 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,30 @@ knitr::opts_chunk$set(
[![Linux Build Status](https://travis-ci.org/metacran/rversions.png?branch=master)](https://travis-ci.org/metacran/rversions)
[![Windows Build status](https://ci.appveyor.com/api/projects/status/github/metacran/rversions)](https://ci.appveyor.com/project/metacran/rversions)

# rversions — query which versions R-release and R-oldrel refer to
# rversions — past and present R versions

## Installation

```{r}
```{r eval = FALSE}
devtools::install_github("metacran/rversions")
```

## Usage

### R-release and R-oldrel

```{r}
library(rversions)
r_release()
r_oldrel()
```

### All R versions and release dates

```{r}
r_versions()
```

## License

MIT © [Gabor Csardi](http://gaborcsardi.org)
MIT © [Gábor Csárdi](http://gaborcsardi.org)
Loading

0 comments on commit 4783625

Please sign in to comment.