|
4 | 4 | import queue |
5 | 5 | import shutil |
6 | 6 | import tempfile |
| 7 | +import zipfile |
7 | 8 | from collections import defaultdict |
8 | 9 | from pathlib import Path |
9 | 10 | from typing import Any |
@@ -292,7 +293,33 @@ def _prepare_output_dir(self, tenant_name: str): |
292 | 293 |
|
293 | 294 | def _create_archive(self): |
294 | 295 | ctx.logger.debug('Creating snapshot archive') |
295 | | - shutil.make_archive(self._archive_dest, 'zip', self._temp_dir) |
| 296 | + base_name = os.fspath(self._archive_dest) |
| 297 | + root_dir = self._temp_dir |
| 298 | + zip_filename = base_name + ".zip" |
| 299 | + archive_dir = os.path.dirname(base_name) |
| 300 | + if not os.path.exists(archive_dir): |
| 301 | + ctx.logger.info("creating %s", archive_dir) |
| 302 | + os.makedirs(archive_dir) |
| 303 | + with zipfile.ZipFile( |
| 304 | + zip_filename, |
| 305 | + "w", |
| 306 | + compression=zipfile.ZIP_DEFLATED, |
| 307 | + compresslevel=9, |
| 308 | + ) as zf: |
| 309 | + base_dir = os.path.join(root_dir, os.curdir) |
| 310 | + base_dir = os.path.normpath(base_dir) |
| 311 | + for dirpath, dirnames, filenames in os.walk(base_dir): |
| 312 | + arcdirpath = os.path.relpath(dirpath, root_dir) |
| 313 | + for name in sorted(dirnames): |
| 314 | + path = os.path.join(dirpath, name) |
| 315 | + arcname = os.path.join(arcdirpath, name) |
| 316 | + zf.write(path, arcname) |
| 317 | + for name in filenames: |
| 318 | + path = os.path.join(dirpath, name) |
| 319 | + path = os.path.normpath(path) |
| 320 | + if os.path.isfile(path): |
| 321 | + arcname = os.path.join(arcdirpath, name) |
| 322 | + zf.write(path, arcname) |
296 | 323 |
|
297 | 324 | def _upload_archive(self): |
298 | 325 | ctx.logger.debug('Uploading archive to manager') |
|
0 commit comments