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

Add option to manually override data directory #71

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
30 changes: 29 additions & 1 deletion src/definitions.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
const PACKAGE_DIR = joinpath(dirname(dirname(pathof(PowerSystemCaseBuilder))))
const DATA_DIR = joinpath(LazyArtifacts.artifact"CaseData", "PowerSystemsTestData-2.0")
const DATA_DIR_KEY = "PSB_DATA_DIR" # Environment variable to check for data directory

# Gets evaluated at compile time to find the data directory. To re-evaluate, force
# recompilation with `Base.compilecache(Base.identify_package("PowerSystemCaseBuilder"))`
function get_data_dir()
if haskey(ENV, DATA_DIR_KEY)
candidate = ENV[DATA_DIR_KEY]
if isdir(candidate)
@debug "Using PSB data dir $candidate from environment variable"
return candidate
else
error(
"The directory specified by the environment variable $DATA_DIR_KEY, $candidate, does not exist.",
)
end
else
candidate = joinpath(LazyArtifacts.artifact"CaseData", "PowerSystemsTestData-2.0")
if isdir(candidate)
@debug "Using default PSB data dir $candidate"
return candidate
else
error(

Check warning on line 23 in src/definitions.jl

View check run for this annotation

Codecov / codecov/patch

src/definitions.jl#L23

Added line #L23 was not covered by tests
"No data dir specified by environment variable $DATA_DIR_KEY, and the default, $candidate, does not exist.",
)
end
end
end

const DATA_DIR = get_data_dir()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure about this? Won’t it get cached the first time and then not re-evaluated?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it will, and this may necessitate another approach — see the initial PR comment above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, didn’t read that all the way through - or your comment above the function. I think the current behavior will be too confusing and that you will have to replace every use of DATA_DIR with a function.

const RTS_DIR = joinpath(LazyArtifacts.artifact"rts", "RTS-GMLC-0.2.2")

const SYSTEM_DESCRIPTORS_FILE = joinpath(PACKAGE_DIR, "src", "system_descriptor.jl")
Expand Down
17 changes: 17 additions & 0 deletions test/test_definitions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@testset "Test data directory configuration" begin
td = mktempdir()
withenv(PSB.DATA_DIR_KEY => td) do
the_data_dir = PSB.get_data_dir()
@test isdir(the_data_dir)
@test the_data_dir == td
end
withenv(PSB.DATA_DIR_KEY => joinpath(td, "DNE")) do
@test_throws ErrorException PSB.get_data_dir()
end
withenv(PSB.DATA_DIR_KEY => nothing) do
the_data_dir = PSB.get_data_dir()
@test isdir(the_data_dir)
@test occursin("PowerSystemsTestData-2.0", the_data_dir)
end
@test isdir(PSB.DATA_DIR)
end
Loading