+from contextlib import closing
import os.path
-import gzip
import tarfile
TARGZ_DEFAULT_COMPRESSION_LEVEL = 9
path_in_tar = archive_name(path)
tar.add(path, path_in_tar)
compression = TARGZ_DEFAULT_COMPRESSION_LEVEL
- tar = tarfile.TarFile.gzopen(tarball_path, 'w', compresslevel=compression)
- try:
+ with closing(tarfile.TarFile.open(tarball_path, 'w:gz',
+ compresslevel=compression)) as tar:
for source in sources:
source_path = source
if os.path.isdir(source):
else:
path_in_tar = archive_name(source_path)
tar.add(source_path, path_in_tar) # filename, arcname
- finally:
- tar.close()
def decompress(tarball_path, base_dir):
"""Decompress the gzipped tarball into directory base_dir.
"""
- # !!! This class method is not documented in the online doc
- # nor is bz2open!
- tar = tarfile.TarFile.gzopen(tarball_path, mode='r')
- try:
+ with closing(tarfile.TarFile.open(tarball_path)) as tar:
tar.extractall(base_dir)
- finally:
- tar.close()