1
0
mirror of synced 2024-11-24 02:04:23 +01:00

Make use of is_dir/isdir for archives

This commit is contained in:
jvoisin 2023-05-03 22:19:19 +02:00
parent 71ecac85b0
commit 2ac8c24dac

View File

@ -105,6 +105,11 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
def _get_member_name(member: ArchiveMember) -> str: def _get_member_name(member: ArchiveMember) -> str:
"""Return the name of the given member.""" """Return the name of the given member."""
@staticmethod
@abc.abstractmethod
def _is_dir(member: ArchiveMember) -> bool:
"""Return true is the given member is a directory."""
@abc.abstractmethod @abc.abstractmethod
def _add_file_to_archive(self, archive: ArchiveClass, member: ArchiveMember, def _add_file_to_archive(self, archive: ArchiveClass, member: ArchiveMember,
full_path: str): full_path: str):
@ -138,8 +143,7 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
local_meta = self._get_member_meta(item) local_meta = self._get_member_meta(item)
member_name = self._get_member_name(item) member_name = self._get_member_name(item)
if member_name[-1] == '/': # pragma: no cover if self._is_dir(item): # pragma: no cover
# `is_dir` is added in Python3.6
continue # don't keep empty folders continue # don't keep empty folders
zin.extract(member=item, path=temp_folder) zin.extract(member=item, path=temp_folder)
@ -183,7 +187,7 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
# we're iterating (and thus inserting) them in lexicographic order. # we're iterating (and thus inserting) them in lexicographic order.
for item in items: for item in items:
member_name = self._get_member_name(item) member_name = self._get_member_name(item)
if member_name[-1] == '/': # `is_dir` is added in Python3.6 if self._is_dir(item):
continue # don't keep empty folders continue # don't keep empty folders
full_path = os.path.join(temp_folder, member_name) full_path = os.path.join(temp_folder, member_name)
@ -374,6 +378,11 @@ class TarParser(ArchiveBasedAbstractParser):
member.mode = permissions member.mode = permissions
return member return member
@staticmethod
def _is_dir(member: ArchiveMember) -> bool:
assert isinstance(member, tarfile.TarInfo) # please mypy
return member.isdir()
class TarGzParser(TarParser): class TarGzParser(TarParser):
compression = ':gz' compression = ':gz'
@ -460,3 +469,8 @@ class ZipParser(ArchiveBasedAbstractParser):
assert isinstance(member, zipfile.ZipInfo) # please mypy assert isinstance(member, zipfile.ZipInfo) # please mypy
member.compress_type = compression member.compress_type = compression
return member return member
@staticmethod
def _is_dir(member: ArchiveMember) -> bool:
assert isinstance(member, zipfile.ZipInfo) # please mypy
return member.is_dir()