Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 1 | \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 | |
| 10 | The \module{tarfile} module makes it possible to read and create tar archives. |
| 11 | Some facts and figures: |
| 12 | |
| 13 | \begin{itemize} |
| 14 | \item reads and writes \module{gzip} and \module{bzip2} compressed archives. |
Lars Gustäbel | c64e402 | 2007-03-13 10:47:19 +0000 | [diff] [blame] | 15 | \item read/write support for the \POSIX{}.1-1988 (ustar) format. |
| 16 | \item read/write support for the GNU tar format including \emph{longname} and |
| 17 | \emph{longlink} extensions, read-only support for the \emph{sparse} |
| 18 | extension. |
| 19 | \item read/write support for the \POSIX{}.1-2001 (pax) format. |
| 20 | \versionadded{2.6} |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 21 | \item handles directories, regular files, hardlinks, symbolic links, fifos, |
| 22 | character devices and block devices and is able to acquire and |
| 23 | restore file information like timestamp, access permissions and owner. |
| 24 | \item can handle tape devices. |
| 25 | \end{itemize} |
| 26 | |
Lars Gustäbel | c64e402 | 2007-03-13 10:47:19 +0000 | [diff] [blame] | 27 | \begin{funcdesc}{open}{name\optional{, mode\optional{, |
| 28 | fileobj\optional{, bufsize}}}, **kwargs} |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 29 | Return a \class{TarFile} object for the pathname \var{name}. |
Lars Gustäbel | c64e402 | 2007-03-13 10:47:19 +0000 | [diff] [blame] | 30 | For detailed information on \class{TarFile} objects and the keyword |
| 31 | arguments that are allowed, see \citetitle{TarFile Objects} |
| 32 | (section \ref{tarfile-objects}). |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 33 | |
| 34 | \var{mode} has to be a string of the form \code{'filemode[:compression]'}, |
| 35 | it defaults to \code{'r'}. Here is a full list of mode combinations: |
| 36 | |
| 37 | \begin{tableii}{c|l}{code}{mode}{action} |
Martin v. Löwis | 78be7df | 2005-03-05 12:47:42 +0000 | [diff] [blame] | 38 | \lineii{'r' or 'r:*'}{Open for reading with transparent compression (recommended).} |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 39 | \lineii{'r:'}{Open for reading exclusively without compression.} |
| 40 | \lineii{'r:gz'}{Open for reading with gzip compression.} |
| 41 | \lineii{'r:bz2'}{Open for reading with bzip2 compression.} |
Lars Gustäbel | 3f8aca1 | 2007-02-06 18:38:13 +0000 | [diff] [blame] | 42 | \lineii{'a' or 'a:'}{Open for appending with no compression. The file |
| 43 | is created if it does not exist.} |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 44 | \lineii{'w' or 'w:'}{Open for uncompressed writing.} |
| 45 | \lineii{'w:gz'}{Open for gzip compressed writing.} |
| 46 | \lineii{'w:bz2'}{Open for bzip2 compressed writing.} |
| 47 | \end{tableii} |
| 48 | |
| 49 | Note that \code{'a:gz'} or \code{'a:bz2'} is not possible. |
| 50 | If \var{mode} is not suitable to open a certain (compressed) file for |
| 51 | reading, \exception{ReadError} is raised. Use \var{mode} \code{'r'} to |
| 52 | avoid this. If a compression method is not supported, |
| 53 | \exception{CompressionError} is raised. |
| 54 | |
Lars Gustäbel | a69aa32 | 2007-02-12 09:25:53 +0000 | [diff] [blame] | 55 | If \var{fileobj} is specified, it is used as an alternative to a file |
| 56 | object opened for \var{name}. It is supposed to be at position 0. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 57 | |
| 58 | For special purposes, there is a second format for \var{mode}: |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 59 | \code{'filemode|[compression]'}. \function{open()} will return a |
| 60 | \class{TarFile} object that processes its data as a stream of |
| 61 | blocks. No random seeking will be done on the file. If given, |
| 62 | \var{fileobj} may be any object that has a \method{read()} or |
| 63 | \method{write()} method (depending on the \var{mode}). |
| 64 | \var{bufsize} specifies the blocksize and defaults to \code{20 * |
| 65 | 512} bytes. Use this variant in combination with |
| 66 | e.g. \code{sys.stdin}, a socket file object or a tape device. |
| 67 | However, such a \class{TarFile} object is limited in that it does |
| 68 | not allow to be accessed randomly, see ``Examples'' |
| 69 | (section~\ref{tar-examples}). The currently possible modes: |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 70 | |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 71 | \begin{tableii}{c|l}{code}{Mode}{Action} |
Martin v. Löwis | 78be7df | 2005-03-05 12:47:42 +0000 | [diff] [blame] | 72 | \lineii{'r|*'}{Open a \emph{stream} of tar blocks for reading with transparent compression.} |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 73 | \lineii{'r|'}{Open a \emph{stream} of uncompressed tar blocks for reading.} |
| 74 | \lineii{'r|gz'}{Open a gzip compressed \emph{stream} for reading.} |
| 75 | \lineii{'r|bz2'}{Open a bzip2 compressed \emph{stream} for reading.} |
| 76 | \lineii{'w|'}{Open an uncompressed \emph{stream} for writing.} |
| 77 | \lineii{'w|gz'}{Open an gzip compressed \emph{stream} for writing.} |
| 78 | \lineii{'w|bz2'}{Open an bzip2 compressed \emph{stream} for writing.} |
| 79 | \end{tableii} |
| 80 | \end{funcdesc} |
| 81 | |
| 82 | \begin{classdesc*}{TarFile} |
| 83 | Class for reading and writing tar archives. Do not use this |
| 84 | class directly, better use \function{open()} instead. |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 85 | See ``TarFile Objects'' (section~\ref{tarfile-objects}). |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 86 | \end{classdesc*} |
| 87 | |
| 88 | \begin{funcdesc}{is_tarfile}{name} |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 89 | Return \constant{True} if \var{name} is a tar archive file, that |
| 90 | the \module{tarfile} module can read. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 91 | \end{funcdesc} |
| 92 | |
| 93 | \begin{classdesc}{TarFileCompat}{filename\optional{, mode\optional{, |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 94 | compression}}} |
| 95 | Class for limited access to tar archives with a |
| 96 | \refmodule{zipfile}-like interface. Please consult the |
| 97 | documentation of the \refmodule{zipfile} module for more details. |
| 98 | \var{compression} must be one of the following constants: |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 99 | \begin{datadesc}{TAR_PLAIN} |
| 100 | Constant for an uncompressed tar archive. |
| 101 | \end{datadesc} |
| 102 | \begin{datadesc}{TAR_GZIPPED} |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 103 | Constant for a \refmodule{gzip} compressed tar archive. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 104 | \end{datadesc} |
| 105 | \end{classdesc} |
| 106 | |
| 107 | \begin{excdesc}{TarError} |
| 108 | Base class for all \module{tarfile} exceptions. |
| 109 | \end{excdesc} |
| 110 | |
| 111 | \begin{excdesc}{ReadError} |
| 112 | Is raised when a tar archive is opened, that either cannot be handled by |
| 113 | the \module{tarfile} module or is somehow invalid. |
| 114 | \end{excdesc} |
| 115 | |
| 116 | \begin{excdesc}{CompressionError} |
| 117 | Is raised when a compression method is not supported or when the data |
| 118 | cannot be decoded properly. |
| 119 | \end{excdesc} |
| 120 | |
| 121 | \begin{excdesc}{StreamError} |
| 122 | Is raised for the limitations that are typical for stream-like |
| 123 | \class{TarFile} objects. |
| 124 | \end{excdesc} |
| 125 | |
| 126 | \begin{excdesc}{ExtractError} |
| 127 | Is raised for \emph{non-fatal} errors when using \method{extract()}, but |
| 128 | only if \member{TarFile.errorlevel}\code{ == 2}. |
| 129 | \end{excdesc} |
| 130 | |
Georg Brandl | ebbeed7 | 2006-12-19 22:06:46 +0000 | [diff] [blame] | 131 | \begin{excdesc}{HeaderError} |
| 132 | Is raised by \method{frombuf()} if the buffer it gets is invalid. |
| 133 | \versionadded{2.6} |
| 134 | \end{excdesc} |
| 135 | |
Lars Gustäbel | c64e402 | 2007-03-13 10:47:19 +0000 | [diff] [blame] | 136 | \begin{datadesc}{USTAR_FORMAT} |
| 137 | \POSIX{}.1-1988 (ustar) format. It supports filenames up to a length of |
| 138 | at best 256 characters and linknames up to 100 characters. The maximum |
| 139 | file size is 8 gigabytes. This is an old and limited but widely |
| 140 | supported format. |
| 141 | \end{datadesc} |
| 142 | |
| 143 | \begin{datadesc}{GNU_FORMAT} |
| 144 | GNU tar format. It supports arbitrarily long filenames and linknames and |
| 145 | files bigger than 8 gigabytes. It is the defacto standard on GNU/Linux |
| 146 | systems. |
| 147 | \end{datadesc} |
| 148 | |
| 149 | \begin{datadesc}{PAX_FORMAT} |
| 150 | \POSIX{}.1-2001 (pax) format. It is the most flexible format with |
| 151 | virtually no limits. It supports long filenames and linknames, large files |
| 152 | and stores pathnames in a portable way. However, not all tar |
| 153 | implementations today are able to handle pax archives properly. |
| 154 | \end{datadesc} |
| 155 | |
| 156 | \begin{datadesc}{DEFAULT_FORMAT} |
| 157 | The default format for creating archives. This is currently |
| 158 | \constant{GNU_FORMAT}. |
| 159 | \end{datadesc} |
| 160 | |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 161 | \begin{seealso} |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 162 | \seemodule{zipfile}{Documentation of the \refmodule{zipfile} |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 163 | standard module.} |
| 164 | |
George Yoshida | d771672 | 2006-04-28 16:40:14 +0000 | [diff] [blame] | 165 | \seetitle[http://www.gnu.org/software/tar/manual/html_node/tar_134.html\#SEC134] |
Georg Brandl | 9a19e5c | 2005-08-27 17:10:35 +0000 | [diff] [blame] | 166 | {GNU tar manual, Basic Tar Format}{Documentation for tar archive files, |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 167 | including GNU tar extensions.} |
| 168 | \end{seealso} |
| 169 | |
| 170 | %----------------- |
| 171 | % TarFile Objects |
| 172 | %----------------- |
| 173 | |
| 174 | \subsection{TarFile Objects \label{tarfile-objects}} |
| 175 | |
| 176 | The \class{TarFile} object provides an interface to a tar archive. A tar |
| 177 | archive is a sequence of blocks. An archive member (a stored file) is made up |
| 178 | of a header block followed by data blocks. It is possible, to store a file in a |
| 179 | tar archive several times. Each archive member is represented by a |
| 180 | \class{TarInfo} object, see \citetitle{TarInfo Objects} (section |
| 181 | \ref{tarinfo-objects}) for details. |
| 182 | |
Lars Gustäbel | c64e402 | 2007-03-13 10:47:19 +0000 | [diff] [blame] | 183 | \begin{classdesc}{TarFile}{name=None, mode='r', fileobj=None, |
| 184 | format=DEFAULT_FORMAT, tarinfo=TarInfo, dereference=False, |
| 185 | ignore_zeros=False, encoding=None, pax_headers=None, debug=0, |
| 186 | errorlevel=0} |
| 187 | |
| 188 | All following arguments are optional and can be accessed as instance |
| 189 | attributes as well. |
| 190 | |
| 191 | \var{name} is the pathname of the archive. It can be omitted if |
| 192 | \var{fileobj} is given. In this case, the file object's \member{name} |
| 193 | attribute is used if it exists. |
| 194 | |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 195 | \var{mode} is either \code{'r'} to read from an existing archive, |
| 196 | \code{'a'} to append data to an existing file or \code{'w'} to create a new |
Lars Gustäbel | c64e402 | 2007-03-13 10:47:19 +0000 | [diff] [blame] | 197 | file overwriting an existing one. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 198 | |
| 199 | If \var{fileobj} is given, it is used for reading or writing data. |
| 200 | If it can be determined, \var{mode} is overridden by \var{fileobj}'s mode. |
Lars Gustäbel | a69aa32 | 2007-02-12 09:25:53 +0000 | [diff] [blame] | 201 | \var{fileobj} will be used from position 0. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 202 | \begin{notice} |
| 203 | \var{fileobj} is not closed, when \class{TarFile} is closed. |
| 204 | \end{notice} |
Lars Gustäbel | c64e402 | 2007-03-13 10:47:19 +0000 | [diff] [blame] | 205 | |
| 206 | \var{format} controls the archive format. It must be one of the constants |
| 207 | \constant{USTAR_FORMAT}, \constant{GNU_FORMAT} or \constant{PAX_FORMAT} |
| 208 | that are defined at module level. |
| 209 | \versionadded{2.6} |
| 210 | |
| 211 | The \var{tarinfo} argument can be used to replace the default |
| 212 | \class{TarInfo} class with a different one. |
| 213 | \versionadded{2.6} |
| 214 | |
| 215 | If \var{dereference} is \code{False}, add symbolic and hard links to the |
| 216 | archive. If it is \code{True}, add the content of the target files to the |
| 217 | archive. This has no effect on systems that do not support symbolic links. |
| 218 | |
| 219 | If \var{ignore_zeros} is \code{False}, treat an empty block as the end of |
| 220 | the archive. If it is \var{True}, skip empty (and invalid) blocks and try |
| 221 | to get as many members as possible. This is only useful for reading |
| 222 | concatenated or damaged archives. |
| 223 | |
| 224 | \var{debug} can be set from \code{0} (no debug messages) up to \code{3} |
| 225 | (all debug messages). The messages are written to \code{sys.stderr}. |
| 226 | |
| 227 | If \var{errorlevel} is \code{0}, all errors are ignored when using |
| 228 | \method{extract()}. Nevertheless, they appear as error messages in the |
| 229 | debug output, when debugging is enabled. If \code{1}, all \emph{fatal} |
| 230 | errors are raised as \exception{OSError} or \exception{IOError} exceptions. |
| 231 | If \code{2}, all \emph{non-fatal} errors are raised as \exception{TarError} |
| 232 | exceptions as well. |
| 233 | |
| 234 | The \var{encoding} argument defines the local character encoding. It |
| 235 | defaults to the value from \function{sys.getfilesystemencoding()} or if |
| 236 | that is \code{None} to \code{"ascii"}. \var{encoding} is used only in |
| 237 | connection with the pax format which stores text data in \emph{UTF-8}. If |
| 238 | it is not set correctly, character conversion will fail with a |
| 239 | \exception{UnicodeError}. |
| 240 | \versionadded{2.6} |
| 241 | |
| 242 | The \var{pax_headers} argument must be a dictionary whose elements are |
| 243 | either unicode objects, numbers or strings that can be decoded to unicode |
| 244 | using \var{encoding}. This information will be added to the archive as a |
| 245 | pax global header. |
| 246 | \versionadded{2.6} |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 247 | \end{classdesc} |
| 248 | |
| 249 | \begin{methoddesc}{open}{...} |
| 250 | Alternative constructor. The \function{open()} function on module level is |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 251 | actually a shortcut to this classmethod. See section~\ref{module-tarfile} |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 252 | for details. |
| 253 | \end{methoddesc} |
| 254 | |
| 255 | \begin{methoddesc}{getmember}{name} |
| 256 | Return a \class{TarInfo} object for member \var{name}. If \var{name} can |
| 257 | not be found in the archive, \exception{KeyError} is raised. |
| 258 | \begin{notice} |
| 259 | If a member occurs more than once in the archive, its last |
Johannes Gijsbers | d345225 | 2004-09-11 16:50:06 +0000 | [diff] [blame] | 260 | occurrence is assumed to be the most up-to-date version. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 261 | \end{notice} |
| 262 | \end{methoddesc} |
| 263 | |
| 264 | \begin{methoddesc}{getmembers}{} |
| 265 | Return the members of the archive as a list of \class{TarInfo} objects. |
| 266 | The list has the same order as the members in the archive. |
| 267 | \end{methoddesc} |
| 268 | |
| 269 | \begin{methoddesc}{getnames}{} |
| 270 | Return the members as a list of their names. It has the same order as |
| 271 | the list returned by \method{getmembers()}. |
| 272 | \end{methoddesc} |
| 273 | |
| 274 | \begin{methoddesc}{list}{verbose=True} |
| 275 | Print a table of contents to \code{sys.stdout}. If \var{verbose} is |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 276 | \constant{False}, only the names of the members are printed. If it is |
| 277 | \constant{True}, output similar to that of \program{ls -l} is produced. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 278 | \end{methoddesc} |
| 279 | |
| 280 | \begin{methoddesc}{next}{} |
| 281 | Return the next member of the archive as a \class{TarInfo} object, when |
| 282 | \class{TarFile} is opened for reading. Return \code{None} if there is no |
| 283 | more available. |
| 284 | \end{methoddesc} |
| 285 | |
Martin v. Löwis | 00a73e7 | 2005-03-04 19:40:34 +0000 | [diff] [blame] | 286 | \begin{methoddesc}{extractall}{\optional{path\optional{, members}}} |
| 287 | Extract all members from the archive to the current working directory |
| 288 | or directory \var{path}. If optional \var{members} is given, it must be |
| 289 | a subset of the list returned by \method{getmembers()}. |
| 290 | Directory informations like owner, modification time and permissions are |
| 291 | set after all members have been extracted. This is done to work around two |
| 292 | problems: A directory's modification time is reset each time a file is |
| 293 | created in it. And, if a directory's permissions do not allow writing, |
| 294 | extracting files to it will fail. |
| 295 | \versionadded{2.5} |
| 296 | \end{methoddesc} |
| 297 | |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 298 | \begin{methoddesc}{extract}{member\optional{, path}} |
| 299 | Extract a member from the archive to the current working directory, |
| 300 | using its full name. Its file information is extracted as accurately as |
| 301 | possible. |
| 302 | \var{member} may be a filename or a \class{TarInfo} object. |
| 303 | You can specify a different directory using \var{path}. |
Martin v. Löwis | 00a73e7 | 2005-03-04 19:40:34 +0000 | [diff] [blame] | 304 | \begin{notice} |
| 305 | Because the \method{extract()} method allows random access to a tar |
| 306 | archive there are some issues you must take care of yourself. See the |
| 307 | description for \method{extractall()} above. |
| 308 | \end{notice} |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 309 | \end{methoddesc} |
| 310 | |
| 311 | \begin{methoddesc}{extractfile}{member} |
| 312 | Extract a member from the archive as a file object. |
| 313 | \var{member} may be a filename or a \class{TarInfo} object. |
| 314 | If \var{member} is a regular file, a file-like object is returned. |
| 315 | If \var{member} is a link, a file-like object is constructed from the |
| 316 | link's target. |
| 317 | If \var{member} is none of the above, \code{None} is returned. |
| 318 | \begin{notice} |
| 319 | The file-like object is read-only and provides the following methods: |
| 320 | \method{read()}, \method{readline()}, \method{readlines()}, |
| 321 | \method{seek()}, \method{tell()}. |
| 322 | \end{notice} |
| 323 | \end{methoddesc} |
| 324 | |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 325 | \begin{methoddesc}{add}{name\optional{, arcname\optional{, recursive}}} |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 326 | Add the file \var{name} to the archive. \var{name} may be any type |
| 327 | of file (directory, fifo, symbolic link, etc.). |
| 328 | If given, \var{arcname} specifies an alternative name for the file in the |
| 329 | archive. Directories are added recursively by default. |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 330 | This can be avoided by setting \var{recursive} to \constant{False}; |
| 331 | the default is \constant{True}. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 332 | \end{methoddesc} |
| 333 | |
| 334 | \begin{methoddesc}{addfile}{tarinfo\optional{, fileobj}} |
| 335 | Add the \class{TarInfo} object \var{tarinfo} to the archive. |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 336 | If \var{fileobj} is given, \code{\var{tarinfo}.size} bytes are read |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 337 | from it and added to the archive. You can create \class{TarInfo} objects |
| 338 | using \method{gettarinfo()}. |
| 339 | \begin{notice} |
| 340 | On Windows platforms, \var{fileobj} should always be opened with mode |
| 341 | \code{'rb'} to avoid irritation about the file size. |
| 342 | \end{notice} |
| 343 | \end{methoddesc} |
| 344 | |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 345 | \begin{methoddesc}{gettarinfo}{\optional{name\optional{, |
| 346 | arcname\optional{, fileobj}}}} |
| 347 | Create a \class{TarInfo} object for either the file \var{name} or |
| 348 | the file object \var{fileobj} (using \function{os.fstat()} on its |
| 349 | file descriptor). You can modify some of the \class{TarInfo}'s |
| 350 | attributes before you add it using \method{addfile()}. If given, |
| 351 | \var{arcname} specifies an alternative name for the file in the |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 352 | archive. |
| 353 | \end{methoddesc} |
| 354 | |
| 355 | \begin{methoddesc}{close}{} |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 356 | Close the \class{TarFile}. In write mode, two finishing zero |
| 357 | blocks are appended to the archive. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 358 | \end{methoddesc} |
| 359 | |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 360 | \begin{memberdesc}{posix} |
Lars Gustäbel | c64e402 | 2007-03-13 10:47:19 +0000 | [diff] [blame] | 361 | Setting this to \constant{True} is equivalent to setting the |
| 362 | \member{format} attribute to \constant{USTAR_FORMAT}, |
| 363 | \constant{False} is equivalent to \constant{GNU_FORMAT}. |
Neal Norwitz | 525b315 | 2004-08-20 01:52:42 +0000 | [diff] [blame] | 364 | \versionchanged[\var{posix} defaults to \constant{False}]{2.4} |
Lars Gustäbel | c64e402 | 2007-03-13 10:47:19 +0000 | [diff] [blame] | 365 | \deprecated{2.6}{Use the \member{format} attribute instead.} |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 366 | \end{memberdesc} |
| 367 | |
| 368 | %----------------- |
| 369 | % TarInfo Objects |
| 370 | %----------------- |
| 371 | |
| 372 | \subsection{TarInfo Objects \label{tarinfo-objects}} |
| 373 | |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 374 | A \class{TarInfo} object represents one member in a |
| 375 | \class{TarFile}. Aside from storing all required attributes of a file |
| 376 | (like file type, size, time, permissions, owner etc.), it provides |
| 377 | some useful methods to determine its type. It does \emph{not} contain |
| 378 | the file's data itself. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 379 | |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 380 | \class{TarInfo} objects are returned by \class{TarFile}'s methods |
| 381 | \method{getmember()}, \method{getmembers()} and \method{gettarinfo()}. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 382 | |
| 383 | \begin{classdesc}{TarInfo}{\optional{name}} |
| 384 | Create a \class{TarInfo} object. |
| 385 | \end{classdesc} |
| 386 | |
| 387 | \begin{methoddesc}{frombuf}{} |
| 388 | Create and return a \class{TarInfo} object from a string buffer. |
Georg Brandl | ebbeed7 | 2006-12-19 22:06:46 +0000 | [diff] [blame] | 389 | \versionadded[Raises \exception{HeaderError} if the buffer is |
| 390 | invalid.]{2.6} |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 391 | \end{methoddesc} |
| 392 | |
Lars Gustäbel | c64e402 | 2007-03-13 10:47:19 +0000 | [diff] [blame] | 393 | \begin{methoddesc}{fromtarfile}{tarfile} |
| 394 | Read the next member from the \class{TarFile} object \var{tarfile} and |
| 395 | return it as a \class{TarInfo} object. |
| 396 | \versionadded{2.6} |
| 397 | \end{methoddesc} |
Georg Brandl | 38c6a22 | 2006-05-10 16:26:03 +0000 | [diff] [blame] | 398 | |
Lars Gustäbel | c64e402 | 2007-03-13 10:47:19 +0000 | [diff] [blame] | 399 | \begin{methoddesc}{tobuf}{\optional{format}} |
| 400 | Create a string buffer from a \class{TarInfo} object. See |
| 401 | \class{TarFile}'s \member{format} argument for information. |
| 402 | \versionchanged[The \var{format} parameter]{2.6} |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 403 | \end{methoddesc} |
| 404 | |
| 405 | A \code{TarInfo} object has the following public data attributes: |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 406 | |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 407 | \begin{memberdesc}{name} |
| 408 | Name of the archive member. |
| 409 | \end{memberdesc} |
| 410 | |
| 411 | \begin{memberdesc}{size} |
| 412 | Size in bytes. |
| 413 | \end{memberdesc} |
| 414 | |
| 415 | \begin{memberdesc}{mtime} |
| 416 | Time of last modification. |
| 417 | \end{memberdesc} |
| 418 | |
| 419 | \begin{memberdesc}{mode} |
| 420 | Permission bits. |
| 421 | \end{memberdesc} |
| 422 | |
| 423 | \begin{memberdesc}{type} |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 424 | File type. \var{type} is usually one of these constants: |
| 425 | \constant{REGTYPE}, \constant{AREGTYPE}, \constant{LNKTYPE}, |
| 426 | \constant{SYMTYPE}, \constant{DIRTYPE}, \constant{FIFOTYPE}, |
| 427 | \constant{CONTTYPE}, \constant{CHRTYPE}, \constant{BLKTYPE}, |
| 428 | \constant{GNUTYPE_SPARSE}. To determine the type of a |
| 429 | \class{TarInfo} object more conveniently, use the \code{is_*()} |
| 430 | methods below. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 431 | \end{memberdesc} |
| 432 | |
| 433 | \begin{memberdesc}{linkname} |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 434 | Name of the target file name, which is only present in |
| 435 | \class{TarInfo} objects of type \constant{LNKTYPE} and |
| 436 | \constant{SYMTYPE}. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 437 | \end{memberdesc} |
| 438 | |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 439 | \begin{memberdesc}{uid} |
| 440 | User ID of the user who originally stored this member. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 441 | \end{memberdesc} |
| 442 | |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 443 | \begin{memberdesc}{gid} |
| 444 | Group ID of the user who originally stored this member. |
| 445 | \end{memberdesc} |
| 446 | |
| 447 | \begin{memberdesc}{uname} |
| 448 | User name. |
| 449 | \end{memberdesc} |
| 450 | |
| 451 | \begin{memberdesc}{gname} |
| 452 | Group name. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 453 | \end{memberdesc} |
| 454 | |
| 455 | A \class{TarInfo} object also provides some convenient query methods: |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 456 | |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 457 | \begin{methoddesc}{isfile}{} |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 458 | Return \constant{True} if the \class{Tarinfo} object is a regular |
| 459 | file. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 460 | \end{methoddesc} |
| 461 | |
| 462 | \begin{methoddesc}{isreg}{} |
| 463 | Same as \method{isfile()}. |
| 464 | \end{methoddesc} |
| 465 | |
| 466 | \begin{methoddesc}{isdir}{} |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 467 | Return \constant{True} if it is a directory. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 468 | \end{methoddesc} |
| 469 | |
| 470 | \begin{methoddesc}{issym}{} |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 471 | Return \constant{True} if it is a symbolic link. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 472 | \end{methoddesc} |
| 473 | |
| 474 | \begin{methoddesc}{islnk}{} |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 475 | Return \constant{True} if it is a hard link. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 476 | \end{methoddesc} |
| 477 | |
| 478 | \begin{methoddesc}{ischr}{} |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 479 | Return \constant{True} if it is a character device. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 480 | \end{methoddesc} |
| 481 | |
| 482 | \begin{methoddesc}{isblk}{} |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 483 | Return \constant{True} if it is a block device. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 484 | \end{methoddesc} |
| 485 | |
| 486 | \begin{methoddesc}{isfifo}{} |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 487 | Return \constant{True} if it is a FIFO. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 488 | \end{methoddesc} |
| 489 | |
| 490 | \begin{methoddesc}{isdev}{} |
Fred Drake | 3bbd115 | 2004-01-13 23:41:32 +0000 | [diff] [blame] | 491 | Return \constant{True} if it is one of character device, block |
| 492 | device or FIFO. |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 493 | \end{methoddesc} |
| 494 | |
| 495 | %------------------------ |
| 496 | % Examples |
| 497 | %------------------------ |
| 498 | |
| 499 | \subsection{Examples \label{tar-examples}} |
| 500 | |
Martin v. Löwis | 00a73e7 | 2005-03-04 19:40:34 +0000 | [diff] [blame] | 501 | How to extract an entire tar archive to the current working directory: |
| 502 | \begin{verbatim} |
| 503 | import tarfile |
| 504 | tar = tarfile.open("sample.tar.gz") |
| 505 | tar.extractall() |
| 506 | tar.close() |
| 507 | \end{verbatim} |
| 508 | |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 509 | How to create an uncompressed tar archive from a list of filenames: |
| 510 | \begin{verbatim} |
| 511 | import tarfile |
| 512 | tar = tarfile.open("sample.tar", "w") |
| 513 | for name in ["foo", "bar", "quux"]: |
| 514 | tar.add(name) |
| 515 | tar.close() |
| 516 | \end{verbatim} |
| 517 | |
| 518 | How to read a gzip compressed tar archive and display some member information: |
| 519 | \begin{verbatim} |
| 520 | import tarfile |
| 521 | tar = tarfile.open("sample.tar.gz", "r:gz") |
| 522 | for tarinfo in tar: |
| 523 | print tarinfo.name, "is", tarinfo.size, "bytes in size and is", |
| 524 | if tarinfo.isreg(): |
| 525 | print "a regular file." |
| 526 | elif tarinfo.isdir(): |
| 527 | print "a directory." |
| 528 | else: |
| 529 | print "something else." |
| 530 | tar.close() |
| 531 | \end{verbatim} |
| 532 | |
| 533 | How to create a tar archive with faked information: |
| 534 | \begin{verbatim} |
| 535 | import tarfile |
| 536 | tar = tarfile.open("sample.tar.gz", "w:gz") |
| 537 | for name in namelist: |
| 538 | tarinfo = tar.gettarinfo(name, "fakeproj-1.0/" + name) |
| 539 | tarinfo.uid = 123 |
| 540 | tarinfo.gid = 456 |
| 541 | tarinfo.uname = "johndoe" |
| 542 | tarinfo.gname = "fake" |
| 543 | tar.addfile(tarinfo, file(name)) |
| 544 | tar.close() |
| 545 | \end{verbatim} |
| 546 | |
| 547 | The \emph{only} way to extract an uncompressed tar stream from |
| 548 | \code{sys.stdin}: |
| 549 | \begin{verbatim} |
| 550 | import sys |
| 551 | import tarfile |
| 552 | tar = tarfile.open(mode="r|", fileobj=sys.stdin) |
| 553 | for tarinfo in tar: |
| 554 | tar.extract(tarinfo) |
| 555 | tar.close() |
| 556 | \end{verbatim} |