@@ -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
3232mranUrlDefault <- 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
106102getValidSnapshots <- 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+
122164getSnapshotUrl <- 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}
0 commit comments