python-2.5.2/win32/Lib/tarfile.py
author jjkang
Fri, 11 Jun 2010 15:22:40 +0800
changeset 2 9da1e5517a66
parent 0 ae805ac0140d
permissions -rw-r--r--
Change SFL to EPL
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
     1
#!/usr/bin/env python
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
     2
# -*- coding: iso-8859-1 -*-
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
     3
#-------------------------------------------------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
     4
# tarfile.py
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
     5
#-------------------------------------------------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
     6
# Copyright (C) 2002 Lars Gustäbel <lars@gustaebel.de>
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
     7
# All rights reserved.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
     8
#
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
     9
# Permission  is  hereby granted,  free  of charge,  to  any person
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    10
# obtaining a  copy of  this software  and associated documentation
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    11
# files  (the  "Software"),  to   deal  in  the  Software   without
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    12
# restriction,  including  without limitation  the  rights to  use,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    13
# copy, modify, merge, publish, distribute, sublicense, and/or sell
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    14
# copies  of  the  Software,  and to  permit  persons  to  whom the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    15
# Software  is  furnished  to  do  so,  subject  to  the  following
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    16
# conditions:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    17
#
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    18
# The above copyright  notice and this  permission notice shall  be
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    19
# included in all copies or substantial portions of the Software.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    20
#
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    21
# THE SOFTWARE IS PROVIDED "AS  IS", WITHOUT WARRANTY OF ANY  KIND,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    22
# EXPRESS OR IMPLIED, INCLUDING  BUT NOT LIMITED TO  THE WARRANTIES
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    23
# OF  MERCHANTABILITY,  FITNESS   FOR  A  PARTICULAR   PURPOSE  AND
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    24
# NONINFRINGEMENT.  IN  NO  EVENT SHALL  THE  AUTHORS  OR COPYRIGHT
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    25
# HOLDERS  BE LIABLE  FOR ANY  CLAIM, DAMAGES  OR OTHER  LIABILITY,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    26
# WHETHER  IN AN  ACTION OF  CONTRACT, TORT  OR OTHERWISE,  ARISING
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    27
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    28
# OTHER DEALINGS IN THE SOFTWARE.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    29
#
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    30
"""Read from and write to tar format archives.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    31
"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    32
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    33
__version__ = "$Revision: 60730 $"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    34
# $Source$
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    35
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    36
version     = "0.8.0"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    37
__author__  = "Lars Gustäbel (lars@gustaebel.de)"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    38
__date__    = "$Date: 2008-02-11 19:36:07 +0100 (Mo, 11 Feb 2008) $"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    39
__cvsid__   = "$Id: tarfile.py 60730 2008-02-11 18:36:07Z lars.gustaebel $"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    40
__credits__ = "Gustavo Niemeyer, Niels Gustäbel, Richard Townsend."
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    41
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    42
#---------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    43
# Imports
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    44
#---------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    45
import sys
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    46
import os
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    47
import shutil
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    48
import stat
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    49
import errno
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    50
import time
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    51
import struct
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    52
import copy
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    53
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    54
if sys.platform == 'mac':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    55
    # This module needs work for MacOS9, especially in the area of pathname
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    56
    # handling. In many places it is assumed a simple substitution of / by the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    57
    # local os.path.sep is good enough to convert pathnames, but this does not
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    58
    # work with the mac rooted:path:name versus :nonrooted:path:name syntax
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    59
    raise ImportError, "tarfile does not work for platform==mac"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    60
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    61
try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    62
    import grp, pwd
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    63
except ImportError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    64
    grp = pwd = None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    65
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    66
# from tarfile import *
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    67
__all__ = ["TarFile", "TarInfo", "is_tarfile", "TarError"]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    68
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    69
#---------------------------------------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    70
# tar constants
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    71
#---------------------------------------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    72
NUL        = "\0"               # the null character
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    73
BLOCKSIZE  = 512                # length of processing blocks
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    74
RECORDSIZE = BLOCKSIZE * 20     # length of records
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    75
MAGIC      = "ustar"            # magic tar string
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    76
VERSION    = "00"               # version number
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    77
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    78
LENGTH_NAME    = 100            # maximum length of a filename
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    79
LENGTH_LINK    = 100            # maximum length of a linkname
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    80
LENGTH_PREFIX  = 155            # maximum length of the prefix field
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    81
MAXSIZE_MEMBER = 077777777777L  # maximum size of a file (11 octal digits)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    82
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    83
REGTYPE  = "0"                  # regular file
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    84
AREGTYPE = "\0"                 # regular file
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    85
LNKTYPE  = "1"                  # link (inside tarfile)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    86
SYMTYPE  = "2"                  # symbolic link
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    87
CHRTYPE  = "3"                  # character special device
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    88
BLKTYPE  = "4"                  # block special device
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    89
DIRTYPE  = "5"                  # directory
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    90
FIFOTYPE = "6"                  # fifo special device
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    91
CONTTYPE = "7"                  # contiguous file
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    92
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    93
GNUTYPE_LONGNAME = "L"          # GNU tar extension for longnames
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    94
GNUTYPE_LONGLINK = "K"          # GNU tar extension for longlink
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    95
GNUTYPE_SPARSE   = "S"          # GNU tar extension for sparse file
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    96
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    97
#---------------------------------------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    98
# tarfile constants
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    99
#---------------------------------------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   100
SUPPORTED_TYPES = (REGTYPE, AREGTYPE, LNKTYPE,  # file types that tarfile
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   101
                   SYMTYPE, DIRTYPE, FIFOTYPE,  # can cope with.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   102
                   CONTTYPE, CHRTYPE, BLKTYPE,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   103
                   GNUTYPE_LONGNAME, GNUTYPE_LONGLINK,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   104
                   GNUTYPE_SPARSE)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   105
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   106
REGULAR_TYPES = (REGTYPE, AREGTYPE,             # file types that somehow
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   107
                 CONTTYPE, GNUTYPE_SPARSE)      # represent regular files
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   108
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   109
#---------------------------------------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   110
# Bits used in the mode field, values in octal.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   111
#---------------------------------------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   112
S_IFLNK = 0120000        # symbolic link
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   113
S_IFREG = 0100000        # regular file
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   114
S_IFBLK = 0060000        # block device
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   115
S_IFDIR = 0040000        # directory
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   116
S_IFCHR = 0020000        # character device
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   117
S_IFIFO = 0010000        # fifo
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   118
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   119
TSUID   = 04000          # set UID on execution
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   120
TSGID   = 02000          # set GID on execution
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   121
TSVTX   = 01000          # reserved
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   122
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   123
TUREAD  = 0400           # read by owner
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   124
TUWRITE = 0200           # write by owner
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   125
TUEXEC  = 0100           # execute/search by owner
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   126
TGREAD  = 0040           # read by group
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   127
TGWRITE = 0020           # write by group
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   128
TGEXEC  = 0010           # execute/search by group
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   129
TOREAD  = 0004           # read by other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   130
TOWRITE = 0002           # write by other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   131
TOEXEC  = 0001           # execute/search by other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   132
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   133
#---------------------------------------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   134
# Some useful functions
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   135
#---------------------------------------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   136
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   137
def stn(s, length):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   138
    """Convert a python string to a null-terminated string buffer.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   139
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   140
    return s[:length] + (length - len(s)) * NUL
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   141
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   142
def nts(s):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   143
    """Convert a null-terminated string field to a python string.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   144
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   145
    # Use the string up to the first null char.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   146
    p = s.find("\0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   147
    if p == -1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   148
        return s
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   149
    return s[:p]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   150
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   151
def nti(s):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   152
    """Convert a number field to a python number.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   153
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   154
    # There are two possible encodings for a number field, see
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   155
    # itn() below.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   156
    if s[0] != chr(0200):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   157
        n = int(nts(s) or "0", 8)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   158
    else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   159
        n = 0L
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   160
        for i in xrange(len(s) - 1):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   161
            n <<= 8
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   162
            n += ord(s[i + 1])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   163
    return n
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   164
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   165
def itn(n, digits=8, posix=False):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   166
    """Convert a python number to a number field.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   167
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   168
    # POSIX 1003.1-1988 requires numbers to be encoded as a string of
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   169
    # octal digits followed by a null-byte, this allows values up to
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   170
    # (8**(digits-1))-1. GNU tar allows storing numbers greater than
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   171
    # that if necessary. A leading 0200 byte indicates this particular
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   172
    # encoding, the following digits-1 bytes are a big-endian
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   173
    # representation. This allows values up to (256**(digits-1))-1.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   174
    if 0 <= n < 8 ** (digits - 1):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   175
        s = "%0*o" % (digits - 1, n) + NUL
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   176
    else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   177
        if posix:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   178
            raise ValueError("overflow in number field")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   179
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   180
        if n < 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   181
            # XXX We mimic GNU tar's behaviour with negative numbers,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   182
            # this could raise OverflowError.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   183
            n = struct.unpack("L", struct.pack("l", n))[0]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   184
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   185
        s = ""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   186
        for i in xrange(digits - 1):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   187
            s = chr(n & 0377) + s
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   188
            n >>= 8
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   189
        s = chr(0200) + s
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   190
    return s
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   191
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   192
def calc_chksums(buf):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   193
    """Calculate the checksum for a member's header by summing up all
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   194
       characters except for the chksum field which is treated as if
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   195
       it was filled with spaces. According to the GNU tar sources,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   196
       some tars (Sun and NeXT) calculate chksum with signed char,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   197
       which will be different if there are chars in the buffer with
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   198
       the high bit set. So we calculate two checksums, unsigned and
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   199
       signed.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   200
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   201
    unsigned_chksum = 256 + sum(struct.unpack("148B", buf[:148]) + struct.unpack("356B", buf[156:512]))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   202
    signed_chksum = 256 + sum(struct.unpack("148b", buf[:148]) + struct.unpack("356b", buf[156:512]))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   203
    return unsigned_chksum, signed_chksum
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   204
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   205
def copyfileobj(src, dst, length=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   206
    """Copy length bytes from fileobj src to fileobj dst.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   207
       If length is None, copy the entire content.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   208
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   209
    if length == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   210
        return
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   211
    if length is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   212
        shutil.copyfileobj(src, dst)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   213
        return
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   214
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   215
    BUFSIZE = 16 * 1024
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   216
    blocks, remainder = divmod(length, BUFSIZE)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   217
    for b in xrange(blocks):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   218
        buf = src.read(BUFSIZE)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   219
        if len(buf) < BUFSIZE:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   220
            raise IOError("end of file reached")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   221
        dst.write(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   222
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   223
    if remainder != 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   224
        buf = src.read(remainder)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   225
        if len(buf) < remainder:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   226
            raise IOError("end of file reached")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   227
        dst.write(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   228
    return
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   229
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   230
filemode_table = (
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   231
    ((S_IFLNK,      "l"),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   232
     (S_IFREG,      "-"),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   233
     (S_IFBLK,      "b"),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   234
     (S_IFDIR,      "d"),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   235
     (S_IFCHR,      "c"),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   236
     (S_IFIFO,      "p")),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   237
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   238
    ((TUREAD,       "r"),),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   239
    ((TUWRITE,      "w"),),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   240
    ((TUEXEC|TSUID, "s"),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   241
     (TSUID,        "S"),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   242
     (TUEXEC,       "x")),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   243
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   244
    ((TGREAD,       "r"),),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   245
    ((TGWRITE,      "w"),),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   246
    ((TGEXEC|TSGID, "s"),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   247
     (TSGID,        "S"),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   248
     (TGEXEC,       "x")),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   249
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   250
    ((TOREAD,       "r"),),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   251
    ((TOWRITE,      "w"),),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   252
    ((TOEXEC|TSVTX, "t"),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   253
     (TSVTX,        "T"),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   254
     (TOEXEC,       "x"))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   255
)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   256
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   257
def filemode(mode):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   258
    """Convert a file's mode to a string of the form
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   259
       -rwxrwxrwx.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   260
       Used by TarFile.list()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   261
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   262
    perm = []
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   263
    for table in filemode_table:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   264
        for bit, char in table:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   265
            if mode & bit == bit:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   266
                perm.append(char)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   267
                break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   268
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   269
            perm.append("-")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   270
    return "".join(perm)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   271
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   272
if os.sep != "/":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   273
    normpath = lambda path: os.path.normpath(path).replace(os.sep, "/")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   274
else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   275
    normpath = os.path.normpath
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   276
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   277
class TarError(Exception):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   278
    """Base exception."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   279
    pass
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   280
class ExtractError(TarError):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   281
    """General exception for extract errors."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   282
    pass
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   283
class ReadError(TarError):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   284
    """Exception for unreadble tar archives."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   285
    pass
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   286
class CompressionError(TarError):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   287
    """Exception for unavailable compression methods."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   288
    pass
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   289
class StreamError(TarError):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   290
    """Exception for unsupported operations on stream-like TarFiles."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   291
    pass
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   292
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   293
#---------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   294
# internal stream interface
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   295
#---------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   296
class _LowLevelFile:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   297
    """Low-level file object. Supports reading and writing.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   298
       It is used instead of a regular file object for streaming
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   299
       access.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   300
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   301
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   302
    def __init__(self, name, mode):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   303
        mode = {
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   304
            "r": os.O_RDONLY,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   305
            "w": os.O_WRONLY | os.O_CREAT | os.O_TRUNC,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   306
        }[mode]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   307
        if hasattr(os, "O_BINARY"):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   308
            mode |= os.O_BINARY
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   309
        self.fd = os.open(name, mode)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   310
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   311
    def close(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   312
        os.close(self.fd)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   313
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   314
    def read(self, size):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   315
        return os.read(self.fd, size)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   316
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   317
    def write(self, s):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   318
        os.write(self.fd, s)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   319
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   320
class _Stream:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   321
    """Class that serves as an adapter between TarFile and
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   322
       a stream-like object.  The stream-like object only
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   323
       needs to have a read() or write() method and is accessed
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   324
       blockwise.  Use of gzip or bzip2 compression is possible.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   325
       A stream-like object could be for example: sys.stdin,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   326
       sys.stdout, a socket, a tape device etc.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   327
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   328
       _Stream is intended to be used only internally.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   329
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   330
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   331
    def __init__(self, name, mode, comptype, fileobj, bufsize):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   332
        """Construct a _Stream object.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   333
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   334
        self._extfileobj = True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   335
        if fileobj is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   336
            fileobj = _LowLevelFile(name, mode)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   337
            self._extfileobj = False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   338
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   339
        if comptype == '*':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   340
            # Enable transparent compression detection for the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   341
            # stream interface
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   342
            fileobj = _StreamProxy(fileobj)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   343
            comptype = fileobj.getcomptype()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   344
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   345
        self.name     = name or ""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   346
        self.mode     = mode
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   347
        self.comptype = comptype
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   348
        self.fileobj  = fileobj
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   349
        self.bufsize  = bufsize
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   350
        self.buf      = ""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   351
        self.pos      = 0L
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   352
        self.closed   = False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   353
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   354
        if comptype == "gz":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   355
            try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   356
                import zlib
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   357
            except ImportError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   358
                raise CompressionError("zlib module is not available")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   359
            self.zlib = zlib
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   360
            self.crc = zlib.crc32("")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   361
            if mode == "r":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   362
                self._init_read_gz()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   363
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   364
                self._init_write_gz()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   365
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   366
        if comptype == "bz2":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   367
            try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   368
                import bz2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   369
            except ImportError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   370
                raise CompressionError("bz2 module is not available")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   371
            if mode == "r":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   372
                self.dbuf = ""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   373
                self.cmp = bz2.BZ2Decompressor()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   374
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   375
                self.cmp = bz2.BZ2Compressor()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   376
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   377
    def __del__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   378
        if hasattr(self, "closed") and not self.closed:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   379
            self.close()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   380
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   381
    def _init_write_gz(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   382
        """Initialize for writing with gzip compression.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   383
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   384
        self.cmp = self.zlib.compressobj(9, self.zlib.DEFLATED,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   385
                                            -self.zlib.MAX_WBITS,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   386
                                            self.zlib.DEF_MEM_LEVEL,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   387
                                            0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   388
        timestamp = struct.pack("<L", long(time.time()))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   389
        self.__write("\037\213\010\010%s\002\377" % timestamp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   390
        if self.name.endswith(".gz"):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   391
            self.name = self.name[:-3]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   392
        self.__write(self.name + NUL)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   393
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   394
    def write(self, s):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   395
        """Write string s to the stream.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   396
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   397
        if self.comptype == "gz":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   398
            self.crc = self.zlib.crc32(s, self.crc)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   399
        self.pos += len(s)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   400
        if self.comptype != "tar":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   401
            s = self.cmp.compress(s)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   402
        self.__write(s)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   403
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   404
    def __write(self, s):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   405
        """Write string s to the stream if a whole new block
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   406
           is ready to be written.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   407
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   408
        self.buf += s
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   409
        while len(self.buf) > self.bufsize:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   410
            self.fileobj.write(self.buf[:self.bufsize])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   411
            self.buf = self.buf[self.bufsize:]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   412
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   413
    def close(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   414
        """Close the _Stream object. No operation should be
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   415
           done on it afterwards.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   416
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   417
        if self.closed:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   418
            return
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   419
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   420
        if self.mode == "w" and self.comptype != "tar":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   421
            self.buf += self.cmp.flush()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   422
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   423
        if self.mode == "w" and self.buf:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   424
            self.fileobj.write(self.buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   425
            self.buf = ""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   426
            if self.comptype == "gz":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   427
                # The native zlib crc is an unsigned 32-bit integer, but
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   428
                # the Python wrapper implicitly casts that to a signed C
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   429
                # long.  So, on a 32-bit box self.crc may "look negative",
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   430
                # while the same crc on a 64-bit box may "look positive".
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   431
                # To avoid irksome warnings from the `struct` module, force
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   432
                # it to look positive on all boxes.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   433
                self.fileobj.write(struct.pack("<L", self.crc & 0xffffffffL))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   434
                self.fileobj.write(struct.pack("<L", self.pos & 0xffffFFFFL))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   435
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   436
        if not self._extfileobj:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   437
            self.fileobj.close()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   438
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   439
        self.closed = True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   440
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   441
    def _init_read_gz(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   442
        """Initialize for reading a gzip compressed fileobj.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   443
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   444
        self.cmp = self.zlib.decompressobj(-self.zlib.MAX_WBITS)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   445
        self.dbuf = ""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   446
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   447
        # taken from gzip.GzipFile with some alterations
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   448
        if self.__read(2) != "\037\213":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   449
            raise ReadError("not a gzip file")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   450
        if self.__read(1) != "\010":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   451
            raise CompressionError("unsupported compression method")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   452
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   453
        flag = ord(self.__read(1))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   454
        self.__read(6)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   455
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   456
        if flag & 4:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   457
            xlen = ord(self.__read(1)) + 256 * ord(self.__read(1))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   458
            self.read(xlen)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   459
        if flag & 8:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   460
            while True:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   461
                s = self.__read(1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   462
                if not s or s == NUL:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   463
                    break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   464
        if flag & 16:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   465
            while True:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   466
                s = self.__read(1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   467
                if not s or s == NUL:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   468
                    break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   469
        if flag & 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   470
            self.__read(2)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   471
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   472
    def tell(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   473
        """Return the stream's file pointer position.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   474
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   475
        return self.pos
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   476
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   477
    def seek(self, pos=0):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   478
        """Set the stream's file pointer to pos. Negative seeking
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   479
           is forbidden.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   480
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   481
        if pos - self.pos >= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   482
            blocks, remainder = divmod(pos - self.pos, self.bufsize)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   483
            for i in xrange(blocks):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   484
                self.read(self.bufsize)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   485
            self.read(remainder)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   486
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   487
            raise StreamError("seeking backwards is not allowed")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   488
        return self.pos
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   489
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   490
    def read(self, size=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   491
        """Return the next size number of bytes from the stream.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   492
           If size is not defined, return all bytes of the stream
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   493
           up to EOF.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   494
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   495
        if size is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   496
            t = []
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   497
            while True:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   498
                buf = self._read(self.bufsize)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   499
                if not buf:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   500
                    break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   501
                t.append(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   502
            buf = "".join(t)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   503
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   504
            buf = self._read(size)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   505
        self.pos += len(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   506
        return buf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   507
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   508
    def _read(self, size):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   509
        """Return size bytes from the stream.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   510
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   511
        if self.comptype == "tar":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   512
            return self.__read(size)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   513
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   514
        c = len(self.dbuf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   515
        t = [self.dbuf]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   516
        while c < size:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   517
            buf = self.__read(self.bufsize)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   518
            if not buf:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   519
                break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   520
            buf = self.cmp.decompress(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   521
            t.append(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   522
            c += len(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   523
        t = "".join(t)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   524
        self.dbuf = t[size:]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   525
        return t[:size]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   526
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   527
    def __read(self, size):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   528
        """Return size bytes from stream. If internal buffer is empty,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   529
           read another block from the stream.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   530
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   531
        c = len(self.buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   532
        t = [self.buf]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   533
        while c < size:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   534
            buf = self.fileobj.read(self.bufsize)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   535
            if not buf:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   536
                break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   537
            t.append(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   538
            c += len(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   539
        t = "".join(t)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   540
        self.buf = t[size:]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   541
        return t[:size]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   542
# class _Stream
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   543
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   544
class _StreamProxy(object):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   545
    """Small proxy class that enables transparent compression
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   546
       detection for the Stream interface (mode 'r|*').
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   547
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   548
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   549
    def __init__(self, fileobj):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   550
        self.fileobj = fileobj
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   551
        self.buf = self.fileobj.read(BLOCKSIZE)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   552
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   553
    def read(self, size):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   554
        self.read = self.fileobj.read
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   555
        return self.buf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   556
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   557
    def getcomptype(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   558
        if self.buf.startswith("\037\213\010"):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   559
            return "gz"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   560
        if self.buf.startswith("BZh91"):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   561
            return "bz2"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   562
        return "tar"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   563
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   564
    def close(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   565
        self.fileobj.close()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   566
# class StreamProxy
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   567
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   568
class _BZ2Proxy(object):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   569
    """Small proxy class that enables external file object
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   570
       support for "r:bz2" and "w:bz2" modes. This is actually
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   571
       a workaround for a limitation in bz2 module's BZ2File
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   572
       class which (unlike gzip.GzipFile) has no support for
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   573
       a file object argument.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   574
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   575
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   576
    blocksize = 16 * 1024
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   577
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   578
    def __init__(self, fileobj, mode):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   579
        self.fileobj = fileobj
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   580
        self.mode = mode
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   581
        self.init()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   582
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   583
    def init(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   584
        import bz2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   585
        self.pos = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   586
        if self.mode == "r":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   587
            self.bz2obj = bz2.BZ2Decompressor()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   588
            self.fileobj.seek(0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   589
            self.buf = ""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   590
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   591
            self.bz2obj = bz2.BZ2Compressor()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   592
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   593
    def read(self, size):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   594
        b = [self.buf]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   595
        x = len(self.buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   596
        while x < size:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   597
            try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   598
                raw = self.fileobj.read(self.blocksize)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   599
                data = self.bz2obj.decompress(raw)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   600
                b.append(data)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   601
            except EOFError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   602
                break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   603
            x += len(data)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   604
        self.buf = "".join(b)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   605
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   606
        buf = self.buf[:size]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   607
        self.buf = self.buf[size:]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   608
        self.pos += len(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   609
        return buf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   610
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   611
    def seek(self, pos):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   612
        if pos < self.pos:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   613
            self.init()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   614
        self.read(pos - self.pos)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   615
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   616
    def tell(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   617
        return self.pos
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   618
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   619
    def write(self, data):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   620
        self.pos += len(data)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   621
        raw = self.bz2obj.compress(data)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   622
        self.fileobj.write(raw)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   623
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   624
    def close(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   625
        if self.mode == "w":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   626
            raw = self.bz2obj.flush()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   627
            self.fileobj.write(raw)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   628
        self.fileobj.close()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   629
# class _BZ2Proxy
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   630
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   631
#------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   632
# Extraction file object
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   633
#------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   634
class _FileInFile(object):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   635
    """A thin wrapper around an existing file object that
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   636
       provides a part of its data as an individual file
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   637
       object.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   638
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   639
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   640
    def __init__(self, fileobj, offset, size, sparse=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   641
        self.fileobj = fileobj
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   642
        self.offset = offset
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   643
        self.size = size
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   644
        self.sparse = sparse
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   645
        self.position = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   646
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   647
    def tell(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   648
        """Return the current file position.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   649
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   650
        return self.position
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   651
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   652
    def seek(self, position):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   653
        """Seek to a position in the file.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   654
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   655
        self.position = position
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   656
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   657
    def read(self, size=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   658
        """Read data from the file.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   659
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   660
        if size is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   661
            size = self.size - self.position
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   662
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   663
            size = min(size, self.size - self.position)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   664
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   665
        if self.sparse is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   666
            return self.readnormal(size)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   667
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   668
            return self.readsparse(size)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   669
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   670
    def readnormal(self, size):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   671
        """Read operation for regular files.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   672
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   673
        self.fileobj.seek(self.offset + self.position)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   674
        self.position += size
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   675
        return self.fileobj.read(size)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   676
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   677
    def readsparse(self, size):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   678
        """Read operation for sparse files.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   679
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   680
        data = []
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   681
        while size > 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   682
            buf = self.readsparsesection(size)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   683
            if not buf:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   684
                break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   685
            size -= len(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   686
            data.append(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   687
        return "".join(data)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   688
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   689
    def readsparsesection(self, size):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   690
        """Read a single section of a sparse file.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   691
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   692
        section = self.sparse.find(self.position)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   693
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   694
        if section is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   695
            return ""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   696
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   697
        size = min(size, section.offset + section.size - self.position)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   698
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   699
        if isinstance(section, _data):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   700
            realpos = section.realpos + self.position - section.offset
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   701
            self.fileobj.seek(self.offset + realpos)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   702
            self.position += size
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   703
            return self.fileobj.read(size)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   704
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   705
            self.position += size
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   706
            return NUL * size
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   707
#class _FileInFile
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   708
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   709
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   710
class ExFileObject(object):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   711
    """File-like object for reading an archive member.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   712
       Is returned by TarFile.extractfile().
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   713
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   714
    blocksize = 1024
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   715
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   716
    def __init__(self, tarfile, tarinfo):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   717
        self.fileobj = _FileInFile(tarfile.fileobj,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   718
                                   tarinfo.offset_data,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   719
                                   tarinfo.size,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   720
                                   getattr(tarinfo, "sparse", None))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   721
        self.name = tarinfo.name
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   722
        self.mode = "r"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   723
        self.closed = False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   724
        self.size = tarinfo.size
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   725
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   726
        self.position = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   727
        self.buffer = ""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   728
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   729
    def read(self, size=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   730
        """Read at most size bytes from the file. If size is not
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   731
           present or None, read all data until EOF is reached.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   732
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   733
        if self.closed:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   734
            raise ValueError("I/O operation on closed file")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   735
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   736
        buf = ""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   737
        if self.buffer:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   738
            if size is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   739
                buf = self.buffer
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   740
                self.buffer = ""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   741
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   742
                buf = self.buffer[:size]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   743
                self.buffer = self.buffer[size:]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   744
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   745
        if size is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   746
            buf += self.fileobj.read()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   747
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   748
            buf += self.fileobj.read(size - len(buf))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   749
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   750
        self.position += len(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   751
        return buf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   752
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   753
    def readline(self, size=-1):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   754
        """Read one entire line from the file. If size is present
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   755
           and non-negative, return a string with at most that
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   756
           size, which may be an incomplete line.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   757
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   758
        if self.closed:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   759
            raise ValueError("I/O operation on closed file")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   760
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   761
        if "\n" in self.buffer:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   762
            pos = self.buffer.find("\n") + 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   763
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   764
            buffers = [self.buffer]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   765
            while True:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   766
                buf = self.fileobj.read(self.blocksize)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   767
                buffers.append(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   768
                if not buf or "\n" in buf:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   769
                    self.buffer = "".join(buffers)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   770
                    pos = self.buffer.find("\n") + 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   771
                    if pos == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   772
                        # no newline found.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   773
                        pos = len(self.buffer)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   774
                    break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   775
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   776
        if size != -1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   777
            pos = min(size, pos)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   778
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   779
        buf = self.buffer[:pos]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   780
        self.buffer = self.buffer[pos:]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   781
        self.position += len(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   782
        return buf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   783
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   784
    def readlines(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   785
        """Return a list with all remaining lines.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   786
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   787
        result = []
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   788
        while True:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   789
            line = self.readline()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   790
            if not line: break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   791
            result.append(line)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   792
        return result
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   793
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   794
    def tell(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   795
        """Return the current file position.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   796
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   797
        if self.closed:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   798
            raise ValueError("I/O operation on closed file")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   799
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   800
        return self.position
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   801
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   802
    def seek(self, pos, whence=os.SEEK_SET):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   803
        """Seek to a position in the file.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   804
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   805
        if self.closed:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   806
            raise ValueError("I/O operation on closed file")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   807
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   808
        if whence == os.SEEK_SET:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   809
            self.position = min(max(pos, 0), self.size)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   810
        elif whence == os.SEEK_CUR:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   811
            if pos < 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   812
                self.position = max(self.position + pos, 0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   813
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   814
                self.position = min(self.position + pos, self.size)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   815
        elif whence == os.SEEK_END:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   816
            self.position = max(min(self.size + pos, self.size), 0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   817
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   818
            raise ValueError("Invalid argument")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   819
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   820
        self.buffer = ""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   821
        self.fileobj.seek(self.position)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   822
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   823
    def close(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   824
        """Close the file object.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   825
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   826
        self.closed = True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   827
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   828
    def __iter__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   829
        """Get an iterator over the file's lines.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   830
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   831
        while True:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   832
            line = self.readline()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   833
            if not line:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   834
                break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   835
            yield line
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   836
#class ExFileObject
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   837
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   838
#------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   839
# Exported Classes
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   840
#------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   841
class TarInfo(object):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   842
    """Informational class which holds the details about an
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   843
       archive member given by a tar header block.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   844
       TarInfo objects are returned by TarFile.getmember(),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   845
       TarFile.getmembers() and TarFile.gettarinfo() and are
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   846
       usually created internally.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   847
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   848
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   849
    def __init__(self, name=""):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   850
        """Construct a TarInfo object. name is the optional name
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   851
           of the member.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   852
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   853
        self.name = name        # member name (dirnames must end with '/')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   854
        self.mode = 0666        # file permissions
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   855
        self.uid = 0            # user id
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   856
        self.gid = 0            # group id
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   857
        self.size = 0           # file size
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   858
        self.mtime = 0          # modification time
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   859
        self.chksum = 0         # header checksum
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   860
        self.type = REGTYPE     # member type
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   861
        self.linkname = ""      # link name
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   862
        self.uname = "user"     # user name
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   863
        self.gname = "group"    # group name
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   864
        self.devmajor = 0       # device major number
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   865
        self.devminor = 0       # device minor number
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   866
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   867
        self.offset = 0         # the tar header starts here
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   868
        self.offset_data = 0    # the file's data starts here
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   869
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   870
    def __repr__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   871
        return "<%s %r at %#x>" % (self.__class__.__name__,self.name,id(self))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   872
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   873
    @classmethod
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   874
    def frombuf(cls, buf):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   875
        """Construct a TarInfo object from a 512 byte string buffer.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   876
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   877
        if len(buf) != BLOCKSIZE:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   878
            raise ValueError("truncated header")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   879
        if buf.count(NUL) == BLOCKSIZE:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   880
            raise ValueError("empty header")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   881
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   882
        tarinfo = cls()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   883
        tarinfo.buf = buf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   884
        tarinfo.name = nts(buf[0:100])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   885
        tarinfo.mode = nti(buf[100:108])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   886
        tarinfo.uid = nti(buf[108:116])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   887
        tarinfo.gid = nti(buf[116:124])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   888
        tarinfo.size = nti(buf[124:136])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   889
        tarinfo.mtime = nti(buf[136:148])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   890
        tarinfo.chksum = nti(buf[148:156])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   891
        tarinfo.type = buf[156:157]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   892
        tarinfo.linkname = nts(buf[157:257])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   893
        tarinfo.uname = nts(buf[265:297])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   894
        tarinfo.gname = nts(buf[297:329])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   895
        tarinfo.devmajor = nti(buf[329:337])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   896
        tarinfo.devminor = nti(buf[337:345])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   897
        prefix = nts(buf[345:500])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   898
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   899
        if prefix and not tarinfo.issparse():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   900
            tarinfo.name = prefix + "/" + tarinfo.name
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   901
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   902
        if tarinfo.chksum not in calc_chksums(buf):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   903
            raise ValueError("invalid header")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   904
        return tarinfo
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   905
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   906
    def tobuf(self, posix=False):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   907
        """Return a tar header as a string of 512 byte blocks.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   908
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   909
        buf = ""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   910
        type = self.type
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   911
        prefix = ""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   912
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   913
        if self.name.endswith("/"):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   914
            type = DIRTYPE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   915
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   916
        if type in (GNUTYPE_LONGNAME, GNUTYPE_LONGLINK):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   917
            # Prevent "././@LongLink" from being normalized.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   918
            name = self.name
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   919
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   920
            name = normpath(self.name)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   921
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   922
        if type == DIRTYPE:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   923
            # directories should end with '/'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   924
            name += "/"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   925
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   926
        linkname = self.linkname
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   927
        if linkname:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   928
            # if linkname is empty we end up with a '.'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   929
            linkname = normpath(linkname)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   930
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   931
        if posix:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   932
            if self.size > MAXSIZE_MEMBER:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   933
                raise ValueError("file is too large (>= 8 GB)")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   934
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   935
            if len(self.linkname) > LENGTH_LINK:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   936
                raise ValueError("linkname is too long (>%d)" % (LENGTH_LINK))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   937
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   938
            if len(name) > LENGTH_NAME:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   939
                prefix = name[:LENGTH_PREFIX + 1]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   940
                while prefix and prefix[-1] != "/":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   941
                    prefix = prefix[:-1]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   942
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   943
                name = name[len(prefix):]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   944
                prefix = prefix[:-1]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   945
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   946
                if not prefix or len(name) > LENGTH_NAME:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   947
                    raise ValueError("name is too long")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   948
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   949
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   950
            if len(self.linkname) > LENGTH_LINK:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   951
                buf += self._create_gnulong(self.linkname, GNUTYPE_LONGLINK)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   952
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   953
            if len(name) > LENGTH_NAME:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   954
                buf += self._create_gnulong(name, GNUTYPE_LONGNAME)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   955
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   956
        parts = [
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   957
            stn(name, 100),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   958
            itn(self.mode & 07777, 8, posix),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   959
            itn(self.uid, 8, posix),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   960
            itn(self.gid, 8, posix),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   961
            itn(self.size, 12, posix),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   962
            itn(self.mtime, 12, posix),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   963
            "        ", # checksum field
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   964
            type,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   965
            stn(self.linkname, 100),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   966
            stn(MAGIC, 6),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   967
            stn(VERSION, 2),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   968
            stn(self.uname, 32),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   969
            stn(self.gname, 32),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   970
            itn(self.devmajor, 8, posix),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   971
            itn(self.devminor, 8, posix),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   972
            stn(prefix, 155)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   973
        ]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   974
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   975
        buf += "".join(parts).ljust(BLOCKSIZE, NUL)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   976
        chksum = calc_chksums(buf[-BLOCKSIZE:])[0]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   977
        buf = buf[:-364] + "%06o\0" % chksum + buf[-357:]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   978
        self.buf = buf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   979
        return buf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   980
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   981
    def _create_gnulong(self, name, type):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   982
        """Create a GNU longname/longlink header from name.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   983
           It consists of an extended tar header, with the length
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   984
           of the longname as size, followed by data blocks,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   985
           which contain the longname as a null terminated string.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   986
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   987
        name += NUL
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   988
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   989
        tarinfo = self.__class__()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   990
        tarinfo.name = "././@LongLink"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   991
        tarinfo.type = type
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   992
        tarinfo.mode = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   993
        tarinfo.size = len(name)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   994
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   995
        # create extended header
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   996
        buf = tarinfo.tobuf()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   997
        # create name blocks
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   998
        buf += name
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   999
        blocks, remainder = divmod(len(name), BLOCKSIZE)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1000
        if remainder > 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1001
            buf += (BLOCKSIZE - remainder) * NUL
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1002
        return buf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1003
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1004
    def isreg(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1005
        return self.type in REGULAR_TYPES
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1006
    def isfile(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1007
        return self.isreg()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1008
    def isdir(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1009
        return self.type == DIRTYPE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1010
    def issym(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1011
        return self.type == SYMTYPE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1012
    def islnk(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1013
        return self.type == LNKTYPE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1014
    def ischr(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1015
        return self.type == CHRTYPE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1016
    def isblk(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1017
        return self.type == BLKTYPE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1018
    def isfifo(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1019
        return self.type == FIFOTYPE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1020
    def issparse(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1021
        return self.type == GNUTYPE_SPARSE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1022
    def isdev(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1023
        return self.type in (CHRTYPE, BLKTYPE, FIFOTYPE)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1024
# class TarInfo
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1025
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1026
class TarFile(object):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1027
    """The TarFile Class provides an interface to tar archives.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1028
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1029
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1030
    debug = 0                   # May be set from 0 (no msgs) to 3 (all msgs)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1031
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1032
    dereference = False         # If true, add content of linked file to the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1033
                                # tar file, else the link.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1034
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1035
    ignore_zeros = False        # If true, skips empty or invalid blocks and
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1036
                                # continues processing.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1037
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1038
    errorlevel = 0              # If 0, fatal errors only appear in debug
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1039
                                # messages (if debug >= 0). If > 0, errors
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1040
                                # are passed to the caller as exceptions.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1041
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1042
    posix = False               # If True, generates POSIX.1-1990-compliant
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1043
                                # archives (no GNU extensions!)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1044
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1045
    fileobject = ExFileObject
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1046
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1047
    def __init__(self, name=None, mode="r", fileobj=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1048
        """Open an (uncompressed) tar archive `name'. `mode' is either 'r' to
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1049
           read from an existing archive, 'a' to append data to an existing
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1050
           file or 'w' to create a new file overwriting an existing one. `mode'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1051
           defaults to 'r'.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1052
           If `fileobj' is given, it is used for reading or writing data. If it
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1053
           can be determined, `mode' is overridden by `fileobj's mode.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1054
           `fileobj' is not closed, when TarFile is closed.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1055
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1056
        if len(mode) > 1 or mode not in "raw":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1057
            raise ValueError("mode must be 'r', 'a' or 'w'")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1058
        self._mode = mode
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1059
        self.mode = {"r": "rb", "a": "r+b", "w": "wb"}[mode]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1060
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1061
        if not fileobj:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1062
            fileobj = file(name, self.mode)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1063
            self._extfileobj = False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1064
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1065
            if name is None and hasattr(fileobj, "name"):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1066
                name = fileobj.name
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1067
            if hasattr(fileobj, "mode"):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1068
                self.mode = fileobj.mode
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1069
            self._extfileobj = True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1070
        self.name = os.path.abspath(name) if name else None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1071
        self.fileobj = fileobj
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1072
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1073
        # Init datastructures
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1074
        self.closed = False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1075
        self.members = []       # list of members as TarInfo objects
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1076
        self._loaded = False    # flag if all members have been read
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1077
        self.offset = self.fileobj.tell()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1078
                                # current position in the archive file
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1079
        self.inodes = {}        # dictionary caching the inodes of
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1080
                                # archive members already added
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1081
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1082
        if self._mode == "r":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1083
            self.firstmember = None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1084
            self.firstmember = self.next()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1085
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1086
        if self._mode == "a":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1087
            # Move to the end of the archive,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1088
            # before the first empty block.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1089
            self.firstmember = None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1090
            while True:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1091
                try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1092
                    tarinfo = self.next()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1093
                except ReadError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1094
                    self.fileobj.seek(0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1095
                    break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1096
                if tarinfo is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1097
                    self.fileobj.seek(- BLOCKSIZE, 1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1098
                    break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1099
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1100
        if self._mode in "aw":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1101
            self._loaded = True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1102
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1103
    #--------------------------------------------------------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1104
    # Below are the classmethods which act as alternate constructors to the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1105
    # TarFile class. The open() method is the only one that is needed for
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1106
    # public use; it is the "super"-constructor and is able to select an
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1107
    # adequate "sub"-constructor for a particular compression using the mapping
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1108
    # from OPEN_METH.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1109
    #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1110
    # This concept allows one to subclass TarFile without losing the comfort of
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1111
    # the super-constructor. A sub-constructor is registered and made available
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1112
    # by adding it to the mapping in OPEN_METH.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1113
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1114
    @classmethod
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1115
    def open(cls, name=None, mode="r", fileobj=None, bufsize=20*512):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1116
        """Open a tar archive for reading, writing or appending. Return
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1117
           an appropriate TarFile class.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1118
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1119
           mode:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1120
           'r' or 'r:*' open for reading with transparent compression
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1121
           'r:'         open for reading exclusively uncompressed
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1122
           'r:gz'       open for reading with gzip compression
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1123
           'r:bz2'      open for reading with bzip2 compression
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1124
           'a' or 'a:'  open for appending
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1125
           'w' or 'w:'  open for writing without compression
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1126
           'w:gz'       open for writing with gzip compression
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1127
           'w:bz2'      open for writing with bzip2 compression
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1128
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1129
           'r|*'        open a stream of tar blocks with transparent compression
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1130
           'r|'         open an uncompressed stream of tar blocks for reading
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1131
           'r|gz'       open a gzip compressed stream of tar blocks
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1132
           'r|bz2'      open a bzip2 compressed stream of tar blocks
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1133
           'w|'         open an uncompressed stream for writing
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1134
           'w|gz'       open a gzip compressed stream for writing
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1135
           'w|bz2'      open a bzip2 compressed stream for writing
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1136
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1137
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1138
        if not name and not fileobj:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1139
            raise ValueError("nothing to open")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1140
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1141
        if mode in ("r", "r:*"):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1142
            # Find out which *open() is appropriate for opening the file.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1143
            for comptype in cls.OPEN_METH:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1144
                func = getattr(cls, cls.OPEN_METH[comptype])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1145
                if fileobj is not None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1146
                    saved_pos = fileobj.tell()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1147
                try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1148
                    return func(name, "r", fileobj)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1149
                except (ReadError, CompressionError):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1150
                    if fileobj is not None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1151
                        fileobj.seek(saved_pos)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1152
                    continue
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1153
            raise ReadError("file could not be opened successfully")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1154
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1155
        elif ":" in mode:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1156
            filemode, comptype = mode.split(":", 1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1157
            filemode = filemode or "r"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1158
            comptype = comptype or "tar"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1159
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1160
            # Select the *open() function according to
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1161
            # given compression.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1162
            if comptype in cls.OPEN_METH:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1163
                func = getattr(cls, cls.OPEN_METH[comptype])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1164
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1165
                raise CompressionError("unknown compression type %r" % comptype)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1166
            return func(name, filemode, fileobj)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1167
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1168
        elif "|" in mode:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1169
            filemode, comptype = mode.split("|", 1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1170
            filemode = filemode or "r"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1171
            comptype = comptype or "tar"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1172
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1173
            if filemode not in "rw":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1174
                raise ValueError("mode must be 'r' or 'w'")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1175
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1176
            t = cls(name, filemode,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1177
                    _Stream(name, filemode, comptype, fileobj, bufsize))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1178
            t._extfileobj = False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1179
            return t
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1180
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1181
        elif mode in "aw":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1182
            return cls.taropen(name, mode, fileobj)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1183
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1184
        raise ValueError("undiscernible mode")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1185
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1186
    @classmethod
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1187
    def taropen(cls, name, mode="r", fileobj=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1188
        """Open uncompressed tar archive name for reading or writing.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1189
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1190
        if len(mode) > 1 or mode not in "raw":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1191
            raise ValueError("mode must be 'r', 'a' or 'w'")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1192
        return cls(name, mode, fileobj)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1193
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1194
    @classmethod
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1195
    def gzopen(cls, name, mode="r", fileobj=None, compresslevel=9):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1196
        """Open gzip compressed tar archive name for reading or writing.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1197
           Appending is not allowed.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1198
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1199
        if len(mode) > 1 or mode not in "rw":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1200
            raise ValueError("mode must be 'r' or 'w'")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1201
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1202
        try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1203
            import gzip
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1204
            gzip.GzipFile
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1205
        except (ImportError, AttributeError):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1206
            raise CompressionError("gzip module is not available")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1207
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1208
        if fileobj is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1209
            fileobj = file(name, mode + "b")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1210
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1211
        try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1212
            t = cls.taropen(name, mode,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1213
                gzip.GzipFile(name, mode, compresslevel, fileobj))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1214
        except IOError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1215
            raise ReadError("not a gzip file")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1216
        t._extfileobj = False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1217
        return t
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1218
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1219
    @classmethod
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1220
    def bz2open(cls, name, mode="r", fileobj=None, compresslevel=9):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1221
        """Open bzip2 compressed tar archive name for reading or writing.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1222
           Appending is not allowed.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1223
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1224
        if len(mode) > 1 or mode not in "rw":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1225
            raise ValueError("mode must be 'r' or 'w'.")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1226
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1227
        try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1228
            import bz2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1229
        except ImportError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1230
            raise CompressionError("bz2 module is not available")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1231
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1232
        if fileobj is not None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1233
            fileobj = _BZ2Proxy(fileobj, mode)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1234
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1235
            fileobj = bz2.BZ2File(name, mode, compresslevel=compresslevel)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1236
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1237
        try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1238
            t = cls.taropen(name, mode, fileobj)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1239
        except IOError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1240
            raise ReadError("not a bzip2 file")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1241
        t._extfileobj = False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1242
        return t
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1243
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1244
    # All *open() methods are registered here.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1245
    OPEN_METH = {
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1246
        "tar": "taropen",   # uncompressed tar
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1247
        "gz":  "gzopen",    # gzip compressed tar
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1248
        "bz2": "bz2open"    # bzip2 compressed tar
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1249
    }
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1250
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1251
    #--------------------------------------------------------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1252
    # The public methods which TarFile provides:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1253
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1254
    def close(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1255
        """Close the TarFile. In write-mode, two finishing zero blocks are
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1256
           appended to the archive.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1257
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1258
        if self.closed:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1259
            return
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1260
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1261
        if self._mode in "aw":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1262
            self.fileobj.write(NUL * (BLOCKSIZE * 2))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1263
            self.offset += (BLOCKSIZE * 2)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1264
            # fill up the end with zero-blocks
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1265
            # (like option -b20 for tar does)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1266
            blocks, remainder = divmod(self.offset, RECORDSIZE)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1267
            if remainder > 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1268
                self.fileobj.write(NUL * (RECORDSIZE - remainder))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1269
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1270
        if not self._extfileobj:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1271
            self.fileobj.close()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1272
        self.closed = True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1273
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1274
    def getmember(self, name):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1275
        """Return a TarInfo object for member `name'. If `name' can not be
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1276
           found in the archive, KeyError is raised. If a member occurs more
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1277
           than once in the archive, its last occurence is assumed to be the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1278
           most up-to-date version.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1279
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1280
        tarinfo = self._getmember(name)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1281
        if tarinfo is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1282
            raise KeyError("filename %r not found" % name)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1283
        return tarinfo
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1284
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1285
    def getmembers(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1286
        """Return the members of the archive as a list of TarInfo objects. The
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1287
           list has the same order as the members in the archive.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1288
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1289
        self._check()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1290
        if not self._loaded:    # if we want to obtain a list of
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1291
            self._load()        # all members, we first have to
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1292
                                # scan the whole archive.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1293
        return self.members
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1294
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1295
    def getnames(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1296
        """Return the members of the archive as a list of their names. It has
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1297
           the same order as the list returned by getmembers().
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1298
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1299
        return [tarinfo.name for tarinfo in self.getmembers()]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1300
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1301
    def gettarinfo(self, name=None, arcname=None, fileobj=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1302
        """Create a TarInfo object for either the file `name' or the file
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1303
           object `fileobj' (using os.fstat on its file descriptor). You can
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1304
           modify some of the TarInfo's attributes before you add it using
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1305
           addfile(). If given, `arcname' specifies an alternative name for the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1306
           file in the archive.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1307
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1308
        self._check("aw")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1309
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1310
        # When fileobj is given, replace name by
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1311
        # fileobj's real name.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1312
        if fileobj is not None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1313
            name = fileobj.name
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1314
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1315
        # Building the name of the member in the archive.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1316
        # Backward slashes are converted to forward slashes,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1317
        # Absolute paths are turned to relative paths.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1318
        if arcname is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1319
            arcname = name
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1320
        arcname = normpath(arcname)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1321
        drv, arcname = os.path.splitdrive(arcname)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1322
        while arcname[0:1] == "/":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1323
            arcname = arcname[1:]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1324
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1325
        # Now, fill the TarInfo object with
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1326
        # information specific for the file.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1327
        tarinfo = TarInfo()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1328
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1329
        # Use os.stat or os.lstat, depending on platform
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1330
        # and if symlinks shall be resolved.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1331
        if fileobj is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1332
            if hasattr(os, "lstat") and not self.dereference:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1333
                statres = os.lstat(name)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1334
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1335
                statres = os.stat(name)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1336
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1337
            statres = os.fstat(fileobj.fileno())
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1338
        linkname = ""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1339
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1340
        stmd = statres.st_mode
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1341
        if stat.S_ISREG(stmd):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1342
            inode = (statres.st_ino, statres.st_dev)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1343
            if not self.dereference and \
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1344
                    statres.st_nlink > 1 and inode in self.inodes:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1345
                # Is it a hardlink to an already
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1346
                # archived file?
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1347
                type = LNKTYPE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1348
                linkname = self.inodes[inode]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1349
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1350
                # The inode is added only if its valid.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1351
                # For win32 it is always 0.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1352
                type = REGTYPE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1353
                if inode[0]:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1354
                    self.inodes[inode] = arcname
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1355
        elif stat.S_ISDIR(stmd):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1356
            type = DIRTYPE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1357
            if arcname[-1:] != "/":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1358
                arcname += "/"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1359
        elif stat.S_ISFIFO(stmd):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1360
            type = FIFOTYPE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1361
        elif stat.S_ISLNK(stmd):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1362
            type = SYMTYPE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1363
            linkname = os.readlink(name)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1364
        elif stat.S_ISCHR(stmd):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1365
            type = CHRTYPE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1366
        elif stat.S_ISBLK(stmd):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1367
            type = BLKTYPE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1368
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1369
            return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1370
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1371
        # Fill the TarInfo object with all
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1372
        # information we can get.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1373
        tarinfo.name = arcname
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1374
        tarinfo.mode = stmd
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1375
        tarinfo.uid = statres.st_uid
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1376
        tarinfo.gid = statres.st_gid
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1377
        if stat.S_ISREG(stmd):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1378
            tarinfo.size = statres.st_size
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1379
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1380
            tarinfo.size = 0L
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1381
        tarinfo.mtime = statres.st_mtime
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1382
        tarinfo.type = type
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1383
        tarinfo.linkname = linkname
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1384
        if pwd:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1385
            try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1386
                tarinfo.uname = pwd.getpwuid(tarinfo.uid)[0]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1387
            except KeyError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1388
                pass
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1389
        if grp:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1390
            try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1391
                tarinfo.gname = grp.getgrgid(tarinfo.gid)[0]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1392
            except KeyError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1393
                pass
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1394
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1395
        if type in (CHRTYPE, BLKTYPE):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1396
            if hasattr(os, "major") and hasattr(os, "minor"):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1397
                tarinfo.devmajor = os.major(statres.st_rdev)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1398
                tarinfo.devminor = os.minor(statres.st_rdev)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1399
        return tarinfo
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1400
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1401
    def list(self, verbose=True):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1402
        """Print a table of contents to sys.stdout. If `verbose' is False, only
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1403
           the names of the members are printed. If it is True, an `ls -l'-like
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1404
           output is produced.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1405
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1406
        self._check()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1407
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1408
        for tarinfo in self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1409
            if verbose:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1410
                print filemode(tarinfo.mode),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1411
                print "%s/%s" % (tarinfo.uname or tarinfo.uid,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1412
                                 tarinfo.gname or tarinfo.gid),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1413
                if tarinfo.ischr() or tarinfo.isblk():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1414
                    print "%10s" % ("%d,%d" \
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1415
                                    % (tarinfo.devmajor, tarinfo.devminor)),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1416
                else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1417
                    print "%10d" % tarinfo.size,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1418
                print "%d-%02d-%02d %02d:%02d:%02d" \
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1419
                      % time.localtime(tarinfo.mtime)[:6],
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1420
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1421
            print tarinfo.name,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1422
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1423
            if verbose:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1424
                if tarinfo.issym():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1425
                    print "->", tarinfo.linkname,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1426
                if tarinfo.islnk():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1427
                    print "link to", tarinfo.linkname,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1428
            print
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1429
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1430
    def add(self, name, arcname=None, recursive=True):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1431
        """Add the file `name' to the archive. `name' may be any type of file
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1432
           (directory, fifo, symbolic link, etc.). If given, `arcname'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1433
           specifies an alternative name for the file in the archive.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1434
           Directories are added recursively by default. This can be avoided by
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1435
           setting `recursive' to False.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1436
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1437
        self._check("aw")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1438
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1439
        if arcname is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1440
            arcname = name
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1441
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1442
        # Skip if somebody tries to archive the archive...
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1443
        if self.name is not None and os.path.abspath(name) == self.name:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1444
            self._dbg(2, "tarfile: Skipped %r" % name)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1445
            return
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1446
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1447
        # Special case: The user wants to add the current
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1448
        # working directory.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1449
        if name == ".":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1450
            if recursive:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1451
                if arcname == ".":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1452
                    arcname = ""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1453
                for f in os.listdir("."):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1454
                    self.add(f, os.path.join(arcname, f))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1455
            return
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1456
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1457
        self._dbg(1, name)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1458
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1459
        # Create a TarInfo object from the file.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1460
        tarinfo = self.gettarinfo(name, arcname)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1461
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1462
        if tarinfo is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1463
            self._dbg(1, "tarfile: Unsupported type %r" % name)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1464
            return
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1465
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1466
        # Append the tar header and data to the archive.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1467
        if tarinfo.isreg():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1468
            f = file(name, "rb")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1469
            self.addfile(tarinfo, f)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1470
            f.close()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1471
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1472
        elif tarinfo.isdir():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1473
            self.addfile(tarinfo)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1474
            if recursive:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1475
                for f in os.listdir(name):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1476
                    self.add(os.path.join(name, f), os.path.join(arcname, f))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1477
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1478
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1479
            self.addfile(tarinfo)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1480
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1481
    def addfile(self, tarinfo, fileobj=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1482
        """Add the TarInfo object `tarinfo' to the archive. If `fileobj' is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1483
           given, tarinfo.size bytes are read from it and added to the archive.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1484
           You can create TarInfo objects using gettarinfo().
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1485
           On Windows platforms, `fileobj' should always be opened with mode
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1486
           'rb' to avoid irritation about the file size.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1487
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1488
        self._check("aw")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1489
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1490
        tarinfo = copy.copy(tarinfo)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1491
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1492
        buf = tarinfo.tobuf(self.posix)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1493
        self.fileobj.write(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1494
        self.offset += len(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1495
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1496
        # If there's data to follow, append it.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1497
        if fileobj is not None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1498
            copyfileobj(fileobj, self.fileobj, tarinfo.size)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1499
            blocks, remainder = divmod(tarinfo.size, BLOCKSIZE)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1500
            if remainder > 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1501
                self.fileobj.write(NUL * (BLOCKSIZE - remainder))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1502
                blocks += 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1503
            self.offset += blocks * BLOCKSIZE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1504
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1505
        self.members.append(tarinfo)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1506
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1507
    def extractall(self, path=".", members=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1508
        """Extract all members from the archive to the current working
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1509
           directory and set owner, modification time and permissions on
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1510
           directories afterwards. `path' specifies a different directory
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1511
           to extract to. `members' is optional and must be a subset of the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1512
           list returned by getmembers().
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1513
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1514
        directories = []
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1515
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1516
        if members is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1517
            members = self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1518
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1519
        for tarinfo in members:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1520
            if tarinfo.isdir():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1521
                # Extract directories with a safe mode.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1522
                directories.append(tarinfo)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1523
                tarinfo = copy.copy(tarinfo)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1524
                tarinfo.mode = 0700
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1525
            self.extract(tarinfo, path)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1526
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1527
        # Reverse sort directories.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1528
        directories.sort(lambda a, b: cmp(a.name, b.name))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1529
        directories.reverse()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1530
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1531
        # Set correct owner, mtime and filemode on directories.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1532
        for tarinfo in directories:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1533
            dirpath = os.path.join(path, tarinfo.name)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1534
            try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1535
                self.chown(tarinfo, dirpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1536
                self.utime(tarinfo, dirpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1537
                self.chmod(tarinfo, dirpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1538
            except ExtractError, e:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1539
                if self.errorlevel > 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1540
                    raise
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1541
                else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1542
                    self._dbg(1, "tarfile: %s" % e)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1543
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1544
    def extract(self, member, path=""):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1545
        """Extract a member from the archive to the current working directory,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1546
           using its full name. Its file information is extracted as accurately
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1547
           as possible. `member' may be a filename or a TarInfo object. You can
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1548
           specify a different directory using `path'.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1549
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1550
        self._check("r")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1551
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1552
        if isinstance(member, TarInfo):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1553
            tarinfo = member
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1554
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1555
            tarinfo = self.getmember(member)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1556
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1557
        # Prepare the link target for makelink().
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1558
        if tarinfo.islnk():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1559
            tarinfo._link_target = os.path.join(path, tarinfo.linkname)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1560
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1561
        try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1562
            self._extract_member(tarinfo, os.path.join(path, tarinfo.name))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1563
        except EnvironmentError, e:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1564
            if self.errorlevel > 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1565
                raise
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1566
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1567
                if e.filename is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1568
                    self._dbg(1, "tarfile: %s" % e.strerror)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1569
                else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1570
                    self._dbg(1, "tarfile: %s %r" % (e.strerror, e.filename))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1571
        except ExtractError, e:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1572
            if self.errorlevel > 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1573
                raise
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1574
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1575
                self._dbg(1, "tarfile: %s" % e)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1576
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1577
    def extractfile(self, member):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1578
        """Extract a member from the archive as a file object. `member' may be
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1579
           a filename or a TarInfo object. If `member' is a regular file, a
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1580
           file-like object is returned. If `member' is a link, a file-like
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1581
           object is constructed from the link's target. If `member' is none of
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1582
           the above, None is returned.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1583
           The file-like object is read-only and provides the following
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1584
           methods: read(), readline(), readlines(), seek() and tell()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1585
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1586
        self._check("r")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1587
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1588
        if isinstance(member, TarInfo):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1589
            tarinfo = member
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1590
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1591
            tarinfo = self.getmember(member)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1592
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1593
        if tarinfo.isreg():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1594
            return self.fileobject(self, tarinfo)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1595
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1596
        elif tarinfo.type not in SUPPORTED_TYPES:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1597
            # If a member's type is unknown, it is treated as a
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1598
            # regular file.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1599
            return self.fileobject(self, tarinfo)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1600
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1601
        elif tarinfo.islnk() or tarinfo.issym():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1602
            if isinstance(self.fileobj, _Stream):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1603
                # A small but ugly workaround for the case that someone tries
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1604
                # to extract a (sym)link as a file-object from a non-seekable
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1605
                # stream of tar blocks.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1606
                raise StreamError("cannot extract (sym)link as file object")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1607
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1608
                # A (sym)link's file object is its target's file object.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1609
                return self.extractfile(self._getmember(tarinfo.linkname,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1610
                                                        tarinfo))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1611
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1612
            # If there's no data associated with the member (directory, chrdev,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1613
            # blkdev, etc.), return None instead of a file object.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1614
            return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1615
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1616
    def _extract_member(self, tarinfo, targetpath):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1617
        """Extract the TarInfo object tarinfo to a physical
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1618
           file called targetpath.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1619
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1620
        # Fetch the TarInfo object for the given name
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1621
        # and build the destination pathname, replacing
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1622
        # forward slashes to platform specific separators.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1623
        if targetpath[-1:] == "/":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1624
            targetpath = targetpath[:-1]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1625
        targetpath = os.path.normpath(targetpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1626
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1627
        # Create all upper directories.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1628
        upperdirs = os.path.dirname(targetpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1629
        if upperdirs and not os.path.exists(upperdirs):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1630
            # Create directories that are not part of the archive with
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1631
            # default permissions.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1632
            os.makedirs(upperdirs)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1633
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1634
        if tarinfo.islnk() or tarinfo.issym():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1635
            self._dbg(1, "%s -> %s" % (tarinfo.name, tarinfo.linkname))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1636
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1637
            self._dbg(1, tarinfo.name)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1638
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1639
        if tarinfo.isreg():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1640
            self.makefile(tarinfo, targetpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1641
        elif tarinfo.isdir():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1642
            self.makedir(tarinfo, targetpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1643
        elif tarinfo.isfifo():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1644
            self.makefifo(tarinfo, targetpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1645
        elif tarinfo.ischr() or tarinfo.isblk():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1646
            self.makedev(tarinfo, targetpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1647
        elif tarinfo.islnk() or tarinfo.issym():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1648
            self.makelink(tarinfo, targetpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1649
        elif tarinfo.type not in SUPPORTED_TYPES:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1650
            self.makeunknown(tarinfo, targetpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1651
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1652
            self.makefile(tarinfo, targetpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1653
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1654
        self.chown(tarinfo, targetpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1655
        if not tarinfo.issym():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1656
            self.chmod(tarinfo, targetpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1657
            self.utime(tarinfo, targetpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1658
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1659
    #--------------------------------------------------------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1660
    # Below are the different file methods. They are called via
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1661
    # _extract_member() when extract() is called. They can be replaced in a
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1662
    # subclass to implement other functionality.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1663
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1664
    def makedir(self, tarinfo, targetpath):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1665
        """Make a directory called targetpath.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1666
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1667
        try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1668
            # Use a safe mode for the directory, the real mode is set
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1669
            # later in _extract_member().
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1670
            os.mkdir(targetpath, 0700)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1671
        except EnvironmentError, e:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1672
            if e.errno != errno.EEXIST:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1673
                raise
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1674
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1675
    def makefile(self, tarinfo, targetpath):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1676
        """Make a file called targetpath.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1677
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1678
        source = self.extractfile(tarinfo)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1679
        target = file(targetpath, "wb")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1680
        copyfileobj(source, target)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1681
        source.close()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1682
        target.close()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1683
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1684
    def makeunknown(self, tarinfo, targetpath):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1685
        """Make a file from a TarInfo object with an unknown type
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1686
           at targetpath.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1687
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1688
        self.makefile(tarinfo, targetpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1689
        self._dbg(1, "tarfile: Unknown file type %r, " \
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1690
                     "extracted as regular file." % tarinfo.type)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1691
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1692
    def makefifo(self, tarinfo, targetpath):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1693
        """Make a fifo called targetpath.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1694
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1695
        if hasattr(os, "mkfifo"):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1696
            os.mkfifo(targetpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1697
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1698
            raise ExtractError("fifo not supported by system")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1699
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1700
    def makedev(self, tarinfo, targetpath):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1701
        """Make a character or block device called targetpath.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1702
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1703
        if not hasattr(os, "mknod") or not hasattr(os, "makedev"):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1704
            raise ExtractError("special devices not supported by system")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1705
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1706
        mode = tarinfo.mode
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1707
        if tarinfo.isblk():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1708
            mode |= stat.S_IFBLK
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1709
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1710
            mode |= stat.S_IFCHR
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1711
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1712
        os.mknod(targetpath, mode,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1713
                 os.makedev(tarinfo.devmajor, tarinfo.devminor))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1714
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1715
    def makelink(self, tarinfo, targetpath):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1716
        """Make a (symbolic) link called targetpath. If it cannot be created
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1717
          (platform limitation), we try to make a copy of the referenced file
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1718
          instead of a link.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1719
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1720
        linkpath = tarinfo.linkname
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1721
        try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1722
            if tarinfo.issym():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1723
                os.symlink(linkpath, targetpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1724
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1725
                # See extract().
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1726
                os.link(tarinfo._link_target, targetpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1727
        except AttributeError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1728
            if tarinfo.issym():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1729
                linkpath = os.path.join(os.path.dirname(tarinfo.name),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1730
                                        linkpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1731
                linkpath = normpath(linkpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1732
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1733
            try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1734
                self._extract_member(self.getmember(linkpath), targetpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1735
            except (EnvironmentError, KeyError), e:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1736
                linkpath = os.path.normpath(linkpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1737
                try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1738
                    shutil.copy2(linkpath, targetpath)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1739
                except EnvironmentError, e:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1740
                    raise IOError("link could not be created")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1741
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1742
    def chown(self, tarinfo, targetpath):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1743
        """Set owner of targetpath according to tarinfo.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1744
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1745
        if pwd and hasattr(os, "geteuid") and os.geteuid() == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1746
            # We have to be root to do so.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1747
            try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1748
                g = grp.getgrnam(tarinfo.gname)[2]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1749
            except KeyError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1750
                try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1751
                    g = grp.getgrgid(tarinfo.gid)[2]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1752
                except KeyError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1753
                    g = os.getgid()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1754
            try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1755
                u = pwd.getpwnam(tarinfo.uname)[2]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1756
            except KeyError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1757
                try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1758
                    u = pwd.getpwuid(tarinfo.uid)[2]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1759
                except KeyError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1760
                    u = os.getuid()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1761
            try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1762
                if tarinfo.issym() and hasattr(os, "lchown"):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1763
                    os.lchown(targetpath, u, g)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1764
                else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1765
                    if sys.platform != "os2emx":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1766
                        os.chown(targetpath, u, g)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1767
            except EnvironmentError, e:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1768
                raise ExtractError("could not change owner")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1769
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1770
    def chmod(self, tarinfo, targetpath):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1771
        """Set file permissions of targetpath according to tarinfo.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1772
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1773
        if hasattr(os, 'chmod'):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1774
            try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1775
                os.chmod(targetpath, tarinfo.mode)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1776
            except EnvironmentError, e:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1777
                raise ExtractError("could not change mode")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1778
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1779
    def utime(self, tarinfo, targetpath):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1780
        """Set modification time of targetpath according to tarinfo.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1781
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1782
        if not hasattr(os, 'utime'):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1783
            return
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1784
        if sys.platform == "win32" and tarinfo.isdir():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1785
            # According to msdn.microsoft.com, it is an error (EACCES)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1786
            # to use utime() on directories.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1787
            return
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1788
        try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1789
            os.utime(targetpath, (tarinfo.mtime, tarinfo.mtime))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1790
        except EnvironmentError, e:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1791
            raise ExtractError("could not change modification time")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1792
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1793
    #--------------------------------------------------------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1794
    def next(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1795
        """Return the next member of the archive as a TarInfo object, when
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1796
           TarFile is opened for reading. Return None if there is no more
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1797
           available.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1798
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1799
        self._check("ra")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1800
        if self.firstmember is not None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1801
            m = self.firstmember
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1802
            self.firstmember = None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1803
            return m
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1804
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1805
        # Read the next block.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1806
        self.fileobj.seek(self.offset)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1807
        while True:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1808
            buf = self.fileobj.read(BLOCKSIZE)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1809
            if not buf:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1810
                return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1811
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1812
            try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1813
                tarinfo = TarInfo.frombuf(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1814
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1815
                # Set the TarInfo object's offset to the current position of the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1816
                # TarFile and set self.offset to the position where the data blocks
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1817
                # should begin.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1818
                tarinfo.offset = self.offset
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1819
                self.offset += BLOCKSIZE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1820
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1821
                tarinfo = self.proc_member(tarinfo)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1822
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1823
            except ValueError, e:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1824
                if self.ignore_zeros:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1825
                    self._dbg(2, "0x%X: empty or invalid block: %s" %
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1826
                              (self.offset, e))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1827
                    self.offset += BLOCKSIZE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1828
                    continue
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1829
                else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1830
                    if self.offset == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1831
                        raise ReadError("empty, unreadable or compressed "
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1832
                                        "file: %s" % e)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1833
                    return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1834
            break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1835
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1836
        # Some old tar programs represent a directory as a regular
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1837
        # file with a trailing slash.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1838
        if tarinfo.isreg() and tarinfo.name.endswith("/"):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1839
            tarinfo.type = DIRTYPE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1840
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1841
        # Directory names should have a '/' at the end.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1842
        if tarinfo.isdir() and not tarinfo.name.endswith("/"):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1843
            tarinfo.name += "/"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1844
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1845
        self.members.append(tarinfo)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1846
        return tarinfo
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1847
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1848
    #--------------------------------------------------------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1849
    # The following are methods that are called depending on the type of a
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1850
    # member. The entry point is proc_member() which is called with a TarInfo
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1851
    # object created from the header block from the current offset. The
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1852
    # proc_member() method can be overridden in a subclass to add custom
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1853
    # proc_*() methods. A proc_*() method MUST implement the following
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1854
    # operations:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1855
    # 1. Set tarinfo.offset_data to the position where the data blocks begin,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1856
    #    if there is data that follows.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1857
    # 2. Set self.offset to the position where the next member's header will
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1858
    #    begin.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1859
    # 3. Return tarinfo or another valid TarInfo object.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1860
    def proc_member(self, tarinfo):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1861
        """Choose the right processing method for tarinfo depending
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1862
           on its type and call it.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1863
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1864
        if tarinfo.type in (GNUTYPE_LONGNAME, GNUTYPE_LONGLINK):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1865
            return self.proc_gnulong(tarinfo)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1866
        elif tarinfo.type == GNUTYPE_SPARSE:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1867
            return self.proc_sparse(tarinfo)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1868
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1869
            return self.proc_builtin(tarinfo)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1870
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1871
    def proc_builtin(self, tarinfo):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1872
        """Process a builtin type member or an unknown member
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1873
           which will be treated as a regular file.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1874
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1875
        tarinfo.offset_data = self.offset
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1876
        if tarinfo.isreg() or tarinfo.type not in SUPPORTED_TYPES:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1877
            # Skip the following data blocks.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1878
            self.offset += self._block(tarinfo.size)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1879
        return tarinfo
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1880
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1881
    def proc_gnulong(self, tarinfo):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1882
        """Process the blocks that hold a GNU longname
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1883
           or longlink member.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1884
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1885
        buf = ""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1886
        count = tarinfo.size
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1887
        while count > 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1888
            block = self.fileobj.read(BLOCKSIZE)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1889
            buf += block
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1890
            self.offset += BLOCKSIZE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1891
            count -= BLOCKSIZE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1892
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1893
        # Fetch the next header and process it.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1894
        b = self.fileobj.read(BLOCKSIZE)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1895
        t = TarInfo.frombuf(b)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1896
        t.offset = self.offset
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1897
        self.offset += BLOCKSIZE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1898
        next = self.proc_member(t)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1899
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1900
        # Patch the TarInfo object from the next header with
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1901
        # the longname information.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1902
        next.offset = tarinfo.offset
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1903
        if tarinfo.type == GNUTYPE_LONGNAME:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1904
            next.name = nts(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1905
        elif tarinfo.type == GNUTYPE_LONGLINK:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1906
            next.linkname = nts(buf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1907
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1908
        return next
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1909
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1910
    def proc_sparse(self, tarinfo):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1911
        """Process a GNU sparse header plus extra headers.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1912
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1913
        buf = tarinfo.buf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1914
        sp = _ringbuffer()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1915
        pos = 386
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1916
        lastpos = 0L
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1917
        realpos = 0L
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1918
        # There are 4 possible sparse structs in the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1919
        # first header.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1920
        for i in xrange(4):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1921
            try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1922
                offset = nti(buf[pos:pos + 12])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1923
                numbytes = nti(buf[pos + 12:pos + 24])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1924
            except ValueError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1925
                break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1926
            if offset > lastpos:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1927
                sp.append(_hole(lastpos, offset - lastpos))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1928
            sp.append(_data(offset, numbytes, realpos))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1929
            realpos += numbytes
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1930
            lastpos = offset + numbytes
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1931
            pos += 24
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1932
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1933
        isextended = ord(buf[482])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1934
        origsize = nti(buf[483:495])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1935
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1936
        # If the isextended flag is given,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1937
        # there are extra headers to process.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1938
        while isextended == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1939
            buf = self.fileobj.read(BLOCKSIZE)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1940
            self.offset += BLOCKSIZE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1941
            pos = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1942
            for i in xrange(21):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1943
                try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1944
                    offset = nti(buf[pos:pos + 12])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1945
                    numbytes = nti(buf[pos + 12:pos + 24])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1946
                except ValueError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1947
                    break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1948
                if offset > lastpos:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1949
                    sp.append(_hole(lastpos, offset - lastpos))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1950
                sp.append(_data(offset, numbytes, realpos))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1951
                realpos += numbytes
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1952
                lastpos = offset + numbytes
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1953
                pos += 24
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1954
            isextended = ord(buf[504])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1955
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1956
        if lastpos < origsize:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1957
            sp.append(_hole(lastpos, origsize - lastpos))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1958
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1959
        tarinfo.sparse = sp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1960
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1961
        tarinfo.offset_data = self.offset
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1962
        self.offset += self._block(tarinfo.size)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1963
        tarinfo.size = origsize
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1964
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1965
        return tarinfo
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1966
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1967
    #--------------------------------------------------------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1968
    # Little helper methods:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1969
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1970
    def _block(self, count):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1971
        """Round up a byte count by BLOCKSIZE and return it,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1972
           e.g. _block(834) => 1024.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1973
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1974
        blocks, remainder = divmod(count, BLOCKSIZE)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1975
        if remainder:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1976
            blocks += 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1977
        return blocks * BLOCKSIZE
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1978
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1979
    def _getmember(self, name, tarinfo=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1980
        """Find an archive member by name from bottom to top.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1981
           If tarinfo is given, it is used as the starting point.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1982
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1983
        # Ensure that all members have been loaded.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1984
        members = self.getmembers()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1985
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1986
        if tarinfo is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1987
            end = len(members)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1988
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1989
            end = members.index(tarinfo)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1990
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1991
        for i in xrange(end - 1, -1, -1):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1992
            if name == members[i].name:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1993
                return members[i]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1994
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1995
    def _load(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1996
        """Read through the entire archive file and look for readable
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1997
           members.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1998
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1999
        while True:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2000
            tarinfo = self.next()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2001
            if tarinfo is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2002
                break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2003
        self._loaded = True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2004
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2005
    def _check(self, mode=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2006
        """Check if TarFile is still open, and if the operation's mode
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2007
           corresponds to TarFile's mode.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2008
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2009
        if self.closed:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2010
            raise IOError("%s is closed" % self.__class__.__name__)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2011
        if mode is not None and self._mode not in mode:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2012
            raise IOError("bad operation for mode %r" % self._mode)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2013
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2014
    def __iter__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2015
        """Provide an iterator object.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2016
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2017
        if self._loaded:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2018
            return iter(self.members)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2019
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2020
            return TarIter(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2021
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2022
    def _dbg(self, level, msg):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2023
        """Write debugging output to sys.stderr.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2024
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2025
        if level <= self.debug:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2026
            print >> sys.stderr, msg
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2027
# class TarFile
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2028
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2029
class TarIter:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2030
    """Iterator Class.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2031
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2032
       for tarinfo in TarFile(...):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2033
           suite...
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2034
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2035
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2036
    def __init__(self, tarfile):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2037
        """Construct a TarIter object.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2038
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2039
        self.tarfile = tarfile
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2040
        self.index = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2041
    def __iter__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2042
        """Return iterator object.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2043
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2044
        return self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2045
    def next(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2046
        """Return the next item using TarFile's next() method.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2047
           When all members have been read, set TarFile as _loaded.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2048
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2049
        # Fix for SF #1100429: Under rare circumstances it can
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2050
        # happen that getmembers() is called during iteration,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2051
        # which will cause TarIter to stop prematurely.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2052
        if not self.tarfile._loaded:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2053
            tarinfo = self.tarfile.next()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2054
            if not tarinfo:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2055
                self.tarfile._loaded = True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2056
                raise StopIteration
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2057
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2058
            try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2059
                tarinfo = self.tarfile.members[self.index]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2060
            except IndexError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2061
                raise StopIteration
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2062
        self.index += 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2063
        return tarinfo
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2064
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2065
# Helper classes for sparse file support
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2066
class _section:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2067
    """Base class for _data and _hole.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2068
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2069
    def __init__(self, offset, size):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2070
        self.offset = offset
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2071
        self.size = size
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2072
    def __contains__(self, offset):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2073
        return self.offset <= offset < self.offset + self.size
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2074
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2075
class _data(_section):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2076
    """Represent a data section in a sparse file.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2077
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2078
    def __init__(self, offset, size, realpos):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2079
        _section.__init__(self, offset, size)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2080
        self.realpos = realpos
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2081
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2082
class _hole(_section):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2083
    """Represent a hole section in a sparse file.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2084
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2085
    pass
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2086
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2087
class _ringbuffer(list):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2088
    """Ringbuffer class which increases performance
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2089
       over a regular list.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2090
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2091
    def __init__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2092
        self.idx = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2093
    def find(self, offset):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2094
        idx = self.idx
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2095
        while True:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2096
            item = self[idx]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2097
            if offset in item:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2098
                break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2099
            idx += 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2100
            if idx == len(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2101
                idx = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2102
            if idx == self.idx:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2103
                # End of File
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2104
                return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2105
        self.idx = idx
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2106
        return item
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2107
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2108
#---------------------------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2109
# zipfile compatible TarFile class
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2110
#---------------------------------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2111
TAR_PLAIN = 0           # zipfile.ZIP_STORED
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2112
TAR_GZIPPED = 8         # zipfile.ZIP_DEFLATED
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2113
class TarFileCompat:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2114
    """TarFile class compatible with standard module zipfile's
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2115
       ZipFile class.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2116
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2117
    def __init__(self, file, mode="r", compression=TAR_PLAIN):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2118
        if compression == TAR_PLAIN:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2119
            self.tarfile = TarFile.taropen(file, mode)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2120
        elif compression == TAR_GZIPPED:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2121
            self.tarfile = TarFile.gzopen(file, mode)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2122
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2123
            raise ValueError("unknown compression constant")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2124
        if mode[0:1] == "r":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2125
            members = self.tarfile.getmembers()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2126
            for m in members:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2127
                m.filename = m.name
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2128
                m.file_size = m.size
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2129
                m.date_time = time.gmtime(m.mtime)[:6]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2130
    def namelist(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2131
        return map(lambda m: m.name, self.infolist())
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2132
    def infolist(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2133
        return filter(lambda m: m.type in REGULAR_TYPES,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2134
                      self.tarfile.getmembers())
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2135
    def printdir(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2136
        self.tarfile.list()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2137
    def testzip(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2138
        return
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2139
    def getinfo(self, name):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2140
        return self.tarfile.getmember(name)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2141
    def read(self, name):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2142
        return self.tarfile.extractfile(self.tarfile.getmember(name)).read()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2143
    def write(self, filename, arcname=None, compress_type=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2144
        self.tarfile.add(filename, arcname)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2145
    def writestr(self, zinfo, bytes):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2146
        try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2147
            from cStringIO import StringIO
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2148
        except ImportError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2149
            from StringIO import StringIO
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2150
        import calendar
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2151
        zinfo.name = zinfo.filename
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2152
        zinfo.size = zinfo.file_size
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2153
        zinfo.mtime = calendar.timegm(zinfo.date_time)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2154
        self.tarfile.addfile(zinfo, StringIO(bytes))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2155
    def close(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2156
        self.tarfile.close()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2157
#class TarFileCompat
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2158
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2159
#--------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2160
# exported functions
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2161
#--------------------
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2162
def is_tarfile(name):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2163
    """Return True if name points to a tar archive that we
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2164
       are able to handle, else return False.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2165
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2166
    try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2167
        t = open(name)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2168
        t.close()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2169
        return True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2170
    except TarError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2171
        return False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2172
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2173
open = TarFile.open