Skip to content

Commit 9cc0bb8

Browse files
committed
update TarFile usage
1 parent 494950a commit 9cc0bb8

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

devtools/tarball.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
from contextlib import closing
12
import os.path
2-
import gzip
33
import tarfile
44

55
TARGZ_DEFAULT_COMPRESSION_LEVEL = 9
@@ -29,25 +29,18 @@ def visit(tar, dirname, names):
2929
path_in_tar = archive_name(path)
3030
tar.add(path, path_in_tar)
3131
compression = TARGZ_DEFAULT_COMPRESSION_LEVEL
32-
tar = tarfile.TarFile.gzopen(tarball_path, 'w', compresslevel=compression)
33-
try:
32+
with closing(tarfile.TarFile.open(tarball_path, 'w:gz',
33+
compresslevel=compression)) as tar:
3434
for source in sources:
3535
source_path = source
3636
if os.path.isdir(source):
3737
os.path.walk(source_path, visit, tar)
3838
else:
3939
path_in_tar = archive_name(source_path)
4040
tar.add(source_path, path_in_tar) # filename, arcname
41-
finally:
42-
tar.close()
4341

4442
def decompress(tarball_path, base_dir):
4543
"""Decompress the gzipped tarball into directory base_dir.
4644
"""
47-
# !!! This class method is not documented in the online doc
48-
# nor is bz2open!
49-
tar = tarfile.TarFile.gzopen(tarball_path, mode='r')
50-
try:
45+
with closing(tarfile.TarFile.open(tarball_path)) as tar:
5146
tar.extractall(base_dir)
52-
finally:
53-
tar.close()

0 commit comments

Comments
 (0)