Debian Codegraph @

R/cran.R

raw
1 # retrieve the release dates of packages
2 cran_pkg_dates = function(full = FALSE, maintainer = 'Yihui Xie') {
3 info = tools::CRAN_package_db()
4 pkgs = info[grep(maintainer, info$Maintainer), 'Package']
5 info = setNames(vector('list', length(pkgs)), pkgs)
6 for (p in pkgs) {
7 message('Processing ', p)
8 x = readLines(u <- sprintf('https://cran.rstudio.com/web/packages/%s/', p))
9 i = which(x == '<td>Published:</td>')
10 if (length(i) == 0) stop('Cannot find the publishing date from ', u)
11 d = as.Date(gsub('</?td>', '', x[i[1] + 1]))
12 x = try_silent(suppressWarnings(readLines(
13 u <- sprintf('https://cran.r-project.org/src/contrib/Archive/%s/', p)
14 )))
15 if (inherits(x, 'try-error')) {
16 info[[p]] = d; next
17 }
18 r = '.+</td><td align="right">(\\d{4,}-\\d{2}-\\d{2}) .+'
19 d = c(d, as.Date(gsub(r, '\\1', grep(r, x, value = TRUE))))
20 info[[p]] = sort(d, decreasing = TRUE)
21 }
22 if (full) info else sort(do.call(c, lapply(info, `[`, 1)), decreasing = TRUE)
23 }
24
25 # return packages that haven't been updated for X days, and can be updated on CRAN
26 cran_updatable = function(days = 90, maintainer = 'Yihui Xie') {
27 info = cran_pkg_dates(TRUE, maintainer)
28 flag = unlist(lapply(info, function(d) {
29 sum(d > Sys.Date() - 180) < 6 && d[1] < Sys.Date() - days
30 }))
31 if (length(pkgs <- names(which(flag))) == 0) return(pkgs)
32 # look into DESCRIPTION in GitHub repos and see if new version has been pushed
33 info = tools::CRAN_package_db()
34 info = info[info$Package %in% pkgs, , drop = FALSE]
35 pkgs = info$Package
36 for (i in seq_len(nrow(info))) {
37 b = grep_sub('^(https://github.com/[^/]+/[^/]+)/issues$', '\\1', info$BugReports[i])
38 if (length(b) != 1) next
39 f = tempfile()
40 u = paste0(b, '/raw/HEAD/DESCRIPTION')
41 if (is.null(tryCatch(download.file(u, f, quiet = TRUE), error = function(e) NULL))) next
42 d = read.dcf(f)
43 file.remove(f)
44 if (!'Version' %in% colnames(d)) next
45 if (as.numeric_version(d[, 'Version']) <= paste0(info$Version[i], '.1')) {
46 pkgs = setdiff(pkgs, info$Package[i])
47 message('Skipped package ', info$Package[i], ' ', d[, 'Version'], ' (no new version).')
48 } else {
49 message('Package can be updated: ', b)
50 }
51 }
52 pkgs
53 }
54
55
56 #' Some utility functions for checking packages
57 #'
58 #' Miscellaneous utility functions to obtain information about the package
59 #' checking environment.
60 #' @export
61 #' @keywords internal
62 is_R_CMD_check = function() {
63 !is.na(check_package_name()) || tolower(Sys.getenv('_R_CHECK_LICENSE_')) == 'true'
64 }
65
66 #' @rdname is_R_CMD_check
67 #' @export
68 is_CRAN_incoming = function() {
69 isTRUE(as.logical(Sys.getenv('_R_CHECK_CRAN_INCOMING_REMOTE_')))
70 }
71
72 #' @rdname is_R_CMD_check
73 #' @export
74 check_package_name = function() {
75 Sys.getenv('_R_CHECK_PACKAGE_NAME_', NA)
76 }
77
78 # is R CMD check running on a package that has a version lower or equal to `version`?
79 #' @rdname is_R_CMD_check
80 #' @export
81 check_old_package = function(name, version) {
82 if (is.na(pkg <- check_package_name()) || !(pkg %in% name)) return(FALSE)
83 tryCatch(packageVersion(pkg) <= version[pkg == name], error = function(e) FALSE)
84 }
85
86 # return package maintainers (with email addresses)
87 pkg_maintainers = function(pkgs) {
88 info = tools::CRAN_package_db()
89 info = info[match(pkgs, info$Package), c('Package', 'Maintainer')]
90 setNames(info$Maintainer, info$Package)
91 }
92
93 #' Submit a source package to CRAN
94 #'
95 #' Build a source package and submit it to CRAN with the \pkg{curl} package.
96 #' @param file The path to the source package tarball. By default, the current
97 #' working directory is treated as the package root directory, and
98 #' automatically built into a tarball, which is deleted after submission. This
99 #' means you should run `xfun::submit_cran()` in the root directory of a
100 #' package project, unless you want to pass a path explicitly to the `file`
101 #' argument.
102 #' @param comment Submission comments for CRAN. By default, if a file
103 #' \file{cran-comments.md} exists, its content will be read and used as the
104 #' comment.
105 #' @param sync Whether to sync the current Git branch with the remote repository
106 #' before submission.
107 #' @seealso `devtools::submit_cran()` does the same job, with a few more
108 #' dependencies in addition to \pkg{curl} (such as \pkg{cli});
109 #' `xfun::submit_cran()` only depends on \pkg{curl}.
110 #' @export
111 submit_cran = function(file = pkg_build(), comment = '', sync = TRUE) {
112 if (sync) git_pull_current()
113 # check if there are any problematic URLs (I often forget this)
114 if (loadable('urlchecker')) getFromNamespace('url_update', 'urlchecker')() else
115 message('The urlchecker package is not available. URL checks skipped.')
116
117 # if the tarball is automatically created, delete it after submission
118 if (missing(file)) on.exit(file.remove(file), add = TRUE)
119
120 # read the maintainer's name/email
121 dir_create(d <- tempfile())
122 on.exit(unlink(d, recursive = TRUE), add = TRUE)
123 desc = file.path(gsub('_.*', '', basename(file)), 'DESCRIPTION')
124 untar(file, desc, exdir = d)
125 # name and email of maintainer
126 info = read.dcf(file.path(d, desc), fields = 'Maintainer')[1, 1]
127 info = unlist(strsplit(info, '( <|>)'))
128
129 # read submission comments from cran-comments.md if exists
130 if (missing(comment) && file_exists(f <- 'cran-comments.md')) {
131 comment = file_string(f)
132 }
133 params = list(
134 uploaded_file = curl::form_file(file), name = info[1], email = info[2],
135 upload = 'Upload package'
136 )
137 params$comment = if (length(comment)) comment
138 server = 'https://xmpalantir.wu.ac.at/cransubmit/index2.php'
139
140 # submit the form
141 h = curl::new_handle()
142 curl::handle_setform(h, .list = params)
143 res = curl::curl_fetch_memory(server, h)
144
145 # find the pkg_id from the response page
146 id = grep_sub(
147 '(.*<input name="pkg_id" type="hidden" value=")([^"]+)(".*)', '\\2',
148 rawToChar(res$content)
149 )
150 if (length(id) != 1) stop('Failed to submit ', file, ' to CRAN')
151
152 # skip the review and submit directly
153 h = curl::new_handle()
154 curl::handle_setform(h, .list = list(pkg_id = id, submit = 'Submit package'))
155 res = curl::curl_fetch_memory(server, h)
156 if (grepl('>Step 3<', rawToChar(res$content))) message(
157 'The package has been submitted. Please confirm the submission in email: ',
158 params$email
159 ) else message('The submission may be unsuccessful.')
160 }