diff --git a/devtools/tarball.py b/devtools/tarball.py index e44d870..e461e8f 100644 --- a/devtools/tarball.py +++ b/devtools/tarball.py @@ -1,5 +1,5 @@ +from contextlib import closing import os.path -import gzip import tarfile TARGZ_DEFAULT_COMPRESSION_LEVEL = 9 @@ -29,8 +29,8 @@ def make_tarball(tarball_path, sources, base_dir, prefix_dir=''): 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): @@ -38,16 +38,9 @@ def make_tarball(tarball_path, sources, base_dir, prefix_dir=''): 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()