Commit c671c5da authored by Jason R. Coombs's avatar Jason R. Coombs

Replace get_zip_class with a specialized constructor.

parent f78c3bea
...@@ -69,17 +69,25 @@ def _build_egg(egg, archive_filename, to_dir): ...@@ -69,17 +69,25 @@ def _build_egg(egg, archive_filename, to_dir):
raise IOError('Could not build the egg.') raise IOError('Could not build the egg.')
def get_zip_class(): class ContextualZipFile(zipfile.ZipFile):
""" """
Supplement ZipFile class to support context manager for Python 2.6 Supplement ZipFile class to support context manager for Python 2.6
""" """
class ContextualZipFile(zipfile.ZipFile):
def __enter__(self): def __enter__(self):
return self return self
def __exit__(self, type, value, traceback): def __exit__(self, type, value, traceback):
self.close() self.close()
@classmethod
def compat(cls, *args, **kwargs):
"""
Construct a ZipFile or ContextualZipFile as appropriate
"""
zf_has_exit = hasattr(zipfile.ZipFile, '__exit__') zf_has_exit = hasattr(zipfile.ZipFile, '__exit__')
return zipfile.ZipFile if zf_has_exit else ContextualZipFile class_ = zipfile.ZipFile if zf_has_exit else cls
return class_(*args, **kwargs)
@contextlib.contextmanager @contextlib.contextmanager
...@@ -90,7 +98,7 @@ def archive_context(filename): ...@@ -90,7 +98,7 @@ def archive_context(filename):
old_wd = os.getcwd() old_wd = os.getcwd()
try: try:
os.chdir(tmpdir) os.chdir(tmpdir)
with get_zip_class()(filename) as archive: with ContextualZipFile.compat(filename) as archive:
archive.extractall() archive.extractall()
# going in the directory # going in the directory
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment