Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes zipping and unzipping work for empty folders for migration. #4582

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions codalab/worker/file_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import bz2
import hashlib
import stat
import zipfile

from codalab.common import BINARY_PLACEHOLDER, UsageError
from codalab.common import parse_linked_bundle_url
Expand Down Expand Up @@ -124,6 +125,9 @@ def zip_directory(
# zip needs to be used with relative paths, so that the final directory structure
# is correct -- https://stackoverflow.com/questions/11249624/zip-stating-absolute-paths-but-only-keeping-part-of-them.
'.',
# -i . is needed for empty folders; -i ./\* so zip recursively.
'-i',
'./\*',
]

if ignore_file:
Expand Down Expand Up @@ -177,6 +181,12 @@ def unzip_directory(fileobj: IO[bytes], directory_path: str, force: bool = False
with tempfile.NamedTemporaryFile() as f:
shutil.copyfileobj(fileobj, f)
f.flush()

with zipfile.ZipFile(f.name, 'r') as zip_file:
# Empty zip files cannot be unzipped: return early.
if len(zip_file.infolist()) == 0:
return

proc = subprocess.Popen(
['unzip', '-q', f.name, '-d', directory_path],
stdout=subprocess.PIPE,
Expand Down
24 changes: 10 additions & 14 deletions tests/unit/worker/file_util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,16 +408,6 @@ def test_always_ignore(self):
self.assertFalse(os.path.exists(os.path.join(output_dir, 'dir', '__MACOSX')))
self.assertFalse(os.path.exists(os.path.join(output_dir, 'dir', '._ignored2')))


class TarArchiveTest(ArchiveTestBase, unittest.TestCase):
"""Archive test for tar/gzip methods."""

def archive(self, *args, **kwargs):
return tar_gzip_directory(*args, **kwargs)

def unarchive(self, *args, **kwargs):
return un_tar_directory(*args, **kwargs)

def test_do_not_always_ignore(self):
temp_dir = tempfile.mkdtemp()
self.addCleanup(lambda: remove_path(temp_dir))
Expand All @@ -431,6 +421,16 @@ def test_do_not_always_ignore(self):
self.assertTrue(os.path.exists(os.path.join(output_dir, 'dir', '__MACOSX')))


class TarArchiveTest(ArchiveTestBase, unittest.TestCase):
"""Archive test for tar/gzip methods."""

def archive(self, *args, **kwargs):
return tar_gzip_directory(*args, **kwargs)

def unarchive(self, *args, **kwargs):
return un_tar_directory(*args, **kwargs)


class ZipArchiveTest(ArchiveTestBase, unittest.TestCase):
"""Archive test for zip methods."""

Expand All @@ -444,10 +444,6 @@ def archive(self, *args, **kwargs):
def unarchive(self, *args, **kwargs):
return unzip_directory(*args, **kwargs)

def test_empty(self):
# zip doesn't create files when it's supposed to create an empty zip file.
pass

def test_exclude_ignore(self):
# TODO(Ashwin): make zip files properly work with exclude ignore.
pass
Loading