Skip to content

Commit

Permalink
Rewrite zip archive creation (#389)
Browse files Browse the repository at this point in the history
This reimplements the functionality of `shutil.make_archive` without
the need for calls to `os.chdir`. This is thread-safe, and fixes the
issues we've been seeing with finished build showing console output
for other builds.

It will also hopefully fix the occasional zip file creation issues
we've been seeing.
  • Loading branch information
josephharrington authored Jul 12, 2017
1 parent 97c122f commit 9177760
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions app/util/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import shutil
import tarfile
import tempfile
import zipfile

from app.util.process_utils import Popen_with_delayed_expansion

Expand Down Expand Up @@ -131,9 +132,17 @@ def zip_directory(target_dir: str, archive_filename: str) -> str:
"""
# Create the archive in a temp location and then move it to the target dir.
# (Otherwise the resulting archive will include an extra zero-byte file.)
tmp_path = shutil.make_archive(tempfile.mktemp(), 'zip', target_dir)
target_path = os.path.join(target_dir, archive_filename)
shutil.move(tmp_path, target_path)
with tempfile.TemporaryDirectory() as temp_dirpath:
tmp_zip_filename = os.path.join(temp_dirpath, 'clusterrunner_tmp__' + archive_filename)
with zipfile.ZipFile(tmp_zip_filename, 'w', compression=zipfile.ZIP_DEFLATED) as zf:
for dirpath, dirnames, filenames in os.walk(target_dir):
for filename in filenames:
path = os.path.normpath(os.path.join(dirpath, filename))
if os.path.isfile(path):
relpath = os.path.relpath(path, target_dir)
zf.write(path, relpath)
shutil.move(tmp_zip_filename, target_path)
return target_path


Expand Down

0 comments on commit 9177760

Please sign in to comment.