summaryrefslogtreecommitdiffstats
path: root/Lib/shutil.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-08-06 15:50:19 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-08-06 15:50:19 (GMT)
commit1121377cdb0bef5a4a07699ae3d93c69a1f47071 (patch)
tree13cd19069226278293cc35e2f7b2b7171a695be1 /Lib/shutil.py
parentaf78ef18241040a91e793bfc511a182d9e5d0bc2 (diff)
downloadcpython-1121377cdb0bef5a4a07699ae3d93c69a1f47071.zip
cpython-1121377cdb0bef5a4a07699ae3d93c69a1f47071.tar.gz
cpython-1121377cdb0bef5a4a07699ae3d93c69a1f47071.tar.bz2
Issue #5411: Added support for the "xztar" format in the shutil module.
Diffstat (limited to 'Lib/shutil.py')
-rw-r--r--Lib/shutil.py27
1 files changed, 23 insertions, 4 deletions
diff --git a/Lib/shutil.py b/Lib/shutil.py
index b59017b..344d9d3 100644
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -21,6 +21,13 @@ except ImportError:
_BZ2_SUPPORTED = False
try:
+ import lzma
+ del lzma
+ _LZMA_SUPPORTED = True
+except ImportError:
+ _LZMA_SUPPORTED = False
+
+try:
from pwd import getpwnam
except ImportError:
getpwnam = None
@@ -580,14 +587,14 @@ def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
"""Create a (possibly compressed) tar file from all the files under
'base_dir'.
- 'compress' must be "gzip" (the default), "bzip2", or None.
+ 'compress' must be "gzip" (the default), "bzip2", "xz", or None.
'owner' and 'group' can be used to define an owner and a group for the
archive that is being built. If not provided, the current owner and group
will be used.
The output tar file will be named 'base_name' + ".tar", possibly plus
- the appropriate compression extension (".gz", or ".bz2").
+ the appropriate compression extension (".gz", ".bz2", or ".xz").
Returns the output filename.
"""
@@ -598,6 +605,10 @@ def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
tar_compression['bzip2'] = 'bz2'
compress_ext['bzip2'] = '.bz2'
+ if _LZMA_SUPPORTED:
+ tar_compression['xz'] = 'xz'
+ compress_ext['xz'] = '.xz'
+
# flags for compression program, each element of list will be an argument
if compress is not None and compress not in compress_ext:
raise ValueError("bad value for 'compress', or compression format not "
@@ -684,6 +695,10 @@ if _BZ2_SUPPORTED:
_ARCHIVE_FORMATS['bztar'] = (_make_tarball, [('compress', 'bzip2')],
"bzip2'ed tar-file")
+if _LZMA_SUPPORTED:
+ _ARCHIVE_FORMATS['xztar'] = (_make_tarball, [('compress', 'xz')],
+ "xz'ed tar-file")
+
def get_archive_formats():
"""Returns a list of supported formats for archiving and unarchiving.
@@ -872,7 +887,7 @@ def _unpack_zipfile(filename, extract_dir):
zip.close()
def _unpack_tarfile(filename, extract_dir):
- """Unpack tar/tar.gz/tar.bz2 `filename` to `extract_dir`
+ """Unpack tar/tar.gz/tar.bz2/tar.xz `filename` to `extract_dir`
"""
try:
tarobj = tarfile.open(filename)
@@ -891,9 +906,13 @@ _UNPACK_FORMATS = {
}
if _BZ2_SUPPORTED:
- _UNPACK_FORMATS['bztar'] = (['.bz2'], _unpack_tarfile, [],
+ _UNPACK_FORMATS['bztar'] = (['.tar.bz2', '.tbz2'], _unpack_tarfile, [],
"bzip2'ed tar-file")
+if _LZMA_SUPPORTED:
+ _UNPACK_FORMATS['xztar'] = (['.tar.xz', '.txz'], _unpack_tarfile, [],
+ "xz'ed tar-file")
+
def _find_unpack_format(filename):
for name, info in _UNPACK_FORMATS.items():
for extension in info[0]: