Skip to content

Commit

Permalink
stages(tar): expose new transform option to tar stage
Browse files Browse the repository at this point in the history
This commit adds a new `transform` 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`.

Note that tar allows only a single `--transform` and we leave it
to the user to construct `sed` expressions if multiple renames
are required.
  • Loading branch information
mvo5 committed Sep 17, 2024
1 parent 66a0033 commit 18c05a8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions stages/org.osbuild.tar
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def main(inputs, output_dir, options):
if options.get("sparse", False):
extra_args += ["--sparse"]

transform = options.get("transform")
if transform:
extra_args += [f"--transform", transform]

# Set up the tar command.
tar_cmd = [
"tar",
Expand Down
6 changes: 5 additions & 1 deletion stages/org.osbuild.tar.meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@
"description": "Make archive files sparse",
"type": "boolean",
"default": false
}
},
"transform": {
"type": "string",
"description": "Used to transform filenames and directly passed to --transform"
}
}
},
"inputs": {
Expand Down
31 changes: 31 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",
"transform": ["transform-cannot-be-passed-multiple-times"],
}, " is not of type 'string'"),
# good
({"filename": "out.tar", "root-node": "include"}, ""),
({"filename": "out.tar", "paths": ["file1"]}, ""),
({"filename": "out.tar", "sparse": True}, ""),
({"filename": "out.tar"}, ""),
({"filename": "out.tar", "transform": "s/foo/bar"}, ""),
])
def test_schema_validation_tar(stage_schema, test_data, expected_err):
test_input = {
Expand Down Expand Up @@ -99,3 +104,29 @@ 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")
@pytest.mark.parametrize("transform,expected_tar_output", [
# unrelated transform
("s/foo/bar/", ["file1", "file2", ""]),
# one file
("s/^file1$/foo99/", ["foo99", "file2", ""]),
# one file
("s/file1/foo99/;s/file2/bar11/", ["foo99", "bar11", ""]),
])
def test_tar_transform(tmp_path, stage_module, fake_inputs, transform, expected_tar_output):
options = {
"filename": "out.tar",
"paths": [
"file1",
"file2",
],
"transform": transform,
}
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 output == expected_tar_output

0 comments on commit 18c05a8

Please sign in to comment.