-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.R
253 lines (222 loc) · 8.41 KB
/
script.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
# Introduction:
# The `revdepcheck` package uses the `crancache::install_packages()` function for package installation. This function, in turn, utilizes `utils::install.packages()` and incorporates an additional caching mechanism for efficiency.
#
# Current Limitations:
# Modifying this behavior would require changes to the source code, which is currently not feasible. There is an existing issue on this topic (https://github.com/r-lib/revdepcheck/issues/187), but it's unlikely to be addressed in the near future.
#
# Proposed Solution:
# The proposed solution is to integrate with the existing workflow by creating a CRAN-like repository. This repository will contain packages from `pak`'s cache, making them accessible via the default `install.packages()` function.
#
# Design Overview:
# 1. Create a CRAN-like repository using the `miniCRAN` package.
# 2. Install the target package and its reverse dependencies using the `pak` package.
# 3. If a package is not available on the default CRAN (e.g., it's hosted on GitHub), retrieve the cached binaries and add them to the miniCRAN repository.
# 4. Run `revdepcheck::revdep_check()` as usual to check reverse dependencies.
catnl <- function(x = "") cat(sprintf("%s\n", x))
if_error <- function(x, y = NULL) {
res <- try(x, silent = TRUE)
if (is(res, "try-error")) {
return(y)
} else {
return(res)
}
}
`%||%` <- function(x, y) {
if (!length(x) || is.null(x)) y else x
}
check_if_pkg_available <- function(pkg, ver = NULL) {
length(
available.packages(
filters = list(
add = TRUE,
function(db) {
if (is.null(ver)) {
db[db[, "Package"] == pkg, ]
} else {
db[db[, "Package"] == pkg & db[, "Version"] == ver, ]
}
}
)
)
) > 0
}
add_to_minicran <- function(x, minicran_path) {
temp_dir <- tempfile()
on.exit(unlink(temp_dir))
dir.create(temp_dir)
# for GH packages we have `pkg_1.2.3.9000_hash.tar.gz` so we need to remove the hash and keep only `pkg_1.2.3.9000.tar.gz`
new_file_name <- gsub("(.*?_.*)_.*?(\\..*)", "\\1\\2", basename(x))
file.copy(x, file.path(temp_dir, new_file_name))
miniCRAN::addLocalPackage(gsub("_.*", "", basename(new_file_name)), temp_dir, minicran_path)
invisible(NULL)
}
add_cache_to_minicran <- function(pkg, version, minicran_path) {
i_cache <- pkgcache::pkg_cache_find(package = pkg, version = version, platform = "source")
if (nrow(i_cache) == 0) return(invisible(NULL))
cli::cli_inform(sprintf("Adding %s to miniCRAN...", pkg))
add_to_minicran(i_cache$fullpath[1], minicran_path)
invisible(NULL)
}
install_and_add_to_minicran <- function(ref, minicran_path) {
cli::cli_inform(sprintf("Installing and adding %s to miniCRAN...", ref))
x <- pak::pkg_install(ref, ask = FALSE)
for (i in seq_len(nrow(x))) {
i_package <- x$package[i]
i_version <- x$version[i]
if (check_if_pkg_available(i_package, i_version)) next
add_cache_to_minicran(i_package, i_version, minicran_path)
}
invisible(NULL)
}
is_installed_from_gh <- function(package_name) {
d <- packageDescription(package_name)
(!is.null(d$GithubSHA1)) || identical(d$RemoteType, "github")
}
args <- commandArgs(trailingOnly = TRUE)
setwd(normalizePath(file.path(args[1])))
number_of_workers <- as.integer(args[2])
timeout <- as.integer(args[3])
# Install required packages
catnl("Installing required packages...")
if (!requireNamespace("pak", quietly = TRUE)) {
install.packages("pak", quiet = TRUE)
}
if (!requireNamespace("pkgcache", quietly = TRUE)) {
install.packages("pkgcache", quiet = TRUE)
}
options(
repos = c(
PPM = pkgcache::repo_resolve("PPM@latest"),
getOption("repos")
)
)
pak::pkg_install(c(
"cli",
"miniCRAN",
"pkgbuild",
"pkgdepends",
"r-lib/revdepcheck",
"usethis",
"yaml"
), ask = FALSE)
# Read config file
cli::cli_h1("Configuration")
cli::cli_progress_bar()
cli::cli_progress_step("Reading `.revdeprefs.yaml` config file...")
if (!file.exists(".revdeprefs.yaml")) {
cli::cli_inform("Missing `.revdeprefs.yaml` file.")
cli::cli_inform("This indicates all reverse dependencies from CRAN.")
refs <- character(0L)
} else {
refs <- yaml::read_yaml(".revdeprefs.yaml")
if (length(refs) == 0) {
cli::cli_inform("Empty `.revdeprefs.yaml` file")
cli::cli_inform("This indicates all reverse dependencies from CRAN.")
}
if (!is.character(refs) && !is.null(refs)) {
cli::cli_abort("Unknown structure of `.revdeprefs.yaml` file. Returning.")
return(NULL)
}
}
cli::cli_progress_done()
cli::cli_inform("References used:")
cli::cli_bullets(refs)
# init
cli::cli_h1("Initiate pre-requisites")
cli::cli_progress_bar()
## miniCRAN
cli::cli_progress_step("Initiating `miniCRAN`...")
minicran_path <- tempfile()
dir.create(minicran_path)
on.exit(unlink(minicran_path, recursive = TRUE))
# added `rlang` as a dummy package as the `pkgs` arg cannot be empty
miniCRAN::makeRepo(pkgs = "rlang", path = minicran_path, type = c("source", .Platform$pkgType))
# add minicran repo path to repos so that revdepcheck can use it
# this is the directory where we will store packages from config file
options("repos" = c(
"minicran" = paste0("file:///", minicran_path),
getOption("repos")
))
### install pkg - only if not available
pkg_name <- read.dcf("DESCRIPTION")[, "Package"]
if (isFALSE(check_if_pkg_available(pkg_name))) {
cli::cli_progress_step("Adding GH release to miniCRAN...")
# try to get the package reference from the DESCRIPTION file (URL field)
pkg_url <- gsub("\n|/$", "", strsplit(read.dcf("DESCRIPTION")[1, "URL"], ",")[[1]])
pkg_url_gh <- grep("github.com", pkg_url, value = TRUE)
pkg_ref_released <- paste0(gsub(".*github.com/", "", pkg_url_gh), "@*release")
if (length(pkg_ref_released) == 0) {
cli::cli_abort("Unable to automatically determine the package reference for GitHub release.")
return(NULL)
}
install_and_add_to_minicran(pkg_ref_released, minicran_path)
}
### add pkgs from GH to miniCRAN
cli::cli_progress_step("Adding packages from GH to miniCRAN...")
x <- pkgdepends::new_pkg_deps("deps::.")
x$resolve()
pkgs_from_gh <- Filter(is_installed_from_gh, Filter(function(x) x %in% rownames(installed.packages()), unique(x$get_resolution()$package)))
for (pkg in pkgs_from_gh) {
add_cache_to_minicran(pkg, installed.packages()[pkg, "Version"], minicran_path)
}
## revdepcheck
cli::cli_progress_step("Initiating `revdepcheck`...")
revdepcheck::revdep_reset()
unlink("./revdep/", recursive = TRUE)
revdepcheck:::db_disconnect(".")
usethis::use_revdep()
revdepcheck:::db_setup(".")
cli::cli_progress_done()
cli::cli_h1("Add refs to revdepcheck")
# include refs in revdepcheck
## Add refs revdepcheck and also to miniCRAN so that it can be found from there
cli::cli_progress_bar("Adding refs to revdepcheck", total = length(refs))
for (ref in refs) {
cli::cli_progress_message("Adding {ref}...")
install_and_add_to_minicran(ref, minicran_path)
ref_pkg <- pkgdepends::parse_pkg_ref(ref)$package
revdepcheck::revdep_add(packages = ref_pkg)
cli::cli_inform("Added {ref} to revdep todo!")
cli::cli_progress_update()
}
cli::cli_progress_done()
cli::cli_inform("All references added!")
cli::cli_inform("The current revdep todo (empty indicates the default - all revdeps):")
print(revdepcheck::revdep_todo())
# Execute
cli::cli_h1("Execute revdepcheck")
revdepcheck::revdep_check(num_workers = number_of_workers, timeout = timeout)
# Print results
cli::cli_h1("Summary")
print(revdepcheck::revdep_summary())
for (revdep in revdepcheck::revdep_todo()$package) {
cli::cli_h2(sprintf("Summary for: %s", revdep))
if_error(print(revdepcheck::revdep_details(revdep = revdep)))
}
cli::cli_h2("revdep/README.md")
catnl(readLines("revdep/README.md", warn = FALSE))
cli::cli_h2("revdep/problems.md")
catnl(readLines("revdep/problems.md", warn = FALSE))
cli::cli_h2("revdep/failures.md")
catnl(readLines("revdep/failures.md", warn = FALSE))
cli::cli_h2("revdep/cran.md")
catnl(readLines("revdep/cran.md", warn = FALSE))
cli::cli_h2("Check duration...")
# this does not include download and install times
if (length(revdepcheck::revdep_summary())) {
print(
setNames(
do.call(
rbind.data.frame,
lapply(
revdepcheck::revdep_summary(),
function(i) c(i$package, if_error(i$old[[1]]$duration) %||% "?", if_error(i$new$duration) %||% "?")
)
),
c("package", "old", "new")
)
)
} else {
print("(empty)")
}
stopifnot(identical(readLines("revdep/problems.md", warn = FALSE), "*Wow, no problems at all. :)*"))