Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: strip complete base directory from tarfile paths #56

Closed
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
6 changes: 4 additions & 2 deletions bin/make_studyvisit_archive
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def write_archive(
# adjust properties to make archive builds reproducible
tinfo = normalize_tarinfo(
tinfo,
input_base_dir,
archive_content_base_dir,
# go with the reported timestamp from DICOM or with default
content[p] or default_timestamp,
Expand All @@ -94,9 +95,10 @@ def write_archive(
tar.addfile(tinfo, fp)


def normalize_tarinfo(tinfo, archive_path, timestamp):
def normalize_tarinfo(tinfo, input_base_dir, archive_path, timestamp):
# strip first level and replace with generated archive root dir name
tinfo.name = str(Path(archive_path, *Path(tinfo.name).parts[1:]))
input_path = (Path('/') / tinfo.name).relative_to(input_base_dir)
tinfo.name = str(Path(archive_path) / input_path)
# be safe
tinfo.uid = 0
tinfo.gid = 0
Expand Down
Empty file added tests/modules/__init__.py
Empty file.
1 change: 1 addition & 0 deletions tests/modules/make_studyvisit_archive.py
24 changes: 24 additions & 0 deletions tests/test_path_handling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

import sys
import tarfile
from pathlib import Path

from .modules.make_studyvisit_archive import main


def test_path_handling(tmp_path):
base_dir = tmp_path / 'input' / 'd_1' / 'd_1_1' / 'd_1_1_1'
base_dir.mkdir(parents=True)
(base_dir / 'file1.txt').write_text('content 1')
(base_dir / 'file2.txt').write_text('content 2')

output_base_dir = tmp_path / 'output'

study_id, visit_id = 'study_1', 'visit_1'
main(str(base_dir), str(output_base_dir), study_id, visit_id)

tar_file = tarfile.open(output_base_dir / study_id / f'{visit_id}_dicom.tar')
assert tar_file.getnames() == [
f'{study_id}_{visit_id}/file1.txt',
f'{study_id}_{visit_id}/file2.txt',
]
Loading