Skip to content

Commit

Permalink
Add download test data function
Browse files Browse the repository at this point in the history
  • Loading branch information
kdheepak committed Feb 18, 2019
1 parent 149f737 commit ecb26f5
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 19 deletions.
22 changes: 3 additions & 19 deletions deps/build.jl
@@ -1,21 +1,5 @@

const POWERSYSTEMSTESTDATA_GITHUB_URL = "https://github.com/NREL/PowerSystemsTestData.git"
include(joinpath(@__DIR__, "../src/utils/data.jl"))
using .UtilsData

base_dir = string(dirname(dirname(@__FILE__)))
const DATA_FOLDER = joinpath(base_dir,"data")

const CLONE_CMD = `git clone --depth 1 $POWERSYSTEMSTESTDATA_GITHUB_URL $DATA_FOLDER`

const PULL_CMD = `git -C $DATA_FOLDER pull`

function download_data()

if !isdir(DATA_FOLDER)
run(CLONE_CMD)
else
run(PULL_CMD)
end

end

download_data()
download(TestData)
4 changes: 4 additions & 0 deletions src/PowerSystems.jl
Expand Up @@ -134,4 +134,8 @@ include("base.jl")
include("utils/print.jl")
include("utils/lodf_calculations.jl")

# Download test data
include("utils/data.jl")
using .UtilsData

end # module
58 changes: 58 additions & 0 deletions src/utils/data.jl
@@ -0,0 +1,58 @@
# this file is included in the build.jl script

module UtilsData

__precompile__(true)

export TestData

abstract type AbstractOS end
abstract type Unix <: AbstractOS end
abstract type BSD <: Unix end

abstract type Windows <: AbstractOS end
abstract type MacOS <: BSD end
abstract type Linux <: BSD end

if Sys.iswindows()
const os = Windows
elseif Sys.isapple()
const os = MacOS
else
const os = Linux
end

abstract type TestData end

if Sys.iswindows()
const POWERSYSTEMSTESTDATA_URL = "https://github.com/NREL/PowerSystemsTestData/archive/master.zip"
else
const POWERSYSTEMSTESTDATA_URL = "https://github.com/NREL/PowerSystemsTestData/archive/master.tar.gz"
end

"""
Download Power System Data into a "data" folder in given argument path.
Defaults to the root of the PowerSystems package.
Returns the downloaded folder name.
"""
function Base.download(::Type{TestData}, folder::AbstractString=joinpath(@__DIR__, "../..") |> abspath)
tempfilename = Base.download(POWERSYSTEMSTESTDATA_URL)
directory = folder |> normpath |> abspath
mkpath(directory)
unzip(os, tempfilename, directory)
mv(joinpath(directory, "PowerSystemsTestData-master"), joinpath(directory, "data"), force=true)
return joinpath(directory, "data")
end

function unzip(::Type{<:BSD}, filename, directory)
@assert success(`tar -xvf $filename -C $directory`) "Unable to extract $filename to $directory"
end

function unzip(::Type{Windows}, filename, directory)
home = (Base.VERSION < v"0.7-") ? JULIA_HOME : Sys.BINDIR
@assert success(`$home/7z x $filename -y -o$directory`) "Unable to extract $filename to $directory"
end

end

8 changes: 8 additions & 0 deletions test/data.jl
@@ -0,0 +1,8 @@


@testset "TestData" begin

@test download(PowerSystems.TestData) |> abspath == joinpath(@__DIR__, "../data") |> abspath

end # testset

2 changes: 2 additions & 0 deletions test/runtests.jl
Expand Up @@ -7,6 +7,8 @@ using Dates
gl = global_logger()
global_logger(ConsoleLogger(gl.stream, Logging.Error))

include("data.jl")

@testset "Check PowerSystems Data" begin
@info "Check bus index"
include("busnumberchecks.jl")
Expand Down

0 comments on commit ecb26f5

Please sign in to comment.