diff options
author | Tarek Ziadé <ziade.tarek@gmail.com> | 2010-04-29 13:34:35 (GMT) |
---|---|---|
committer | Tarek Ziadé <ziade.tarek@gmail.com> | 2010-04-29 13:34:35 (GMT) |
commit | ffa155ad83ac40c3eeb55cbe8fe16a304a23cfe1 (patch) | |
tree | b1317a8eb32c18185fb2f852b2eb3d3b0004e107 /Lib/shutil.py | |
parent | bf043491a10e65706c0305050de86c91ab2fae62 (diff) | |
download | cpython-ffa155ad83ac40c3eeb55cbe8fe16a304a23cfe1.zip cpython-ffa155ad83ac40c3eeb55cbe8fe16a304a23cfe1.tar.gz cpython-ffa155ad83ac40c3eeb55cbe8fe16a304a23cfe1.tar.bz2 |
make sure shutil checks for bz2 availability before it uses it.
Diffstat (limited to 'Lib/shutil.py')
-rw-r--r-- | Lib/shutil.py | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/Lib/shutil.py b/Lib/shutil.py index c07f394..601d9c2 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -14,6 +14,12 @@ import errno import tarfile try: + import bz2 + _BZ2_SUPPORTED = True +except ImportError: + _BZ2_SUPPORTED = False + +try: from pwd import getpwnam except ImportError: getpwnam = None @@ -376,13 +382,17 @@ def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0, Returns the output filename. """ - tar_compression = {'gzip': 'gz', 'bzip2': 'bz2', None: ''} - compress_ext = {'gzip': '.gz', 'bzip2': '.bz2'} + tar_compression = {'gzip': 'gz', None: ''} + compress_ext = {'gzip': '.gz'} + + if _BZ2_SUPPORTED: + tar_compression['bzip2'] = 'bz2' + compress_ext['bzip2'] = '.bz2' # flags for compression program, each element of list will be an argument if compress is not None and compress not in compress_ext.keys(): - raise ValueError("bad value for 'compress': must be None, 'gzip', or " - "'bzip2'") + raise ValueError("bad value for 'compress', or compression format not " + "supported : {0}".format(compress)) archive_name = base_name + '.tar' + compress_ext.get(compress, '') archive_dir = os.path.dirname(archive_name) @@ -488,6 +498,10 @@ _ARCHIVE_FORMATS = { 'zip': (_make_zipfile, [],"ZIP file") } +if _BZ2_SUPPORTED: + _ARCHIVE_FORMATS['bztar'] = (_make_tarball, [('compress', 'bzip2')], + "bzip2'ed tar-file") + def get_archive_formats(): """Returns a list of supported formats for archiving and unarchiving. @@ -690,11 +704,14 @@ def _unpack_tarfile(filename, extract_dir): _UNPACK_FORMATS = { 'gztar': (['.tar.gz', '.tgz'], _unpack_tarfile, [], "gzip'ed tar-file"), - 'bztar': (['.bz2'], _unpack_tarfile, [], "bzip2'ed tar-file"), 'tar': (['.tar'], _unpack_tarfile, [], "uncompressed tar file"), 'zip': (['.zip'], _unpack_zipfile, [], "ZIP file") } +if _BZ2_SUPPORTED: + _UNPACK_FORMATS['bztar'] = (['.bz2'], _unpack_tarfile, [], + "bzip2'ed tar-file") + def _find_unpack_format(filename): for name, info in _UNPACK_FORMATS.items(): for extension in info[0]: |