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