[Python-checkins] r56019 - in python/trunk: Doc/lib/libtarfile.tex Lib/tarfile.py Lib/test/test_tarfile.py Misc/NEWS

lars.gustaebel python-checkins at python.org
Mon Jun 18 13:42:16 CEST 2007


Author: lars.gustaebel
Date: Mon Jun 18 13:42:11 2007
New Revision: 56019

Modified:
   python/trunk/Doc/lib/libtarfile.tex
   python/trunk/Lib/tarfile.py
   python/trunk/Lib/test/test_tarfile.py
   python/trunk/Misc/NEWS
Log:
Added exclude keyword argument to the TarFile.add() method.


Modified: python/trunk/Doc/lib/libtarfile.tex
==============================================================================
--- python/trunk/Doc/lib/libtarfile.tex	(original)
+++ python/trunk/Doc/lib/libtarfile.tex	Mon Jun 18 13:42:11 2007
@@ -314,13 +314,16 @@
     \end{notice}
 \end{methoddesc}
 
-\begin{methoddesc}{add}{name\optional{, arcname\optional{, recursive}}}
+\begin{methoddesc}{add}{name\optional{, arcname\optional{, recursive\optional{, exclude}}}}
     Add the file \var{name} to the archive. \var{name} may be any type
     of file (directory, fifo, symbolic link, etc.).
     If given, \var{arcname} specifies an alternative name for the file in the
     archive. Directories are added recursively by default.
-    This can be avoided by setting \var{recursive} to \constant{False};
-    the default is \constant{True}.
+    This can be avoided by setting \var{recursive} to \constant{False}.
+    If \var{exclude} is given it must be a function that takes one filename
+    argument and returns a boolean value. Depending on this value the
+    respective file is either excluded (\constant{True}) or added
+    (\constant{False}).
 \end{methoddesc}
 
 \begin{methoddesc}{addfile}{tarinfo\optional{, fileobj}}

Modified: python/trunk/Lib/tarfile.py
==============================================================================
--- python/trunk/Lib/tarfile.py	(original)
+++ python/trunk/Lib/tarfile.py	Mon Jun 18 13:42:11 2007
@@ -1923,18 +1923,24 @@
                     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 @@
                 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 @@
             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)

Modified: python/trunk/Lib/test/test_tarfile.py
==============================================================================
--- python/trunk/Lib/test/test_tarfile.py	(original)
+++ python/trunk/Lib/test/test_tarfile.py	Mon Jun 18 13:42:11 2007
@@ -558,6 +558,27 @@
         os.chdir(cwd)
         self.assert_(tar.getnames() == [], "added the archive to itself")
 
+    def test_exclude(self):
+        tempdir = os.path.join(TEMPDIR, "exclude")
+        os.mkdir(tempdir)
+        try:
+            for name in ("foo", "bar", "baz"):
+                name = os.path.join(tempdir, name)
+                open(name, "wb").close()
+
+            def exclude(name):
+                return os.path.isfile(name)
+
+            tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1")
+            tar.add(tempdir, arcname="empty_dir", exclude=exclude)
+            tar.close()
+
+            tar = tarfile.open(tmpname, "r")
+            self.assertEqual(len(tar.getmembers()), 1)
+            self.assertEqual(tar.getnames()[0], "empty_dir")
+        finally:
+            shutil.rmtree(tempdir)
+
 
 class StreamWriteTest(unittest.TestCase):
 

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Mon Jun 18 13:42:11 2007
@@ -231,6 +231,8 @@
 Library
 -------
 
+- tarfile.py: Added "exclude" keyword argument to TarFile.add().
+
 - Bug #1734723: Fix repr.Repr() so it doesn't ignore the maxtuple attribute.
 
 - The urlopen function of urllib2 now has an optional timeout parameter (note 


More information about the Python-checkins mailing list