From 3ef632e06316de4d04a3cfee62e23b2b3b0e2398 Mon Sep 17 00:00:00 2001 From: Lorenzo Contento <16554887+lcontento@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:46:50 +0100 Subject: [PATCH] Allow access to the artifact path in a relocatable way (#32) Co-authored-by: Curtis Vogt --- Project.toml | 2 +- README.md | 2 +- src/TZJData.jl | 11 ++++++++++- test/runtests.jl | 4 ++-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index e6661eb..4b929c6 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "TZJData" uuid = "dc5dba14-91b3-4cab-a142-028a31da12f7" authors = ["Curtis Vogt "] -version = "1.3.0+2024b" +version = "1.3.1+2024b" [deps] Artifacts = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" diff --git a/README.md b/README.md index f0375ad..d8dd10c 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ julia> using TimeZones: TZJFile julia> function load(tzname) rel_path = joinpath(split(tzname, '/')) - return open(TZJFile.read, joinpath(TZJData.ARTIFACT_DIR, rel_path), "r")(tzname) + return open(TZJFile.read, joinpath(TZJData.artifact_dir(), rel_path), "r")(tzname) end load (generic function with 1 method) diff --git a/src/TZJData.jl b/src/TZJData.jl index f37bd21..4e92287 100644 --- a/src/TZJData.jl +++ b/src/TZJData.jl @@ -2,7 +2,14 @@ module TZJData using Artifacts -const ARTIFACT_DIR = artifact"tzjdata" +# Avoid using a constant to define the artifact directory as this will hardcode the path +# to the location used during pre-compilation which can be problematic if the Julia depot +# relocated afterwards. One scenario where this can occur is when this package is used +# within a system image. +artifact_dir() = artifact"tzjdata" + +# Deprecation for TZJData.jl v1 +Base.@deprecate_binding ARTIFACT_DIR artifact_dir() false const TZDATA_VERSION = let artifact_dict = Artifacts.parse_toml(joinpath(@__DIR__, "..", "Artifacts.toml")) @@ -11,4 +18,6 @@ const TZDATA_VERSION = let m !== nothing ? m[:version] : error("Unable to determine tzdata version") end +precompile(artifact_dir, ()) + end diff --git a/test/runtests.jl b/test/runtests.jl index 2add4ff..d53d5d2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -46,7 +46,7 @@ function sha256sum(tarball_path) end @testset "TZJData.jl" begin - @test isdir(TZJData.ARTIFACT_DIR) + @test isdir(TZJData.artifact_dir()) @test occursin(r"^\d{4}[a-z]$", TZJData.TZDATA_VERSION) @testset "validate unpublished artifact" begin @@ -69,7 +69,7 @@ end @testset "load compiled" begin cache = Dict{String,Tuple{TimeZone,Class}}() - _reload_cache!(cache, TZJData.ARTIFACT_DIR) + _reload_cache!(cache, TZJData.artifact_dir()) @test !isempty(cache) end