Skip to content

Commit

Permalink
Updates for PPM API changes
Browse files Browse the repository at this point in the history
No need to query the snapshots any more, we can
always use the date based snapshots.

Closes #115.
  • Loading branch information
gaborcsardi committed Aug 8, 2024
1 parent 24cdb88 commit a0828a5
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 532 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* `parse_packages()` now does not throw a warning for empty `PACKAGES*`
files (#107).

* `repo_set()` and the `ppm_*()` functions, e.g. `ppm_snapshots()`, now
work again after the PPM API changes (#110, #115).

# pkgcache 2.2.2

* pkgcache now treats R 4.5.x (current R-devel) macOS binaries
Expand Down
438 changes: 0 additions & 438 deletions R/onload.R

Large diffs are not rendered by default.

39 changes: 4 additions & 35 deletions R/ppm.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,45 +107,14 @@ get_ppm_base_url <- function() {
#' ppm_snapshots()

ppm_snapshots <- function() {
snp <- synchronise(async_get_ppm_versions(forget = TRUE))
last <- as.Date(format_iso_8601(Sys.time())) - 1
dts <- seq(as.Date("2017-10-10"), last, by = 1)
data_frame(
date = parse_iso_8601(names(snp)),
id = as.integer(unname(snp))
date = dts,
id = as.character(dts)
)
}

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:
Expand Down
2 changes: 1 addition & 1 deletion R/repo-set.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ repo_sugar_ppm <- function(x, nm) {

# if we may have binaries, then get the distro data as well
synchronise(when_all(
async_get_ppm_versions(date = if (as.character(date) == "latest") NULL else date),
ppm_snapshots(),
if (binaries) {
async_get_ppm_status(
distribution = current$distribution,
Expand Down
39 changes: 15 additions & 24 deletions tests/testthat/_snaps/ppm.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
# ppm_snapshots

Code
ppm_snapshots()
ppm_snapshots()[1:1000, ]
Output
# A data frame: 6 x 2
date id
<dttm> <int>
1 2023-02-20 00:00:00 16856763
2 2023-02-21 00:00:00 16882269
3 2023-02-22 00:00:00 16903694
4 2023-02-24 00:00:00 16958782
5 2023-02-27 00:00:00 17028146
6 2023-02-28 00:00:00 17054670
# A data frame: 1,000 x 2
date id
* <date> <chr>
1 2017-10-10 2017-10-10
2 2017-10-11 2017-10-11
3 2017-10-12 2017-10-12
4 2017-10-13 2017-10-13
5 2017-10-14 2017-10-14
6 2017-10-15 2017-10-15
7 2017-10-16 2017-10-16
8 2017-10-17 2017-10-17
9 2017-10-18 2017-10-18
10 2017-10-19 2017-10-19
# i 990 more rows

# ppm_platforms

Expand All @@ -25,20 +30,6 @@
2 centos8 linux centos8 centos 8 TRUE
3 rhel9 linux rhel9 rockylinux 9 TRUE

# async_get_ppm_versions 2

Code
ret
Output
2021-01-25T00:00:00Z 2021-01-26T00:00:00Z 2021-01-27T00:00:00Z
"997643" "1014755" "1033374"
2021-01-28T00:00:00Z 2021-01-29T00:00:00Z 2021-02-01T00:00:00Z
"1053473" "1069075" "1123445"
2021-02-02T00:00:00Z 2021-02-03T00:00:00Z 2021-02-04T00:00:00Z
"1140568" "1160641" "1175516"
2021-02-05T00:00:00Z
"1194160"

# async_get_ppm_status 2

Code
Expand Down
35 changes: 1 addition & 34 deletions tests/testthat/test-ppm.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ test_that("ppm_snapshots", {
"async_get_ppm_versions",
function(...) async_constant(ver)
)
expect_snapshot(ppm_snapshots())
expect_snapshot(ppm_snapshots()[1:1000,])
})

test_that("ppm_platforms", {
Expand All @@ -57,39 +57,6 @@ test_that("ppm_platforms", {
expect_snapshot(ppm_platforms())
})

test_that("async_get_ppm_versions", {
mockery::stub(
async_get_ppm_versions,
"download_file",
function(...) stop("nope")
)
# uses cache by default
expect_silent(synchronise(async_get_ppm_versions()))

# forget = TRUE forces an update
expect_error(synchronise(async_get_ppm_versions(forget = TRUE)))
})

test_that("async_get_ppm_versions 2", {
withr::local_envvar(
PKGCACHE_PPM_TRANSACTIONS_URL = repo$url("/ppmversions")
)

ret <- synchronise(async_get_ppm_versions(date = "2100-10-10"))
expect_snapshot(ret)
})

test_that("async_get_ppm_versions 3", {
withr::local_envvar(
PKGCACHE_PPM_TRANSACTIONS_URL = repo$url("/bogus")
)

expect_warning(
synchronise(async_get_ppm_versions(date = "2100-10-10")),
"Failed to download PPM versions"
)
})

test_that("async_get_ppm_status", {
mockery::stub(
async_get_ppm_status,
Expand Down

0 comments on commit a0828a5

Please sign in to comment.