Skip to content
Snippets Groups Projects
Commit 632673f6 authored by Antoine Lambert's avatar Antoine Lambert
Browse files

tarball: Try to get archive format before unpacking it

By default shutil.unpack_archive will try to match the archive format
from its extension but it does not do it in a case insensitive way.
Thus trying to extract a file named "archive.ZIP" will fail.

So try to detect archive format in a case insensitive way by processing
the data returned by shutil.get_unpack_formats prior unpacking.
parent e45733e9
No related branches found
Tags v0.13.2
No related merge requests found
......@@ -90,11 +90,16 @@ def uncompress(tarpath: str, dest: str):
"""
try:
os.makedirs(dest, exist_ok=True)
shutil.unpack_archive(tarpath, extract_dir=dest)
format = None
for format_, exts, _ in shutil.get_unpack_formats():
if any([tarpath.lower().endswith(ext.lower()) for ext in exts]):
format = format_
break
shutil.unpack_archive(tarpath, extract_dir=dest, format=format)
except shutil.ReadError as e:
raise ValueError(f"Problem during unpacking {tarpath}. Reason: {e}")
except NotImplementedError:
if tarpath.endswith(".zip"):
if tarpath.lower().endswith(".zip"):
_unpack_zip(tarpath, dest)
else:
raise
......
......@@ -222,3 +222,21 @@ def test_unpcompress_zip_imploded(tmp_path, datadir):
tarball.uncompress(zippath, extract_dir)
assert len(os.listdir(extract_dir)) > 0
def test_uncompress_upper_archive_extension(tmp_path, datadir):
"""Copy test archives in a temporary directory but turn their names
to uppercase, then check they can be successfully extracted.
"""
archives_path = os.path.join(datadir, "archives")
archive_files = [
f
for f in os.listdir(archives_path)
if os.path.isfile(os.path.join(archives_path, f))
]
for archive_file in archive_files:
archive_file_upper = os.path.join(tmp_path, archive_file.upper())
extract_dir = os.path.join(tmp_path, archive_file)
shutil.copy(os.path.join(archives_path, archive_file), archive_file_upper)
tarball.uncompress(archive_file_upper, extract_dir)
assert len(os.listdir(extract_dir)) > 0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment