summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorLars Gustäbel <lars@gustaebel.de>2009-09-12 10:44:00 (GMT)
committerLars Gustäbel <lars@gustaebel.de>2009-09-12 10:44:00 (GMT)
commit049d2aa952ae8deb8932a2b84aa1d503b83ad6a5 (patch)
treed285d20f9522eecbc90596b36114c466dcb98d12 /Lib
parent01054d7f89227066ea3c42ad815f7a58a156340c (diff)
downloadcpython-049d2aa952ae8deb8932a2b84aa1d503b83ad6a5.zip
cpython-049d2aa952ae8deb8932a2b84aa1d503b83ad6a5.tar.gz
cpython-049d2aa952ae8deb8932a2b84aa1d503b83ad6a5.tar.bz2
Merged revisions 74750 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r74750 | lars.gustaebel | 2009-09-12 12:28:15 +0200 (Sat, 12 Sep 2009) | 9 lines Issue #6856: Add a filter keyword argument to TarFile.add(). The filter argument must be a function that takes a TarInfo object argument, changes it and returns it again. If the function returns None the TarInfo object will be excluded from the archive. The exclude argument is deprecated from now on, because it does something similar but is not as flexible. ........
Diffstat (limited to 'Lib')
-rw-r--r--Lib/tarfile.py27
-rw-r--r--Lib/test/test_tarfile.py28
2 files changed, 49 insertions, 6 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index ebaffd9..7b7c25e 100644
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1898,13 +1898,16 @@ class TarFile(object):
print("link to", tarinfo.linkname, end=' ')
print()
- def add(self, name, arcname=None, recursive=True, exclude=None):
+ def add(self, name, arcname=None, recursive=True, exclude=None, filter=None):
"""Add the file `name' to the archive. `name' may be any type of file
(directory, fifo, symbolic link, etc.). If given, `arcname'
specifies an alternative name for the file in the archive.
Directories are added recursively by default. This can be avoided by
setting `recursive' to False. `exclude' is a function that should
- return True for each filename to be excluded.
+ return True for each filename to be excluded. `filter' is a function
+ that expects a TarInfo object argument and returns the changed
+ TarInfo object, if it returns None the TarInfo object will be
+ excluded from the archive.
"""
self._check("aw")
@@ -1912,9 +1915,13 @@ class TarFile(object):
arcname = name
# Exclude pathnames.
- if exclude is not None and exclude(name):
- self._dbg(2, "tarfile: Excluded %r" % name)
- return
+ if exclude is not None:
+ import warnings
+ warnings.warn("use the filter argument instead",
+ DeprecationWarning, 2)
+ if exclude(name):
+ self._dbg(2, "tarfile: Excluded %r" % name)
+ return
# Skip if somebody tries to archive the archive...
if self.name is not None and os.path.abspath(name) == self.name:
@@ -1930,6 +1937,13 @@ class TarFile(object):
self._dbg(1, "tarfile: Unsupported type %r" % name)
return
+ # Change or exclude the TarInfo object.
+ if filter is not None:
+ tarinfo = filter(tarinfo)
+ if tarinfo is None:
+ self._dbg(2, "tarfile: Excluded %r" % name)
+ return
+
# Append the tar header and data to the archive.
if tarinfo.isreg():
f = bltn_open(name, "rb")
@@ -1940,7 +1954,8 @@ class TarFile(object):
self.addfile(tarinfo)
if recursive:
for f in os.listdir(name):
- self.add(os.path.join(name, f), os.path.join(arcname, f), recursive, exclude)
+ self.add(os.path.join(name, f), os.path.join(arcname, f),
+ recursive, exclude, filter)
else:
self.addfile(tarinfo)
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index 83f4a8c..e916879 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -659,6 +659,34 @@ class WriteTest(WriteTestBase):
finally:
shutil.rmtree(tempdir)
+ def test_filter(self):
+ tempdir = os.path.join(TEMPDIR, "filter")
+ os.mkdir(tempdir)
+ try:
+ for name in ("foo", "bar", "baz"):
+ name = os.path.join(tempdir, name)
+ open(name, "wb").close()
+
+ def filter(tarinfo):
+ if os.path.basename(tarinfo.name) == "bar":
+ return
+ tarinfo.uid = 123
+ tarinfo.uname = "foo"
+ return tarinfo
+
+ tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1")
+ tar.add(tempdir, arcname="empty_dir", filter=filter)
+ tar.close()
+
+ tar = tarfile.open(tmpname, "r")
+ for tarinfo in tar:
+ self.assertEqual(tarinfo.uid, 123)
+ self.assertEqual(tarinfo.uname, "foo")
+ self.assertEqual(len(tar.getmembers()), 3)
+ tar.close()
+ finally:
+ shutil.rmtree(tempdir)
+
# Guarantee that stored pathnames are not modified. Don't
# remove ./ or ../ or double slashes. Still make absolute
# pathnames relative.