Skip to content

Commit

Permalink
Use os.makedirs to build tree in extract_file
Browse files Browse the repository at this point in the history
Since baaede6, `is_compressed` returns `True` when expected. This forces
`extract_file_request` to call `extract_file` to retrieve the file. This method
was failing because `os.mkdir` is not able to create directories recursively.
This commit uses `os.makedirs` instead.
  • Loading branch information
sevein committed Oct 19, 2021
1 parent 85658e1 commit 0fb9d0a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 3 additions & 3 deletions storage_service/locations/models/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -1633,14 +1633,14 @@ def extract_file(self, relative_path="", extract_path=None):
# copy only one file out of aip
head, tail = os.path.split(full_path)
src = os.path.join(head, relative_path)
os.mkdir(os.path.join(extract_path, basename))
os.makedirs(os.path.dirname(output_path))
LOGGER.info("Copying from: %s to %s", src, output_path)
shutil.copy(src, output_path)
else:
src = full_path
LOGGER.info("Copying from: %s to %s", full_path, output_path)
shutil.copytree(full_path, output_path)

LOGGER.info("Copying from: %s to %s", src, output_path)

if not relative_path:
self.local_path_location = ss_internal
self.local_path = output_path
Expand Down
12 changes: 12 additions & 0 deletions storage_service/locations/tests/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,18 @@ def test_extract_file_file_from_uncompressed_aip(self):
assert output_path == os.path.join(self.tmp_dir, basedir, "manifest-md5.txt")
assert os.path.isfile(output_path)

def test_extract_file_nested_file_from_uncompressed_aip(self):
""" It should return a single file from an uncompressed aip (with nested path) """
package = models.Package.objects.get(
uuid="0d4e739b-bf60-4b87-bc20-67a379b28cea"
)
basedir = package.get_base_directory()
output_path, extract_path = package.extract_file(
relative_path="working_bag/data/test.txt", extract_path=self.tmp_dir
)
assert output_path == os.path.join(self.tmp_dir, basedir, "data/test.txt")
assert os.path.isfile(output_path)

def test_extract_file_file_from_compressed_aip(self):
""" It should return a single file from a 7zip compressed aip """
package = models.Package.objects.get(
Expand Down

0 comments on commit 0fb9d0a

Please sign in to comment.