Skip to content

Commit

Permalink
🐛 FIX: Ensure base directory always created inextract_tree (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell authored Nov 9, 2020
1 parent 04596dc commit 99a1cf7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion archive_path/tar_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from contextlib import contextmanager, suppress
import io
import itertools
from pathlib import Path
from pathlib import Path, PurePosixPath
import posixpath
import tarfile
from types import TracebackType
Expand Down Expand Up @@ -399,14 +399,24 @@ def extract_tree(
:param cb_descript: the description to return in the callback
:raises NotADirectoryError: If the zip path is not a directory
"""
if not self.is_dir():
raise NotADirectoryError(f"Source is not a directory: {self.at}")

if callback is None:
callback = lambda action, value: None # noqa: E731
else:
callback("init", {"total": 1, "description": "Counting objects to extract"})
count = sum(1 for _ in self.glob(pattern, include_virtual=False))
callback("init", {"total": count, "description": cb_descript})

# always make base directory
Path(outpath).joinpath(PurePosixPath(self.at)).mkdir(
parents=True, exist_ok=True
)

for path in self.glob(pattern, include_virtual=False):
callback("update", 1)
info = self._tarfile.getmember(path.at)
Expand Down
12 changes: 11 additions & 1 deletion archive_path/zip_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import io
import itertools
import os
from pathlib import Path
from pathlib import Path, PurePosixPath
import posixpath
import threading
from types import TracebackType
Expand Down Expand Up @@ -410,7 +410,12 @@ def extract_tree(
:param cb_descript: the description to return in the callback
:raises NotADirectoryError: If the zip path is not a directory
"""
if not self.is_dir():
raise NotADirectoryError(f"Source is not a directory: {self.at}")

outpath = cast(str, os.path.abspath(outpath))

if callback is None:
Expand All @@ -420,6 +425,11 @@ def extract_tree(
count = sum(1 for _ in self.glob(pattern, include_virtual=False))
callback("init", {"total": count, "description": cb_descript})

# always make base directory
Path(outpath).joinpath(PurePosixPath(self.at)).mkdir(
parents=True, exist_ok=True
)

for path in self.glob(pattern, include_virtual=False):
callback("update", 1)
try:
Expand Down

0 comments on commit 99a1cf7

Please sign in to comment.