Skip to content

Commit 4b145ee

Browse files
authored
Refactor pigz support (#113)
* Refactor pigz support * Add tests
1 parent 0329f54 commit 4b145ee

File tree

4 files changed

+83
-10
lines changed

4 files changed

+83
-10
lines changed

src/ArchiveUtils.jl

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ end
151151

152152
# Stripped down copy of `Pkg.Artifacts.archive_artifact` that supports a custom `package`
153153
# function
154-
function _archive_artifact(hash::SHA1, tarball_path::String;
155-
honor_overrides::Bool=false,
156-
package::Function=_package_fast)
154+
function archive_artifact(hash::SHA1, tarball_path::String;
155+
honor_overrides::Bool=false,
156+
package::Function=package)
157157

158158
if !artifact_exists(hash)
159159
error("Unable to archive artifact $(bytes2hex(hash.bytes)): does not exist!")
@@ -168,12 +168,20 @@ function _archive_artifact(hash::SHA1, tarball_path::String;
168168
end
169169
end
170170

171-
# Copy of `Pkg.PlatformEngines.package` but using `pigz` instead of `7z`
172-
function _package_fast(src_dir::AbstractString, tarball_path::AbstractString)
171+
# An improved version of `Pkg.PlatformEngines.package`
172+
function package(src_dir::AbstractString, tarball_path::AbstractString;
173+
format::AbstractString="gzip")
173174
rm(tarball_path, force=true)
174-
pigz() do pigz_exe
175-
open(pipeline(`$pigz_exe -9`, stdout=tarball_path), write=true) do io
176-
Tar.create(src_dir, io)
177-
end
175+
176+
# Note: For compressing gzip files we use pigz since it uses threading where as p7zip
177+
# does not.
178+
compress_cmd = if format == "gzip"
179+
pipeline(`$(pigz()) -9`, stdout=tarball_path)
180+
else
181+
pipeline(`$(p7zip()) a -si -t$format -mx9 $tarball_path`, stdout=devnull)
182+
end
183+
184+
open(compress_cmd, write=true) do io
185+
Tar.create(src_dir, io)
178186
end
179187
end

src/Prefix.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ function package(prefix::Prefix,
173173
@info("Tree hash of contents of $(basename(out_path)): $(tree_hash)")
174174
end
175175

176-
tarball_hash = _archive_artifact(tree_hash, out_path; honor_overrides=false)
176+
tarball_hash = archive_artifact(tree_hash, out_path; honor_overrides=false)
177177
if verbose
178178
@info("SHA256 of $(basename(out_path)): $(tarball_hash)")
179179
end

test/archive_utils.jl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using BinaryBuilderBase: archive_artifact, package
2+
using Pkg.Artifacts: create_artifact, remove_artifact, with_artifacts_directory
3+
using Pkg: PlatformEngines
4+
5+
@testset "Archive Utils" begin
6+
@testset "package" begin
7+
mktempdir() do prefix
8+
# Create random files
9+
mkpath(joinpath(prefix, "bin"))
10+
mkpath(joinpath(prefix, "lib"))
11+
mkpath(joinpath(prefix, "etc"))
12+
bar_path = joinpath(prefix, "bin", "bar.sh")
13+
open(bar_path, "w") do f
14+
write(f, "#!/bin/sh\n")
15+
write(f, "echo yolo\n")
16+
end
17+
baz_path = joinpath(prefix, "lib", "baz.so")
18+
open(baz_path, "w") do f
19+
write(f, "this is not an actual .so\n")
20+
end
21+
22+
qux_path = joinpath(prefix, "etc", "qux.conf")
23+
open(qux_path, "w") do f
24+
write(f, "use_julia=true\n")
25+
end
26+
27+
mktempdir() do output_dir
28+
for (format, ext) in [("gzip", "gz"), ("xz", "xz")]
29+
tarball_path = joinpath(output_dir, "foo.tar.$ext")
30+
package(prefix, tarball_path; format=format)
31+
@test isfile(tarball_path)
32+
33+
# Test that we can inspect the contents of the tarball
34+
contents = PlatformEngines.list_tarball_files(tarball_path)
35+
@test "bin/bar.sh" in contents
36+
@test "lib/baz.so" in contents
37+
@test "etc/qux.conf" in contents
38+
end
39+
end
40+
end
41+
end
42+
43+
@testset "Artifact archival" begin
44+
mktempdir() do art_dir
45+
with_artifacts_directory(art_dir) do
46+
hash = create_artifact(p -> touch(joinpath(p, "foo")))
47+
tarball_path = joinpath(art_dir, "foo.tar.gz")
48+
archive_artifact(hash, tarball_path)
49+
@test "foo" in PlatformEngines.list_tarball_files(tarball_path)
50+
rm(tarball_path)
51+
52+
# Test custom `package` function and ensure failure if no `tarball_path` file
53+
# is created.
54+
package_alt(src_dir, tarball_path) = nothing
55+
@test !isfile(tarball_path)
56+
@test_throws SystemError archive_artifact(hash, tarball_path, package=package_alt)
57+
58+
# Test archiving something that doesn't exist fails
59+
remove_artifact(hash)
60+
@test_throws ErrorException archive_artifact(hash, tarball_path)
61+
end
62+
end
63+
end
64+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Run all our tests
22
include("compat.jl")
3+
include("archive_utils.jl")
34
include("dependencies.jl")
45
include("platforms.jl")
56
include("prefix.jl")

0 commit comments

Comments
 (0)