Skip to content

Commit

Permalink
Add a download flag to get_bridgestan_path (#236)
Browse files Browse the repository at this point in the history
* Add a download flag to get_bridgestan_path

* Fix DESCRIPTION

* Consistency updates
  • Loading branch information
WardBrian committed Jun 18, 2024
1 parent 1bde73d commit 6549ff9
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 31 deletions.
3 changes: 2 additions & 1 deletion R/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Encoding: UTF-8
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE, r6 = TRUE)
Suggests:
testthat (>= 3.0.0)
testthat (>= 3.0.0),
withr (>= 3.0.0)
Imports:
R6 (>= 2.4.0)
Config/testthat/edition: 3
20 changes: 14 additions & 6 deletions R/R/compile.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,32 @@ set_bridgestan_path <- function(path) {
#' By default this is set to the value of the environment
#' variable `BRIDGESTAN`.
#'
#' If there is no path set, this function will download
#' a matching version of BridgeStan to a folder called
#' `.bridgestan` in the user's home directory.
#' If there is no path set and the argument `download` is TRUE,
#' this function will download a copy of the BridgeStan source code
#' for the currently installed version under a folder called
#' `.bridgestan` in the user's home directory if one is not already
#' present.
#'
#' @seealso [set_bridgestan_path()]
get_bridgestan_path <- function() {
get_bridgestan_path <- function(download=TRUE) {
# try to get from environment
path <- Sys.getenv("BRIDGESTAN", unset = "")
if (path == "") {
path <- CURRENT_BRIDGESTAN
tryCatch({
path <- tryCatch({
path <- CURRENT_BRIDGESTAN
verify_bridgestan_path(path)
path
}, error = function(e) {
if (!download) {
print("BridgeStan not found at location specified by $BRIDGESTAN environment variable.")
return("")
}
print(paste0("BridgeStan not found at location specified by $BRIDGESTAN ",
"environment variable, downloading version ", packageVersion("bridgestan"),
" to ", path))
get_bridgestan_src()
print("Done!")
path
})
num_files <- length(list.files(HOME_BRIDGESTAN))
if (num_files >= 5) {
Expand Down
10 changes: 6 additions & 4 deletions R/man/get_bridgestan_path.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions R/tests/testthat/test_download.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

test_that("downloading source code works", {

withr::with_envvar(c("BRIDGESTAN" = ""), {
existing <- get_bridgestan_path(download = FALSE)

if (existing != "") {
unlink(existing, recursive = TRUE)
}

expect_equal(get_bridgestan_path(download = FALSE), "")
verify_bridgestan_path(get_bridgestan_path(download = TRUE))
verify_bridgestan_path(get_bridgestan_path(download = FALSE))
})
})
12 changes: 7 additions & 5 deletions docs/languages/julia.md
Original file line number Diff line number Diff line change
Expand Up @@ -526,25 +526,27 @@ Run BridgeStan’s Makefile on a `.stan` file, creating the `.so` used by StanMo
This function checks that the path to BridgeStan is valid and will error if it is not. This can be set with [`set_bridgestan_path!()`](julia.md#BridgeStan.set_bridgestan_path!).


<a target='_blank' href='https://github.com/roualdes/bridgestan/blob/main/julia/src/compile.jl#L65-L78' class='documenter-source'>source</a><br>
<a target='_blank' href='https://github.com/roualdes/bridgestan/blob/main/julia/src/compile.jl#L74-L87' class='documenter-source'>source</a><br>

<a id='BridgeStan.get_bridgestan_path' href='#BridgeStan.get_bridgestan_path'>#</a>
**`BridgeStan.get_bridgestan_path`** &mdash; *Function*.



```julia
get_bridgestan_path() -> String
get_bridgestan_path(;download=true) -> String
```

Return the path the the BridgeStan directory.

If the environment variable `BRIDGESTAN` is set, this will be returned. Otherwise, this function downloads a matching version of BridgeStan under a folder called `.bridgestan` in the user's home directory.
If the environment variable `$BRIDGESTAN` is set, this will be returned.

If `$BRIDGESTAN` is not set and `download` is true, this function downloads a copy of the BridgeStan source code for the currently installed version under a folder called `.bridgestan` in the user's home directory if one is not already present.

See [`set_bridgestan_path!()`](julia.md#BridgeStan.set_bridgestan_path!) to set the path from within Julia.


<a target='_blank' href='https://github.com/roualdes/bridgestan/blob/main/julia/src/compile.jl#L19-L29' class='documenter-source'>source</a><br>
<a target='_blank' href='https://github.com/roualdes/bridgestan/blob/main/julia/src/compile.jl#L19-L32' class='documenter-source'>source</a><br>

<a id='BridgeStan.set_bridgestan_path!' href='#BridgeStan.set_bridgestan_path!'>#</a>
**`BridgeStan.set_bridgestan_path!`** &mdash; *Function*.
Expand All @@ -558,5 +560,5 @@ set_bridgestan_path!(path)
Set the path BridgeStan.


<a target='_blank' href='https://github.com/roualdes/bridgestan/blob/main/julia/src/compile.jl#L54-L58' class='documenter-source'>source</a><br>
<a target='_blank' href='https://github.com/roualdes/bridgestan/blob/main/julia/src/compile.jl#L63-L67' class='documenter-source'>source</a><br>

29 changes: 19 additions & 10 deletions julia/src/compile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function get_make()
end


function validate_stan_dir(path::AbstractString)
function verify_bridgestan_path(path::AbstractString)
if !isdir(path)
error("Path does not exist!\n$path")
end
Expand All @@ -17,23 +17,32 @@ end


"""
get_bridgestan_path() -> String
get_bridgestan_path(;download=true) -> String
Return the path the the BridgeStan directory.
If the environment variable `BRIDGESTAN` is set, this will be returned.
Otherwise, this function downloads a matching version of BridgeStan under
a folder called `.bridgestan` in the user's home directory.
If the environment variable `\$BRIDGESTAN` is set, this will be returned.
If `\$BRIDGESTAN` is not set and `download` is true, this function downloads
a copy of the BridgeStan source code for the currently installed version under
a folder called `.bridgestan` in the user's home directory if one is not already
present.
See [`set_bridgestan_path!()`](@ref) to set the path from within Julia.
"""
function get_bridgestan_path()
function get_bridgestan_path(; download::Bool = true)
path = get(ENV, "BRIDGESTAN", "")
if path == ""
path = CURRENT_BRIDGESTAN
try
validate_stan_dir(path)
path = CURRENT_BRIDGESTAN
verify_bridgestan_path(path)
catch
if !download
println(
"BridgeStan not found at location specified by \$BRIDGESTAN environment variable",
)
return ""
end
println(
"BridgeStan not found at location specified by \$BRIDGESTAN " *
"environment variable, downloading version $pkg_version to $path",
Expand All @@ -57,7 +66,7 @@ end
Set the path BridgeStan.
"""
function set_bridgestan_path!(path::AbstractString)
validate_stan_dir(path)
verify_bridgestan_path(path)
ENV["BRIDGESTAN"] = path
end

Expand All @@ -82,7 +91,7 @@ function compile_model(
make_args::AbstractVector{String} = String[],
)
bridgestan = get_bridgestan_path()
validate_stan_dir(bridgestan)
verify_bridgestan_path(bridgestan)

if !isfile(stan_file)
throw(SystemError("Stan file not found: $stan_file"))
Expand Down
12 changes: 11 additions & 1 deletion julia/test/util_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ end

@testset "download" begin
withenv("BRIDGESTAN" => nothing) do
BridgeStan.validate_stan_dir(BridgeStan.get_bridgestan_path())
existing = BridgeStan.get_bridgestan_path(download = false)
if existing != "" && isdir(existing)
rm(existing; recursive = true)
end

# first call - download doesn't occur
@test "" == BridgeStan.get_bridgestan_path(download = false)
# download occurs
BridgeStan.verify_bridgestan_path(BridgeStan.get_bridgestan_path())
# download doesn't occur, returns path from 2nd call
BridgeStan.verify_bridgestan_path(BridgeStan.get_bridgestan_path(download = false))
end
end
16 changes: 12 additions & 4 deletions python/bridgestan/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ def set_bridgestan_path(path: Union[str, os.PathLike]) -> None:
os.environ["BRIDGESTAN"] = path


def get_bridgestan_path() -> str:
def get_bridgestan_path(download: bool = True) -> str:
"""
Get the path to BridgeStan.
By default this is set to the value of the environment
variable ``BRIDGESTAN``.
If there is no path set, this function will download
a matching version of BridgeStan to a folder called
``.bridgestan`` in the user's home directory.
If there is no path set and ``download`` is True, this
function will download a copy of the BridgeStan source code
for the currently installed version under a folder called
``.bridgestan`` in the user's home directory if one is not
already present.
See also :func:`set_bridgestan_path`
"""
Expand All @@ -62,6 +64,12 @@ def get_bridgestan_path() -> str:
path = os.fspath(CURRENT_BRIDGESTAN)
verify_bridgestan_path(path)
except ValueError:
if not download:
print(
"BridgeStan not found at location specified by $BRIDGESTAN environment variable"
)
return ""

print(
"BridgeStan not found at location specified by $BRIDGESTAN "
f"environment variable, downloading version {__version__} to {path}"
Expand Down
7 changes: 7 additions & 0 deletions python/test/test_download.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import os
import shutil
from unittest import mock

import bridgestan as bs


@mock.patch.dict(os.environ, {"BRIDGESTAN": ""})
def test_download_bridgestan():
if bs.download.CURRENT_BRIDGESTAN.is_dir():
shutil.rmtree(bs.download.CURRENT_BRIDGESTAN)

# first call doesn't download
assert bs.compile.get_bridgestan_path(download=False) == ""
bs.compile.verify_bridgestan_path(bs.compile.get_bridgestan_path())
bs.compile.verify_bridgestan_path(bs.compile.get_bridgestan_path(download=False))

0 comments on commit 6549ff9

Please sign in to comment.