diff options
author | Lars Gustäbel <lars@gustaebel.de> | 2009-09-12 10:28:15 (GMT) |
---|---|---|
committer | Lars Gustäbel <lars@gustaebel.de> | 2009-09-12 10:28:15 (GMT) |
commit | 21121e64b4245e51b85b9d2bc9b29acb86ae79eb (patch) | |
tree | 8ffeaf69afab8ef548aea364b49b6c86476f80ab /Lib/test | |
parent | d4c7eb16475f6ca662120aeda4518051f031c0d8 (diff) | |
download | cpython-21121e64b4245e51b85b9d2bc9b29acb86ae79eb.zip cpython-21121e64b4245e51b85b9d2bc9b29acb86ae79eb.tar.gz cpython-21121e64b4245e51b85b9d2bc9b29acb86ae79eb.tar.bz2 |
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/test')
-rw-r--r-- | Lib/test/test_tarfile.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 8c9b0ec..1240e1a 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -660,6 +660,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. |