blob: ce07c3ebd47d3f5a7d984dd2592a1ed6fa2f6817 [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
Martin v. Löwis75b9da42004-08-18 13:57:44 +0000264 of the above restrictions.
265 \versionchanged[\var{posix} defaults to false.]{2.4}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000266\end{memberdesc}
267
Fred Drake3bbd1152004-01-13 23:41:32 +0000268\begin{memberdesc}{dereference}
269 If false, add symbolic and hard links to archive. If true, add the
270 content of the target files to the archive. This has no effect on
271 systems that do not support symbolic links.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000272\end{memberdesc}
273
Fred Drake3bbd1152004-01-13 23:41:32 +0000274\begin{memberdesc}{ignore_zeros}
275 If false, treat an empty block as the end of the archive. If true,
276 skip empty (and invalid) blocks and try to get as many members as
277 possible. This is only useful for concatenated or damaged
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000278 archives.
279\end{memberdesc}
280
281\begin{memberdesc}{debug=0}
Fred Drake3bbd1152004-01-13 23:41:32 +0000282 To be set from \code{0} (no debug messages; the default) up to
283 \code{3} (all debug messages). The messages are written to
284 \code{sys.stdout}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000285\end{memberdesc}
286
Fred Drake3bbd1152004-01-13 23:41:32 +0000287\begin{memberdesc}{errorlevel}
288 If \code{0} (the default), all errors are ignored when using
289 \method{extract()}. Nevertheless, they appear as error messages
290 in the debug output, when debugging is enabled. If \code{1}, all
291 \emph{fatal} errors are raised as \exception{OSError} or
292 \exception{IOError} exceptions. If \code{2}, all \emph{non-fatal}
293 errors are raised as \exception{TarError} exceptions as well.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000294\end{memberdesc}
295
296%-----------------
297% TarInfo Objects
298%-----------------
299
300\subsection{TarInfo Objects \label{tarinfo-objects}}
301
Fred Drake3bbd1152004-01-13 23:41:32 +0000302A \class{TarInfo} object represents one member in a
303\class{TarFile}. Aside from storing all required attributes of a file
304(like file type, size, time, permissions, owner etc.), it provides
305some useful methods to determine its type. It does \emph{not} contain
306the file's data itself.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000307
Fred Drake3bbd1152004-01-13 23:41:32 +0000308\class{TarInfo} objects are returned by \class{TarFile}'s methods
309\method{getmember()}, \method{getmembers()} and \method{gettarinfo()}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000310
311\begin{classdesc}{TarInfo}{\optional{name}}
312 Create a \class{TarInfo} object.
313\end{classdesc}
314
315\begin{methoddesc}{frombuf}{}
316 Create and return a \class{TarInfo} object from a string buffer.
317\end{methoddesc}
318
319\begin{methoddesc}{tobuf}{}
320 Create a string buffer from a \class{TarInfo} object.
321\end{methoddesc}
322
323A \code{TarInfo} object has the following public data attributes:
Fred Drake3bbd1152004-01-13 23:41:32 +0000324
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000325\begin{memberdesc}{name}
326 Name of the archive member.
327\end{memberdesc}
328
329\begin{memberdesc}{size}
330 Size in bytes.
331\end{memberdesc}
332
333\begin{memberdesc}{mtime}
334 Time of last modification.
335\end{memberdesc}
336
337\begin{memberdesc}{mode}
338 Permission bits.
339\end{memberdesc}
340
341\begin{memberdesc}{type}
Fred Drake3bbd1152004-01-13 23:41:32 +0000342 File type. \var{type} is usually one of these constants:
343 \constant{REGTYPE}, \constant{AREGTYPE}, \constant{LNKTYPE},
344 \constant{SYMTYPE}, \constant{DIRTYPE}, \constant{FIFOTYPE},
345 \constant{CONTTYPE}, \constant{CHRTYPE}, \constant{BLKTYPE},
346 \constant{GNUTYPE_SPARSE}. To determine the type of a
347 \class{TarInfo} object more conveniently, use the \code{is_*()}
348 methods below.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000349\end{memberdesc}
350
351\begin{memberdesc}{linkname}
Fred Drake3bbd1152004-01-13 23:41:32 +0000352 Name of the target file name, which is only present in
353 \class{TarInfo} objects of type \constant{LNKTYPE} and
354 \constant{SYMTYPE}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000355\end{memberdesc}
356
Fred Drake3bbd1152004-01-13 23:41:32 +0000357\begin{memberdesc}{uid}
358 User ID of the user who originally stored this member.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000359\end{memberdesc}
360
Fred Drake3bbd1152004-01-13 23:41:32 +0000361\begin{memberdesc}{gid}
362 Group ID of the user who originally stored this member.
363\end{memberdesc}
364
365\begin{memberdesc}{uname}
366 User name.
367\end{memberdesc}
368
369\begin{memberdesc}{gname}
370 Group name.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000371\end{memberdesc}
372
373A \class{TarInfo} object also provides some convenient query methods:
Fred Drake3bbd1152004-01-13 23:41:32 +0000374
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000375\begin{methoddesc}{isfile}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000376 Return \constant{True} if the \class{Tarinfo} object is a regular
377 file.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000378\end{methoddesc}
379
380\begin{methoddesc}{isreg}{}
381 Same as \method{isfile()}.
382\end{methoddesc}
383
384\begin{methoddesc}{isdir}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000385 Return \constant{True} if it is a directory.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000386\end{methoddesc}
387
388\begin{methoddesc}{issym}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000389 Return \constant{True} if it is a symbolic link.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000390\end{methoddesc}
391
392\begin{methoddesc}{islnk}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000393 Return \constant{True} if it is a hard link.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000394\end{methoddesc}
395
396\begin{methoddesc}{ischr}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000397 Return \constant{True} if it is a character device.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000398\end{methoddesc}
399
400\begin{methoddesc}{isblk}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000401 Return \constant{True} if it is a block device.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000402\end{methoddesc}
403
404\begin{methoddesc}{isfifo}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000405 Return \constant{True} if it is a FIFO.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000406\end{methoddesc}
407
408\begin{methoddesc}{isdev}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000409 Return \constant{True} if it is one of character device, block
410 device or FIFO.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000411\end{methoddesc}
412
413%------------------------
414% Examples
415%------------------------
416
417\subsection{Examples \label{tar-examples}}
418
419How to create an uncompressed tar archive from a list of filenames:
420\begin{verbatim}
421import tarfile
422tar = tarfile.open("sample.tar", "w")
423for name in ["foo", "bar", "quux"]:
424 tar.add(name)
425tar.close()
426\end{verbatim}
427
428How to read a gzip compressed tar archive and display some member information:
429\begin{verbatim}
430import tarfile
431tar = tarfile.open("sample.tar.gz", "r:gz")
432for tarinfo in tar:
433 print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
434 if tarinfo.isreg():
435 print "a regular file."
436 elif tarinfo.isdir():
437 print "a directory."
438 else:
439 print "something else."
440tar.close()
441\end{verbatim}
442
443How to create a tar archive with faked information:
444\begin{verbatim}
445import tarfile
446tar = tarfile.open("sample.tar.gz", "w:gz")
447for name in namelist:
448 tarinfo = tar.gettarinfo(name, "fakeproj-1.0/" + name)
449 tarinfo.uid = 123
450 tarinfo.gid = 456
451 tarinfo.uname = "johndoe"
452 tarinfo.gname = "fake"
453 tar.addfile(tarinfo, file(name))
454tar.close()
455\end{verbatim}
456
457The \emph{only} way to extract an uncompressed tar stream from
458\code{sys.stdin}:
459\begin{verbatim}
460import sys
461import tarfile
462tar = tarfile.open(mode="r|", fileobj=sys.stdin)
463for tarinfo in tar:
464 tar.extract(tarinfo)
465tar.close()
466\end{verbatim}