Skip to content

Commit

Permalink
stages(tar): expose new transforms option to tar stage
Browse files Browse the repository at this point in the history
This commit adds a new `transforms` option to the tar stages that
maps directly to the `--transform=` comamndline argument of tar(1).

This allows to transform the names while files/dirs are added to
a tarfile. This is useful for the `gcp` pipeline for
bootc-image-builder where we want to create a gcp tar file that
expects the disk image filename in the tar to be exactly `disk.raw`.
  • Loading branch information
mvo5 committed Sep 17, 2024
1 parent 66a0033 commit b16d0ca
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions stages/org.osbuild.tar
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def main(inputs, output_dir, options):
tarfmt = options.get("format", "gnu")
root_node = options.get("root-node", "include")
paths = options.get("paths", [])
transform = options.get("transforms", [])

extra_args = []
# Set environment variables for the tar operation.
Expand All @@ -33,6 +34,10 @@ def main(inputs, output_dir, options):
if options.get("sparse", False):
extra_args += ["--sparse"]

transforms = options.get("transforms")
if transforms:
extra_args += [f"--transform={t}" for t in transforms]

# Set up the tar command.
tar_cmd = [
"tar",
Expand Down
9 changes: 8 additions & 1 deletion stages/org.osbuild.tar.meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@
"description": "Make archive files sparse",
"type": "boolean",
"default": false
}
},
"transforms": {
"type": "array",
"items": {
"type": "string",
"description": "Transformation of names during tar(1)"
}
}
}
},
"inputs": {
Expand Down
25 changes: 25 additions & 0 deletions stages/test/test_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
"root-node": "include",
"paths": ["file1"]
}, "{'filename': 'out.tar', 'root-node': 'include', 'paths': ['file1']} is not valid under any of the given schemas"),
({
"filename": "foo",
"transforms": "not-an-array",
}, "'not-an-array' is not of type 'array'"),
# good
({"filename": "out.tar", "root-node": "include"}, ""),
({"filename": "out.tar", "paths": ["file1"]}, ""),
({"filename": "out.tar", "sparse": True}, ""),
({"filename": "out.tar"}, ""),
({"filename": "out.tar", "transforms": ["s/foo/bar"]}, ""),
])
def test_schema_validation_tar(stage_schema, test_data, expected_err):
test_input = {
Expand Down Expand Up @@ -99,3 +104,23 @@ def test_tar_paths(tmp_path, stage_module, fake_inputs):
assert os.path.exists(tar_path)
output = subprocess.check_output(["tar", "-tf", tar_path], encoding="utf-8").split("\n")
assert ["file2", "file1", "file1", "file2", ""] == output


@pytest.mark.skipif(not has_executable("tar"), reason="no tar executable")
def test_tar_transforms(tmp_path, stage_module, fake_inputs):
options = {
"filename": "out.tar",
"paths": [
"file1",
"file2",
],
"transforms": [
"s/file1/foo99/",
],
}
stage_module.main(fake_inputs, tmp_path, options)

tar_path = os.path.join(tmp_path, "out.tar")
assert os.path.exists(tar_path)
output = subprocess.check_output(["tar", "-tf", tar_path], encoding="utf-8").split("\n")
assert ["foo99", "file2"] == output

0 comments on commit b16d0ca

Please sign in to comment.