Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/api/private/deposit_read.py
Show All 29 Lines | def aggregate_tarballs(extraction_dir, archive_paths): | ||||
Args: | Args: | ||||
extraction_dir (path): Path to use for the tarballs computation | extraction_dir (path): Path to use for the tarballs computation | ||||
archive_paths ([str]): Deposit's archive paths | archive_paths ([str]): Deposit's archive paths | ||||
Returns: | Returns: | ||||
Tuple (directory to clean up, archive path (aggregated or not)) | Tuple (directory to clean up, archive path (aggregated or not)) | ||||
""" | """ | ||||
if len(archive_paths) > 1: | # rebuild one zip archive from (possibly) multiple ones | ||||
# need to rebuild one archive from multiple ones | |||||
os.makedirs(extraction_dir, 0o755, exist_ok=True) | os.makedirs(extraction_dir, 0o755, exist_ok=True) | ||||
dir_path = tempfile.mkdtemp(prefix='swh.deposit-', | dir_path = tempfile.mkdtemp(prefix='swh.deposit-', dir=extraction_dir) | ||||
dir=extraction_dir) | |||||
# root folder to build an aggregated tarball | # root folder to build an aggregated tarball | ||||
aggregated_tarball_rootdir = os.path.join(dir_path, 'aggregate') | aggregated_tarball_rootdir = os.path.join(dir_path, 'aggregate') | ||||
os.makedirs(aggregated_tarball_rootdir, 0o755, exist_ok=True) | os.makedirs(aggregated_tarball_rootdir, 0o755, exist_ok=True) | ||||
# uncompress in a temporary location all archives | # uncompress in a temporary location all archives | ||||
for archive_path in archive_paths: | for archive_path in archive_paths: | ||||
tarball.uncompress(archive_path, aggregated_tarball_rootdir) | tarball.uncompress(archive_path, aggregated_tarball_rootdir) | ||||
# Aggregate into one big tarball the multiple smaller ones | # Aggregate into one big tarball the multiple smaller ones | ||||
temp_tarpath = tarball.compress( | temp_tarpath = tarball.compress( | ||||
aggregated_tarball_rootdir + '.zip', | aggregated_tarball_rootdir + '.zip', | ||||
nature='zip', | nature='zip', | ||||
dirpath_or_files=aggregated_tarball_rootdir) | dirpath_or_files=aggregated_tarball_rootdir) | ||||
# can already clean up temporary directory | # can already clean up temporary directory | ||||
shutil.rmtree(aggregated_tarball_rootdir) | shutil.rmtree(aggregated_tarball_rootdir) | ||||
try: | try: | ||||
yield temp_tarpath | yield temp_tarpath | ||||
finally: | finally: | ||||
shutil.rmtree(dir_path) | shutil.rmtree(dir_path) | ||||
else: # only 1 archive, no need to do fancy actions (and no cleanup step) | |||||
yield archive_paths[0] | |||||
class SWHDepositReadArchives(SWHPrivateAPIView, SWHGetDepositAPI, | class SWHDepositReadArchives(SWHPrivateAPIView, SWHGetDepositAPI, | ||||
DepositReadMixin): | DepositReadMixin): | ||||
"""Dedicated class to read a deposit's raw archives content. | """Dedicated class to read a deposit's raw archives content. | ||||
Only GET is supported. | Only GET is supported. | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 158 Lines • Show Last 20 Lines |