-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_cran_file.R
58 lines (52 loc) · 1.56 KB
/
read_cran_file.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#' Read a file from CRAN
#' @description Read a file from CRAN package source.
#' @inheritParams standard_args
#' @keywords internal
read_cran_file <- function(pac, version, file, repos = "https://cran.rstudio.com/") {
stopifnot(is_online())
last_version <- pac_last(pac, repos)
if (isTRUE(!is.null(version) && version != last_version)) {
base_url <- sprintf("https://cran.r-project.org/src/contrib/Archive/%s", pac)
} else {
base_url <- "https://cran.r-project.org/src/contrib"
version <- last_version
}
d_url <- sprintf(
"%s/%s_%s.tar.gz",
base_url,
pac,
version
)
temp_tar <- tempfile(fileext = ".tar.gz")
download <- try(
expr = {
suppressWarnings(utils::download.file(d_url,
destfile = temp_tar,
quiet = TRUE
))
},
silent = TRUE
)
if (inherits(download, "try-error")) {
result <- NA
} else {
temp_dir <- tempdir()
utils::untar(temp_tar, exdir = temp_dir)
if (file == "DESCRIPTION") {
result <- as.list(read.dcf(file.path(temp_dir, pac, "DESCRIPTION"))[1, ])
} else if (file == "NAMESPACE") {
result <- readLines(file.path(temp_dir, pac, "NAMESPACE"), warn = FALSE)
} else if (file == "NEWS") {
news_name <- intersect(list.files(file.path(temp_dir, pac)), c("NEWS.md", "NEWS", "NEWS.Rmd"))
if (length(news_name) == 0) {
warning("NEWS file not found")
return(NA)
}
result <- readLines(file.path(temp_dir, pac, news_name[1]), warn = FALSE)
} else {
stop("Invalid file name")
}
}
unlink(temp_tar)
result
}