Skip to content

Commit

Permalink
Julia: replace LazyArtifacts with manual download (#227)
Browse files Browse the repository at this point in the history
* Replace LazyArtifacts with a manual download

* Add missing R warning

* More symlink munging

* Update docs

* Fix argument ordering
  • Loading branch information
WardBrian authored Apr 17, 2024
1 parent 36d391a commit 6f8a516
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 70 deletions.
8 changes: 1 addition & 7 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,7 @@ jobs:
- name: Create tarball
run: |
tar --exclude-vcs -chzvf bridgestan-${{ inputs.new_version }}.tar.gz --transform 's,^,bridgestan-${{ inputs.new_version }}/,' *
# Note: because of the order of operations here, the Julia package inside the tarball can never point to itself.
- name: Update Julia artifact
if: ${{ !inputs.is_rerun }}
run: |
julia ./julia/updateArtifacts.jl bridgestan-${{ inputs.new_version }}.tar.gz "v${{ inputs.new_version }}"
tar --exclude-vcs --hard-dereference -chzvf bridgestan-${{ inputs.new_version }}.tar.gz --transform 's,^,bridgestan-${{ inputs.new_version }}/,' *
- name: Setup git identity
run: |
Expand Down
6 changes: 6 additions & 0 deletions R/R/compile.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ get_bridgestan_path <- function() {
" to ", path))
get_bridgestan_src()
})
num_files <- length(list.files(HOME_BRIDGESTAN))
if (num_files >= 5) {
warning(paste0("Found ", num_files, " different versions of BridgeStan in ",
HOME_BRIDGESTAN, ". Consider deleting old versions to save space."))
}
print("Done!")
}

return(path)
Expand Down
6 changes: 5 additions & 1 deletion docs/internals/development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ Julia

* Julia dependencies:

* `LazyArtifacts <https://docs.julialang.org/en/v1/stdlib/LazyArtifacts/>`_
* `Tar <https://docs.julialang.org/en/v1/stdlib/Tar/>`_ (standard library)
* `TOML <https://docs.julialang.org/en/v1/stdlib/TOML/>`_ (standard library)
* `Downloads <https://docs.julialang.org/en/v1/stdlib/Downloads/>`_ (standard library)
* `Inflate.jl <https://github.com/GunnarFarneback/Inflate.jl>`_ (external)


* Julia code is formatted using `JuliaFormatter <https://github.com/domluna/JuliaFormatter.jl>`_.

Expand Down
12 changes: 7 additions & 5 deletions docs/languages/julia.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ from the BridgeStan folder.
To use the BridgeStan source you've manually downloaded instead of one the package will download for you, you must use [`set_bridgestan_path()`](BridgeStan.set_bridgestan_path!) or the `$BRIDGESTAN` environment variable.


Note that the Julia package depends on Julia 1.8+.
Note that the Julia package depends on Julia 1.6+ and the `Inflate` package.


<a id='Example-Program'></a>
Expand Down Expand Up @@ -526,7 +526,7 @@ 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!()`.


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

<a id='BridgeStan.get_bridgestan_path' href='#BridgeStan.get_bridgestan_path'>#</a>
**`BridgeStan.get_bridgestan_path`** &mdash; *Function*.
Expand All @@ -539,10 +539,12 @@ get_bridgestan_path() -> String

Return the path the the BridgeStan directory.

If the environment variable `BRIDGESTAN` is set, this will be returned. Otherwise, this function downloads an artifact containing the BridgeStan repository and returns the path to the extracted 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.

See `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#L6-L14' class='documenter-source'>source</a><br>

<a target='_blank' href='https://github.com/roualdes/bridgestan/blob/main/julia/src/compile.jl#L19-L29' 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 @@ -556,5 +558,5 @@ set_bridgestan_path!(path)
Set the path BridgeStan.


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

7 changes: 0 additions & 7 deletions julia/Artifacts.toml

This file was deleted.

16 changes: 13 additions & 3 deletions julia/Project.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
name = "BridgeStan"
uuid = "c88b6f0a-829e-4b0b-94b7-f06ab5908f5a"
authors = ["Brian Ward <bward@flatironinstitute.org>", "Bob Carpenter <bcarpenter@flatironinstitute.org>", "Edward Roualdes <eroualdes@csuchico.edu>"]
authors = [
"Brian Ward <bward@flatironinstitute.org>",
"Bob Carpenter <bcarpenter@flatironinstitute.org>",
"Edward Roualdes <eroualdes@csuchico.edu>",
]
version = "2.4.0"

[deps]
LazyArtifacts = "4af54fe1-eca0-43a8-85a7-787d91b784e3"
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
Inflate = "d25df0c9-e2be-5dd7-82c8-3ad0b3e990b9"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
Tar = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"

[compat]
Downloads = "1"
TOML = "1"
Inflate = "0.1"
Tar = "1"
julia = "1.6"
LazyArtifacts = "1"
2 changes: 1 addition & 1 deletion julia/docs/src/julia.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ one the package will download for you, you must use
[`set_bridgestan_path()`](BridgeStan.set_bridgestan_path!) or the `$BRIDGESTAN`
environment variable.

Note that the Julia package depends on Julia 1.8+.
Note that the Julia package depends on Julia 1.6+ and the `Inflate` package.

## Example Program

Expand Down
3 changes: 1 addition & 2 deletions julia/src/BridgeStan.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module BridgeStan

using LazyArtifacts

export StanModel,
name,
model_info,
Expand Down Expand Up @@ -29,6 +27,7 @@ export StanModel,
new_rng

include("model.jl")
include("download.jl")
include("compile.jl")

"""
Expand Down
50 changes: 34 additions & 16 deletions julia/src/compile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,53 @@ function get_make()
get(ENV, "MAKE", Sys.iswindows() ? "mingw32-make.exe" : "make")
end


function validate_stan_dir(path::AbstractString)
if !isdir(path)
error("Path does not exist!\n$path")
end
if !isfile(joinpath(path, "Makefile"))
error(
"Makefile does not exist at path! Make sure it was installed correctly.\n$path",
)
end
end


"""
get_bridgestan_path() -> String
Return the path the the BridgeStan directory.
If the environment variable `BRIDGESTAN` is set, this will be returned. Otherwise, this
function downloads an artifact containing the BridgeStan repository and returns the path to
the extracted 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.
See `set_bridgestan_path!()` to set the path from within Julia.
"""
function get_bridgestan_path()
path = get(ENV, "BRIDGESTAN", "")
if path == ""
artifact_path = artifact"bridgestan"
path = joinpath(artifact_path, only(readdir(artifact_path)))
path = CURRENT_BRIDGESTAN
try
validate_stan_dir(path)
catch
println(
"BridgeStan not found at location specified by \$BRIDGESTAN " *
"environment variable, downloading version $pkg_version to $path",
)
get_bridgestan_src()
num_files = length(readdir(HOME_BRIDGESTAN))
if num_files >= 5
@warn "Found $num_files different versions of BridgeStan in $HOME_BRIDGESTAN. " *
"Consider deleting old versions to save space."
end
println("Done!")
end
end
return path
end

function validate_stan_dir(path::AbstractString)
if !isdir(path)
error("Path does not exist!\n$path")
end
if !isfile(joinpath(path, "Makefile"))
error(
"Makefile does not exist at path! Make sure it was installed correctly.\n$path",
)
end
end


"""
set_bridgestan_path!(path)
Expand Down
63 changes: 63 additions & 0 deletions julia/src/download.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Downloads, Tar, TOML, Inflate

"""
Windows-friendly way to get the user's home directory.
"""
function get_home()
if Sys.iswindows()
if haskey(ENV, "USERPROFILE")
userhome = ENV["USERPROFILE"]
elseif !haskey(ENV, "HOMEPATH")
userhome = path
else
drive = get(ENV, "HOMEDRIVE", "")
userhome = joinpath(drive, ENV["HOMEPATH"])
end
return userhome
else
return expanduser("~/")
end
end

function get_version()
return VersionNumber(
TOML.parsefile(joinpath(dirname(@__DIR__), "Project.toml"))["version"],
)
end
const pkg_version = get_version()

const HOME_BRIDGESTAN = joinpath(get_home(), ".bridgestan")
const CURRENT_BRIDGESTAN = joinpath(HOME_BRIDGESTAN, "bridgestan-$pkg_version")

const RETRIES = 5


function get_bridgestan_src()

url =
"https://github.com/roualdes/bridgestan/releases/download/" *
"v$pkg_version/bridgestan-$pkg_version.tar.gz"
mkpath(HOME_BRIDGESTAN)
tmp = nothing
err_text = "Failed to download BridgeStan $pkg_version from github.com."
for i = 1:RETRIES
try
tmp = Downloads.download(url)
break
catch
if i == RETRIES
error(err_text)
end
println(err_text)
println("Retrying ($(i+1)/$RETRIES)...")
sleep(1)
end
end

try
tmp_extr = Tar.extract(IOBuffer(Inflate.inflate_gzip(tmp)), copy_symlinks = true)
mv(joinpath(tmp_extr, "bridgestan-$pkg_version"), CURRENT_BRIDGESTAN)
catch
error("Failed to unpack $tmp during installation")
end
end
28 changes: 0 additions & 28 deletions julia/updateArtifacts.jl

This file was deleted.

0 comments on commit 6f8a516

Please sign in to comment.