-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.R
438 lines (364 loc) · 14.5 KB
/
update.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
dryrun <- FALSE
#' @export
capitalize <- function(x) {
vapply(x, FUN.VALUE = NA_character_, FUN = function(s) {
if (is.na(s) || nchar(s) == 0) return(s)
first <- substr(s, start = 1L, stop = 1L)
tail <- substr(s, start = 2L, stop = nchar(s))
paste0(toupper(first), tail)
})
} ## capitalize()
#' @export
read_cranberries_removed <- local({
data <- NULL
function(rss = "https://dirk.eddelbuettel.com/cranberries/cran/removed/index.rss") {
if (!is.null(data)) return(data)
## Find archived packages and when they were archived
bfr <- readLines(rss)
pattern <- ".*<title>Package ([^ ]+) .*<[/]title>.*"
pkgs <- grep(pattern, bfr, value = TRUE)
pkgs <- sub(pattern, "\\1", pkgs)
pattern <- ".*<pubDate>([^<]+)<[/]pubDate>.*"
dates <- grep(pattern, bfr, value = TRUE)
dates <- sub(pattern, "\\1", dates)
stopifnot(length(pkgs) == length(dates))
tzs <- sub(".* ([[:alpha:]]+)$", "\\1", dates)
tz <- tzs[1]
stopifnot(all(tz == tzs))
timestamps <- strptime(dates, format = "%a, %d %b %Y %H:%M:%S", tz = tz)
db <- data.frame(package = pkgs, archived_on = timestamps)
## Drop old duplicates
db <- db[order(db$archived_on, decreasing = TRUE), ]
db <- subset(db, !duplicated(db$package))
## Sanity check
stopifnot(!any(duplicated(db$package)))
data <<- db
data
}
}) ## read_cranberries_removed()
#' @export
cran_events <- local({
data <- NULL
function(url = "https://cran.r-project.org/src/contrib/PACKAGES.in") {
if (!is.null(data)) return(data)
con <- url(url)
db <- read.dcf(con)
db <- as.data.frame(db)
## Assert assumption about one entry per package
dups <- db$Package[duplicated(db$Package)]
if (length(dups) > 0) {
warning(sprintf("Detected duplicated package entries in %s: [n=%d] %s", url, length(dups), paste(sQuote(dups), collapse = ", ")))
for (pkg in dups) {
idxs <- which(db$Package == pkg)
db_pkg <- lapply(db[idxs,], function(x) {
x <- unique(na.omit(x))
if (length(x) == 0) x <- NA_character_
x
})
db[idxs[1], ] <- db_pkg
db <- db[-idxs[-1], ]
}
stopifnot(!any(duplicated(db$Package)))
}
names <- colnames(db)
names <- tolower(names)
names <- gsub("-", "_", names)
colnames(db) <- names
db_active <- subset(db, !is.na(x_cran_comment))
db_legacy <- subset(db, is.na(x_cran_comment))
## Extract date and type of event
## Note: Be forgiving for typos like "2016-07-1"
pattern <- ".*([[:digit:]]{4,4}-[[:digit:]]{1,2}-[[:digit:]]{1,2}).*"
dates <- rep(as.Date(NA_integer_), times = nrow(db_active))
idxs <- grep(pattern, db_active$x_cran_comment)
values <- sub(pattern, "\\1", db_active$x_cran_comment[idxs])
values <- as.Date(values)
dates[idxs] <- values
db_active$x_cran_comment_date <- dates
pattern <- "^([Uu]narchived?|[Aa]rchived?|[Rr]enamed?|[Oo]rphaned?|[Rr]eplaced?|[Rr]emoved?)"
events <- gsub("[[:blank:]:].*", "", db_active$x_cran_comment)
events <- tolower(events)
events[events == "archive"] <- "archived"
events[!events %in% c("archived", "orphaned", "removed", "renamed", "replaced")] <- NA_character_
db_active$x_cran_comment_event <- events
reasons <- rep(NA_character_, times = nrow(db_active))
idxs <- which(!is.na(events))
values <- gsub("^.* on [[:digit:]-]+[[:blank:]]*(|,|;|:|as|at)([[:blank:]]+|[.])", "", db_active$x_cran_comment[idxs])
## Anonymize email addresses
values <- sub("[[:blank:]]+<[^>@]+@[^>@]+>", "", values)
values <- gsub("[[:blank:]]+", " ", values)
values <- gsub("[\n]+", " ", values)
values <- gsub("(^[[:blank:]]+|[[:blank:].]+$)", "", values)
values <- gsub("[.] , ([A-Z])", ". \\1", values)
values <- gsub(". , ", ", ", values)
values <- gsub("^for ", "", values)
values <- gsub("^the maintainer's ", "maintainer's ", values, fixed = TRUE)
values <- capitalize(values)
reasons[idxs] <- values
db_active$x_cran_comment_reason <- reasons
db_legacy$x_cran_comment_date <- as.Date(NA_integer_)
db_legacy$x_cran_comment_event <- NA_character_
db_legacy$x_cran_comment_reason <- NA_character_
db <- rbind(db_legacy, db_active)
## Anonymize email addresses
values <- db$x_cran_history
values <- sub("[[:blank:]]+<[^>@]+@[^>@]+>", "", values)
db$x_cran_history <- values
cols <- c("package", "x_cran_comment_date", "x_cran_comment_event", "x_cran_comment_reason", "x_cran_comment", "x_cran_history", "replaced_by", "additional_repositories", "maintainer", "license_restricts_use", "systemrequirements", "lazydatacompression", "license_is_foss", "os_type", "url")
db <- db[, cols]
db <- db[order(db$x_cran_comment_date, decreasing = TRUE), ]
data <<- db
}
}) ## cran_events()
#' @export
cran_current <- local({
data <- NULL
function() {
if (!is.null(data)) return(data)
db <- tools:::CRAN_current_db()
tarballs <- rownames(db)
pkgs <- gsub("_.*", "", tarballs)
data <<- cbind(data.frame(package = pkgs, tarball = tarball), db)
}
}) ## cran_current()
#' @export
cran_archived <- local({
data <- NULL
function() {
if (!is.null(data)) return(data)
data <<- tools:::CRAN_archive_db()
}
}) ## cran_archived()
#' @importFrom jsonlite read_json
#' @export
cranhaven_pkgs <- local({
## Manual imports for now
read_json <- jsonlite::read_json
data <- NULL
function(url = "https://raw.githubusercontent.com/cranhaven/cranhaven.r-universe.dev/main/packages.json") {
if (!is.null(data)) return(data)
data <<- read_json(url)
data
}
}) ## cranhaven_pkgs()
cran_pkg_description <- local({
db <- list()
function(pkg) {
desc <- db[[pkg]]
if (!is.null(desc)) return(desc)
url <- sprintf("https://raw.githubusercontent.com/cran/%s/master/DESCRIPTION", pkg)
desc <- tryCatch(read.dcf(url(url)), error = function(ex) list())
db[[pkg]] <<- desc
desc
}
}) ## cran_pkg_description()
cran_pkg_annotations <- function(pkgs) {
desc <- lapply(pkgs, FUN = cran_pkg_description)
desc <- lapply(desc, FUN = function(d) {
res <- data.frame(Package = NA_character_, URL = NA_character_, BugReports = NA_character_, Maintainer = NA_character_)
names <- intersect(colnames(d), names(res))
for (name in names) res[[name]] <- d[, name]
res
})
desc <- do.call(rbind, desc)
# desc$Maintainer <- sub("[[:space:]]*<.*", "", desc$Maintainer)
# desc$Maintainer <- gsub("(^\"|\"$)", "", desc$Maintainer)
colnames(desc) <- tolower(colnames(desc))
colnames(desc)[2:3] <- paste0("package_", colnames(desc)[2:3])
desc
} ## cran_pkg_annotations()
#' @export
call_git <- function(cmd = c("add", "branch", "checkout", "clone", "commit", "push", "rm"), ..., env = character(), stdout = FALSE, stderr = FALSE) {
cmd <- match.arg(cmd)
if (dryrun) return(0L)
args <- c(cmd, ...)
res <- system2("git", args = args, env = env, stdout = stdout, stderr = stderr)
res
} ## call_git()
main <- function() {
## Find archived packages and when they were archived
message("Querying CRANberries removed")
removed <- read_cranberries_removed()
## Get current CRAN packages
message("Querying CRAN for available packages")
cran_pkgs <- unname(available.packages(repos = "https://cloud.r-project.org")[, "Package"])
## Get archived CRAN packages
message("Querying CRAN for archived packages")
cran_archived <- tools:::CRAN_archive_db()
cran_archived_pkgs <- names(cran_archived)
github_repo <- "https://github.com/cranhaven/cranhaven.r-universe.dev"
runiverse_repo <- "https://cranhaven.r-universe.dev"
message("Querying CRAN for package events")
events <- cran_events()
events <- subset(events, package %in% removed$package)
events <- events[, c("package", "x_cran_comment_date", "x_cran_comment_event", "x_cran_comment_reason", "x_cran_history")]
## Packages archived within the last five weeks should be on CRANhaven
cranhaven <- data.frame(package = removed$package, on_cran = (removed$package %in% cran_pkgs), archived_on = removed$archived_on, url = github_repo, branch = file.path("package", removed$package), subdir = removed$package)
cranhaven <- subset(cranhaven, archived_on >= Sys.time() - 5*7*24*3600)
cranhaven <- cranhaven[order(cranhaven$package), ]
cranhaven <- merge(cranhaven, events, by = "package")
cranhaven_all <- cranhaven
message("Number of CRAN packages archived during the last five weeks: ", nrow(cranhaven_all))
cranhaven <- subset(cranhaven_all, !on_cran)
rownames(cranhaven) <- NULL
message("Number of CRAN packages archived during the last five weeks that are still not on CRAN: ", nrow(cranhaven))
stopifnot(!any(duplicated(cranhaven$package)))
## Returns status = 128 if no such branches exist, which is okay
message("Checkout main branch")
res <- call_git("checkout", args = "main")
message("Listing all local or remote package branches")
branches <- call_git("branch", c("--all", "--list", shQuote('*/package/*')), stdout = TRUE, stderr = TRUE)
branches <- sub("^[* ]+.*package/", "package/", branches)
message(sprintf("Branches: [n=%d] %s", length(branches), paste(branches, collapse = ", ")))
message("Adding newly archived packages")
pkgs <- setdiff(cranhaven$package, basename(branches))
message(sprintf(" - packages to add: [n=%d] %s", length(pkgs), paste(pkgs, collapse = ", ")))
failed <- c()
for (kk in seq_along(pkgs)) {
pkg <- pkgs[kk]
message(sprintf("%d/%d Package %s", kk, length(pkgs), sQuote(pkg)))
ee <- match(pkg, cranhaven$package)
entry <- cranhaven[ee, ]
stopifnot(entry$package == pkg)
branch <- entry$branch
message(" - Branch: ", branch)
message(" - Checkout main branch")
res <- call_git("checkout", "main")
## Create empty package branch?
message(" - Create branch")
output <- call_git("checkout", "--orphan", branch, stdout = TRUE, stderr = TRUE)
status <- attr(output, "status")
if (!is.null(status)) {
failed <- c(failed, pkg)
next
}
message(" - Erase branch")
res <- call_git("rm", "-rf", ".")
if (res != 0) {
failed <- c(failed, pkg)
next
}
message(" - Clone package")
url <- paste0("https://github.com/cran/", pkg)
res <- call_git("clone", "--depth=1", url, pkg)
if (res != 0) {
failed <- c(failed, pkg)
next
}
message(" - Prune package")
unlink(file.path(pkg, ".git"), recursive = TRUE, force = TRUE)
res <- call_git("add", pkg)
if (res != 0) {
failed <- c(failed, pkg)
next
}
message(" - Update Additional_repositories")
if (!dryrun) {
field <- "Additional_repositories"
file <- file.path(pkg, "DESCRIPTION")
desc <- desc0 <- read.dcf(file)
if (field %in% colnames(desc)) {
repos <- desc[,field]
if (!grepl(runiverse_repo, repos)) {
repos <- paste(c(repos, runiverse_repo), collapse = ",\n")
desc[,field] <- repos
}
} else {
repos <- matrix(runiverse_repo, ncol = 1L)
colnames(repos) <- field
desc <- cbind(desc, repos)
}
if (!identical(desc, desc0)) {
write.dcf(desc, file = file)
}
}
message(" - Commit package")
when <- entry$archived_on
when <- format(when, format = "%F %T %z")
env <- paste0(c("GIT_AUTHOR_DATE=", "GIT_COMMITTER_DATE="), shQuote(when))
msg <- sprintf("Add %s to CRANhaven, because archived on %s", pkg, when)
output <- call_git("commit", "-a", "-m", shQuote(msg), env = env, stdout = TRUE, stderr = TRUE)
status <- attr(output, "status")
if (!is.null(status)) {
print(status)
print(output)
failed <- c(failed, pkg)
stop("Failed")
next
}
message(" - Push branch")
res <- call_git("push", "--set-upstream", "origin", branch)
if (res != 0) {
failed <- c(failed, pkg)
next
}
message("-- Checkout main branch")
res <- call_git("checkout", "main")
} ## for (kk in ...)
if (length(failed) > 0) {
stop(sprintf("Failed to create branches for %d package(s): %s", length(failed), paste(sQuote(failed), collapse = ", ")))
}
message("Removing expired and unarchived packages")
pkgs <- setdiff(basename(branches), cranhaven$package)
message(sprintf(" - packages to remove: [n=%d] %s", length(pkgs), paste(pkgs, collapse = ", ")))
message(" - Checkout main branch")
res <- call_git("checkout", "main")
failed <- c()
for (kk in seq_along(pkgs)) {
pkg <- pkgs[kk]
message(sprintf("%d/%d Package %s", kk, length(pkgs), sQuote(pkg)))
bb <- match(pkg, basename(branches))
branch <- branches[bb]
message(" - Branch: ", branch)
message(" - Delete remote branch")
res <- call_git("push", "origin", "--delete", branch)
if (res != 0) {
message(" - failed")
failed <- c(failed, pkg)
}
message(" - Delete local branch")
res <- call_git("branch", "-D", branch)
if (res != 0) {
message(" - failed")
failed <- c(failed, pkg)
}
} ## for (kk in ...)
if (length(failed) > 0) {
failed <- unique(failed)
warning(sprintf("Failed to delete branches for %d package(s): %s", length(failed), paste(sQuote(failed), collapse = ", ")), immediate. = TRUE)
}
message("Querying CRAN package descriptions")
annotations <- cran_pkg_annotations(cranhaven$package)
cranhaven <- merge(cranhaven, annotations, by = "package")
## Identify diff
pkgs <- cranhaven$package
pkgs_prev <- vapply(cranhaven_pkgs(), FUN = function(x) x$package, FUN.VALUE = NA_character_)
pkgs_removed <- setdiff(setdiff(pkgs, pkgs_prev), cran_archived_pkgs)
diff <- list(
removed = pkgs_removed,
archived = setdiff(setdiff(pkgs, pkgs_prev), pkgs_removed),
unarchived = intersect(setdiff(pkgs_prev, pkgs), cran_pkgs),
expired = setdiff(setdiff(pkgs_prev, pkgs), cran_pkgs)
)
str(diff)
if (sum(lengths(diff)) > 0) {
msg <- "CRAN updates:"
for (what in names(diff)) {
if (length(diff[[what]]) > 0) {
msg <- c(msg, sprintf("%s %s.", tools::toTitleCase(what), paste(sQuote(diff[[what]]), collapse = ", ")))
}
}
msg <- paste(msg, collapse = " ")
message(msg)
## Write packages.json for R-universe
jsonlite::write_json(cranhaven, "packages.json", pretty = TRUE)
message("packages.json written")
message("Commit packages.json updates")
call_git("commit", "-a", "-m", shQuote(msg))
message("Push updates")
call_git("push")
} else {
message("Nothing changed")
}
} ## main()
main()