blob: fec2737dc512844aec6f4293931ec5610db5d188 [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{}
259 standard. This limits the length of filenames to at most 256 and
260 link names to 100 characters. A \exception{ValueError} is raised
261 if a pathname exceeds this limit. If false, create a GNU tar
262 compatible archive. It will not be \POSIX{} compliant, but can
263 store pathnames of unlimited length.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000264\end{memberdesc}
265
Fred Drake3bbd1152004-01-13 23:41:32 +0000266\begin{memberdesc}{dereference}
267 If false, add symbolic and hard links to archive. If true, add the
268 content of the target files to the archive. This has no effect on
269 systems that do not support symbolic links.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000270\end{memberdesc}
271
Fred Drake3bbd1152004-01-13 23:41:32 +0000272\begin{memberdesc}{ignore_zeros}
273 If false, treat an empty block as the end of the archive. If true,
274 skip empty (and invalid) blocks and try to get as many members as
275 possible. This is only useful for concatenated or damaged
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000276 archives.
277\end{memberdesc}
278
279\begin{memberdesc}{debug=0}
Fred Drake3bbd1152004-01-13 23:41:32 +0000280 To be set from \code{0} (no debug messages; the default) up to
281 \code{3} (all debug messages). The messages are written to
282 \code{sys.stdout}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000283\end{memberdesc}
284
Fred Drake3bbd1152004-01-13 23:41:32 +0000285\begin{memberdesc}{errorlevel}
286 If \code{0} (the default), all errors are ignored when using
287 \method{extract()}. Nevertheless, they appear as error messages
288 in the debug output, when debugging is enabled. If \code{1}, all
289 \emph{fatal} errors are raised as \exception{OSError} or
290 \exception{IOError} exceptions. If \code{2}, all \emph{non-fatal}
291 errors are raised as \exception{TarError} exceptions as well.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000292\end{memberdesc}
293
294%-----------------
295% TarInfo Objects
296%-----------------
297
298\subsection{TarInfo Objects \label{tarinfo-objects}}
299
Fred Drake3bbd1152004-01-13 23:41:32 +0000300A \class{TarInfo} object represents one member in a
301\class{TarFile}. Aside from storing all required attributes of a file
302(like file type, size, time, permissions, owner etc.), it provides
303some useful methods to determine its type. It does \emph{not} contain
304the file's data itself.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000305
Fred Drake3bbd1152004-01-13 23:41:32 +0000306\class{TarInfo} objects are returned by \class{TarFile}'s methods
307\method{getmember()}, \method{getmembers()} and \method{gettarinfo()}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000308
309\begin{classdesc}{TarInfo}{\optional{name}}
310 Create a \class{TarInfo} object.
311\end{classdesc}
312
313\begin{methoddesc}{frombuf}{}
314 Create and return a \class{TarInfo} object from a string buffer.
315\end{methoddesc}
316
317\begin{methoddesc}{tobuf}{}
318 Create a string buffer from a \class{TarInfo} object.
319\end{methoddesc}
320
321A \code{TarInfo} object has the following public data attributes:
Fred Drake3bbd1152004-01-13 23:41:32 +0000322
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000323\begin{memberdesc}{name}
324 Name of the archive member.
325\end{memberdesc}
326
327\begin{memberdesc}{size}
328 Size in bytes.
329\end{memberdesc}
330
331\begin{memberdesc}{mtime}
332 Time of last modification.
333\end{memberdesc}
334
335\begin{memberdesc}{mode}
336 Permission bits.
337\end{memberdesc}
338
339\begin{memberdesc}{type}
Fred Drake3bbd1152004-01-13 23:41:32 +0000340 File type. \var{type} is usually one of these constants:
341 \constant{REGTYPE}, \constant{AREGTYPE}, \constant{LNKTYPE},
342 \constant{SYMTYPE}, \constant{DIRTYPE}, \constant{FIFOTYPE},
343 \constant{CONTTYPE}, \constant{CHRTYPE}, \constant{BLKTYPE},
344 \constant{GNUTYPE_SPARSE}. To determine the type of a
345 \class{TarInfo} object more conveniently, use the \code{is_*()}
346 methods below.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000347\end{memberdesc}
348
349\begin{memberdesc}{linkname}
Fred Drake3bbd1152004-01-13 23:41:32 +0000350 Name of the target file name, which is only present in
351 \class{TarInfo} objects of type \constant{LNKTYPE} and
352 \constant{SYMTYPE}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000353\end{memberdesc}
354
Fred Drake3bbd1152004-01-13 23:41:32 +0000355\begin{memberdesc}{uid}
356 User ID of the user who originally stored this member.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000357\end{memberdesc}
358
Fred Drake3bbd1152004-01-13 23:41:32 +0000359\begin{memberdesc}{gid}
360 Group ID of the user who originally stored this member.
361\end{memberdesc}
362
363\begin{memberdesc}{uname}
364 User name.
365\end{memberdesc}
366
367\begin{memberdesc}{gname}
368 Group name.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000369\end{memberdesc}
370
371A \class{TarInfo} object also provides some convenient query methods:
Fred Drake3bbd1152004-01-13 23:41:32 +0000372
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000373\begin{methoddesc}{isfile}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000374 Return \constant{True} if the \class{Tarinfo} object is a regular
375 file.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000376\end{methoddesc}
377
378\begin{methoddesc}{isreg}{}
379 Same as \method{isfile()}.
380\end{methoddesc}
381
382\begin{methoddesc}{isdir}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000383 Return \constant{True} if it is a directory.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000384\end{methoddesc}
385
386\begin{methoddesc}{issym}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000387 Return \constant{True} if it is a symbolic link.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000388\end{methoddesc}
389
390\begin{methoddesc}{islnk}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000391 Return \constant{True} if it is a hard link.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000392\end{methoddesc}
393
394\begin{methoddesc}{ischr}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000395 Return \constant{True} if it is a character device.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000396\end{methoddesc}
397
398\begin{methoddesc}{isblk}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000399 Return \constant{True} if it is a block device.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000400\end{methoddesc}
401
402\begin{methoddesc}{isfifo}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000403 Return \constant{True} if it is a FIFO.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000404\end{methoddesc}
405
406\begin{methoddesc}{isdev}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000407 Return \constant{True} if it is one of character device, block
408 device or FIFO.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000409\end{methoddesc}
410
411%------------------------
412% Examples
413%------------------------
414
415\subsection{Examples \label{tar-examples}}
416
417How to create an uncompressed tar archive from a list of filenames:
418\begin{verbatim}
419import tarfile
420tar = tarfile.open("sample.tar", "w")
421for name in ["foo", "bar", "quux"]:
422 tar.add(name)
423tar.close()
424\end{verbatim}
425
426How to read a gzip compressed tar archive and display some member information:
427\begin{verbatim}
428import tarfile
429tar = tarfile.open("sample.tar.gz", "r:gz")
430for tarinfo in tar:
431 print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
432 if tarinfo.isreg():
433 print "a regular file."
434 elif tarinfo.isdir():
435 print "a directory."
436 else:
437 print "something else."
438tar.close()
439\end{verbatim}
440
441How to create a tar archive with faked information:
442\begin{verbatim}
443import tarfile
444tar = tarfile.open("sample.tar.gz", "w:gz")
445for name in namelist:
446 tarinfo = tar.gettarinfo(name, "fakeproj-1.0/" + name)
447 tarinfo.uid = 123
448 tarinfo.gid = 456
449 tarinfo.uname = "johndoe"
450 tarinfo.gname = "fake"
451 tar.addfile(tarinfo, file(name))
452tar.close()
453\end{verbatim}
454
455The \emph{only} way to extract an uncompressed tar stream from
456\code{sys.stdin}:
457\begin{verbatim}
458import sys
459import tarfile
460tar = tarfile.open(mode="r|", fileobj=sys.stdin)
461for tarinfo in tar:
462 tar.extract(tarinfo)
463tar.close()
464\end{verbatim}