Skip to content

Commit 1fadd5a

Browse files
committed
Merge branch 'dev'
2 parents e8da46c + f3b32d2 commit 1fadd5a

File tree

12 files changed

+564
-237
lines changed

12 files changed

+564
-237
lines changed

DESCRIPTION

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@ Description: The goal of checkpoint is to solve the problem of package
1616
Immediately after completion of the rsync mirror process, the process takes a
1717
snapshot, thus creating the archive. Snapshot archives exist starting from
1818
2014-09-17.
19-
Version: 0.3.11
20-
Date: 2015-08-04
19+
Version: 0.3.12
20+
Date: 2015-08-26
2121
Author: Revolution Analytics
2222
Maintainer: Andrie de Vries <[email protected]>
2323
Copyright: Revolution Analytics
2424
License: GPL-2
2525
URL: http://projects.revolutionanalytics.com/documents/rrt/rrtpkgs/
2626
BugReports: http://www.github.com/RevolutionAnalytics/checkpoint/issues
2727
Imports:
28-
utils,
29-
xml2
28+
utils
3029
Depends:
3130
R(>= 3.1.1)
3231
Suggests:

NAMESPACE

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,3 @@ export(checkpoint)
44
export(getValidSnapshots)
55
export(setSnapshot)
66
importFrom(utils,install.packages)
7-
importFrom(xml2,read_xml)
8-
importFrom(xml2,xml_find_all)
9-
importFrom(xml2,xml_text)

R/checkpoint.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ checkpoint <- function(snapshotDate, project = getwd(), R.version, scanForPackag
6969

7070

7171
mran <- mranUrl()
72-
opts <- setDownloadOption(mran)
73-
on.exit(options(opts))
7472
snapshoturl <- getSnapshotUrl(snapshotDate = snapshotDate)
7573

7674

R/mranUrl.R

Lines changed: 98 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,37 @@ stopIfInvalidDate <- function(snapshotDate){
55
if(!grepl("^\\d{4}-\\d{2}-\\d{2}$", snapshotDate))
66
stop("snapshotDate must be a valid date using format YYYY-MM-DD", call. = FALSE)
77
if(as.Date(snapshotDate) < as.Date("2014-09-17"))
8-
stop("Snapshots are only available after 2014-09-17", call. = FALSE)
8+
stop("Snapshots are only available after 2014-09-17", call. = FALSE)
99
if(as.Date(snapshotDate) > Sys.Date())
1010
stop("snapshotDate can not be in the future!", call. = FALSE)
1111

1212
}
1313

14-
testHttps <- function(https){
15-
tf = tempfile()
16-
dir.create(tf)
17-
on.exit(unlink(tf))
18-
testpkg = "memoise"
19-
repos <- paste0(https, "snapshot/2014-09-12/")
20-
tryCatch(utils::install.packages(testpkg, lib = tf,
21-
repos = repos ,
22-
dependencies = FALSE,
23-
type = "source",
24-
quiet = TRUE))
25-
if(testpkg %in% names(installed.packages(lib.loc = tf)[, "Package"])) {
26-
TRUE
27-
} else {
28-
FALSE
29-
}
30-
}
14+
# testHttps <- function(https){
15+
# tf = tempfile()
16+
# dir.create(tf)
17+
# on.exit(unlink(tf))
18+
# testpkg = "memoise"
19+
# repos <- paste0(https, "snapshot/2014-09-12/")
20+
# tryCatch(suppressWarnings(utils::install.packages(testpkg, lib = tf,
21+
# repos = repos ,
22+
# dependencies = FALSE,
23+
# type = "source",
24+
# quiet = TRUE)))
25+
# if(testpkg %in% installed.packages(lib.loc = tf)[, "Package"]) {
26+
# TRUE
27+
# } else {
28+
# FALSE
29+
# }
30+
# }
3131

3232
mranUrlDefault <- function(){
3333
http = "http://mran.revolutionanalytics.com/"
3434
https = gsub("http://", replacement = "https://", http)
35-
if(getRversion() >= "3.2.0") {
36-
if(testHttps(https)) https else http
35+
if(getRversion() >= "3.2.0" && httpsSupported()) {
36+
https
37+
# Attempt to connect
38+
# if unable to connect, stop with warning
3739
} else {
3840
http
3941
}
@@ -48,31 +50,26 @@ isHttpsUrl <- function(url){
4850
grepl("^https://", url)
4951
}
5052

51-
setDownloadOption <- function(mranUrl){
52-
53-
# is.recent = getRversion() >= "3.2.2"
54-
# is.unix = .Platform$OS.type == "unix"
55-
is.os.x = length(grep(pattern = "darwin", R.version$os)) > 0
56-
# is.win = .Platform$OS.type == "windows"
57-
58-
method <- if(isHttpsUrl(mranUrl)){
59-
switch(.Platform$OS.type,
60-
windows = "wininet",
61-
unix = if(capabilities("libcurl")) "libcurl" else "wget"
62-
)
63-
} else {
64-
switch(.Platform$OS.type,
65-
windows = {utils::setInternet2(TRUE); "wininet"},
66-
unix = if(is.os.x) "curl" else "wget"
67-
)
68-
}
69-
70-
options(download.file.method = method, url.method = method)
71-
}
72-
73-
resetDownloadOption <- function(opts){
74-
options(opts)
75-
}
53+
# setDownloadOption <- function(mranUrl){
54+
#
55+
# download.method <- switch(
56+
# .Platform$OS.type,
57+
# windows = "wininet",
58+
# unix = if(capabilities("libcurl")) "libcurl" else "curl"
59+
# )
60+
# url.method <- switch(
61+
# .Platform$OS.type,
62+
# windows = "wininet",
63+
# unix = if(capabilities("libcurl")) "libcurl" else "internal"
64+
# )
65+
#
66+
# options(download.file.method = download.method,
67+
# url.method = url.method)
68+
# }
69+
#
70+
# resetDownloadOption <- function(opts){
71+
# options(opts)
72+
# }
7673

7774

7875
# ------------------------------------------------------------------------
@@ -101,49 +98,78 @@ setCheckpointUrl <- function(url){
10198
#'
10299
#' @param mranRootUrl URL of MRAN root, e.g. \code{"http://mran.revolutionanalytics.com/snapshot/"}
103100
#'
104-
#' @importFrom xml2 read_xml xml_find_all xml_text
105101
#' @export
106102
getValidSnapshots <- function(mranRootUrl = mranUrl()){
107-
opts <- setDownloadOption(mranRootUrl)
108-
on.exit(resetDownloadOption(opts))
109-
text <- tryCatch(suppressWarnings(read_xml(mranRootUrl, as_html = TRUE)), error=function(e)e)
103+
text <- tryCatch(readLines(mranRootUrl, warn = TRUE), error=function(e)e)
110104
if(inherits(text, "error")) {
111105
stop(sprintf("Unable to download from MRAN: %s", text$message))
112106
}
113-
links <- xml_find_all(text, "//a")
114-
dates <- xml_text(links)
115-
idx <- grep("\\d{4}-\\d{2}-\\d{2}/", dates)
116-
gsub("/$", "", dates[idx])
107+
ptn <- "\\d{4}-\\d{2}-\\d{2}"
108+
idx <- grep(ptn, text)
109+
gsub(sprintf("^<a href=.*?>(%s).*?</a>.*$", ptn), "\\1", text[idx])
117110
}
118111

119112

120113
# ------------------------------------------------------------------------
121114

115+
libcurl <- function() isTRUE(unname(capabilities("libcurl")))
116+
117+
url <- function(url){
118+
if(getRversion() >= "3.2.0"){
119+
method <- switch(.Platform$OS.type,
120+
"unix" = if(libcurl()) "libcurl" else "default",
121+
"windows" = method <- "wininet",
122+
"default"
123+
)
124+
base::url(url, method = method)
125+
} else {
126+
base::url(url)
127+
}
128+
}
129+
130+
httpsSupported <- function(mran = "https://mran.revolutionanalytics.com/snapshot/"){
131+
con <- suppressWarnings({
132+
tryCatch(url(mran),
133+
error = function(e)e)
134+
})
135+
if(inherits(con, "error")) return(FALSE)
136+
on.exit(close(con))
137+
x <- suppressWarnings(
138+
tryCatch(readLines(con, warn = FALSE),
139+
error = function(e)e)
140+
)
141+
if(!inherits(x, "error")) return(TRUE)
142+
if(x$message == "cannot open the connection") return(FALSE)
143+
warning(x$message)
144+
FALSE
145+
}
146+
147+
148+
is.404 <- function(mran){
149+
if(isHttpsUrl(mran) && !httpsSupported(mran)) {
150+
warning("It seems that https URLs are not supported on this platform")
151+
return(TRUE)
152+
}
153+
con <- url(mran)
154+
on.exit(close(con))
155+
x <- suppressWarnings(
156+
tryCatch(readLines(con, warn = FALSE),
157+
error = function(e)e)
158+
)
159+
if(inherits(x, "error")) return(TRUE)
160+
ptn <- "404.*Not Found"
161+
any(grepl(ptn, x))
162+
}
163+
122164
getSnapshotUrl <- function(snapshotDate, mranRootUrl = mranUrl()){
123165

124-
opts <- setDownloadOption(mranRootUrl)
125-
on.exit(resetDownloadOption(opts))
126-
mran.root = url(mranRootUrl)
127-
snapshot.url = paste(gsub("/$", "", mranRootUrl), snapshotDate, sep = "/")
128-
on.exit(close(mran.root))
129-
res <- tryCatch(
130-
suppressWarnings(readLines(mran.root)),
131-
error = function(e) e
132-
)
133-
if(inherits(res, "error")) {
166+
if(is.404(mranRootUrl)){
134167
warning("Unable to reach MRAN root at ", mranRootUrl, call. = FALSE)
135-
return(snapshot.url)
136168
}
137169

138-
con = url(snapshot.url)
139-
on.exit(close(con), add = TRUE)
140-
res <- tryCatch(
141-
suppressWarnings(readLines(con)),
142-
error = function(e) e
143-
)
144-
if(inherits(res, "error")) {
170+
snapshot.url = paste(gsub("/$", "", mranRootUrl), snapshotDate, sep = "/")
171+
if(is.404(snapshot.url)){
145172
warning("Unable to find snapshot on MRAN at ", snapshot.url, call. = FALSE)
146-
return(snapshot.url)
147173
}
148174
snapshot.url
149175
}

R/setSnapshot.R

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ setSnapshot <- function(snapshotDate){
99
if (missing(snapshotDate) || is.null(snapshotDate)) return(getOption("repos"))
1010
mran <- mranUrl()
1111
repoDate <- paste0(mran, snapshotDate)
12-
setDownloadOption(mran)
13-
response <- tryCatch(
14-
suppressWarnings(readLines(repoDate)),
15-
error = function(e)e
16-
)
17-
if(inherits(response, "error")) stop(paste0("Invalid snapshot date."))
12+
13+
if(is.404(repoDate)) stop(paste0("Invalid snapshot date."))
1814
options(repos = c(CRAN = repoDate))
1915
message(paste("Using CRAN mirror at", repoDate))
2016
}

tests/test-all.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
library(testthat)
2+
test_check("checkpoint")

0 commit comments

Comments
 (0)