Skip to content

Commit

Permalink
Commit accumulated changes to assertions and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
torfason committed Mar 28, 2024
1 parent ad8361e commit c3c9a08
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
^\.Rproj\.user$
^LICENSE\.md$
^README\.Rmd$
^scratch$
^gitpins$
^\.github$
^.lintr$
^gitpins_.*$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
.DS_Store

# Manual additions
/scratch/
/gitpins/
/gitpins_*/
9 changes: 9 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
linters: linters_with_defaults(
infix_spaces_linter = NULL, # 40
line_length_linter = NULL, # 13
spaces_inside_linter = NULL, # 9
object_name_linter = NULL # 5
)
exclusions: list(
"man/*.Rd"
)
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: gitpins
Type: Package
Title: Pin urls to local file and version with git
Version: 0.1.3
Version: 0.1.3.9001
Author: Magnus Thor Torfason
Maintainer: Magnus Thor Torfason <[email protected]>
Description: Pin URLs to local file and version the pins with git.
Expand All @@ -16,7 +16,8 @@ Imports:
digest,
gert,
here,
jsonlite
jsonlite,
checkmate
Suggests:
tibble,
testthat (>= 3.0.0),
Expand All @@ -25,6 +26,6 @@ Suggests:
Depends:
R (>= 4.1.0)
Config/testthat/edition: 3
RoxygenNote: 7.1.2
RoxygenNote: 7.2.3
URL: https://github.com/torfason/gitpins
BugReports: https://github.com/torfason/gitpins/issues
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
export(gitpin)
export(list_pins)
export(pin)
importFrom(checkmate,assert_flag)
importFrom(checkmate,assert_int)
importFrom(checkmate,assert_number)
importFrom(checkmate,assert_posixct)
importFrom(checkmate,assert_string)
6 changes: 6 additions & 0 deletions R/0_gitpins-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
#' To get started, check out the help for the [gitpin()] function, or take a
#' look at the info on GitHub (see links below).
#'
#' @importFrom checkmate assert_string
#' @importFrom checkmate assert_number
#' @importFrom checkmate assert_flag
#' @importFrom checkmate assert_int
#' @importFrom checkmate assert_posixct
#'
#' @docType package
#' @name gitpins
#' @md
Expand Down
51 changes: 29 additions & 22 deletions R/gitpin.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
#' Convert date-time variable to sub-millisecond timestamp
#'
#' @param the_datetime a date variable
#' @return a character representation of `the_datetime` as a sub-millisecond timestamp
#' @return a character representation of `the_datetime` as a sub-millisecond
#' timestamp
#' @md
#' @keywords internal
tstamp <- function(the_datetime) {
strftime(the_datetime , "%Y-%m-%d %H:%M:%OS5")
assert_posixct(the_datetime)

strftime(the_datetime, "%Y-%m-%d %H:%M:%OS5")
}

#' Convert date-time variable to file-compatible timestamp
Expand All @@ -19,7 +22,9 @@ tstamp <- function(the_datetime) {
#' @md
#' @keywords internal
fstamp <- function(the_datetime) {
strftime(the_datetime , "%Y-%m-%d.%H%M%S")
assert_posixct(the_datetime)

strftime(the_datetime, "%Y-%m-%d.%H%M%S")
}

#' Initialize gitpins repository
Expand Down Expand Up @@ -53,7 +58,8 @@ init_gitpins <- function() {
#' @md
#' @export
pin <- function(url, refresh_hours=12) {
#url <- "https://raw.githubusercontent.com/vincentarelbundock/Rdatasets/master/csv/boot/acme.csv"
assert_string(url)
assert_number(refresh_hours)

# Default frequency of downloads, configurable with config_gitpins()
# could be to redownload if not the same day as the last download.
Expand All @@ -64,16 +70,17 @@ pin <- function(url, refresh_hours=12) {
stopifnot(!is.null(url) && is.character(url) && length(url)==1)
url_hash <- digest::digest(url)
timestamp <- Sys.time()
destfile_data <- file.path(.globals$repo,paste0(url_hash,".data"))
destfile_temp <- file.path(.globals$repo,paste0(url_hash,".temp"))
on.exit(if(file.exists(destfile_temp))file.remove(destfile_temp))
destfile_info <- file.path(.globals$repo,paste0(url_hash,".info"))
destfile_json <- file.path(.globals$repo,paste0(url_hash,".json"))
destfile_data <- file.path(.globals$repo, paste0(url_hash, ".data"))
destfile_temp <- file.path(.globals$repo, paste0(url_hash, ".temp"))
on.exit(if (file.exists(destfile_temp)) file.remove(destfile_temp)) # nolint
destfile_json <- file.path(.globals$repo, paste0(url_hash, ".json"))

# Read some metadata, to determine what to do next
if ( file.exists(destfile_json) ) {
meta_last <- readLines(destfile_json) |> jsonlite::fromJSON()
delta_hours <- as.double(difftime(timestamp, as.POSIXct(meta_last$timestamp), units="hours"))
delta_hours <- as.double(difftime(timestamp,
as.POSIXct(meta_last$timestamp),
units="hours"))
if ( delta_hours < refresh_hours ) {
recent_version_found <- TRUE
}
Expand All @@ -84,16 +91,16 @@ pin <- function(url, refresh_hours=12) {

# Do the download and determine next steps
if ( recent_version_found ) {
message(paste("Recent version found, using it ..."))
message(paste("pin() found recent version, using it ..."))
} else {

tryCatch(
{ dl_result <- curl::curl_download(url, destfile_temp, quiet=TRUE) },
error=function(e) {},
warning=function(e) {}
{ curl::curl_download(url, destfile_temp, quiet=TRUE) },
error=function(e) {}, # nolint
warning=function(e) {} # nolint
)

if ( !file.exists(destfile_temp) || file.size(destfile_temp)==0) {
if ( !file.exists(destfile_temp) || file.size(destfile_temp)==0 ) {
# Download failed in some way
if (!file.exists(destfile_data)) {
stop("Download failed and no earlier version found: Aborting!")
Expand All @@ -106,13 +113,12 @@ pin <- function(url, refresh_hours=12) {
# Download succeeded
message("Downloaded fresh version ...")
file.copy(destfile_temp, destfile_data, overwrite = TRUE)
#writeLines(c(timestamp, url), destfile_info)
list(timestamp=jsonlite::unbox(tstamp(timestamp)), url=jsonlite::unbox(url)) |>
jsonlite::toJSON(pretty = TRUE, simplifyVector=TRUE) |>
writeLines(destfile_json)

gert::git_add(
basename(c(destfile_data,destfile_json)), # Must be relative to repo root
basename(c(destfile_data, destfile_json)), # Must be relative to repo root
repo=.globals$repo)
gert::git_commit_all(paste0("[", tstamp(timestamp), "] ", url), repo=.globals$repo)
}
Expand All @@ -138,6 +144,8 @@ gitpin <- pin
#' @md
#' @export
list_pins <- function(history=FALSE) {
assert_flag(history)

init_gitpins()

# Function to return empty data.frame on fresh repo instead of erroring
Expand All @@ -157,19 +165,19 @@ list_pins <- function(history=FALSE) {
lapply(as.data.frame) |>
do.call(what=rbind)
if (is.null(d.result)) d.result <- data.frame(timestamp=character(), url=character())
d.result <- d.result[order(d.result$timestamp, decreasing=TRUE),]
d.result <- d.result[order(d.result$timestamp, decreasing=TRUE), ]
} else {
# Return result based on what is in the gitlog
logmessages <- get_repo_log_messages()
d.result <- data.frame(
timestamp = gsub('\\[(.*)\\].*','\\1',logmessages),
url = gsub('.*\\] (.*)','\\1',logmessages) |>
timestamp = gsub("\\[(.*)\\].*", "\\1", logmessages),
url = gsub(".*\\] (.*)", "\\1", logmessages) |>
sub(pattern="\\n", replacement="")
)
}

# Be nice and return a tibble if it is available
if (requireNamespace("tibble")){
if (requireNamespace("tibble")) {
d.result <- tibble::as_tibble(d.result)
}
d.result
Expand All @@ -181,4 +189,3 @@ list_pins <- function(history=FALSE) {
clear_old_pins <- function() {
stop("not implemented")
}

2 changes: 1 addition & 1 deletion build/build_and_release_process.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
gpdir <- here::here("gitpins")
if (file.exists(gpdir)) {
file.rename(gpdir, paste0(gpdir,"_",fstamp(Sys.time())))
file.rename(gpdir, paste0(gpdir,"_",gitpins:::fstamp(Sys.time())))
}
devtools::build()
devtools::document()
Expand Down
3 changes: 2 additions & 1 deletion inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CMD
api
borked
deduplication
CMD
3 changes: 2 additions & 1 deletion man/tstamp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions tests/testthat/test-gitpin-local.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@

test_that("gitpins work with local server",{
test_that("gitpins work with local server", {

# Skip test if suggested packages callr and servr are not installed
if ( !requireNamespace("callr", quietly = TRUE) ||
!requireNamespace("servr", quietly = TRUE) ) {
skip()
skip("Packages callr or servr not found, skipping test")
}

# Set here with reference to the test file
here::i_am("tests/testthat/test-gitpin-local.R") |> expect_message("gitpins")

# If here does not get us the path to the local www_root, we panic
if(!file.exists(here::here("tests","www_root"))) stop("Web root not found, skipping test")
if (!file.exists(here::here("tests", "www_root")))
stop("Web root not found, skipping test")

# Function to start a local server.
callr_servr <- function() {
host <- getOption("servr.host", "127.0.0.1")
port <- servr:::random_port()
dir <- here::here("tests","www_root")
dir <- here::here("tests", "www_root")
proc <- callr::r_bg(servr::httd,
args=list(daemon=FALSE, dir=dir, host=host, port=port),
package="servr")
Expand Down Expand Up @@ -75,4 +76,3 @@ test_that("gitpins work with local server",{
"Download failed, using last good version ...")

})

2 changes: 1 addition & 1 deletion tests/testthat/test-gitpin-remote.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

test_that("gitpin works with remote URLs",{
test_that("gitpin works with remote URLs", {

skip_if_offline()

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-list_gitpins.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test_that("listing is in default order", {

# Nohist should be subset of hist
matches <- listing_hist$timestamp %in% listing_nohist$timestamp
listing_hist_subset <- listing_hist[matches,]
listing_hist_subset <- listing_hist[matches, ]
expect_equal(listing_hist_subset, listing_nohist)

})

0 comments on commit c3c9a08

Please sign in to comment.