/
ppm.R
313 lines (283 loc) · 9.58 KB
/
ppm.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
#' Returns the current Posit Package Manager (PPM) repository URL
#'
#' @details
#' This URL has the form `{base}/{repo}`, e.g.
#' `https://packagemanager.posit.co/all`.
#'
#' To configure a hosted PPM instance, set the `PKGCACHE_PPM_URL`
#' environment variable to the base URL (e.g.
#' `https://packagemanager.posit.co`).
#'
#' To use [repo_add()] with PPM snapshots, you may also set the
#' `PKGCACHE_PPM_REPO` environment variable to the name of the default
#' repository.
#'
#' On Linux, instead of setting these environment variables, you can also
#' add a PPM repository to the `repos` option, see [base::options()].
#' If the environment variables are not set, then `ppm_repo_url()` will
#' try to extract the PPM base URL and repository name from this option.
#'
#' If the `PKGCACHE_PPM_URL` environment variable is not set, and the
#' `repos` option does not contain a PPM URL (on Linux), then pkgcache
#' uses the public PPM instance at `https://packagemanager.posit.co`, with
#' the `cran` repository.
#'
#' @return String scalar, the repository URL of the configured PPM
#' instance. If no PPM instance is configured, then the URL of the Posit
#' Public Package Manager instance. It includes the repository name, e.g.
#' `https://packagemanager.posit.co/all`.
#' @export
#' @seealso The 'pkgcache and Posit Package Manager on Linux'
#' article at <`r pkgdown_url()`>.
#' @seealso [repo_resolve()] and [repo_add()] to find and configure PPM
#' snapshots.
#' @family PPM functions
#' @examples
#' ppm_repo_url()
ppm_repo_url <- function() {
# New env vars first
ppm_env <- Sys.getenv("PKGCACHE_PPM_URL")
if (ppm_env != "") {
return(paste0(
ppm_env,
"/",
Sys.getenv("PKGCACHE_PPM_REPO", "cran")
))
}
# Old env var
rspm_env <- Sys.getenv("PKGCACHE_RSPM_URL")
if (rspm_env != "") {
return(rspm_env)
}
# If getOption("repos") has a PPM URL, then use that
repos <- as.character(getOption("repos"))
ppm <- is_ppm_linux_repo_url(repos)
if (any(ppm)) {
mch <- re_match(repos[ppm][1], re_ppm_linux())
return(paste0(mch$base, mch$repo))
}
# Otherwise default
"https://packagemanager.posit.co/cran"
}
get_ppm_base_url <- function() {
dirname(ppm_repo_url())
}
#' List all available Posit Package Manager (PPM) snapshots
#'
#' @details
#' The repository URL of a snapshot has the following form on Windows:
#' ```
#' {base}/{repo}/{id}
#' ```
#' where `{base}` is the base URL for PPM (see [ppm_repo_url()]) and
#' `{id}` is either the date or id of the snapshot, or `latest` for
#' the latest snapshot. E.g. these are equivalent:
#' ```
#' https://packagemanager.posit.co/cran/5
#' https://packagemanager.posit.co/cran/2017-10-10
#' ```
#'
#' On a Linux distribution that has PPM support, the repository URL that
#' contains the binary packages looks like this:
#' ```
#' {base}/{repo}/__linux__/{binary_url}/{id}
#' ```
#' where `{id}` is as before, and `{binary_url}` is a code name for a release
#' of a supported Linux distribution. See the `binary_url` column of the
#' result of [ppm_platforms()] for these code names.
#'
#' @return Data frame with two columns:
#' - `date`: the time the snapshot was taken, a `POSIXct` vector,
#' - `id`: integer id of the snapshot, this can be used in the repository
#' URL.
#'
#' @seealso The 'pkgcache and Posit Package Manager on Linux'
#' article at <`r pkgdown_url()`>.
#' @family PPM functions
#' @export
#' @examplesIf !pkgcache:::is_rcmd_check()
#' ppm_snapshots()
ppm_snapshots <- function() {
snp <- synchronise(async_get_ppm_versions(forget = TRUE))
data_frame(
date = parse_iso_8601(names(snp)),
id = as.integer(unname(snp))
)
}
async_get_ppm_versions <- function(forget = FALSE, date = NULL) {
tmp1 <- tempfile()
def <- if (forget ||
(!is.null(date) && date < names(pkgenv$ppm_versions[1])) ||
(!is.null(date) && date > last(names(pkgenv$ppm_versions)))) {
url <- Sys.getenv(
"PKGCACHE_PPM_TRANSACTIONS_URL",
paste0(get_ppm_base_url(), "/__api__/sources/1/transactions?_limit=10000")
)
tmp <- tempfile()
download_file(url, tmp1)$
then(function(res) {
resp <- jsonlite::fromJSON(tmp1, simplifyVector = FALSE)
vrs <- structure(
vcapply(resp, function(x) as.character(x$id)),
names = vcapply(resp, function(x) as.character(x$published_to))
)
pkgenv$ppm_versions <- vrs[order(as.Date(names(vrs)))]
})$
catch(error = function(err) {
warning("Failed to download PPM versions")
})
} else {
async_constant()
}
def$
finally(function() unlink(tmp1))$
then(function() pkgenv$ppm_versions)
}
#' List all platforms supported by Posit Package Manager (PPM)
#'
#' @return Data frame with columns:
#' - `name`: platform name, this is essentially an identifier,
#' - `os`: operating system, `linux`, `windows` or `macOS` currently,
#' - `binary_url`: the URL segment of the binary repository URL of this
#' platform, see [ppm_snapshots()].
#' - `distribution`: for Linux platforms the name of the distribution,
#' - `release`: for Linux platforms, the name of the release,
#' - `binaries`: whether PPM builds binaries for this platform.
#'
#' @seealso The 'pkgcache and Posit Package Manager on Linux'
#' article at <`r pkgdown_url()`>.
#' @family PPM functions
#' @export
#' @examplesIf !pkgcache:::is_rcmd_check()
#' ppm_platforms()
ppm_platforms <- function() {
plt <- synchronise(async_get_ppm_status(forget = TRUE))$distros
plt$binary_url[plt$binary_url == ""] <- NA_character_
as_data_frame(plt)
}
async_get_ppm_status <- function(forget = FALSE, distribution = NULL,
release = NULL, r_version = NULL) {
tmp2 <- tempfile()
# is this a known distro?
known <- if (is.null(distribution)) {
TRUE
} else if (is.null(release)) {
distribution %in% pkgenv$ppm_distros_cached$distribution
} else {
mch <- which(
distribution == pkgenv$ppm_distros_cached$distribution &
release == pkgenv$ppm_distros_cached$release
)
!is.na(mch)
}
rver_known <- if (is.null(r_version)) {
TRUE
} else {
r_version <- get_minor_r_version(r_version)
r_version %in% pkgenv$ppm_r_versions_cached
}
# can we used the cached values? Only if
# * not a forced update, and
# * distro is known, or we already updated.
# * r_Version is known, or we already updated
updated <- !is.null(pkgenv$ppm_distros)
cached <- !forget && (known || updated) && (rver_known || updated)
def <- if (isTRUE(cached)) {
pkgenv$ppm_distros <- pkgenv$ppm_distros_cached
pkgenv$ppm_r_versions <- pkgenv$ppm_r_versions_cached
async_constant()
} else {
url <- Sys.getenv(
"PKGCACHE_PPM_STATUS_URL",
paste0(get_ppm_base_url(), "/__api__/status")
)
download_file(url, tmp2)$
then(function(res) {
stat <- jsonlite::fromJSON(tmp2, simplifyVector = FALSE)
dst <- data.frame(
stringsAsFactors = FALSE,
name = vcapply(stat$distros, "[[", "name"),
os = vcapply(stat$distros, "[[", "os"),
binary_url = vcapply(stat$distros, "[[", "binaryURL"),
distribution = vcapply(stat$distros, "[[", "distribution"),
release = vcapply(stat$distros, "[[", "release"),
binaries = vlapply(stat$distros, "[[", "binaries")
)
pkgenv$ppm_distros <- dst
pkgenv$ppm_distros_cached <- dst
rvers <- unlist(stat$r_versions)
pkgenv$ppm_r_versions <- rvers
pkgenv$ppm_r_versions_cached <- rvers
})$
catch(error = function(err) {
warning("Failed to download PPM status")
})
}
def$
finally(function() unlink(tmp2))$
then(function() {
list(
distros = pkgenv$ppm_distros,
r_versions = pkgenv$ppm_r_versions
)
})
}
#' Does PPM build binary packages for the current platform?
#'
#' @return `TRUE` or `FALSE`.
#'
#' @export
#' @seealso The 'pkgcache and Posit Package Manager on Linux'
#' article at <`r pkgdown_url()`>.
#' @family PPM functions
#' @examplesIf !pkgcache:::is_rcmd_check()
#' current_r_platform()
#' ppm_has_binaries()
ppm_has_binaries <- function() {
current <- current_r_platform_data()
binaries <-
(! tolower(Sys.getenv("PKGCACHE_PPM_BINARIES")) %in% c("no", "false", "0", "off")) &&
current$cpu == "x86_64" &&
(current$os == "mingw32" || grepl("linux", current$os))
if (!binaries) return(FALSE)
current_rver <- get_minor_r_version(getRversion())
synchronise(async_get_ppm_status(
distribution = current$distribution,
release = current$release,
r_version = current_rver
))
distros <- pkgenv$ppm_distros
rver <- pkgenv$ppm_r_versions
if (current$os == "mingw32") {
binaries <- binaries &&
"windows" %in% distros$os &&
all(distros$binaries[distros$os == "windows"]) &&
current_rver %in% rver
} else {
mch <- which(
distros$distribution == current$distribution &
distros$release == current$release
)
binaries <- binaries &&
length(mch) == 1 &&
distros$binaries[mch] &&
current_rver %in% rver
}
binaries
}
#' List all R versions supported by Posit Package Manager (PPM)
#'
#' @return Data frame with columns:
#' - `r_version`: minor R versions, i.e. version numbers containing the
#' first two components of R versions supported by this PPM instance.
#'
#' @seealso The 'pkgcache and Posit Package Manager on Linux'
#' article at <`r pkgdown_url()`>.
#' @family PPM functions
#' @export
#' @examplesIf !pkgcache:::is_rcmd_check()
#' ppm_r_versions()
ppm_r_versions <- function() {
plt <- synchronise(async_get_ppm_status(forget = TRUE))$r_versions
data_frame(r_version = plt)
}