blob: c84112340df847aab4be73fc0b053cbfe6bce498 [file] [log] [blame]
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001\section{\module{tarfile} --- Read and write tar archive files}
2
3\declaremodule{standard}{tarfile}
4\modulesynopsis{Read and write tar-format archive files.}
5\versionadded{2.3}
6
7\moduleauthor{Lars Gust\"abel}{lars@gustaebel.de}
8\sectionauthor{Lars Gust\"abel}{lars@gustaebel.de}
9
10The \module{tarfile} module makes it possible to read and create tar archives.
11Some facts and figures:
12
13\begin{itemize}
14\item reads and writes \module{gzip} and \module{bzip2} compressed archives.
Fred Drake3bbd1152004-01-13 23:41:32 +000015\item creates \POSIX{} 1003.1-1990 compliant or GNU tar compatible archives.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000016\item reads GNU tar extensions \emph{longname}, \emph{longlink} and
17 \emph{sparse}.
18\item stores pathnames of unlimited length using GNU tar extensions.
19\item handles directories, regular files, hardlinks, symbolic links, fifos,
20 character devices and block devices and is able to acquire and
21 restore file information like timestamp, access permissions and owner.
22\item can handle tape devices.
23\end{itemize}
24
25\begin{funcdesc}{open}{\optional{name\optional{, mode
26 \optional{, fileobj\optional{, bufsize}}}}}
27 Return a \class{TarFile} object for the pathname \var{name}.
28 For detailed information on \class{TarFile} objects,
29 see \citetitle{TarFile Objects} (section \ref{tarfile-objects}).
30
31 \var{mode} has to be a string of the form \code{'filemode[:compression]'},
32 it defaults to \code{'r'}. Here is a full list of mode combinations:
33
34 \begin{tableii}{c|l}{code}{mode}{action}
35 \lineii{'r'}{Open for reading with transparent compression (recommended).}
36 \lineii{'r:'}{Open for reading exclusively without compression.}
37 \lineii{'r:gz'}{Open for reading with gzip compression.}
38 \lineii{'r:bz2'}{Open for reading with bzip2 compression.}
39 \lineii{'a' or 'a:'}{Open for appending with no compression.}
40 \lineii{'w' or 'w:'}{Open for uncompressed writing.}
41 \lineii{'w:gz'}{Open for gzip compressed writing.}
42 \lineii{'w:bz2'}{Open for bzip2 compressed writing.}
43 \end{tableii}
44
45 Note that \code{'a:gz'} or \code{'a:bz2'} is not possible.
46 If \var{mode} is not suitable to open a certain (compressed) file for
47 reading, \exception{ReadError} is raised. Use \var{mode} \code{'r'} to
48 avoid this. If a compression method is not supported,
49 \exception{CompressionError} is raised.
50
51 If \var{fileobj} is specified, it is used as an alternative to
52 a file object opened for \var{name}.
53
54 For special purposes, there is a second format for \var{mode}:
Fred Drake3bbd1152004-01-13 23:41:32 +000055 \code{'filemode|[compression]'}. \function{open()} will return a
56 \class{TarFile} object that processes its data as a stream of
57 blocks. No random seeking will be done on the file. If given,
58 \var{fileobj} may be any object that has a \method{read()} or
59 \method{write()} method (depending on the \var{mode}).
60 \var{bufsize} specifies the blocksize and defaults to \code{20 *
61 512} bytes. Use this variant in combination with
62 e.g. \code{sys.stdin}, a socket file object or a tape device.
63 However, such a \class{TarFile} object is limited in that it does
64 not allow to be accessed randomly, see ``Examples''
65 (section~\ref{tar-examples}). The currently possible modes:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000066
Fred Drake3bbd1152004-01-13 23:41:32 +000067 \begin{tableii}{c|l}{code}{Mode}{Action}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000068 \lineii{'r|'}{Open a \emph{stream} of uncompressed tar blocks for reading.}
69 \lineii{'r|gz'}{Open a gzip compressed \emph{stream} for reading.}
70 \lineii{'r|bz2'}{Open a bzip2 compressed \emph{stream} for reading.}
71 \lineii{'w|'}{Open an uncompressed \emph{stream} for writing.}
72 \lineii{'w|gz'}{Open an gzip compressed \emph{stream} for writing.}
73 \lineii{'w|bz2'}{Open an bzip2 compressed \emph{stream} for writing.}
74 \end{tableii}
75\end{funcdesc}
76
77\begin{classdesc*}{TarFile}
78 Class for reading and writing tar archives. Do not use this
79 class directly, better use \function{open()} instead.
Fred Drake3bbd1152004-01-13 23:41:32 +000080 See ``TarFile Objects'' (section~\ref{tarfile-objects}).
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000081\end{classdesc*}
82
83\begin{funcdesc}{is_tarfile}{name}
Fred Drake3bbd1152004-01-13 23:41:32 +000084 Return \constant{True} if \var{name} is a tar archive file, that
85 the \module{tarfile} module can read.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000086\end{funcdesc}
87
88\begin{classdesc}{TarFileCompat}{filename\optional{, mode\optional{,
Fred Drake3bbd1152004-01-13 23:41:32 +000089 compression}}}
90 Class for limited access to tar archives with a
91 \refmodule{zipfile}-like interface. Please consult the
92 documentation of the \refmodule{zipfile} module for more details.
93 \var{compression} must be one of the following constants:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000094 \begin{datadesc}{TAR_PLAIN}
95 Constant for an uncompressed tar archive.
96 \end{datadesc}
97 \begin{datadesc}{TAR_GZIPPED}
Fred Drake3bbd1152004-01-13 23:41:32 +000098 Constant for a \refmodule{gzip} compressed tar archive.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000099 \end{datadesc}
100\end{classdesc}
101
102\begin{excdesc}{TarError}
103 Base class for all \module{tarfile} exceptions.
104\end{excdesc}
105
106\begin{excdesc}{ReadError}
107 Is raised when a tar archive is opened, that either cannot be handled by
108 the \module{tarfile} module or is somehow invalid.
109\end{excdesc}
110
111\begin{excdesc}{CompressionError}
112 Is raised when a compression method is not supported or when the data
113 cannot be decoded properly.
114\end{excdesc}
115
116\begin{excdesc}{StreamError}
117 Is raised for the limitations that are typical for stream-like
118 \class{TarFile} objects.
119\end{excdesc}
120
121\begin{excdesc}{ExtractError}
122 Is raised for \emph{non-fatal} errors when using \method{extract()}, but
123 only if \member{TarFile.errorlevel}\code{ == 2}.
124\end{excdesc}
125
126\begin{seealso}
Fred Drake3bbd1152004-01-13 23:41:32 +0000127 \seemodule{zipfile}{Documentation of the \refmodule{zipfile}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000128 standard module.}
129
130 \seetitle[http://www.gnu.org/manual/tar/html_chapter/tar_8.html\#SEC118]
131 {GNU tar manual, Standard Section}{Documentation for tar archive files,
132 including GNU tar extensions.}
133\end{seealso}
134
135%-----------------
136% TarFile Objects
137%-----------------
138
139\subsection{TarFile Objects \label{tarfile-objects}}
140
141The \class{TarFile} object provides an interface to a tar archive. A tar
142archive is a sequence of blocks. An archive member (a stored file) is made up
143of a header block followed by data blocks. It is possible, to store a file in a
144tar archive several times. Each archive member is represented by a
145\class{TarInfo} object, see \citetitle{TarInfo Objects} (section
146\ref{tarinfo-objects}) for details.
147
148\begin{classdesc}{TarFile}{\optional{name
149 \optional{, mode\optional{, fileobj}}}}
150 Open an \emph{(uncompressed)} tar archive \var{name}.
151 \var{mode} is either \code{'r'} to read from an existing archive,
152 \code{'a'} to append data to an existing file or \code{'w'} to create a new
153 file overwriting an existing one. \var{mode} defaults to \code{'r'}.
154
155 If \var{fileobj} is given, it is used for reading or writing data.
156 If it can be determined, \var{mode} is overridden by \var{fileobj}'s mode.
157 \begin{notice}
158 \var{fileobj} is not closed, when \class{TarFile} is closed.
159 \end{notice}
160\end{classdesc}
161
162\begin{methoddesc}{open}{...}
163 Alternative constructor. The \function{open()} function on module level is
Fred Drake3bbd1152004-01-13 23:41:32 +0000164 actually a shortcut to this classmethod. See section~\ref{module-tarfile}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000165 for details.
166\end{methoddesc}
167
168\begin{methoddesc}{getmember}{name}
169 Return a \class{TarInfo} object for member \var{name}. If \var{name} can
170 not be found in the archive, \exception{KeyError} is raised.
171 \begin{notice}
172 If a member occurs more than once in the archive, its last
173 occurence is assumed to be the most up-to-date version.
174 \end{notice}
175\end{methoddesc}
176
177\begin{methoddesc}{getmembers}{}
178 Return the members of the archive as a list of \class{TarInfo} objects.
179 The list has the same order as the members in the archive.
180\end{methoddesc}
181
182\begin{methoddesc}{getnames}{}
183 Return the members as a list of their names. It has the same order as
184 the list returned by \method{getmembers()}.
185\end{methoddesc}
186
187\begin{methoddesc}{list}{verbose=True}
188 Print a table of contents to \code{sys.stdout}. If \var{verbose} is
Fred Drake3bbd1152004-01-13 23:41:32 +0000189 \constant{False}, only the names of the members are printed. If it is
190 \constant{True}, output similar to that of \program{ls -l} is produced.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000191\end{methoddesc}
192
193\begin{methoddesc}{next}{}
194 Return the next member of the archive as a \class{TarInfo} object, when
195 \class{TarFile} is opened for reading. Return \code{None} if there is no
196 more available.
197\end{methoddesc}
198
199\begin{methoddesc}{extract}{member\optional{, path}}
200 Extract a member from the archive to the current working directory,
201 using its full name. Its file information is extracted as accurately as
202 possible.
203 \var{member} may be a filename or a \class{TarInfo} object.
204 You can specify a different directory using \var{path}.
205\end{methoddesc}
206
207\begin{methoddesc}{extractfile}{member}
208 Extract a member from the archive as a file object.
209 \var{member} may be a filename or a \class{TarInfo} object.
210 If \var{member} is a regular file, a file-like object is returned.
211 If \var{member} is a link, a file-like object is constructed from the
212 link's target.
213 If \var{member} is none of the above, \code{None} is returned.
214 \begin{notice}
215 The file-like object is read-only and provides the following methods:
216 \method{read()}, \method{readline()}, \method{readlines()},
217 \method{seek()}, \method{tell()}.
218 \end{notice}
219\end{methoddesc}
220
Fred Drake3bbd1152004-01-13 23:41:32 +0000221\begin{methoddesc}{add}{name\optional{, arcname\optional{, recursive}}}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000222 Add the file \var{name} to the archive. \var{name} may be any type
223 of file (directory, fifo, symbolic link, etc.).
224 If given, \var{arcname} specifies an alternative name for the file in the
225 archive. Directories are added recursively by default.
Fred Drake3bbd1152004-01-13 23:41:32 +0000226 This can be avoided by setting \var{recursive} to \constant{False};
227 the default is \constant{True}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000228\end{methoddesc}
229
230\begin{methoddesc}{addfile}{tarinfo\optional{, fileobj}}
231 Add the \class{TarInfo} object \var{tarinfo} to the archive.
Fred Drake3bbd1152004-01-13 23:41:32 +0000232 If \var{fileobj} is given, \code{\var{tarinfo}.size} bytes are read
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000233 from it and added to the archive. You can create \class{TarInfo} objects
234 using \method{gettarinfo()}.
235 \begin{notice}
236 On Windows platforms, \var{fileobj} should always be opened with mode
237 \code{'rb'} to avoid irritation about the file size.
238 \end{notice}
239\end{methoddesc}
240
Fred Drake3bbd1152004-01-13 23:41:32 +0000241\begin{methoddesc}{gettarinfo}{\optional{name\optional{,
242 arcname\optional{, fileobj}}}}
243 Create a \class{TarInfo} object for either the file \var{name} or
244 the file object \var{fileobj} (using \function{os.fstat()} on its
245 file descriptor). You can modify some of the \class{TarInfo}'s
246 attributes before you add it using \method{addfile()}. If given,
247 \var{arcname} specifies an alternative name for the file in the
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000248 archive.
249\end{methoddesc}
250
251\begin{methoddesc}{close}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000252 Close the \class{TarFile}. In write mode, two finishing zero
253 blocks are appended to the archive.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000254\end{methoddesc}
255
Fred Drake3bbd1152004-01-13 23:41:32 +0000256\begin{memberdesc}{posix}
257 If true, create a \POSIX{} 1003.1-1990 compliant archive. GNU
258 extensions are not used, because they are not part of the \POSIX{}
Neal Norwitzd96d1012004-07-20 22:23:02 +0000259 standard. This limits the length of filenames to at most 256,
260 link names to 100 characters and the maximum file size to 8
261 gigabytes. A \exception{ValueError} is raised if a file exceeds
262 this limit. If false, create a GNU tar compatible archive. It
263 will not be \POSIX{} compliant, but can store files without any
264 of the above restrictions.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000265\end{memberdesc}
266
Fred Drake3bbd1152004-01-13 23:41:32 +0000267\begin{memberdesc}{dereference}
268 If false, add symbolic and hard links to archive. If true, add the
269 content of the target files to the archive. This has no effect on
270 systems that do not support symbolic links.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000271\end{memberdesc}
272
Fred Drake3bbd1152004-01-13 23:41:32 +0000273\begin{memberdesc}{ignore_zeros}
274 If false, treat an empty block as the end of the archive. If true,
275 skip empty (and invalid) blocks and try to get as many members as
276 possible. This is only useful for concatenated or damaged
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000277 archives.
278\end{memberdesc}
279
280\begin{memberdesc}{debug=0}
Fred Drake3bbd1152004-01-13 23:41:32 +0000281 To be set from \code{0} (no debug messages; the default) up to
282 \code{3} (all debug messages). The messages are written to
283 \code{sys.stdout}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000284\end{memberdesc}
285
Fred Drake3bbd1152004-01-13 23:41:32 +0000286\begin{memberdesc}{errorlevel}
287 If \code{0} (the default), all errors are ignored when using
288 \method{extract()}. Nevertheless, they appear as error messages
289 in the debug output, when debugging is enabled. If \code{1}, all
290 \emph{fatal} errors are raised as \exception{OSError} or
291 \exception{IOError} exceptions. If \code{2}, all \emph{non-fatal}
292 errors are raised as \exception{TarError} exceptions as well.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000293\end{memberdesc}
294
295%-----------------
296% TarInfo Objects
297%-----------------
298
299\subsection{TarInfo Objects \label{tarinfo-objects}}
300
Fred Drake3bbd1152004-01-13 23:41:32 +0000301A \class{TarInfo} object represents one member in a
302\class{TarFile}. Aside from storing all required attributes of a file
303(like file type, size, time, permissions, owner etc.), it provides
304some useful methods to determine its type. It does \emph{not} contain
305the file's data itself.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000306
Fred Drake3bbd1152004-01-13 23:41:32 +0000307\class{TarInfo} objects are returned by \class{TarFile}'s methods
308\method{getmember()}, \method{getmembers()} and \method{gettarinfo()}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000309
310\begin{classdesc}{TarInfo}{\optional{name}}
311 Create a \class{TarInfo} object.
312\end{classdesc}
313
314\begin{methoddesc}{frombuf}{}
315 Create and return a \class{TarInfo} object from a string buffer.
316\end{methoddesc}
317
318\begin{methoddesc}{tobuf}{}
319 Create a string buffer from a \class{TarInfo} object.
320\end{methoddesc}
321
322A \code{TarInfo} object has the following public data attributes:
Fred Drake3bbd1152004-01-13 23:41:32 +0000323
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000324\begin{memberdesc}{name}
325 Name of the archive member.
326\end{memberdesc}
327
328\begin{memberdesc}{size}
329 Size in bytes.
330\end{memberdesc}
331
332\begin{memberdesc}{mtime}
333 Time of last modification.
334\end{memberdesc}
335
336\begin{memberdesc}{mode}
337 Permission bits.
338\end{memberdesc}
339
340\begin{memberdesc}{type}
Fred Drake3bbd1152004-01-13 23:41:32 +0000341 File type. \var{type} is usually one of these constants:
342 \constant{REGTYPE}, \constant{AREGTYPE}, \constant{LNKTYPE},
343 \constant{SYMTYPE}, \constant{DIRTYPE}, \constant{FIFOTYPE},
344 \constant{CONTTYPE}, \constant{CHRTYPE}, \constant{BLKTYPE},
345 \constant{GNUTYPE_SPARSE}. To determine the type of a
346 \class{TarInfo} object more conveniently, use the \code{is_*()}
347 methods below.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000348\end{memberdesc}
349
350\begin{memberdesc}{linkname}
Fred Drake3bbd1152004-01-13 23:41:32 +0000351 Name of the target file name, which is only present in
352 \class{TarInfo} objects of type \constant{LNKTYPE} and
353 \constant{SYMTYPE}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000354\end{memberdesc}
355
Fred Drake3bbd1152004-01-13 23:41:32 +0000356\begin{memberdesc}{uid}
357 User ID of the user who originally stored this member.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000358\end{memberdesc}
359
Fred Drake3bbd1152004-01-13 23:41:32 +0000360\begin{memberdesc}{gid}
361 Group ID of the user who originally stored this member.
362\end{memberdesc}
363
364\begin{memberdesc}{uname}
365 User name.
366\end{memberdesc}
367
368\begin{memberdesc}{gname}
369 Group name.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000370\end{memberdesc}
371
372A \class{TarInfo} object also provides some convenient query methods:
Fred Drake3bbd1152004-01-13 23:41:32 +0000373
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000374\begin{methoddesc}{isfile}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000375 Return \constant{True} if the \class{Tarinfo} object is a regular
376 file.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000377\end{methoddesc}
378
379\begin{methoddesc}{isreg}{}
380 Same as \method{isfile()}.
381\end{methoddesc}
382
383\begin{methoddesc}{isdir}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000384 Return \constant{True} if it is a directory.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000385\end{methoddesc}
386
387\begin{methoddesc}{issym}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000388 Return \constant{True} if it is a symbolic link.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000389\end{methoddesc}
390
391\begin{methoddesc}{islnk}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000392 Return \constant{True} if it is a hard link.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000393\end{methoddesc}
394
395\begin{methoddesc}{ischr}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000396 Return \constant{True} if it is a character device.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000397\end{methoddesc}
398
399\begin{methoddesc}{isblk}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000400 Return \constant{True} if it is a block device.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000401\end{methoddesc}
402
403\begin{methoddesc}{isfifo}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000404 Return \constant{True} if it is a FIFO.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000405\end{methoddesc}
406
407\begin{methoddesc}{isdev}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000408 Return \constant{True} if it is one of character device, block
409 device or FIFO.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000410\end{methoddesc}
411
412%------------------------
413% Examples
414%------------------------
415
416\subsection{Examples \label{tar-examples}}
417
418How to create an uncompressed tar archive from a list of filenames:
419\begin{verbatim}
420import tarfile
421tar = tarfile.open("sample.tar", "w")
422for name in ["foo", "bar", "quux"]:
423 tar.add(name)
424tar.close()
425\end{verbatim}
426
427How to read a gzip compressed tar archive and display some member information:
428\begin{verbatim}
429import tarfile
430tar = tarfile.open("sample.tar.gz", "r:gz")
431for tarinfo in tar:
432 print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
433 if tarinfo.isreg():
434 print "a regular file."
435 elif tarinfo.isdir():
436 print "a directory."
437 else:
438 print "something else."
439tar.close()
440\end{verbatim}
441
442How to create a tar archive with faked information:
443\begin{verbatim}
444import tarfile
445tar = tarfile.open("sample.tar.gz", "w:gz")
446for name in namelist:
447 tarinfo = tar.gettarinfo(name, "fakeproj-1.0/" + name)
448 tarinfo.uid = 123
449 tarinfo.gid = 456
450 tarinfo.uname = "johndoe"
451 tarinfo.gname = "fake"
452 tar.addfile(tarinfo, file(name))
453tar.close()
454\end{verbatim}
455
456The \emph{only} way to extract an uncompressed tar stream from
457\code{sys.stdin}:
458\begin{verbatim}
459import sys
460import tarfile
461tar = tarfile.open(mode="r|", fileobj=sys.stdin)
462for tarinfo in tar:
463 tar.extract(tarinfo)
464tar.close()
465\end{verbatim}