Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions nemo_run/core/packaging/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.

import os
import shlex
from dataclasses import dataclass
from pathlib import Path

Expand Down Expand Up @@ -53,7 +54,8 @@ def package(self, path: Path, job_dir: str, name: str) -> str:

# Create initial empty tar file
ctx = Context()
ctx.run(f"tar -cf {output_file}.tmp --files-from /dev/null")
tmp_file = shlex.quote(f"{output_file}.tmp")
ctx.run(f"tar -cf {tmp_file} --files-from /dev/null")

for include_pattern, relative_path in zip(self.include_pattern, self.relative_path):
if include_pattern == "":
Expand All @@ -63,12 +65,14 @@ def package(self, path: Path, job_dir: str, name: str) -> str:

with ctx.cd(relative_path):
# Append files directly to the main tar archive
cmd = f"find {relative_include_pattern} -type f -print0 | xargs -0 tar -rf {output_file}.tmp"
cmd = (
f"find {relative_include_pattern} -type f -print0 | xargs -0 tar -rf {tmp_file}"
)
ctx.run(cmd)

# Gzip the final result
gzip_cmd = f"gzip -c {output_file}.tmp > {output_file}"
rm_cmd = f"rm {output_file}.tmp"
gzip_cmd = f"gzip -c {tmp_file} > {shlex.quote(output_file)}"
rm_cmd = f"rm {tmp_file}"

ctx.run(gzip_cmd)
ctx.run(rm_cmd)
Expand Down
23 changes: 23 additions & 0 deletions test/core/packaging/test_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,37 @@
import os
import shlex
import subprocess
import tarfile
import tempfile
from pathlib import Path
from unittest.mock import patch

import pytest

from nemo_run.core.packaging.pattern import PatternPackager
from test.conftest import MockContext


@pytest.mark.parametrize("spaced_part", ["job_dir", "name"])
@patch("nemo_run.core.packaging.pattern.Context", MockContext)
def test_package_with_spaces_in_output_path(tmp_path, spaced_part):
source = tmp_path / "source"
source.mkdir()
(source / "input.txt").write_text("training input")
job_dir = tmp_path / ("job output" if spaced_part == "job_dir" else "job")
job_dir.mkdir()
name = "training package" if spaced_part == "name" else "training"
packager = PatternPackager(include_pattern=str(source / "*"), relative_path=str(source))

output = packager.package(source, str(job_dir), name)

assert output == str(job_dir / f"{name}.tar.gz")
with tarfile.open(output) as archive:
assert archive.getnames() == ["input.txt"]
assert archive.extractfile("input.txt").read() == b"training input"
assert not Path(output + ".tmp").exists()


@patch("nemo_run.core.packaging.pattern.Context", MockContext)
def test_package_with_include_pattern_rel_path(tmpdir):
# Create extra files in a separate directory
Expand Down
Loading