-
Notifications
You must be signed in to change notification settings - Fork 152
/
install-bioc.R
304 lines (250 loc) · 8.5 KB
/
install-bioc.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
#' Install a development package from the Bioconductor git repository
#'
#' This function requires `git` to be installed on your system in order to
#' be used.
#'
#' It is vectorised so you can install multiple packages with
#' a single command.
#'
#' This is intended as an aid for Bioconductor developers. If you want to
#' install the release version of a Bioconductor package one can use the
#' `BiocManager` package.
#' @inheritParams install_git
#' @param repo Repository address in the format
#' `[username:password@@][release/]repo[#commit]`. Valid values for
#' the release are \sQuote{devel},
#' \sQuote{release} (the default if none specified), or numeric release
#' numbers (e.g. \sQuote{3.3}).
#' @param mirror The Bioconductor git mirror to use
#' @param ... Other arguments passed on to [utils::install.packages()].
#' @inheritParams install_github
#' @export
#' @family package installation
#' @examples
#' \dontrun{
#' install_bioc("SummarizedExperiment")
#' install_bioc("devel/SummarizedExperiment")
#' install_bioc("3.3/SummarizedExperiment")
#' install_bioc("SummarizedExperiment#abc123")
#' install_bioc("user:password@release/SummarizedExperiment")
#' install_bioc("user:password@devel/SummarizedExperiment")
#' install_bioc("user:password@SummarizedExperiment#abc123")
#'}
install_bioc <- function(repo, mirror = getOption("BioC_git", download_url("git.bioconductor.org/packages")),
git = c("auto", "git2r", "external"),
dependencies = NA,
upgrade = c("default", "ask", "always", "never"),
force = FALSE,
quiet = FALSE,
build = TRUE, build_opts = c("--no-resave-data", "--no-manual", "--no-build-vignettes"),
build_manual = FALSE, build_vignettes = FALSE,
repos = getOption("repos"),
type = getOption("pkgType"),
...) {
remotes <- lapply(repo, bioc_remote, mirror = mirror, git = match.arg(git))
install_remotes(remotes,
dependencies = dependencies,
upgrade = upgrade,
force = force,
quiet = quiet,
build = build,
build_opts = build_opts,
build_manual = build_manual,
build_vignettes = build_vignettes,
repos = repos,
type = type,
...)
}
bioc_remote <- function(repo, mirror = getOption("BioC_git", download_url("git.bioconductor.org/packages")),
git = c("auto", "git2r", "external"), ...) {
git <- match.arg(git)
if (git == "auto") {
git <- if (!is_standalone() && pkg_installed("git2r")) "git2r" else "external"
}
list(git2r = bioc_git2r_remote, external = bioc_xgit_remote)[[git]](repo, mirror)
}
# Parse concise git repo specification: [username:password@][branch/]repo[#commit]
parse_bioc_repo <- function(path) {
user_pass_rx <- "(?:([^:]+):([^:@]+)@)?"
release_rx <- "(?:(devel|release|[0-9.]+)/)?"
repo_rx <- "([^/@#]+)"
commit_rx <- "(?:[#]([a-zA-Z0-9]+))?"
bioc_rx <- sprintf("^(?:%s%s%s%s|(.*))$", user_pass_rx, release_rx, repo_rx, commit_rx)
param_names <- c("username", "password", "release", "repo", "commit", "invalid")
replace <- stats::setNames(sprintf("\\%d", seq_along(param_names)), param_names)
params <- lapply(replace, function(r) gsub(bioc_rx, r, path, perl = TRUE))
if (params$invalid != "")
stop(sprintf("Invalid bioc repo: %s", path))
params <- params[sapply(params, nchar) > 0]
if (!is.null(params$release) && !is.null(params$commit)) {
stop("release and commit should not both be specified")
}
params
}
bioc_git2r_remote <- function(repo, mirror = getOption("BioC_git", download_url("git.bioconductor.org/packages"))) {
meta <- parse_bioc_repo(repo)
branch <- bioconductor_branch(meta$release, meta$sha)
if (!is.null(meta$username) && !is.null(meta$password)) {
meta$credentials <- git2r::cred_user_pass(meta$username, meta$password)
}
remote("bioc_git2r",
mirror = mirror,
repo = meta$repo,
release = meta$release %||% "release",
sha = meta$commit,
branch = branch,
credentials = meta$credentials
)
}
bioc_xgit_remote <- function(repo, mirror = getOption("BioC_git", download_url("git.bioconductor.org/packages"))) {
meta <- parse_bioc_repo(repo)
branch <- bioconductor_branch(meta$release, meta$sha)
if (!is.null(meta$username) && !is.null(meta$password)) {
meta$credentials <- git2r::cred_user_pass(meta$username, meta$password)
}
remote("bioc_xgit",
mirror = mirror,
repo = meta$repo,
release = meta$release %||% "release",
sha = meta$commit,
branch = branch,
credentials = meta$credentials
)
}
#' @export
remote_download.bioc_git2r_remote <- function(x, quiet = FALSE) {
url <- paste0(x$mirror, "/", x$repo)
if (!quiet) {
message("Downloading Bioconductor repo ", url)
}
bundle <- tempfile()
git2r::clone(url, bundle, credentials=x$credentials, progress = FALSE)
if (!is.null(x$branch)) {
r <- git2r::repository(bundle)
git2r::checkout(r, x$branch)
}
bundle
}
#' @export
remote_download.bioc_xgit_remote <- function(x, quiet = FALSE) {
url <- paste0(x$mirror, "/", x$repo)
if (!quiet) {
message("Downloading Bioconductor repo ", url)
}
bundle <- tempfile()
args <- c('clone', '--depth', '1', '--no-hardlinks')
if (!is.null(x$branch) && x$branch != 'HEAD') {
args <- c(args, "--branch", x$branch)
}
args <- c(args, x$args, url, bundle)
git(paste0(args, collapse = " "), quiet = quiet)
bundle
}
#' @export
remote_metadata.bioc_git2r_remote <- function(x, bundle = NULL, source = NULL, sha = NULL) {
url <- paste0(x$mirror, "/", x$repo)
if (!is.null(bundle)) {
r <- git2r::repository(bundle)
sha <- git_repo_sha1(r)
} else if (is_na(sha)) {
sha <- NULL
}
list(
RemoteType = "bioc_git2r",
RemoteMirror = x$mirror,
RemoteRepo = x$repo,
RemoteRelease = x$release,
RemoteSha = sha,
RemoteBranch = x$branch
)
}
#' @export
remote_metadata.bioc_xgit_remote <- function(x, bundle = NULL, source = NULL, sha = NULL) {
if (is_na(sha)) {
sha <- NULL
}
list(
RemoteType = "bioc_xgit",
RemoteMirror = x$mirror,
RemoteRepo = x$repo,
RemoteRelease = x$release,
RemoteSha = sha,
RemoteBranch = x$branch,
RemoteArgs = if (length(x$args) > 0) paste0(deparse(x$args), collapse = " ")
)
}
#' @export
remote_package_name.bioc_git2r_remote <- function(remote, ...) {
remote$repo
}
#' @export
remote_package_name.bioc_xgit_remote <- function(remote, ...) {
remote$repo
}
#' @export
remote_sha.bioc_git2r_remote <- function(remote, ...) {
tryCatch({
url <- paste0(remote$mirror, "/", remote$repo)
res <- git2r::remote_ls(url, credentials=remote$credentials)
found <- grep(pattern = paste0("/", remote$branch), x = names(res))
if (length(found) == 0) {
return(NA_character_)
}
unname(res[found[1]])
}, error = function(e) NA_character_)
}
#' @export
remote_sha.bioc_xgit_remote <- function(remote, ...) {
url <- paste0(remote$mirror, "/", remote$repo)
ref <- remote$branch
refs <- git(paste("ls-remote", url, ref))
refs_df <- read.delim(text = refs, stringsAsFactors = FALSE, sep = "\t",
header = FALSE)
names(refs_df) <- c("sha", "ref")
refs_df$sha[[1]] %||% NA_character_
}
bioconductor_branch <- function(release, sha) {
if (!is.null(sha)) {
sha
} else {
if (is.null(release)) {
release <- Sys.getenv("R_BIOC_VERSION", "release")
}
if (release == "release") {
release <- bioconductor_release()
} else if (release == bioconductor$get_devel_version()) {
release <- "devel"
}
switch(
tolower(release),
devel = "HEAD",
paste0("RELEASE_", gsub("\\.", "_", release))
)
}
}
bioconductor_release <- function() {
tmp <- tempfile()
download(tmp, download_url("bioconductor.org/config.yaml"), quiet = TRUE)
gsub("release_version:[[:space:]]+\"([[:digit:].]+)\"", "\\1",
grep("release_version:", readLines(tmp), value = TRUE))
}
#' @export
format.bioc_git2r_remote <- function(x, ...) {
"Bioc"
}
#' @export
format.bioc_xgit_remote <- function(x, ...) {
"Bioc"
}
# sha of most recent commit
git_repo_sha1 <- function(r) {
rev <- git2r::repository_head(r)
if (is.null(rev)) {
return(NULL)
}
if (git2r::is_commit(rev)) {
rev$sha
} else {
git2r::branch_target(rev)
}
}