Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Proof of concept implementation for local package caching #268

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions R/type-local.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,34 @@ parse_remote_local <- function(specs, config, ...) {
resolve_remote_local <- function(remote, direct, config, cache,
dependencies, ...) {

# prepare a default resolution
sources <- paste0("file://", normalizePath(remote$path, mustWork = FALSE))
resolve_from_description(remote$path, sources, remote, direct,
res <- resolve_from_description(remote$path, sources, remote, direct,
config, cache, dependencies[[2 - direct]])

# save the source as the remote url
res$metadata[["RemoteUrl"]] <- sources[[1]]

# collect the list of local package files
pkg_files <- list.files(
remote$path[[1]],
full.names = TRUE,
recursive = TRUE,
# this will exclude hidden files, but maybe
# some package rely on them?
all.files = FALSE
)

# save the md5sum
# TODO: make one sum out of it?
# TODO: use sha256 and digest?
if (length(pkg_files) > 0) {
md5sum <- paste0(tools::md5sum(pkg_files), collapse = ".")
res$extra[[1]][["remotemd5sum"]] <- md5sum
res$metadata[["RemoteMD5Sum"]] <- md5sum
}

res
}

download_remote_local <- function(resolution, target, target_tree, config,
Expand All @@ -45,7 +70,36 @@ download_remote_local <- function(resolution, target, target_tree, config,
}

satisfy_remote_local <- function(resolution, candidate, config, ...) {
## TODO: we can probably do better than this
## 1. package name must match
if (resolution$package != candidate$package) {
return(structure(FALSE, reason = "Package names differ"))
}

## 2. installed from the same identical local source is good
if (candidate$type == "installed") {
want_reinst <- is_true_param(resolution$params[[1]], "reinstall")
if (want_reinst) {
return(structure(FALSE, reason = "Re-install requested"))
}
# check if the file path matches
candidate_url <- candidate$extra[[1]][["remoteurl"]] %||% NA_character_
local_url <- resolution$sources[[1]]
if (!identical(candidate_url, local_url)) {
return(structure(FALSE, reason = "Installed package path mismatch"))
}

# check if the md5sum maches
candidate_md5 <- candidate$extra[[1]][["remotemd5sum"]] %||% NA_character_
local_md5 <- resolution$extra[[1]][["remotemd5sum"]] %||% NA_character_
if (!identical(candidate_md5, local_md5)) {
return(structure(FALSE, reason = "Installed package md5sum mismatch"))
}

# it's good!
return(TRUE)
}

## 3. no other candidate works
FALSE
}

Expand Down
10 changes: 6 additions & 4 deletions tests/testthat/test-type-local.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ test_that("download_remote error", {
expect_true(all(dl$download_status == "Failed"))
})

test_that("satisfy", {
## Always FALSE, independently of arguments
expect_false(satisfy_remote_local())
})
# remove this test for now
#
# test_that("satisfy", {
# ## Always FALSE, independently of arguments
# expect_false(satisfy_remote_local())
# })