Changeset View
Changeset View
Standalone View
Standalone View
swh/core/tarball.py
Show All 16 Lines | def _unpack_tar(tarpath: str, extract_dir: str) -> str: | ||||
"""Unpack tarballs unsupported by the standard python library. Examples | """Unpack tarballs unsupported by the standard python library. Examples | ||||
include tar.Z, tar.lz, tar.x, etc.... | include tar.Z, tar.lz, tar.x, etc.... | ||||
As this implementation relies on the `tar` command, this function supports | As this implementation relies on the `tar` command, this function supports | ||||
the same compression the tar command supports. | the same compression the tar command supports. | ||||
This expects the `extract_dir` to exist. | This expects the `extract_dir` to exist. | ||||
Raises | Raises: | ||||
shutil.ReadError in case of issue uncompressing the archive (tarpath | shutil.ReadError in case of issue uncompressing the archive (tarpath | ||||
does not exist, extract_dir does not exist, etc...) | does not exist, extract_dir does not exist, etc...) | ||||
Returns | Returns: | ||||
full path to the uncompressed directory. | full path to the uncompressed directory. | ||||
""" | """ | ||||
try: | try: | ||||
run(["tar", "xf", tarpath, "-C", extract_dir], check=True) | run(["tar", "xf", tarpath, "-C", extract_dir], check=True) | ||||
return extract_dir | return extract_dir | ||||
except Exception as e: | except Exception as e: | ||||
raise shutil.ReadError( | raise shutil.ReadError( | ||||
f"Unable to uncompress {tarpath} to {extract_dir}. Reason: {e}" | f"Unable to uncompress {tarpath} to {extract_dir}. Reason: {e}" | ||||
) | ) | ||||
def _unpack_zip(zippath: str, extract_dir: str) -> str: | |||||
"""Unpack zip files unsupported by the standard python library, for instance | |||||
those with legacy compression type 6 (implode). | |||||
This expects the `extract_dir` to exist. | |||||
Raises: | |||||
shutil.ReadError in case of issue uncompressing the archive (zippath | |||||
does not exist, extract_dir does not exist, etc...) | |||||
Returns: | |||||
full path to the uncompressed directory. | |||||
""" | |||||
try: | |||||
run(["unzip", "-d", extract_dir, zippath], check=True) | |||||
return extract_dir | |||||
except Exception as e: | |||||
raise shutil.ReadError( | |||||
f"Unable to uncompress {zippath} to {extract_dir}. Reason: {e}" | |||||
) | |||||
def register_new_archive_formats(): | def register_new_archive_formats(): | ||||
"""Register new archive formats to uncompress | """Register new archive formats to uncompress | ||||
""" | """ | ||||
registered_formats = [f[0] for f in shutil.get_unpack_formats()] | registered_formats = [f[0] for f in shutil.get_unpack_formats()] | ||||
for name, extensions, function in ADDITIONAL_ARCHIVE_FORMATS: | for name, extensions, function in ADDITIONAL_ARCHIVE_FORMATS: | ||||
if name in registered_formats: | if name in registered_formats: | ||||
continue | continue | ||||
Show All 15 Lines | def uncompress(tarpath: str, dest: str): | ||||
Raises: | Raises: | ||||
ValueError when a problem occurs during unpacking | ValueError when a problem occurs during unpacking | ||||
""" | """ | ||||
try: | try: | ||||
shutil.unpack_archive(tarpath, extract_dir=dest) | shutil.unpack_archive(tarpath, extract_dir=dest) | ||||
except shutil.ReadError as e: | except shutil.ReadError as e: | ||||
raise ValueError(f"Problem during unpacking {tarpath}. Reason: {e}") | raise ValueError(f"Problem during unpacking {tarpath}. Reason: {e}") | ||||
except NotImplementedError: | |||||
if tarpath.endswith(".zip"): | |||||
return _unpack_zip(tarpath, dest) | |||||
else: | |||||
vlorentz: to show the original traceback | |||||
raise | |||||
normalize_permissions(dest) | normalize_permissions(dest) | ||||
def normalize_permissions(path: str): | def normalize_permissions(path: str): | ||||
"""Normalize the permissions of all files and directories under `path`. | """Normalize the permissions of all files and directories under `path`. | ||||
This makes all subdirectories and files with the user executable bit set mode | This makes all subdirectories and files with the user executable bit set mode | ||||
▲ Show 20 Lines • Show All 76 Lines • Show Last 20 Lines |
to show the original traceback