| 1 |
#' Run \command{R CMD check} on the reverse dependencies of a package |
| 2 |
#' |
| 3 |
#' Install the source package, figure out the reverse dependencies on CRAN, |
| 4 |
#' download all of their source packages, and run \command{R CMD check} on them |
| 5 |
#' in parallel. The number of parallel downloads can be set via |
| 6 |
#' `options(xfun.rev_check.download_cores = n)` and the number of parallel |
| 7 |
#' checks can be set via `options(mc.cores = n)` (both default to |
| 8 |
#' [parallel::detectCores()]). |
| 9 |
#' |
| 10 |
#' Everything occurs under the current working directory, and you are |
| 11 |
#' recommended to call this function under a designated directory, especially |
| 12 |
#' when the number of reverse dependencies is large, because all source packages |
| 13 |
#' will be downloaded to this directory, and all \file{*.Rcheck} directories |
| 14 |
#' will be generated under this directory, too. |
| 15 |
#' |
| 16 |
#' If a source tarball of the expected version has been downloaded before (under |
| 17 |
#' the \file{tarball} directory), it will not be downloaded again (to save time |
| 18 |
#' and bandwidth). |
| 19 |
#' |
| 20 |
#' After a package has been checked, the associated \file{*.Rcheck} directory |
| 21 |
#' will be deleted if the check was successful (no warnings or errors or notes), |
| 22 |
#' which means if you see a \file{*.Rcheck} directory, it means the check |
| 23 |
#' failed, and you need to take a look at the log files under that directory. |
| 24 |
#' |
| 25 |
#' The time to finish the check is recorded for each package. As the check goes |
| 26 |
#' on, the total remaining time will be roughly estimated via `n * mean(times)`, |
| 27 |
#' where `n` is the number of packages remaining to be checked, and `times` is a |
| 28 |
#' vector of elapsed time of packages that have been checked. |
| 29 |
#' |
| 30 |
#' If a check on a reverse dependency failed, its \file{*.Rcheck} directory will |
| 31 |
#' be renamed to \file{*.Rcheck2}, and another check will be run against the |
| 32 |
#' CRAN version of the package unless `options(xfun.rev_check.compare = FALSE)` |
| 33 |
#' is set. If the logs of the two checks are the same, it means no new problems |
| 34 |
#' were introduced in the package, and you can probably ignore this particular |
| 35 |
#' reverse dependency. The function `compare_Rcheck()` can be used to create a |
| 36 |
#' summary of all the differences in the check logs under \file{*.Rcheck} and |
| 37 |
#' \file{*.Rcheck2}. This will be done automatically if |
| 38 |
#' `options(xfun.rev_check.summary = TRUE)` has been set. |
| 39 |
#' |
| 40 |
#' A recommended workflow is to use a special directory to run `rev_check()`, |
| 41 |
#' set the global [options()] `xfun.rev_check.src_dir` and `repos` in the R |
| 42 |
#' startup (see `?`[`Startup`]) profile file `.Rprofile` under this directory, |
| 43 |
#' and (optionally) set `R_LIBS_USER` in \file{.Renviron} to use a special |
| 44 |
#' library path (so that your usual library will not be cluttered). Then run |
| 45 |
#' `xfun::rev_check(pkg)` once, investigate and fix the problems or (if you |
| 46 |
#' believe it was not your fault) ignore broken packages in the file |
| 47 |
#' \file{00ignore}, and run `xfun::rev_check(pkg)` again to recheck the failed |
| 48 |
#' packages. Repeat this process until all \file{*.Rcheck} directories are gone. |
| 49 |
#' |
| 50 |
#' As an example, I set `options(repos = c(CRAN = 'https://cran.rstudio.com'), |
| 51 |
#' xfun.rev_check.src_dir = '~/Dropbox/repo')` in \file{.Rprofile}, and |
| 52 |
#' `R_LIBS_USER=~/R-tmp` in \file{.Renviron}. Then I can run, for example, |
| 53 |
#' `xfun::rev_check('knitr')` repeatedly under a special directory |
| 54 |
#' \file{~/Downloads/revcheck}. Reverse dependencies and their dependencies will |
| 55 |
#' be installed to \file{~/R-tmp}, and \pkg{knitr} will be installed from |
| 56 |
#' \file{~/Dropbox/repo/kintr}. |
| 57 |
#' @param pkg The package name. |
| 58 |
#' @param which Which types of reverse dependencies to check. See |
| 59 |
#' [tools::package_dependencies()] for possible values. The special value |
| 60 |
#' `'hard'` means the hard dependencies, i.e., `c('Depends', 'Imports', |
| 61 |
#' 'LinkingTo')`. |
| 62 |
#' @param recheck A vector of package names to be (re)checked. If not provided |
| 63 |
#' and there are any \file{*.Rcheck} directories left by certain packages |
| 64 |
#' (this often means these packages failed the last time), `recheck` will be |
| 65 |
#' these packages; if there are no \file{*.Rcheck} directories but a text file |
| 66 |
#' \file{recheck} exists, `recheck` will be the character vector read from |
| 67 |
#' this file. This provides a way for you to manually specify the packages to |
| 68 |
#' be checked. If there are no packages to be rechecked, all reverse |
| 69 |
#' dependencies will be checked. |
| 70 |
#' @param ignore A vector of package names to be ignored in \command{R CMD |
| 71 |
#' check}. If this argument is missing and a file \file{00ignore} exists, the |
| 72 |
#' file will be read as a character vector and passed to this argument. |
| 73 |
#' @param update Whether to update all packages before the check. |
| 74 |
#' @param src The path of the source package directory. |
| 75 |
#' @param src_dir The parent directory of the source package directory. This can |
| 76 |
#' be set in a global option if all your source packages are under a common |
| 77 |
#' parent directory. |
| 78 |
#' @param timeout Timeout in seconds for \command{R CMD check} to check each |
| 79 |
#' package. The (approximate) total time can be limited by the global option |
| 80 |
#' `xfun.rev_check.timeout_total`. |
| 81 |
#' @return A named numeric vector with the names being package names of reverse |
| 82 |
#' dependencies; `0` indicates check success, `1` indicates failure, and `2` |
| 83 |
#' indicates that a package was not checked due to global timeout. |
| 84 |
#' @seealso `devtools::revdep_check()` is more sophisticated, but currently has |
| 85 |
#' a few major issues that affect me: (1) It always deletes the |
| 86 |
#' \file{*.Rcheck} directories |
| 87 |
#' (<https://github.com/r-lib/devtools/issues/1395>), which makes it difficult |
| 88 |
#' to know more information about the failures; (2) It does not fully install |
| 89 |
#' the source package before checking its reverse dependencies |
| 90 |
#' (<https://github.com/r-lib/devtools/pull/1397>); (3) I feel it is fairly |
| 91 |
#' difficult to iterate the check (ignore the successful packages and only |
| 92 |
#' check the failed packages); by comparison, `xfun::rev_check()` only |
| 93 |
#' requires you to run a short command repeatedly (failed packages are |
| 94 |
#' indicated by the existing \file{*.Rcheck} directories, and automatically |
| 95 |
#' checked again the next time). |
| 96 |
#' |
| 97 |
#' `xfun::rev_check()` borrowed a very nice feature from |
| 98 |
#' `devtools::revdep_check()`: estimating and displaying the remaining time. |
| 99 |
#' This is particularly useful for packages with huge numbers of reverse |
| 100 |
#' dependencies. |
| 101 |
#' @export |
| 102 |
rev_check = function( |
| 103 |
pkg, which = 'all', recheck = NULL, ignore = NULL, update = TRUE, |
| 104 |
timeout = getOption('xfun.rev_check.timeout', 15 * 60), |
| 105 |
src = file.path(src_dir, pkg), src_dir = getOption('xfun.rev_check.src_dir') |
| 106 |
) { |
| 107 |
if (length(src) != 1 || !dir_exists(src)) stop( |
| 108 |
'The package source dir (the "src" argument) must be an existing directory' |
| 109 |
) |
| 110 |
|
| 111 |
message('Installing the source package ', src) |
| 112 |
install_dir(path.expand(src)) |
| 113 |
|
| 114 |
db = available.packages(type = 'source') |
| 115 |
|
| 116 |
# install packages that are not loadable (testing in parallel) |
| 117 |
p_install = function(pkgs) { |
| 118 |
pkgs_up = NULL |
| 119 |
if (update) { |
| 120 |
message('Updating all R packages...') |
| 121 |
pkgs_up = intersect(old.packages(checkBuilt = TRUE)[, 'Package'], pkgs) |
| 122 |
pkg_install(pkgs_up) |
| 123 |
} |
| 124 |
pkgs = setdiff(pkgs, pkgs_up) # don't install pkgs that were just updated |
| 125 |
print(system.time( |
| 126 |
pkg_install(unlist(plapply(pkgs, function(p) if (!loadable(p, new_session = TRUE)) p))) |
| 127 |
)) |
| 128 |
} |
| 129 |
|
| 130 |
unlink('*.Rcheck2', recursive = TRUE) |
| 131 |
if (missing(recheck)) { |
| 132 |
dirs = list.files('.', '.+[.]Rcheck$') |
| 133 |
pkgs = gsub('.Rcheck$', '', dirs) |
| 134 |
recheck = if (length(pkgs) == 0 && file_exists('recheck')) { |
| 135 |
scan('recheck', 'character') |
| 136 |
} else pkgs |
| 137 |
} |
| 138 |
pkgs = if (length(recheck)) { |
| 139 |
p_install(pkg_dep(recheck, db, which = 'all')) |
| 140 |
recheck |
| 141 |
} else { |
| 142 |
res = check_deps(pkg, db, which) |
| 143 |
message('Installing dependencies of reverse dependencies') |
| 144 |
res$install = setdiff(res$install, ignore_deps()) |
| 145 |
print(system.time(p_install(res$install))) |
| 146 |
res$check |
| 147 |
} |
| 148 |
pkgs = intersect(pkgs, rownames(db)) # make sure the pkgs are on CRAN |
| 149 |
|
| 150 |
lib_cran = './library-cran' |
| 151 |
on.exit(unlink(lib_cran, recursive = TRUE), add = TRUE) |
| 152 |
dir.create(lib_cran, showWarnings = FALSE) |
| 153 |
pkg_install(pkg, lib = lib_cran) # the CRAN version of the package |
| 154 |
|
| 155 |
f = tempfile('check-done', fileext = '.rds') |
| 156 |
l = tempfile('check-lock'); on.exit(unlink(c(f, l)), add = TRUE) |
| 157 |
n = length(pkgs) |
| 158 |
if (n == 0) { |
| 159 |
message('No reverse dependencies to be checked for the package ', pkg); return() |
| 160 |
} |
| 161 |
|
| 162 |
if (missing(ignore) && file_exists('00ignore')) ignore = scan('00ignore', 'character') |
| 163 |
if (length(ignore)) { |
| 164 |
message('Ignoring packages: ', paste(ignore, collapse = ' ')) |
| 165 |
unlink(sprintf('%s.Rcheck', ignore), recursive = TRUE) |
| 166 |
pkgs = setdiff(pkgs, ignore) |
| 167 |
if ((n <- length(pkgs)) == 0) { |
| 168 |
message('No packages left to be checked'); return() |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
message('Downloading tarballs') |
| 173 |
tars = download_tarball(pkgs, db, dir = 'tarball') |
| 174 |
tars = setNames(tars, pkgs) |
| 175 |
|
| 176 |
t0 = Sys.time() |
| 177 |
tt = getOption('xfun.rev_check.timeout_total', Inf) |
| 178 |
message('Checking ', n, ' packages: ', paste(pkgs, collapse = ' ')) |
| 179 |
|
| 180 |
res = plapply(pkgs, function(p) { |
| 181 |
d = sprintf('%s.Rcheck', p) |
| 182 |
if (!p %in% rownames(db)) { |
| 183 |
message('Checking ', p, ' (aborted since it is no longer on CRAN') |
| 184 |
unlink(d, recursive = TRUE) |
| 185 |
return() |
| 186 |
} |
| 187 |
|
| 188 |
timing = function() { |
| 189 |
# in case two packages finish at exactly the same time |
| 190 |
while (file_exists(l)) Sys.sleep(.1) |
| 191 |
file.create(l); on.exit(unlink(l), add = TRUE) |
| 192 |
done = c(if (file_exists(f)) readRDS(f), p) |
| 193 |
saveRDS(done, f) |
| 194 |
n2 = length(setdiff(pkgs, done)) # remaining packages |
| 195 |
t1 = Sys.time(); t2 = Sys.time() + n2 * (t1 - t0) / (n - n2) |
| 196 |
message( |
| 197 |
'Packages remaining: ', n2, '/', n, '; Expect to finish at ', t2, |
| 198 |
' (', format(round(difftime(t2, t1))), ')' |
| 199 |
) |
| 200 |
# 0 (FALSE): success; 1: failure |
| 201 |
setNames(as.integer(dir_exists(d)), p) |
| 202 |
} |
| 203 |
|
| 204 |
if (!file_exists(z <- tars[p])) { |
| 205 |
dir.create(d, showWarnings = FALSE) |
| 206 |
return(timing()) |
| 207 |
} |
| 208 |
|
| 209 |
# timeout; package not checked |
| 210 |
if (is_timeout(t0, tt)) return(setNames(2L, p)) |
| 211 |
|
| 212 |
check_it = function(args = NULL, ...) { |
| 213 |
system2( |
| 214 |
file.path(R.home('bin'), 'R'), |
| 215 |
c(args, 'CMD', 'check', '--no-manual', shQuote(z)), |
| 216 |
stdout = FALSE, stderr = FALSE, timeout = timeout, ... |
| 217 |
) |
| 218 |
} |
| 219 |
check_it() |
| 220 |
|
| 221 |
if (!clean_Rcheck(d)) { |
| 222 |
if (!dir_exists(d)) {dir.create(d); return(timing())} |
| 223 |
# try to install missing LaTeX packages for vignettes if possible, then recheck |
| 224 |
vigs = list.files( |
| 225 |
file.path(d, 'vign_test', p, 'vignettes'), '[.](Rnw|Rmd)$', |
| 226 |
ignore.case = TRUE, full.names = TRUE |
| 227 |
) |
| 228 |
pkg_load2('tinytex') |
| 229 |
if (length(vigs) && any(file_exists(with_ext(vigs, 'log')))) { |
| 230 |
if (tinytex::is_tinytex()) for (vig in vigs) in_dir(dirname(vig), { |
| 231 |
Rscript(shQuote(c('-e', 'if (grepl("[.]Rnw$", f <- commandArgs(T), ignore.case = T)) knitr::knit2pdf(f) else rmarkdown::render(f)', basename(vig)))) |
| 232 |
}) |
| 233 |
check_it() |
| 234 |
if (clean_Rcheck(d)) return(timing()) |
| 235 |
} |
| 236 |
# if there are still missing LaTeX packages, install them and recheck |
| 237 |
l0 = tinytex::tl_pkgs() |
| 238 |
lapply( |
| 239 |
list.files(d, '[.]log$', full.names = TRUE, recursive = TRUE), |
| 240 |
tinytex::parse_install, quiet = TRUE |
| 241 |
) |
| 242 |
if (!identical(l0, tinytex::tl_pkgs())) { |
| 243 |
check_it() |
| 244 |
if (clean_Rcheck(d)) return(timing()) |
| 245 |
} |
| 246 |
# clean up the check log, and recheck with the current CRAN version of pkg |
| 247 |
cleanup = function() in_dir(d, { |
| 248 |
clean_log() |
| 249 |
# so that I can easily preview it in the Finder on macOS |
| 250 |
file_exists('00install.out') && file.rename('00install.out', '00install.log') |
| 251 |
}) |
| 252 |
# ignore vignettes that failed to build for unknown reasons |
| 253 |
cleanup() |
| 254 |
if (clean_Rcheck(d)) return(timing()) |
| 255 |
# whether to check the package against the CRAN version? |
| 256 |
if (!getOption('xfun.rev_check.compare', TRUE)) return(timing()) |
| 257 |
file.rename(d, d2 <- paste0(d, '2')) |
| 258 |
check_it('--no-environ', env = tweak_r_libs(lib_cran)) |
| 259 |
if (!dir_exists(d)) file.rename(d2, d) else { |
| 260 |
cleanup() |
| 261 |
if (identical_logs(c(d, d2))) unlink(c(d, d2), recursive = TRUE) |
| 262 |
} |
| 263 |
} |
| 264 |
timing() |
| 265 |
}) |
| 266 |
if (getOption('xfun.rev_check.summary', FALSE)) { |
| 267 |
html = compare_Rcheck(); if (isTRUE(grepl('[.]html$', html))) browseURL(html) |
| 268 |
} |
| 269 |
unlist(res) |
| 270 |
} |
| 271 |
|
| 272 |
# remove the OK lines in the check log |
| 273 |
clean_log = function() { |
| 274 |
if (!file_exists(l <- '00check.log')) return() |
| 275 |
x = grep('^[*].+OK$', read_utf8(l), invert = TRUE, value = TRUE) |
| 276 |
# don't want diffs in random tempdir/tempfile paths when comparing check logs |
| 277 |
x[grep(dirname(tempdir()), x, fixed = TRUE)] = 'RANDOM TEMPDIR/TEMPFILE PATH DELETED' |
| 278 |
# delete the download progress |
| 279 |
x = grep('^\\s*\\[\\d+%] Downloaded \\d+ bytes...\\s*$', x, invert = TRUE, value = TRUE) |
| 280 |
# delete lines of the form "address 0x1067143eb, cause 'illegal opcode'" |
| 281 |
x = grep("address 0x[[:xdigit:]]+, cause '[^']+'", x, invert = TRUE, value = TRUE) |
| 282 |
# delete lines of the form "* current time: 2026-07-07 14:49:46 UTC" |
| 283 |
x = grep('^[*] current time: ', x, invert = TRUE, value = TRUE) |
| 284 |
x = recheck_vig(x) |
| 285 |
x = tail(x, -2) |
| 286 |
writeLines(x, l) # remove the first 2 lines (log dir name and R version) |
| 287 |
x |
| 288 |
} |
| 289 |
|
| 290 |
# sometimes R CMD check fails to build vignettes for unknown reasons; try to |
| 291 |
# recheck the package in this case |
| 292 |
recheck_vig = function(x) { |
| 293 |
if (!any(i1 <- (x == '* checking re-building of vignette outputs ... WARNING'))) |
| 294 |
return(x) |
| 295 |
|
| 296 |
i1 = which(i1)[1] |
| 297 |
i2 = which(x == 'Execution halted') |
| 298 |
i2 = i2[i2 > i1] |
| 299 |
if (length(i2) == 0) return(x) |
| 300 |
|
| 301 |
i3 = grep('^[*] checking ', x) # next checking item |
| 302 |
i3 = i3[i3 > i1] |
| 303 |
if (length(i3)) { |
| 304 |
i2 = i2[i2 < i3[1]] # 'Execution halted' needs to appear before next '* checking' |
| 305 |
if (length(i2) == 0) return(x) |
| 306 |
} |
| 307 |
|
| 308 |
# if no explicit errors were found in processing vignettes (except pandoc |
| 309 |
# error), remove the relevant log |
| 310 |
i2 = tail(i2, 1) |
| 311 |
if (length(grep('pandoc document conversion failed with error', x[i1:i2])) > 0 || |
| 312 |
length(grep('Error: processing vignette .+ failed with diagnostics:', x[i1:i2])) == 0) |
| 313 |
x = x[-(i1:i2)] |
| 314 |
x |
| 315 |
} |
| 316 |
|
| 317 |
# are the check logs identical under a series of *.Rcheck directories? |
| 318 |
identical_logs = function(dirs) { |
| 319 |
if (length(dirs) < 2) return(FALSE) |
| 320 |
if (!all(file_exists(logs <- file.path(dirs, '00check.log')))) return(FALSE) |
| 321 |
x = read_utf8(logs[1]) |
| 322 |
for (i in 2:length(dirs)) if (!identical(x, read_utf8(logs[i]))) return(FALSE) |
| 323 |
TRUE |
| 324 |
} |
| 325 |
|
| 326 |
# delete files/dirs that are usually not helpful |
| 327 |
clean_Rcheck2 = function(dir = '.') { |
| 328 |
owd = setwd(dir); on.exit(setwd(owd), add = TRUE) |
| 329 |
ds = list.files('.', '.+[.]Rcheck$') |
| 330 |
for (d in c(ds, paste0(ds, '2'))) { |
| 331 |
f1 = list.files(d, full.names = TRUE) |
| 332 |
f2 = file.path(d, c('00_pkg_src', '00check.log', '00install.log'), fsep = '/') |
| 333 |
unlink(setdiff(f1, f2), recursive = TRUE) |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
# add a new library path to R_LIBS_USER |
| 338 |
tweak_r_libs = function(new) { |
| 339 |
x = read_all(existing_files(c('~/.Renviron', '.Renviron'))) |
| 340 |
x = grep('^\\s*#', x, invert = TRUE, value = TRUE) |
| 341 |
x = gsub('^\\s+|\\s+$', '', x) |
| 342 |
x = x[x != ''] |
| 343 |
i = grep('^R_LIBS_USER=.+', x) |
| 344 |
if (length(i)) { |
| 345 |
x[i[1]] = sub('(="?)', path_sep('\\1', new), x[i[1]]) |
| 346 |
x |
| 347 |
} else { |
| 348 |
v = Sys.getenv('R_LIBS_USER') |
| 349 |
v = if (v == '') new else path_sep(new, v) |
| 350 |
c(paste0('R_LIBS_USER=', v), x) |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
# separate paths by the path separator on a specific platform |
| 355 |
path_sep = function(...) paste(..., sep = .Platform$path.sep) |
| 356 |
|
| 357 |
# a shorthand of tools::package_dependencies() |
| 358 |
pkg_dep = function(x, ...) { |
| 359 |
if (length(x)) unique(unlist(tools::package_dependencies(x, ...))) |
| 360 |
} |
| 361 |
|
| 362 |
# calculate the packages required to check a package |
| 363 |
check_deps = function(x, db = available.packages(), which = 'all') { |
| 364 |
if (identical(which, 'hard')) which = c('Depends', 'Imports', 'LinkingTo') |
| 365 |
x0 = db[, 'Package'] # all available packages |
| 366 |
# packages that reverse depend on me |
| 367 |
x1 = pkg_dep(x, db, which, reverse = TRUE) |
| 368 |
x1 = intersect(x1, x0) |
| 369 |
# only check a sample of soft reverse dependencies (useful if there are too many) |
| 370 |
if (identical(which, 'all') && (n <- getOption('xfun.rev_check.sample', 100)) >= 0) { |
| 371 |
x2 = pkg_dep(x, db, c('Suggests', 'Enhances'), reverse = TRUE) |
| 372 |
x2 = intersect(x2, x0) |
| 373 |
if (n < length(x2)) x1 = c(setdiff(x1, x2), sample(x2, n)) |
| 374 |
} |
| 375 |
# to R CMD check x1, I have to install all their dependencies |
| 376 |
x2 = pkg_dep(x1, db, 'all') |
| 377 |
# and for those dependencies, I have to install the default dependencies |
| 378 |
x3 = pkg_dep(x2, db, recursive = TRUE) |
| 379 |
list(check = x1, install = intersect(c(x1, x2, x3), x0)) |
| 380 |
} |
| 381 |
|
| 382 |
#' Submit check jobs to crandalf |
| 383 |
#' |
| 384 |
#' Check the reverse dependencies of a package using the crandalf service: |
| 385 |
#' <https://github.com/yihui/crandalf>. If the number of reverse |
| 386 |
#' dependencies is large, they will be split into batches and pushed to crandalf |
| 387 |
#' one by one. |
| 388 |
#' |
| 389 |
#' Due to the time limit of a single job on GitHub Actions (6 hours), you will |
| 390 |
#' have to split the large number of reverse dependencies into batches and check |
| 391 |
#' them sequentially on GitHub (at most 5 jobs in parallel). The function |
| 392 |
#' `crandalf_check()` does this automatically when necessary. It requires |
| 393 |
#' the \command{git} command to be available. |
| 394 |
#' |
| 395 |
#' The function `crandalf_results()` fetches check results from GitHub |
| 396 |
#' after all checks are completed, merge the results, and show a full summary of |
| 397 |
#' check results. It requires `gh` (GitHub CLI: |
| 398 |
#' <https://cli.github.com/manual/>) to be installed and you also need to |
| 399 |
#' authenticate with your GitHub account beforehand. |
| 400 |
#' @param pkg The package name of which the reverse dependencies are to be |
| 401 |
#' checked. |
| 402 |
#' @param size The number of reverse dependencies to be checked in each job. |
| 403 |
#' @param jobs The number of jobs to run in GitHub Actions (by default, all jobs |
| 404 |
#' are submitted, but you can choose to submit the first few jobs). |
| 405 |
#' @param which The type of dependencies (see [rev_check()]). |
| 406 |
#' @export |
| 407 |
crandalf_check = function(pkg, size = 400, jobs = Inf, which = 'all') { |
| 408 |
git_test_branch() |
| 409 |
git_co('main') |
| 410 |
on.exit(git_co('main'), add = TRUE) |
| 411 |
git_test_branch() |
| 412 |
|
| 413 |
# do everything inside the check-pkg branch |
| 414 |
b = paste0('check-', pkg) |
| 415 |
if (git_co(b, stderr = FALSE) != 0) { |
| 416 |
git_co(c('-b', b)) |
| 417 |
file.create('recheck') |
| 418 |
git(c('add', 'recheck')) |
| 419 |
git(c('commit', '-m', paste('Revcheck', pkg))) |
| 420 |
git('push') |
| 421 |
message( |
| 422 |
'Please create a pull request from the branch ', b, |
| 423 |
' on GitHub and re-run xfun::crandalf_check("', pkg, '").' |
| 424 |
) |
| 425 |
return(invisible()) |
| 426 |
} |
| 427 |
git(c('merge', 'main')) |
| 428 |
|
| 429 |
x = check_deps(pkg, which = which)$check |
| 430 |
n = length(x) |
| 431 |
if (n <= size) { |
| 432 |
message('No need to split ', n, ' reverse dependencies into batches of size ', size, '.') |
| 433 |
if (any(grepl('Your branch is ahead of ', git('status', stdout = TRUE)))) { |
| 434 |
git('push') |
| 435 |
} else if (Sys.which('gh') != '') { |
| 436 |
gh(c('workflow', 'run', 'rev-check.yaml', '--ref', b)) |
| 437 |
message('Triggering rev-check.yaml job against ', b, ' branch in crandalf repo on GitHub.') |
| 438 |
} else { |
| 439 |
message('Remember to re-run the last job for the package ', pkg, ' on GitHub.') |
| 440 |
} |
| 441 |
return(invisible()) |
| 442 |
} |
| 443 |
|
| 444 |
b = ceiling(n/size) |
| 445 |
i = rep(seq_len(b), each = size)[seq_len(n)] |
| 446 |
k = 1 |
| 447 |
# use an id in the commit so that I know which jobs are for the same pkg |
| 448 |
id = format(Sys.time(), '%Y%m%d%H%M') |
| 449 |
for (p in head(split(x, i), jobs)) { |
| 450 |
message('Batch ', k) |
| 451 |
writeLines(p, 'recheck') |
| 452 |
git(c('add', 'recheck')) |
| 453 |
git(c('commit', '-m', paste( |
| 454 |
c(id, 'checking:', head(p, 3), '...'), collapse = ' ' |
| 455 |
))) |
| 456 |
git('push') |
| 457 |
Sys.sleep(10) |
| 458 |
k = k + 1 |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
#' @param repo The crandalf repo on GitHub (of the form `user/repo` such as |
| 463 |
#' `"yihui/crandalf"`). Usually you do not need to specify it, unless you |
| 464 |
#' are not calling this function inside the crandalf project, because |
| 465 |
#' \command{gh} should be able to figure out the repo automatically. |
| 466 |
#' @param limit The maximum of records for \command{gh run list} to retrieve. |
| 467 |
#' You only need a larger number if the check results are very early in the |
| 468 |
#' GitHub Action history. |
| 469 |
#' @param wait Number of seconds to wait if not all jobs have been completed on |
| 470 |
#' GitHub. By default, this function checks the status every 5 minutes until |
| 471 |
#' all jobs are completed. Set `wait` to 0 to disable waiting (and throw |
| 472 |
#' an error immediately when any jobs are not completed). |
| 473 |
#' @rdname crandalf_check |
| 474 |
#' @export |
| 475 |
crandalf_results = function(pkg, repo = NA, limit = 200, wait = 5 * 60) { |
| 476 |
res = crandalf_jobs(pkg, repo, limit) |
| 477 |
if (NROW(res) == 0) { |
| 478 |
stop('Did not find check results for ', pkg, ' from GitHub Actions.') |
| 479 |
} |
| 480 |
if (any(res[, 1] != 'completed')) { |
| 481 |
if (wait <= 0) stop('Please wait till all jobs have been completed on GitHub Actions.') |
| 482 |
status = NULL |
| 483 |
repeat { |
| 484 |
res = crandalf_jobs(pkg, repo, limit) |
| 485 |
if (all(res[, 1] == 'completed')) break |
| 486 |
if (is.null(status) || !identical(status, table(res[, 1]))) { |
| 487 |
status = table(res[, 1]) |
| 488 |
timestamp() |
| 489 |
print(status) |
| 490 |
} |
| 491 |
Sys.sleep(wait) |
| 492 |
} |
| 493 |
} |
| 494 |
ids = grep_sub('^(\\d+) checking: .+', '\\1', res[, 3]) |
| 495 |
i = if (length(ids) > 0) grep(sprintf('^%s checking: ', ids[1]), res[, 3]) else { |
| 496 |
head(which(res[, 2] == 'failure'), 1) |
| 497 |
} |
| 498 |
res = res[i, , drop = FALSE] |
| 499 |
res = res[res[, 2] == 'failure', , drop = FALSE] |
| 500 |
if (NROW(res) == 0) { |
| 501 |
stop('Did not find any failed results on GitHub Actions.') |
| 502 |
} |
| 503 |
for (i in seq_len(nrow(res))) { |
| 504 |
message('Downloading check results (', i, '/', nrow(res), ')') |
| 505 |
gh_run('download', res[i, 7], '-D', tempfile('crandalf-', '.'), repo = repo) |
| 506 |
} |
| 507 |
if (interactive()) browseURL(crandalf_merge(pkg)) |
| 508 |
} |
| 509 |
|
| 510 |
# retrieve the first N jobs info |
| 511 |
crandalf_jobs = function(pkg, repo = NA, limit = 200) { |
| 512 |
res = gh_run('list', '-L', limit, '-w', 'rev-check', repo = repo) |
| 513 |
res = res[grep(paste0('rev-check\tcheck-', pkg), res)] |
| 514 |
do.call(rbind, strsplit(res, '\t')) |
| 515 |
} |
| 516 |
|
| 517 |
crandalf_merge = function(pkg) { |
| 518 |
unlink(list.files('.', '[.]Rcheck2?$'), recursive = TRUE) |
| 519 |
x1 = x2 = x3 = NULL |
| 520 |
f1 = '00check_diffs.html'; f3 = 'latex.txt' |
| 521 |
for (d in list.files('.', '^crandalf-.+')) { |
| 522 |
if (!dir_exists(d)) next |
| 523 |
p = file.path(d, 'macOS-rev-check-results') |
| 524 |
if (file_exists(f <- file.path(p, f1))) { |
| 525 |
x = read_utf8(f) |
| 526 |
x1 = if (length(x1) == 0) x else { |
| 527 |
i1 = grep('<body>', x)[1] |
| 528 |
i2 = tail(grep('</body>', x), 1) |
| 529 |
i3 = tail(grep('</body>', x1), 1) |
| 530 |
append(x1, x[(i1 + 1):(i2 - 1)], i3 - 1) |
| 531 |
} |
| 532 |
file.remove(f) |
| 533 |
} |
| 534 |
if (file_exists(f <- file.path(p, 'recheck2'))) { |
| 535 |
x2 = c(x2, read_utf8(f)) |
| 536 |
file.remove(f) |
| 537 |
} |
| 538 |
cs = list.files(p, '[.]Rcheck[2]?$', full.names = TRUE) |
| 539 |
file.rename(cs, basename(cs)) |
| 540 |
if (file_exists(f <- file.path(p, f3))) { |
| 541 |
x3 = c(x3, read_utf8(f)) |
| 542 |
file.remove(f) |
| 543 |
} |
| 544 |
unlink(d, recursive = TRUE) |
| 545 |
} |
| 546 |
write_utf8(x1, f1) # the full summary |
| 547 |
|
| 548 |
# store newly detected missing latex packages in latex.txt and commit/push |
| 549 |
git_co('main') |
| 550 |
append_unique(x3, f3) |
| 551 |
find_missing_latex() |
| 552 |
git(c('commit', '-m', 'add more latex packages', f3)) |
| 553 |
git('push') |
| 554 |
|
| 555 |
git_co(paste0('check-', pkg)) |
| 556 |
r = '[.]Rcheck2$' |
| 557 |
write_utf8(sort(unique(c(x2, gsub(r, '', list.files('.', r))))), 'recheck') |
| 558 |
f1 |
| 559 |
} |
| 560 |
|
| 561 |
mc_cores = function(name = 'mc.cores') getOption(name, parallel::detectCores()) |
| 562 |
|
| 563 |
# mclapply() with a different default for mc.cores and disable prescheduling |
| 564 |
plapply = function(X, FUN, ...) { |
| 565 |
parallel::mclapply(X, FUN, ..., mc.cores = mc_cores(), mc.preschedule = FALSE) |
| 566 |
} |
| 567 |
|
| 568 |
# download the source package from CRAN |
| 569 |
download_tarball = function(p, db = available.packages(type = 'source'), dir = '.', retry = 3) { |
| 570 |
if (!dir_exists(dir)) dir.create(dir, recursive = TRUE) |
| 571 |
z = file.path(dir, sprintf('%s_%s.tar.gz', p, db[p, 'Version'])) |
| 572 |
parallel::mcmapply(function(p, z) { |
| 573 |
# remove other versions of the package tarball |
| 574 |
unlink(setdiff(list.files(dir, sprintf('^%s_.+.tar.gz', p), full.names = TRUE), z)) |
| 575 |
u = paste(db[p, 'Repository'], basename(z), sep = '/') |
| 576 |
for (i in seq_len(retry)) { |
| 577 |
if (file_exists(z)) break |
| 578 |
try(download.file(u, z, quiet = TRUE, mode = 'wb')) |
| 579 |
} |
| 580 |
}, p, z, SIMPLIFY = FALSE, mc.cores = mc_cores('xfun.rev_check.download_cores')) |
| 581 |
z |
| 582 |
} |
| 583 |
|
| 584 |
# clean up *.Rcheck if there are no warnings, errors, or notes in the log |
| 585 |
clean_Rcheck = function(dir, log = read_utf8(file.path(dir, '00check.log'))) { |
| 586 |
# do not check the status line |
| 587 |
if (length(grep('^Status: ', tail(log, 1)))) log = head(log, -1) |
| 588 |
if (length(grep('(WARNING|ERROR|NOTE)$', log)) == 0 || |
| 589 |
length(grep('[*] checking whether package .+ can be installed ... ERROR', log))) |
| 590 |
unlink(dir, recursive = TRUE) |
| 591 |
!dir_exists(dir) |
| 592 |
} |
| 593 |
|
| 594 |
#' @rdname rev_check |
| 595 |
#' @param status_only If `TRUE`, only compare the final statuses of the |
| 596 |
#' checks (the last line of \file{00check.log}), and delete \file{*.Rcheck} |
| 597 |
#' and \file{*.Rcheck2} if the statuses are identical, otherwise write out the |
| 598 |
#' full diffs of the logs. If `FALSE`, compare the full logs under |
| 599 |
#' \file{*.Rcheck} and \file{*.Rcheck2}. |
| 600 |
#' @param output The output Markdown file to which the diffs in check logs will |
| 601 |
#' be written. If the \pkg{markdown} package is available, the Markdown file |
| 602 |
#' will be converted to HTML, so you can see the diffs more clearly. |
| 603 |
#' @export |
| 604 |
compare_Rcheck = function(status_only = TRUE, output = '00check_diffs.md') { |
| 605 |
if (length(dirs <- list.files('.', '.+[.]Rcheck$')) == 0) { |
| 606 |
# clean up the `recheck` file |
| 607 |
if (file_exists('recheck')) writeLines(character(), 'recheck') |
| 608 |
return() |
| 609 |
} |
| 610 |
d2 = function(d) c(d, paste0(d, '2')) |
| 611 |
logs = function(d) file.path(d2(d), '00check.log') |
| 612 |
res = NULL |
| 613 |
if (!status_only && Sys.which('diff') == '') |
| 614 |
warning("The command 'diff' is not available; will not calculate exact diffs in logs.") |
| 615 |
for (d in dirs) { |
| 616 |
f = existing_files(logs(d)) |
| 617 |
if (status_only && length(f) == 2) { |
| 618 |
status_line = function(file) { |
| 619 |
x = tail(read_utf8(file), 1) |
| 620 |
if (grepl('^Status: ', x)) x else { |
| 621 |
warning('The last line of ', file, ' is not the status.') |
| 622 |
NULL |
| 623 |
} |
| 624 |
} |
| 625 |
# if the check with current CRAN version of package also failed, or the |
| 626 |
# two statues are the same, chances are we are good to go |
| 627 |
s1 = status_line(f[1]) |
| 628 |
if (length(grep('Status: .*\\d+ ERROR', s1)) || identical(s1, status_line(f[2]))) { |
| 629 |
unlink(d2(d), recursive = TRUE); next |
| 630 |
} |
| 631 |
} |
| 632 |
res = c( |
| 633 |
res, paste('##', p <- sans_ext(d)), '', |
| 634 |
sprintf('[CRAN version](https://cran.rstudio.com/package=%s) (-) vs current version (+):\n', p), |
| 635 |
'```diff', file_diff(f), '```', '' |
| 636 |
) |
| 637 |
if (length(res2 <- cran_check_page(p, NULL))) res = c( |
| 638 |
res, 'CRAN check logs:\n\n```', head_tail(unique(unlist(strsplit(res2, '\n')))), '```\n' |
| 639 |
) |
| 640 |
} |
| 641 |
if (length(res) == 0) return() |
| 642 |
res = c('---', 'title: xfun::rev_check() results', '---', '', res) |
| 643 |
xfun::write_utf8(res, output) |
| 644 |
if (!loadable('litedown')) return(output) |
| 645 |
html_file = litedown::mark(output, text = res) |
| 646 |
if (!getOption('xfun.rev_check.keep_md', FALSE)) unlink(output) |
| 647 |
html_file |
| 648 |
} |
| 649 |
|
| 650 |
# keep the first and last n elements in x, and omit the middle |
| 651 |
head_tail = function(x, n = 10) { |
| 652 |
if (length(x) <= 2 * n) return(x) |
| 653 |
c(head(x, n), '....', tail(x, n)) |
| 654 |
} |
| 655 |
|
| 656 |
# compute the diffs of two files; if diffs too large, dedup them |
| 657 |
file_diff = function(files, len = 200, use_diff = Sys.which('diff') != '') { |
| 658 |
n = length(files) |
| 659 |
if (n == 0) return() |
| 660 |
if (n == 1) { |
| 661 |
f = tempfile(); on.exit(unlink(f), add = TRUE); file.create(f) |
| 662 |
files = c(f, files) |
| 663 |
} |
| 664 |
d = if (use_diff) { |
| 665 |
suppressWarnings(system2('diff', shQuote(files), stdout = TRUE)) |
| 666 |
} else { |
| 667 |
c(paste('<', read_utf8(files[1])), '---', paste('>', read_utf8(files[2]))) |
| 668 |
} |
| 669 |
if (length(d) >= len) d = unique(d) |
| 670 |
gsub('^>', '+', gsub('^<', '-', d)) |
| 671 |
} |
| 672 |
|
| 673 |
# specify a list of package names to be ignored when installing all dependencies |
| 674 |
ignore_deps = function() { |
| 675 |
if (file_exists('00ignore_deps')) scan('00ignore_deps', 'character') |
| 676 |
} |
| 677 |
|
| 678 |
# download a check summary of a package from CRAN |
| 679 |
cran_check_page = function(pkg, con = '00check-cran.log') { |
| 680 |
u = sprintf('https://cran.rstudio.com/web/checks/check_results_%s.html', pkg) |
| 681 |
x = read_utf8(u) |
| 682 |
if (length(i <- grep('Check Details', x, ignore.case = TRUE)) == 0) return() |
| 683 |
x = x[i[1]:length(x)] |
| 684 |
x = gsub('<[^>]+>', '', x) |
| 685 |
x = gsub(' ', ' ', x) |
| 686 |
x = gsub('>', '>', x) |
| 687 |
x = gsub('<', '<', x) |
| 688 |
x = gsub('\\s+', ' ', x) |
| 689 |
x = paste(trimws(x), collapse = '\n') |
| 690 |
x = gsub('\n\n+', '\n\n', x) |
| 691 |
if (length(con) == 1) writeLines(x, con) else x |
| 692 |
} |
| 693 |
|
| 694 |
# download CRAN check summaries of all failed packages |
| 695 |
cran_check_pages = function() { |
| 696 |
dirs = list.files('.', '[.]Rcheck$') |
| 697 |
for (d in dirs) { |
| 698 |
if (dir_exists(d)) in_dir(d, cran_check_page(gsub('[.]Rcheck$', '', d))) |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
# parse the check log for missing LaTeX packages and install them |
| 703 |
find_missing_latex = function() { |
| 704 |
dirs = list.files('.', '[.]Rcheck2?$') |
| 705 |
pkgs = NULL |
| 706 |
for (d in dirs) { |
| 707 |
if (dir_exists(d)) pkgs = c(pkgs, in_dir( |
| 708 |
d, tinytex::parse_packages('00check.log', quiet = c(TRUE, FALSE, FALSE)) |
| 709 |
)) |
| 710 |
} |
| 711 |
pkgs = unique(pkgs) |
| 712 |
if (file_exists(f <- 'latex.txt')) append_unique(pkgs, f) |
| 713 |
pkgs |
| 714 |
} |