|
1 | 1 | #!/usr/bin/env Rscript |
2 | | -if (!requireNamespace("revdepcheck")) { |
3 | | - stop('Install revdepcheck: remotes::install_github("r-lib/revdepcheck")') |
4 | | -} |
5 | | -library("revdepcheck") |
6 | | -options(warn = 1L) |
7 | | - |
8 | | -available_cores <- function() { |
9 | | - getenv <- function(name) { |
10 | | - as.integer(Sys.getenv(name, NA_character_)) |
11 | | - } |
12 | | - getopt <- function(name) { |
13 | | - as.integer(getOption(name, NA_integer_)) |
14 | | - } |
15 | | - if (is.finite(n <- getopt("mc.cores") + 1L)) return(n) |
16 | | - if (is.finite(n <- getopt("Ncpus") + 1L)) return(n) |
17 | | - if (is.finite(n <- getenv("PBS_NUM_PPN"))) return(n) |
18 | | - if (is.finite(n <- getenv("SLURM_CPUS_PER_TASK"))) return(n) |
19 | | - if (is.finite(n <- getenv("NSLOTS"))) return(n) |
20 | | - 1L |
21 | | -} |
22 | 2 |
|
23 | 3 | precheck <- function() { |
24 | 4 | ## WORKAROUND: Remove checked pkgs that use file links, which otherwise |
25 | 5 | ## produce warnings which are promoted to errors by revdepcheck. |
26 | 6 | unlink("revdep/checks/aroma.affymetrix", recursive = TRUE) |
27 | 7 | } |
28 | 8 |
|
29 | | -check <- function() { |
30 | | - if (file_test("-f", p <- Sys.getenv("R_CHECK_ENVIRON", "~/.R/check.Renviron"))) { |
31 | | - cat(sprintf("R CMD check will use env vars from %s\n", sQuote(p))) |
32 | | - cat(sprintf("To disable, set 'R_CHECK_ENVIRON=false' (a fake pathname)\n")) |
33 | | - } |
34 | | - |
35 | | - envs <- Sys.getenv() |
36 | | - envs <- envs[grep("^_?R_CHECK_", names(envs))] |
37 | | - if (length(envs) > 0L) { |
38 | | - envs <- sprintf(" %02d. %s=%s", seq_along(envs), names(envs), envs) |
39 | | - envs <- paste(envs, collapse="\n") |
40 | | - cat(sprintf("Detected R-specific env vars that may affect R CMD check:\n%s\n", envs)) |
41 | | - } |
42 | | - |
43 | | - precheck() |
44 | | - revdep_check(bioc = TRUE, num_workers = available_cores(), |
45 | | - timeout = as.difftime(30, units = "mins"), quiet = FALSE) |
46 | | -} |
47 | | - |
48 | | - |
49 | | -todo <- function() { |
50 | | - pkgs <- tryCatch(revdep_todo(), error = function(ex) NA) |
51 | | - if (identical(pkgs, NA)) { |
52 | | - cat("Revdepcheck has not been initiated\n") |
53 | | - return() |
54 | | - } |
55 | | - pkgs <- subset(pkgs, status == "todo") |
56 | | - if (nrow(pkgs) == 0) { |
57 | | - cat("There are no packages on the revdepcheck todo list\n") |
58 | | - } else { |
59 | | - cat(sprintf("%d. %s\n", seq_len(nrow(pkgs)), pkgs$package)) |
60 | | - } |
61 | | -} |
62 | | - |
63 | | -parse_pkgs <- function(pkgs) { |
64 | | - pkgs <- unlist(strsplit(pkgs, split = ",", fixed = TRUE)) |
65 | | - pkgs <- gsub("[ \t'\"‘’]", "", pkgs) |
66 | | - sort(unique(pkgs)) |
67 | | -} |
68 | | - |
69 | | -revdep_init <- function() { |
70 | | - if (!revdepcheck:::db_exists(".")) revdepcheck:::db_setup(".") |
71 | | -} |
72 | | - |
73 | | -revdep_todo_reset <- function() { |
74 | | - revdep_init() |
75 | | - db <- revdepcheck:::db(".") |
76 | | - df <- data.frame(package = character(0L), stringsAsFactors = FALSE) |
77 | | - DBI::dbWriteTable(db, "todo", df, overwrite = TRUE, append = FALSE) |
78 | | -} |
79 | | - |
80 | | -revdep_this_package <- local({ |
81 | | - pkg <- NULL |
82 | | - function() { |
83 | | - if (is.null(pkg)) pkg <<- desc::desc(file = "DESCRIPTION")$get("Package") |
84 | | - pkg |
85 | | - } |
86 | | -}) |
87 | | - |
88 | | -revdep_children <- local({ |
89 | | - cache <- list() |
90 | | - function(pkg = NULL) { |
91 | | - if (is.null(pkg)) pkg <- revdep_this_package() |
92 | | - pkgs <- cache[[pkg]] |
93 | | - if (is.null(pkgs)) { |
94 | | - pkgs <- revdepcheck:::cran_revdeps(pkg) |
95 | | - pkgs <- setdiff(pkgs, pkg) ## WORKAROUND |
96 | | - cache[[pkg]] <- pkgs |
97 | | - } |
98 | | - pkgs |
99 | | - } |
100 | | -}) |
101 | | - |
102 | | -revdep_pkgs_with_status <- function(status = c("error", "failure")) { |
103 | | - status <- match.arg(status) |
104 | | - res <- revdepcheck::revdep_summary() |
105 | | - if (status == "failure") { |
106 | | - names(which(sapply(res, FUN = .subset2, "status") == "E")) |
107 | | - } else if (status == "error") { |
108 | | - field <- switch(status, error = "errors") |
109 | | - has_status <- vapply(res, FUN = function(x) { |
110 | | - z <- x[["new"]][[field]] |
111 | | - is.character(z) && any(nchar(z) > 0) |
112 | | - }, FUN.VALUE = NA, USE.NAMES = TRUE) |
113 | | - has_status <- !is.na(has_status) & has_status |
114 | | - names(has_status)[has_status] |
115 | | - } |
116 | | -} |
117 | | - |
118 | | -revdep_preinstall_libs <- function() { |
119 | | - lib_paths <- .libPaths() |
120 | | - lib_paths[1] <- sprintf("%s-revdepcheck", lib_paths[1]) |
121 | | - dir.create(lib_paths[1], recursive = TRUE, showWarnings = FALSE) |
122 | | - lib_paths |
123 | | -} |
124 | | - |
125 | | -revdep_preinstall <- function(pkgs) { |
126 | | - oopts <- options(Ncpus = available_cores()) |
127 | | - lib_paths_org <- .libPaths() |
128 | | - on.exit({ |
129 | | - .libPaths(lib_paths_org) |
130 | | - options(oopts) |
131 | | - }) |
132 | | - .libPaths(revdep_preinstall_libs()) |
133 | | - |
134 | | - pkgs <- unique(pkgs) |
135 | | - message(sprintf("Triggering crancache builds by pre-installing %d packages: %s", length(pkgs), paste(sQuote(pkgs), collapse = ", "))) |
136 | | - message(".libPaths():") |
137 | | - message(paste(paste0(" - ", .libPaths()), collapse = "\n")) |
138 | | - ## Install one-by-one to update cache sooner |
139 | | - for (kk in seq_along(pkgs)) { |
140 | | - pkg <- pkgs[kk] |
141 | | - message(sprintf("Pre-installing package %d of %d: %s (Ncpus = %d)", |
142 | | - kk, length(pkgs), pkg, getOption("Ncpus", 1L))) |
143 | | - crancache::install_packages(pkg, dependencies = c("Depends", "Imports", "LinkingTo", "Suggests")) |
144 | | - } |
145 | | -} |
146 | | - |
147 | | -revdep_preinstall_update <- function() { |
148 | | - oopts <- options(Ncpus = available_cores()) |
149 | | - lib_paths_org <- .libPaths() |
150 | | - on.exit({ |
151 | | - .libPaths(lib_paths_org) |
152 | | - options(oopts) |
153 | | - }) |
154 | | - .libPaths(revdep_preinstall_libs()) |
155 | | - |
156 | | - message("Update crancache for all pre-installing packages:") |
157 | | - message(".libPaths():") |
158 | | - message(paste(paste0(" - ", .libPaths()), collapse = "\n")) |
159 | | - message(sprintf("Ncpus=%d", getOption("Ncpus", 1L))) |
160 | | - crancache::update_packages(ask = FALSE) |
161 | | -} |
162 | | - |
163 | | - |
164 | | -args <- base::commandArgs(trailingOnly = TRUE) |
165 | | -if ("--reset" %in% args) { |
166 | | - revdep_reset() |
167 | | -} else if ("--todo-reset" %in% args) { |
168 | | - revdep_todo_reset() |
169 | | - todo() |
170 | | -} else if ("--todo" %in% args) { |
171 | | - todo() |
172 | | -} else if ("--add" %in% args) { |
173 | | - pos <- which("--add" == args) |
174 | | - if (pos == length(args)) stop("Missing value for option '--add'") |
175 | | - pkgs <- parse_pkgs(args[seq(from = pos + 1L, to = length(args))]) |
176 | | - revdep_add(packages = pkgs) |
177 | | - todo() |
178 | | -} else if ("--rm" %in% args) { |
179 | | - pos <- which("--rm" == args) |
180 | | - if (pos == length(args)) stop("Missing value for option '--rm'") |
181 | | - pkgs <- parse_pkgs(args[seq(from = pos + 1L, to = length(args))]) |
182 | | - revdep_rm(packages = pkgs) |
183 | | - todo() |
184 | | -} else if ("--add-broken" %in% args) { |
185 | | - revdep_add_broken() |
186 | | - todo() |
187 | | -} else if ("--add-error" %in% args) { |
188 | | -# res <- revepcheck::revdep_summary() |
189 | | - pkgs <- revdep_pkgs_with_status("error") |
190 | | - str(pkgs) |
191 | | - revdep_add(packages = pkgs) |
192 | | - todo() |
193 | | -} else if ("--add-all" %in% args) { |
194 | | - revdep_init() |
195 | | - pkgs <- revdep_children() |
196 | | - for (pkg in pkgs) { |
197 | | - pkgs <- c(pkgs, revdepcheck:::cran_revdeps(pkg)) |
198 | | - } |
199 | | - pkgs <- unique(pkgs) |
200 | | - revdep_add(packages = pkgs) |
201 | | - todo() |
202 | | -} else if ("--add-grandchildren" %in% args) { |
203 | | - revdep_init() |
204 | | - pkgs <- NULL |
205 | | - for (pkg in revdep_children()) { |
206 | | - pkgs <- c(pkgs, revdepcheck:::cran_revdeps(pkg)) |
207 | | - } |
208 | | - pkgs <- unique(pkgs) |
209 | | - revdep_add(packages = pkgs) |
210 | | - todo() |
211 | | -} else if ("--show-check" %in% args) { |
212 | | - pos <- which("--show-check" == args) |
213 | | - if (pos == length(args)) stop("Missing value for option '--show-check") |
214 | | - pkgs <- parse_pkgs(args[seq(from = pos + 1L, to = length(args))]) |
215 | | - for (pkg in pkgs) { |
216 | | - for (dir in c("old", "new")) { |
217 | | - path <- file.path("revdep", "checks", pkg, dir, sprintf("%s.Rcheck", pkg)) |
218 | | - if (!utils::file_test("-d", path)) next |
219 | | - pathname <- file.path(path, "00check.log") |
220 | | - cat("-----------------------------------------------\n") |
221 | | - cat(sprintf("%s (%s):\n", pkg, dir)) |
222 | | - cat("-----------------------------------------------\n") |
223 | | - bfr <- readLines(pathname, warn = FALSE) |
224 | | - tail <- tail(bfr, n = 20L) |
225 | | - writeLines(tail) |
226 | | - } |
227 | | - } |
228 | | -} else if ("--list-children" %in% args) { |
229 | | - pkg <- revdep_this_package() |
230 | | - pkgs <- revdepcheck:::cran_revdeps(pkg) |
231 | | - cat(sprintf("[n=%d] %s\n", length(pkgs), paste(pkgs, collapse = " "))) |
232 | | -} else if ("--list-error" %in% args) { |
233 | | - cat(paste(revdep_pkgs_with_status("error"), collapse = " "), "\n", sep="") |
234 | | -} else if ("--list-failure" %in% args) { |
235 | | - cat(paste(revdep_pkgs_with_status("failure"), collapse = " "), "\n", sep="") |
236 | | -} else if ("--add-error" %in% args) { |
237 | | - revdepcheck::revdep_add(packages = revdep_pkgs_with_status("error")) |
238 | | -} else if ("--add-failure" %in% args) { |
239 | | - revdepcheck::revdep_add(packages = revdep_pkgs_with_status("failure")) |
240 | | -} else if ("--preinstall-update" %in% args) { |
241 | | - revdep_preinstall_update() |
242 | | -} else if ("--preinstall-children" %in% args) { |
243 | | - pkg <- revdep_this_package() |
244 | | - pkgs <- revdepcheck:::cran_revdeps(pkg) |
245 | | - revdep_preinstall(pkgs) |
246 | | -} else if ("--preinstall-error" %in% args) { |
247 | | - res <- revdepcheck::revdep_summary() |
248 | | - revdep_preinstall(revdep_pkgs_with_status("error")) |
249 | | -} else if ("--preinstall-failure" %in% args) { |
250 | | - res <- revdepcheck::revdep_summary() |
251 | | - revdep_preinstall(revdep_pkgs_with_status("failure")) |
252 | | -} else if ("--preinstall-todo" %in% args) { |
253 | | - todo <- revdep_todo() |
254 | | - revdep_preinstall(todo$package) |
255 | | -} else if ("--preinstall" %in% args) { |
256 | | - pos <- which("--preinstall" == args) |
257 | | - if (pos == length(args)) stop("Missing value for option '--preinstall'") |
258 | | - pkgs <- parse_pkgs(args[seq(from = pos + 1L, to = length(args))]) |
259 | | - revdep_preinstall(pkgs) |
260 | | -} else { |
261 | | - stopifnot(length(args) == 0L) |
262 | | - check() |
263 | | - revdep_report(all = TRUE) |
264 | | -} |
| 9 | +revdepcheck.extras::run() |
0 commit comments