summaryrefslogtreecommitdiff
path: root/Lib/tarfile.py
diff options
context:
space:
mode:
authorLars Gustäbel <lars@gustaebel.de>2007-06-18 11:42:11 +0000
committerLars Gustäbel <lars@gustaebel.de>2007-06-18 11:42:11 +0000
commit104490e6159e2b541cf51c17399453906f59f825 (patch)
tree4c16ba783779df699c598d5008ec71ed348cb6ec /Lib/tarfile.py
parent9d0476f7da7be1a03a5b5d8e48d072b83be2f31d (diff)
downloadcpython-git-104490e6159e2b541cf51c17399453906f59f825.tar.gz
Added exclude keyword argument to the TarFile.add() method.
Diffstat (limited to 'Lib/tarfile.py')
-rw-r--r--Lib/tarfile.py14
1 files changed, 10 insertions, 4 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 107041e828..1ab13f0e41 100644
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1923,18 +1923,24 @@ class TarFile(object):
print "link to", tarinfo.linkname,
print
- def add(self, name, arcname=None, recursive=True):
+ def add(self, name, arcname=None, recursive=True, exclude=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.
+ setting `recursive' to False. `exclude' is a function that should
+ return True for each filename to be excluded.
"""
self._check("aw")
if arcname is None:
arcname = name
+ # Exclude pathnames.
+ if exclude is not None and 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:
self._dbg(2, "tarfile: Skipped %r" % name)
@@ -1947,7 +1953,7 @@ class TarFile(object):
if arcname == ".":
arcname = ""
for f in os.listdir(name):
- self.add(f, os.path.join(arcname, f))
+ self.add(f, os.path.join(arcname, f), recursive, exclude)
return
self._dbg(1, name)
@@ -1969,7 +1975,7 @@ 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))
+ self.add(os.path.join(name, f), os.path.join(arcname, f), recursive, exclude)
else:
self.addfile(tarinfo)