Skip to content

Commit a8401d9

Browse files
authored
Merge pull request #32 from Permian-Global-Research/DEV-1
added windows tzdata download helper
2 parents d8a25bc + b5be10a commit a8401d9

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

R/internal-helpers.R

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#' Download the latest IANA timezone database on Windows
2+
#' A helper function taken from https://github.com/apache/arrow/blob/a6f736c962adddd0f7078a10338fe8f1445c583b/python/pyarrow/util.py#L233
3+
#' @return Invisible
4+
#' @details For some reason we have failures on windows relating to the timezone
5+
#' database. This function is a workaround to download the latest IANA timezone
6+
#' database on Windows. It is used in tests and vignettes.
7+
#' @noRd
8+
#' @keywords internal
9+
download_tzdata_on_windows <- function(tzdir = NULL) {
10+
if (.Platform$OS.type != "windows") {
11+
cli::cli_alert_info(
12+
"Timezone database is already provided by {(.Platform)$OS.type}"
13+
)
14+
return(NULL)
15+
}
16+
17+
# Define paths
18+
if (is.null(tzdir)) {
19+
tzdata_path <- path.expand(
20+
file.path(Sys.getenv("USERPROFILE"), "Downloads", "tzdata")
21+
)
22+
} else {
23+
tzdata_path <- tzdir
24+
}
25+
tzdata_compressed <- file.path(tzdata_path, "tzdata.tar.gz")
26+
27+
# Create directory if it doesn't exist
28+
if (!dir.exists(tzdata_path)) {
29+
dir.create(tzdata_path, recursive = TRUE)
30+
}
31+
32+
# Download the latest IANA timezone database
33+
utils::download.file(
34+
"https://data.iana.org/time-zones/tzdata-latest.tar.gz", tzdata_compressed,
35+
mode = "wb"
36+
)
37+
38+
# Check if the file exists
39+
if (!file.exists(tzdata_compressed)) {
40+
cli::cli_abort("Failed to download the timezone database.")
41+
}
42+
43+
# Extract the tar.gz file
44+
utils::untar(tzdata_compressed, exdir = tzdata_path)
45+
46+
# Download the windowsZones.xml file
47+
windows_zones_url <- paste0(
48+
"https://raw.githubusercontent.com/unicode-org/",
49+
"cldr/master/common/supplemental/windowsZones.xml"
50+
)
51+
windows_zones_path <- file.path(tzdata_path, "windowsZones.xml")
52+
utils::download.file(windows_zones_url, windows_zones_path, mode = "wb")
53+
54+
# Find the "northamerica" file and replace "April" with "Apr"
55+
northamerica_file <- file.path(tzdata_path, "northamerica")
56+
if (file.exists(northamerica_file)) {
57+
file_contents <- readLines(northamerica_file)
58+
file_contents <- gsub("April", "Apr", file_contents)
59+
writeLines(file_contents, northamerica_file)
60+
} else {
61+
cli::cli_warn("northamerica file not found in tzdata_path")
62+
}
63+
64+
return(tzdata_path)
65+
}
66+
67+
#' wrapper for download_tzdata_on_windows for GitHub Actions
68+
#' @noRd
69+
#' @keywords internal
70+
download_tzdata_on_windows_gha <- function() {
71+
if (Sys.getenv("RUNNER_OS") == "Windows") {
72+
return(download_tzdata_on_windows("C:/Users/runneradmin/Downloads/tzdata"))
73+
} else {
74+
cli::cli_alert_info(
75+
"Not running on Windows GitHub Action"
76+
)
77+
return(NULL)
78+
}
79+
}

tests/testthat/setup.R

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pcreek <- system.file(
66
sf::read_sf()
77

88
# --- Setup the testing environment.
9+
windows_tz_dir <- chewie:::download_tzdata_on_windows_gha()
910
aum <- options(arrow.unsafe_metadata = TRUE)
1011
temp_dir <- tempdir()
1112
old_chewie_cache <- chewie::chewie_get_cache()
@@ -34,6 +35,9 @@ withr::defer(
3435
options(chewie.testing = NULL)
3536
options(aum)
3637
fs::file_delete(".Renviron")
38+
if (!is.null(windows_tz_dir)) {
39+
fs::dir_delete(windows_tz_dir)
40+
}
3741
},
3842
envir = teardown_env()
3943
)

vignettes/gedi-L1B-waveforms.Rmd

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ knitr::opts_chunk$set(
1212
collapse = TRUE,
1313
comment = "#>"
1414
)
15+
windows_tz_dir <- chewie:::download_tzdata_on_windows_gha()
1516
options(arrow.unsafe_metadata = TRUE)
1617
options("chewie.testing" = TRUE)
1718
temp_dir <- tempdir()

0 commit comments

Comments
 (0)