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.
........
This commit is contained in:
Lars Gustäbel 2009-09-12 10:44:00 +00:00
parent 01054d7f89
commit 049d2aa952
4 changed files with 76 additions and 8 deletions

View file

@ -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.