Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
104490e6
Commit
104490e6
authored
Jun 18, 2007
by
Lars Gustäbel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added exclude keyword argument to the TarFile.add() method.
parent
9d0476f7
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
39 additions
and
7 deletions
+39
-7
Doc/lib/libtarfile.tex
Doc/lib/libtarfile.tex
+6
-3
Lib/tarfile.py
Lib/tarfile.py
+10
-4
Lib/test/test_tarfile.py
Lib/test/test_tarfile.py
+21
-0
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Doc/lib/libtarfile.tex
View file @
104490e6
...
@@ -314,13 +314,16 @@ tar archive several times. Each archive member is represented by a
...
@@ -314,13 +314,16 @@ tar archive several times. Each archive member is represented by a
\end{notice}
\end{notice}
\end{methoddesc}
\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
Add the file
\var
{
name
}
to the archive.
\var
{
name
}
may be any type
of file (directory, fifo, symbolic link, etc.).
of file (directory, fifo, symbolic link, etc.).
If given,
\var
{
arcname
}
specifies an alternative name for the file in the
If given,
\var
{
arcname
}
specifies an alternative name for the file in the
archive. Directories are added recursively by default.
archive. Directories are added recursively by default.
This can be avoided by setting
\var
{
recursive
}
to
\constant
{
False
}
;
This can be avoided by setting
\var
{
recursive
}
to
\constant
{
False
}
.
the default is
\constant
{
True
}
.
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}
\end{methoddesc}
\begin{methoddesc}
{
addfile
}{
tarinfo
\optional
{
, fileobj
}}
\begin{methoddesc}
{
addfile
}{
tarinfo
\optional
{
, fileobj
}}
...
...
Lib/tarfile.py
View file @
104490e6
...
@@ -1923,18 +1923,24 @@ class TarFile(object):
...
@@ -1923,18 +1923,24 @@ class TarFile(object):
print "
link
to
", tarinfo.linkname,
print "
link
to
", tarinfo.linkname,
print
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
"""Add the file `name' to the archive. `name' may be any type of file
(directory, fifo, symbolic link, etc.). If given, `arcname'
(directory, fifo, symbolic link, etc.). If given, `arcname'
specifies an alternative name for the file in the archive.
specifies an alternative name for the file in the archive.
Directories are added recursively by default. This can be avoided by
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
")
self._check("
aw
")
if arcname is None:
if arcname is None:
arcname = name
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...
# Skip if somebody tries to archive the archive...
if self.name is not None and os.path.abspath(name) == self.name:
if self.name is not None and os.path.abspath(name) == self.name:
self._dbg(2, "
tarfile
:
Skipped
%
r" % name)
self._dbg(2, "
tarfile
:
Skipped
%
r" % name)
...
@@ -1947,7 +1953,7 @@ class TarFile(object):
...
@@ -1947,7 +1953,7 @@ class TarFile(object):
if arcname == "
.
":
if arcname == "
.
":
arcname = ""
arcname = ""
for f in os.listdir(name):
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
return
self._dbg(1, name)
self._dbg(1, name)
...
@@ -1969,7 +1975,7 @@ class TarFile(object):
...
@@ -1969,7 +1975,7 @@ class TarFile(object):
self.addfile(tarinfo)
self.addfile(tarinfo)
if recursive:
if recursive:
for f in os.listdir(name):
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:
else:
self.addfile(tarinfo)
self.addfile(tarinfo)
...
...
Lib/test/test_tarfile.py
View file @
104490e6
...
@@ -558,6 +558,27 @@ class WriteTest(unittest.TestCase):
...
@@ -558,6 +558,27 @@ class WriteTest(unittest.TestCase):
os
.
chdir
(
cwd
)
os
.
chdir
(
cwd
)
self
.
assert_
(
tar
.
getnames
()
==
[],
"added the archive to itself"
)
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
):
class
StreamWriteTest
(
unittest
.
TestCase
):
...
...
Misc/NEWS
View file @
104490e6
...
@@ -231,6 +231,8 @@ Core and builtins
...
@@ -231,6 +231,8 @@ Core and builtins
Library
Library
-------
-------
- tarfile.py: Added "exclude" keyword argument to TarFile.add().
- Bug #1734723: Fix repr.Repr() so it doesn'
t
ignore
the
maxtuple
attribute
.
- 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
-
The
urlopen
function
of
urllib2
now
has
an
optional
timeout
parameter
(
note
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment