summaryrefslogtreecommitdiff
path: root/Lib/tarfile.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/tarfile.py')
-rwxr-xr-xLib/tarfile.py46
1 files changed, 29 insertions, 17 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index f9e5c189a1..720bbf7d13 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1410,9 +1410,9 @@ class TarFile(object):
can be determined, `mode' is overridden by `fileobj's mode.
`fileobj' is not closed, when TarFile is closed.
"""
- modes = {"r": "rb", "a": "r+b", "w": "wb"}
+ modes = {"r": "rb", "a": "r+b", "w": "wb", "x": "xb"}
if mode not in modes:
- raise ValueError("mode must be 'r', 'a' or 'w'")
+ raise ValueError("mode must be 'r', 'a', 'w' or 'x'")
self.mode = mode
self._mode = modes[mode]
@@ -1525,6 +1525,15 @@ class TarFile(object):
'w:bz2' open for writing with bzip2 compression
'w:xz' open for writing with lzma compression
+ 'x' or 'x:' create a tarfile exclusively without compression, raise
+ an exception if the file is already created
+ 'x:gz' create an gzip compressed tarfile, raise an exception
+ if the file is already created
+ 'x:bz2' create an bzip2 compressed tarfile, raise an exception
+ if the file is already created
+ 'x:xz' create an lzma compressed tarfile, raise an exception
+ if the file is already created
+
'r|*' open a stream of tar blocks with transparent compression
'r|' open an uncompressed stream of tar blocks for reading
'r|gz' open a gzip compressed stream of tar blocks
@@ -1583,7 +1592,7 @@ class TarFile(object):
t._extfileobj = False
return t
- elif mode in ("a", "w"):
+ elif mode in ("a", "w", "x"):
return cls.taropen(name, mode, fileobj, **kwargs)
raise ValueError("undiscernible mode")
@@ -1592,8 +1601,8 @@ class TarFile(object):
def taropen(cls, name, mode="r", fileobj=None, **kwargs):
"""Open uncompressed tar archive name for reading or writing.
"""
- if mode not in ("r", "a", "w"):
- raise ValueError("mode must be 'r', 'a' or 'w'")
+ if mode not in ("r", "a", "w", "x"):
+ raise ValueError("mode must be 'r', 'a', 'w' or 'x'")
return cls(name, mode, fileobj, **kwargs)
@classmethod
@@ -1601,8 +1610,8 @@ class TarFile(object):
"""Open gzip compressed tar archive name for reading or writing.
Appending is not allowed.
"""
- if mode not in ("r", "w"):
- raise ValueError("mode must be 'r' or 'w'")
+ if mode not in ("r", "w", "x"):
+ raise ValueError("mode must be 'r', 'w' or 'x'")
try:
import gzip
@@ -1635,8 +1644,8 @@ class TarFile(object):
"""Open bzip2 compressed tar archive name for reading or writing.
Appending is not allowed.
"""
- if mode not in ("r", "w"):
- raise ValueError("mode must be 'r' or 'w'.")
+ if mode not in ("r", "w", "x"):
+ raise ValueError("mode must be 'r', 'w' or 'x'")
try:
import bz2
@@ -1664,8 +1673,8 @@ class TarFile(object):
"""Open lzma compressed tar archive name for reading or writing.
Appending is not allowed.
"""
- if mode not in ("r", "w"):
- raise ValueError("mode must be 'r' or 'w'")
+ if mode not in ("r", "w", "x"):
+ raise ValueError("mode must be 'r', 'w' or 'x'")
try:
import lzma
@@ -1752,7 +1761,7 @@ class TarFile(object):
addfile(). If given, `arcname' specifies an alternative name for the
file in the archive.
"""
- self._check("aw")
+ self._check("awx")
# When fileobj is given, replace name by
# fileobj's real name.
@@ -1843,14 +1852,17 @@ class TarFile(object):
tarinfo.devminor = os.minor(statres.st_rdev)
return tarinfo
- def list(self, verbose=True):
+ def list(self, verbose=True, *, members=None):
"""Print a table of contents to sys.stdout. If `verbose' is False, only
the names of the members are printed. If it is True, an `ls -l'-like
- output is produced.
+ output is produced. `members' is optional and must be a subset of the
+ list returned by getmembers().
"""
self._check()
- for tarinfo in self:
+ if members is None:
+ members = self
+ for tarinfo in members:
if verbose:
_safe_print(stat.filemode(tarinfo.mode))
_safe_print("%s/%s" % (tarinfo.uname or tarinfo.uid,
@@ -1883,7 +1895,7 @@ class TarFile(object):
TarInfo object, if it returns None the TarInfo object will be
excluded from the archive.
"""
- self._check("aw")
+ self._check("awx")
if arcname is None:
arcname = name
@@ -1940,7 +1952,7 @@ class TarFile(object):
On Windows platforms, `fileobj' should always be opened with mode
'rb' to avoid irritation about the file size.
"""
- self._check("aw")
+ self._check("awx")
tarinfo = copy.copy(tarinfo)