Skip to content

Commit

Permalink
update TarFile usage
Browse files Browse the repository at this point in the history
  • Loading branch information
cdunn2001 committed Jan 24, 2015
1 parent 494950a commit 9cc0bb8
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions devtools/tarball.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from contextlib import closing
import os.path
import gzip
import tarfile

TARGZ_DEFAULT_COMPRESSION_LEVEL = 9
Expand Down Expand Up @@ -29,25 +29,18 @@ def visit(tar, dirname, names):
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):
os.path.walk(source_path, visit, tar)
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()

0 comments on commit 9cc0bb8

Please sign in to comment.