Skip to content

Commit bd8444e

Browse files
authored
Determine data repo from DEPOT_PATH (#52)
1 parent 705492e commit bd8444e

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

src/DataSets.jl

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,29 +67,44 @@ include("storage_drivers.jl")
6767
#-------------------------------------------------------------------------------
6868
# Global datasets configuration for current Julia session
6969

70-
function expand_project_path(path)
70+
function data_project_from_path(path; depot_paths)
7171
if path == "@"
72-
return path
72+
ActiveDataProject()
7373
elseif path == ""
74-
return joinpath(homedir(), ".julia", "datasets", "Data.toml")
74+
# We will not throw an error here because this gets call in __init__, and we
75+
# do not want to interrupt the loading of the package. Instead, we omit this
76+
# project.
77+
if isempty(depot_paths)
78+
@warn "Julia depot data project (for an empty dataset path) can not be constructed because DEPOT_PATH is empty."
79+
return nothing
80+
end
81+
depot = first(depot_paths)
82+
# Julia is perfectly happy with DEPOT_PATHs that are not absolute, and hence their
83+
# interpretation changes when the user cd-s around in their session.
84+
#
85+
# https://github.com/JuliaLang/julia/issues/44958
86+
#
87+
# To offer a little bit more reliability here for the user, we absolutize the
88+
# path when DataSets gets loaded, so that things would not be affected by the
89+
# user changing directories.
90+
if !isabspath(depot)
91+
depot = abspath(expanduser(depot))
92+
@warn "Julia depot path ($(first(depot_paths))) not absolute. Fixing data project path relative to current working directory." depot
93+
end
94+
TomlFileDataProject(joinpath(depot, "datasets", "Data.toml"))
7595
else
96+
# In other cases, we expect a reasonable absolute (or relative) path from
97+
# the user, which can either points directly to a file, unless it is an existing
98+
# directory.
7699
path = abspath(expanduser(path))
77100
if isdir(path)
78101
path = joinpath(path, "Data.toml")
79102
end
80-
end
81-
path
82-
end
83-
84-
function data_project_from_path(path)
85-
if path == "@"
86-
ActiveDataProject()
87-
else
88-
TomlFileDataProject(expand_project_path(path))
103+
TomlFileDataProject(path)
89104
end
90105
end
91106

92-
function create_project_stack(env)
107+
function create_project_stack(env, depot_paths)
93108
stack = []
94109
env_search_path = get(env, "JULIA_DATASETS_PATH", nothing)
95110
if isnothing(env_search_path)
@@ -99,7 +114,8 @@ function create_project_stack(env)
99114
split(env_search_path, Sys.iswindows() ? ';' : ':')
100115
end
101116
for path in paths
102-
project = data_project_from_path(path)
117+
project = data_project_from_path(path; depot_paths)
118+
isnothing(project) && continue
103119
push!(stack, project)
104120
end
105121
StackedDataProject(stack)
@@ -124,11 +140,13 @@ interpreted as follows:
124140
For directories, the filename "Data.toml" is implicitly appended.
125141
`expanduser()` is used to expand the user's home directory.
126142
- As in `DEPOT_PATH`, an *empty* path component means the user's default
127-
Julia home directory, `joinpath(homedir(), ".julia", "datasets")`
143+
Julia depot (e.g. `~/.julia/datasets`), determined by the first element
144+
of `DEPOT_PATH`.
128145
129-
This simplified version of the code loading rules (LOAD_PATH/DEPOT_PATH) is
146+
This simplified version of the code loading rules (`LOAD_PATH`/`DEPOT_PATH``) is
130147
used as it seems unlikely that we'll want data location to be version-
131-
dependent in the same way that that code is.
148+
dependent in the same way that that code is. Note that any changes to `DEPOT_PATH`
149+
after `DataSets` has been loaded do not affect `DataSets.PROJECT`.
132150
133151
Unlike `LOAD_PATH`, `JULIA_DATASETS_PATH` is represented inside the program as
134152
a `StackedDataProject`, and users can add custom projects by defining their own
@@ -146,7 +164,7 @@ function __init__()
146164
# be unnecessary and can cause problems if those driver modules use
147165
# Requires-like code loading.
148166
if !_isprecompiling()
149-
global PROJECT = create_project_stack(ENV)
167+
global PROJECT = create_project_stack(ENV, DEPOT_PATH)
150168
for proj in PROJECT.projects
151169
try
152170
add_storage_driver(proj)

src/repl.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ function parse_data_repl_cmd(cmdstr)
212212
if subcmd == "push"
213213
path = popfirst!(tokens)
214214
return quote
215-
proj = $DataSets.data_project_from_path($path)
215+
proj = $DataSets.data_project_from_path($path; depot_paths=DEPOT_PATH)
216216
stack = $DataSets.PROJECT
217217
pushfirst!(stack, proj)
218218
stack

test/projects.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ end
114114
joinpath(@__DIR__, "Data.toml"),
115115
""], paths_sep)
116116
fake_env = Dict("JULIA_DATASETS_PATH"=>datasets_paths)
117-
proj = DataSets.create_project_stack(fake_env)
117+
proj = DataSets.create_project_stack(fake_env, [joinpath(homedir(), ".julia"), joinpath("root", "julia")])
118118
@test proj.projects[1] isa ActiveDataProject
119119

120120
@test proj.projects[2] isa TomlFileDataProject
@@ -129,6 +129,14 @@ end
129129
DataSets.__init__()
130130
@test DataSets.PROJECT.projects[1] isa TomlFileDataProject
131131
@test project_name(DataSets.PROJECT.projects[1]) == joinpath(@__DIR__, "Data.toml")
132+
133+
# Test a few edge cases too:
134+
@test_logs (
135+
:warn, "Julia depot data project (for an empty dataset path) can not be constructed because DEPOT_PATH is empty."
136+
) DataSets.create_project_stack(Dict("JULIA_DATASETS_PATH"=>"foo$(paths_sep)"), [])
137+
@test_logs (
138+
:warn, "Julia depot path (relative/depot/path) not absolute. Fixing data project path relative to current working directory."
139+
) DataSets.create_project_stack(Dict("JULIA_DATASETS_PATH"=>"$(paths_sep)/foo"), ["relative/depot/path"])
132140
end
133141

134142
@testset "config!() metadata update" begin

0 commit comments

Comments
 (0)