Skip to content

Commit

Permalink
fix: expand ~ to user home dir
Browse files Browse the repository at this point in the history
Signed-off-by: Jericho Tolentino <[email protected]>
  • Loading branch information
jericht committed Jan 14, 2025
1 parent d697602 commit 2a4aead
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/deadline/job_attachments/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -1117,10 +1117,13 @@ def set_root_path(self, original_root: str, new_root: str) -> None:
"""
Changes the root path for downloading output files, (which is the root path
saved in the S3 metadata for the output manifest by default,) with a custom path.
(It will store the new root path as an absolute path.)
The `new_root` path will have the following transformations applied:
- Normalizes the path with `os.path.normpath` (e.g. removes unnecessary relative paths `..`)
- Expands leading tilde (~) character to corresponding user home directory with pathlib.Path.expanduser
- Makes the path absolute if a relative path is given (prepends current working directory) with pathlib.Path.absolute
"""
# Need to use absolute to not resolve symlinks, but need normpath to get rid of relative paths, i.e. '..'
new_root = str(os.path.normpath(Path(new_root).absolute()))
new_root = str(os.path.normpath(Path(new_root).expanduser().absolute()))

if original_root not in self.outputs_by_root:
raise ValueError(
Expand Down
21 changes: 21 additions & 0 deletions test/unit/deadline_job_attachments/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,27 @@ def test_OutputDownloader_set_root_path_wrong_root_throws_exception(
with pytest.raises(ValueError):
output_downloader.set_root_path(original_root="/wrong_root", new_root="/new_root_path")

@mock_aws
def test_OutputDownloader_set_root_path_expanduser(self) -> None:
# GIVEN
mock_outputdownloader = MagicMock()
original_root = os.path.join("/tmp", "original_root")
new_root = os.path.join("~", "asset_root")
mock_outputdownloader.outputs_by_root = {
original_root: "groot",
}

# WHEN
OutputDownloader.set_root_path(
self=mock_outputdownloader,
original_root=original_root,
new_root=new_root,
)

# THEN
assert len(mock_outputdownloader.outputs_by_root) == 1
assert os.path.expanduser(new_root) in mock_outputdownloader.outputs_by_root

@mock_aws
def test_OutputDownloader_download_job_output_when_skip(
self, farm_id, queue_id, tmp_path: Path
Expand Down

0 comments on commit 2a4aead

Please sign in to comment.