Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
File: tarfile.py
# -*- coding: iso-8859-1 -*-
[0] Fix | Delete
#-------------------------------------------------------------------
[1] Fix | Delete
# tarfile.py
[2] Fix | Delete
#-------------------------------------------------------------------
[3] Fix | Delete
[4] Fix | Delete
# All rights reserved.
[5] Fix | Delete
#
[6] Fix | Delete
# Permission is hereby granted, free of charge, to any person
[7] Fix | Delete
# obtaining a copy of this software and associated documentation
[8] Fix | Delete
# files (the "Software"), to deal in the Software without
[9] Fix | Delete
# restriction, including without limitation the rights to use,
[10] Fix | Delete
# copy, modify, merge, publish, distribute, sublicense, and/or sell
[11] Fix | Delete
# copies of the Software, and to permit persons to whom the
[12] Fix | Delete
# Software is furnished to do so, subject to the following
[13] Fix | Delete
# conditions:
[14] Fix | Delete
#
[15] Fix | Delete
# The above copyright notice and this permission notice shall be
[16] Fix | Delete
# included in all copies or substantial portions of the Software.
[17] Fix | Delete
#
[18] Fix | Delete
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
[19] Fix | Delete
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
[20] Fix | Delete
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
[21] Fix | Delete
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
[22] Fix | Delete
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
[23] Fix | Delete
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
[24] Fix | Delete
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
[25] Fix | Delete
# OTHER DEALINGS IN THE SOFTWARE.
[26] Fix | Delete
#
[27] Fix | Delete
"""Read from and write to tar format archives.
[28] Fix | Delete
"""
[29] Fix | Delete
[30] Fix | Delete
__version__ = "$Revision: 85213 $"
[31] Fix | Delete
# $Source$
[32] Fix | Delete
[33] Fix | Delete
version = "0.9.0"
[34] Fix | Delete
[35] Fix | Delete
__date__ = "$Date$"
[36] Fix | Delete
__cvsid__ = "$Id$"
[37] Fix | Delete
[38] Fix | Delete
[39] Fix | Delete
#---------
[40] Fix | Delete
# Imports
[41] Fix | Delete
#---------
[42] Fix | Delete
from __builtin__ import open as bltn_open
[43] Fix | Delete
import sys
[44] Fix | Delete
import os
[45] Fix | Delete
import shutil
[46] Fix | Delete
import stat
[47] Fix | Delete
import errno
[48] Fix | Delete
import time
[49] Fix | Delete
import struct
[50] Fix | Delete
import copy
[51] Fix | Delete
import re
[52] Fix | Delete
import operator
[53] Fix | Delete
[54] Fix | Delete
try:
[55] Fix | Delete
import grp, pwd
[56] Fix | Delete
except ImportError:
[57] Fix | Delete
grp = pwd = None
[58] Fix | Delete
[59] Fix | Delete
# from tarfile import *
[60] Fix | Delete
__all__ = ["TarFile", "TarInfo", "is_tarfile", "TarError"]
[61] Fix | Delete
[62] Fix | Delete
#---------------------------------------------------------
[63] Fix | Delete
# tar constants
[64] Fix | Delete
#---------------------------------------------------------
[65] Fix | Delete
NUL = "\0" # the null character
[66] Fix | Delete
BLOCKSIZE = 512 # length of processing blocks
[67] Fix | Delete
RECORDSIZE = BLOCKSIZE * 20 # length of records
[68] Fix | Delete
GNU_MAGIC = "ustar \0" # magic gnu tar string
[69] Fix | Delete
POSIX_MAGIC = "ustar\x0000" # magic posix tar string
[70] Fix | Delete
[71] Fix | Delete
LENGTH_NAME = 100 # maximum length of a filename
[72] Fix | Delete
LENGTH_LINK = 100 # maximum length of a linkname
[73] Fix | Delete
LENGTH_PREFIX = 155 # maximum length of the prefix field
[74] Fix | Delete
[75] Fix | Delete
REGTYPE = "0" # regular file
[76] Fix | Delete
AREGTYPE = "\0" # regular file
[77] Fix | Delete
LNKTYPE = "1" # link (inside tarfile)
[78] Fix | Delete
SYMTYPE = "2" # symbolic link
[79] Fix | Delete
CHRTYPE = "3" # character special device
[80] Fix | Delete
BLKTYPE = "4" # block special device
[81] Fix | Delete
DIRTYPE = "5" # directory
[82] Fix | Delete
FIFOTYPE = "6" # fifo special device
[83] Fix | Delete
CONTTYPE = "7" # contiguous file
[84] Fix | Delete
[85] Fix | Delete
GNUTYPE_LONGNAME = "L" # GNU tar longname
[86] Fix | Delete
GNUTYPE_LONGLINK = "K" # GNU tar longlink
[87] Fix | Delete
GNUTYPE_SPARSE = "S" # GNU tar sparse file
[88] Fix | Delete
[89] Fix | Delete
XHDTYPE = "x" # POSIX.1-2001 extended header
[90] Fix | Delete
XGLTYPE = "g" # POSIX.1-2001 global header
[91] Fix | Delete
SOLARIS_XHDTYPE = "X" # Solaris extended header
[92] Fix | Delete
[93] Fix | Delete
USTAR_FORMAT = 0 # POSIX.1-1988 (ustar) format
[94] Fix | Delete
GNU_FORMAT = 1 # GNU tar format
[95] Fix | Delete
PAX_FORMAT = 2 # POSIX.1-2001 (pax) format
[96] Fix | Delete
DEFAULT_FORMAT = GNU_FORMAT
[97] Fix | Delete
[98] Fix | Delete
#---------------------------------------------------------
[99] Fix | Delete
# tarfile constants
[100] Fix | Delete
#---------------------------------------------------------
[101] Fix | Delete
# File types that tarfile supports:
[102] Fix | Delete
SUPPORTED_TYPES = (REGTYPE, AREGTYPE, LNKTYPE,
[103] Fix | Delete
SYMTYPE, DIRTYPE, FIFOTYPE,
[104] Fix | Delete
CONTTYPE, CHRTYPE, BLKTYPE,
[105] Fix | Delete
GNUTYPE_LONGNAME, GNUTYPE_LONGLINK,
[106] Fix | Delete
GNUTYPE_SPARSE)
[107] Fix | Delete
[108] Fix | Delete
# File types that will be treated as a regular file.
[109] Fix | Delete
REGULAR_TYPES = (REGTYPE, AREGTYPE,
[110] Fix | Delete
CONTTYPE, GNUTYPE_SPARSE)
[111] Fix | Delete
[112] Fix | Delete
# File types that are part of the GNU tar format.
[113] Fix | Delete
GNU_TYPES = (GNUTYPE_LONGNAME, GNUTYPE_LONGLINK,
[114] Fix | Delete
GNUTYPE_SPARSE)
[115] Fix | Delete
[116] Fix | Delete
# Fields from a pax header that override a TarInfo attribute.
[117] Fix | Delete
PAX_FIELDS = ("path", "linkpath", "size", "mtime",
[118] Fix | Delete
"uid", "gid", "uname", "gname")
[119] Fix | Delete
[120] Fix | Delete
# Fields in a pax header that are numbers, all other fields
[121] Fix | Delete
# are treated as strings.
[122] Fix | Delete
PAX_NUMBER_FIELDS = {
[123] Fix | Delete
"atime": float,
[124] Fix | Delete
"ctime": float,
[125] Fix | Delete
"mtime": float,
[126] Fix | Delete
"uid": int,
[127] Fix | Delete
"gid": int,
[128] Fix | Delete
"size": int
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
#---------------------------------------------------------
[132] Fix | Delete
# Bits used in the mode field, values in octal.
[133] Fix | Delete
#---------------------------------------------------------
[134] Fix | Delete
S_IFLNK = 0120000 # symbolic link
[135] Fix | Delete
S_IFREG = 0100000 # regular file
[136] Fix | Delete
S_IFBLK = 0060000 # block device
[137] Fix | Delete
S_IFDIR = 0040000 # directory
[138] Fix | Delete
S_IFCHR = 0020000 # character device
[139] Fix | Delete
S_IFIFO = 0010000 # fifo
[140] Fix | Delete
[141] Fix | Delete
TSUID = 04000 # set UID on execution
[142] Fix | Delete
TSGID = 02000 # set GID on execution
[143] Fix | Delete
TSVTX = 01000 # reserved
[144] Fix | Delete
[145] Fix | Delete
TUREAD = 0400 # read by owner
[146] Fix | Delete
TUWRITE = 0200 # write by owner
[147] Fix | Delete
TUEXEC = 0100 # execute/search by owner
[148] Fix | Delete
TGREAD = 0040 # read by group
[149] Fix | Delete
TGWRITE = 0020 # write by group
[150] Fix | Delete
TGEXEC = 0010 # execute/search by group
[151] Fix | Delete
TOREAD = 0004 # read by other
[152] Fix | Delete
TOWRITE = 0002 # write by other
[153] Fix | Delete
TOEXEC = 0001 # execute/search by other
[154] Fix | Delete
[155] Fix | Delete
#---------------------------------------------------------
[156] Fix | Delete
# initialization
[157] Fix | Delete
#---------------------------------------------------------
[158] Fix | Delete
ENCODING = sys.getfilesystemencoding()
[159] Fix | Delete
if ENCODING is None:
[160] Fix | Delete
ENCODING = sys.getdefaultencoding()
[161] Fix | Delete
[162] Fix | Delete
#---------------------------------------------------------
[163] Fix | Delete
# Some useful functions
[164] Fix | Delete
#---------------------------------------------------------
[165] Fix | Delete
[166] Fix | Delete
def stn(s, length):
[167] Fix | Delete
"""Convert a python string to a null-terminated string buffer.
[168] Fix | Delete
"""
[169] Fix | Delete
return s[:length] + (length - len(s)) * NUL
[170] Fix | Delete
[171] Fix | Delete
def nts(s):
[172] Fix | Delete
"""Convert a null-terminated string field to a python string.
[173] Fix | Delete
"""
[174] Fix | Delete
# Use the string up to the first null char.
[175] Fix | Delete
p = s.find("\0")
[176] Fix | Delete
if p == -1:
[177] Fix | Delete
return s
[178] Fix | Delete
return s[:p]
[179] Fix | Delete
[180] Fix | Delete
def nti(s):
[181] Fix | Delete
"""Convert a number field to a python number.
[182] Fix | Delete
"""
[183] Fix | Delete
# There are two possible encodings for a number field, see
[184] Fix | Delete
# itn() below.
[185] Fix | Delete
if s[0] != chr(0200):
[186] Fix | Delete
try:
[187] Fix | Delete
n = int(nts(s).strip() or "0", 8)
[188] Fix | Delete
except ValueError:
[189] Fix | Delete
raise InvalidHeaderError("invalid header")
[190] Fix | Delete
else:
[191] Fix | Delete
n = 0L
[192] Fix | Delete
for i in xrange(len(s) - 1):
[193] Fix | Delete
n <<= 8
[194] Fix | Delete
n += ord(s[i + 1])
[195] Fix | Delete
return n
[196] Fix | Delete
[197] Fix | Delete
def itn(n, digits=8, format=DEFAULT_FORMAT):
[198] Fix | Delete
"""Convert a python number to a number field.
[199] Fix | Delete
"""
[200] Fix | Delete
# POSIX 1003.1-1988 requires numbers to be encoded as a string of
[201] Fix | Delete
# octal digits followed by a null-byte, this allows values up to
[202] Fix | Delete
# (8**(digits-1))-1. GNU tar allows storing numbers greater than
[203] Fix | Delete
# that if necessary. A leading 0200 byte indicates this particular
[204] Fix | Delete
# encoding, the following digits-1 bytes are a big-endian
[205] Fix | Delete
# representation. This allows values up to (256**(digits-1))-1.
[206] Fix | Delete
if 0 <= n < 8 ** (digits - 1):
[207] Fix | Delete
s = "%0*o" % (digits - 1, n) + NUL
[208] Fix | Delete
else:
[209] Fix | Delete
if format != GNU_FORMAT or n >= 256 ** (digits - 1):
[210] Fix | Delete
raise ValueError("overflow in number field")
[211] Fix | Delete
[212] Fix | Delete
if n < 0:
[213] Fix | Delete
# XXX We mimic GNU tar's behaviour with negative numbers,
[214] Fix | Delete
# this could raise OverflowError.
[215] Fix | Delete
n = struct.unpack("L", struct.pack("l", n))[0]
[216] Fix | Delete
[217] Fix | Delete
s = ""
[218] Fix | Delete
for i in xrange(digits - 1):
[219] Fix | Delete
s = chr(n & 0377) + s
[220] Fix | Delete
n >>= 8
[221] Fix | Delete
s = chr(0200) + s
[222] Fix | Delete
return s
[223] Fix | Delete
[224] Fix | Delete
def uts(s, encoding, errors):
[225] Fix | Delete
"""Convert a unicode object to a string.
[226] Fix | Delete
"""
[227] Fix | Delete
if errors == "utf-8":
[228] Fix | Delete
# An extra error handler similar to the -o invalid=UTF-8 option
[229] Fix | Delete
# in POSIX.1-2001. Replace untranslatable characters with their
[230] Fix | Delete
# UTF-8 representation.
[231] Fix | Delete
try:
[232] Fix | Delete
return s.encode(encoding, "strict")
[233] Fix | Delete
except UnicodeEncodeError:
[234] Fix | Delete
x = []
[235] Fix | Delete
for c in s:
[236] Fix | Delete
try:
[237] Fix | Delete
x.append(c.encode(encoding, "strict"))
[238] Fix | Delete
except UnicodeEncodeError:
[239] Fix | Delete
x.append(c.encode("utf8"))
[240] Fix | Delete
return "".join(x)
[241] Fix | Delete
else:
[242] Fix | Delete
return s.encode(encoding, errors)
[243] Fix | Delete
[244] Fix | Delete
def calc_chksums(buf):
[245] Fix | Delete
"""Calculate the checksum for a member's header by summing up all
[246] Fix | Delete
characters except for the chksum field which is treated as if
[247] Fix | Delete
it was filled with spaces. According to the GNU tar sources,
[248] Fix | Delete
some tars (Sun and NeXT) calculate chksum with signed char,
[249] Fix | Delete
which will be different if there are chars in the buffer with
[250] Fix | Delete
the high bit set. So we calculate two checksums, unsigned and
[251] Fix | Delete
signed.
[252] Fix | Delete
"""
[253] Fix | Delete
unsigned_chksum = 256 + sum(struct.unpack("148B", buf[:148]) + struct.unpack("356B", buf[156:512]))
[254] Fix | Delete
signed_chksum = 256 + sum(struct.unpack("148b", buf[:148]) + struct.unpack("356b", buf[156:512]))
[255] Fix | Delete
return unsigned_chksum, signed_chksum
[256] Fix | Delete
[257] Fix | Delete
def copyfileobj(src, dst, length=None):
[258] Fix | Delete
"""Copy length bytes from fileobj src to fileobj dst.
[259] Fix | Delete
If length is None, copy the entire content.
[260] Fix | Delete
"""
[261] Fix | Delete
if length == 0:
[262] Fix | Delete
return
[263] Fix | Delete
if length is None:
[264] Fix | Delete
shutil.copyfileobj(src, dst)
[265] Fix | Delete
return
[266] Fix | Delete
[267] Fix | Delete
BUFSIZE = 16 * 1024
[268] Fix | Delete
blocks, remainder = divmod(length, BUFSIZE)
[269] Fix | Delete
for b in xrange(blocks):
[270] Fix | Delete
buf = src.read(BUFSIZE)
[271] Fix | Delete
if len(buf) < BUFSIZE:
[272] Fix | Delete
raise IOError("end of file reached")
[273] Fix | Delete
dst.write(buf)
[274] Fix | Delete
[275] Fix | Delete
if remainder != 0:
[276] Fix | Delete
buf = src.read(remainder)
[277] Fix | Delete
if len(buf) < remainder:
[278] Fix | Delete
raise IOError("end of file reached")
[279] Fix | Delete
dst.write(buf)
[280] Fix | Delete
return
[281] Fix | Delete
[282] Fix | Delete
filemode_table = (
[283] Fix | Delete
((S_IFLNK, "l"),
[284] Fix | Delete
(S_IFREG, "-"),
[285] Fix | Delete
(S_IFBLK, "b"),
[286] Fix | Delete
(S_IFDIR, "d"),
[287] Fix | Delete
(S_IFCHR, "c"),
[288] Fix | Delete
(S_IFIFO, "p")),
[289] Fix | Delete
[290] Fix | Delete
((TUREAD, "r"),),
[291] Fix | Delete
((TUWRITE, "w"),),
[292] Fix | Delete
((TUEXEC|TSUID, "s"),
[293] Fix | Delete
(TSUID, "S"),
[294] Fix | Delete
(TUEXEC, "x")),
[295] Fix | Delete
[296] Fix | Delete
((TGREAD, "r"),),
[297] Fix | Delete
((TGWRITE, "w"),),
[298] Fix | Delete
((TGEXEC|TSGID, "s"),
[299] Fix | Delete
(TSGID, "S"),
[300] Fix | Delete
(TGEXEC, "x")),
[301] Fix | Delete
[302] Fix | Delete
((TOREAD, "r"),),
[303] Fix | Delete
((TOWRITE, "w"),),
[304] Fix | Delete
((TOEXEC|TSVTX, "t"),
[305] Fix | Delete
(TSVTX, "T"),
[306] Fix | Delete
(TOEXEC, "x"))
[307] Fix | Delete
)
[308] Fix | Delete
[309] Fix | Delete
def filemode(mode):
[310] Fix | Delete
"""Convert a file's mode to a string of the form
[311] Fix | Delete
-rwxrwxrwx.
[312] Fix | Delete
Used by TarFile.list()
[313] Fix | Delete
"""
[314] Fix | Delete
perm = []
[315] Fix | Delete
for table in filemode_table:
[316] Fix | Delete
for bit, char in table:
[317] Fix | Delete
if mode & bit == bit:
[318] Fix | Delete
perm.append(char)
[319] Fix | Delete
break
[320] Fix | Delete
else:
[321] Fix | Delete
perm.append("-")
[322] Fix | Delete
return "".join(perm)
[323] Fix | Delete
[324] Fix | Delete
class TarError(Exception):
[325] Fix | Delete
"""Base exception."""
[326] Fix | Delete
pass
[327] Fix | Delete
class ExtractError(TarError):
[328] Fix | Delete
"""General exception for extract errors."""
[329] Fix | Delete
pass
[330] Fix | Delete
class ReadError(TarError):
[331] Fix | Delete
"""Exception for unreadable tar archives."""
[332] Fix | Delete
pass
[333] Fix | Delete
class CompressionError(TarError):
[334] Fix | Delete
"""Exception for unavailable compression methods."""
[335] Fix | Delete
pass
[336] Fix | Delete
class StreamError(TarError):
[337] Fix | Delete
"""Exception for unsupported operations on stream-like TarFiles."""
[338] Fix | Delete
pass
[339] Fix | Delete
class HeaderError(TarError):
[340] Fix | Delete
"""Base exception for header errors."""
[341] Fix | Delete
pass
[342] Fix | Delete
class EmptyHeaderError(HeaderError):
[343] Fix | Delete
"""Exception for empty headers."""
[344] Fix | Delete
pass
[345] Fix | Delete
class TruncatedHeaderError(HeaderError):
[346] Fix | Delete
"""Exception for truncated headers."""
[347] Fix | Delete
pass
[348] Fix | Delete
class EOFHeaderError(HeaderError):
[349] Fix | Delete
"""Exception for end of file headers."""
[350] Fix | Delete
pass
[351] Fix | Delete
class InvalidHeaderError(HeaderError):
[352] Fix | Delete
"""Exception for invalid headers."""
[353] Fix | Delete
pass
[354] Fix | Delete
class SubsequentHeaderError(HeaderError):
[355] Fix | Delete
"""Exception for missing and invalid extended headers."""
[356] Fix | Delete
pass
[357] Fix | Delete
[358] Fix | Delete
#---------------------------
[359] Fix | Delete
# internal stream interface
[360] Fix | Delete
#---------------------------
[361] Fix | Delete
class _LowLevelFile:
[362] Fix | Delete
"""Low-level file object. Supports reading and writing.
[363] Fix | Delete
It is used instead of a regular file object for streaming
[364] Fix | Delete
access.
[365] Fix | Delete
"""
[366] Fix | Delete
[367] Fix | Delete
def __init__(self, name, mode):
[368] Fix | Delete
mode = {
[369] Fix | Delete
"r": os.O_RDONLY,
[370] Fix | Delete
"w": os.O_WRONLY | os.O_CREAT | os.O_TRUNC,
[371] Fix | Delete
}[mode]
[372] Fix | Delete
if hasattr(os, "O_BINARY"):
[373] Fix | Delete
mode |= os.O_BINARY
[374] Fix | Delete
self.fd = os.open(name, mode, 0666)
[375] Fix | Delete
[376] Fix | Delete
def close(self):
[377] Fix | Delete
os.close(self.fd)
[378] Fix | Delete
[379] Fix | Delete
def read(self, size):
[380] Fix | Delete
return os.read(self.fd, size)
[381] Fix | Delete
[382] Fix | Delete
def write(self, s):
[383] Fix | Delete
os.write(self.fd, s)
[384] Fix | Delete
[385] Fix | Delete
class _Stream:
[386] Fix | Delete
"""Class that serves as an adapter between TarFile and
[387] Fix | Delete
a stream-like object. The stream-like object only
[388] Fix | Delete
needs to have a read() or write() method and is accessed
[389] Fix | Delete
blockwise. Use of gzip or bzip2 compression is possible.
[390] Fix | Delete
A stream-like object could be for example: sys.stdin,
[391] Fix | Delete
sys.stdout, a socket, a tape device etc.
[392] Fix | Delete
[393] Fix | Delete
_Stream is intended to be used only internally.
[394] Fix | Delete
"""
[395] Fix | Delete
[396] Fix | Delete
def __init__(self, name, mode, comptype, fileobj, bufsize):
[397] Fix | Delete
"""Construct a _Stream object.
[398] Fix | Delete
"""
[399] Fix | Delete
self._extfileobj = True
[400] Fix | Delete
if fileobj is None:
[401] Fix | Delete
fileobj = _LowLevelFile(name, mode)
[402] Fix | Delete
self._extfileobj = False
[403] Fix | Delete
[404] Fix | Delete
if comptype == '*':
[405] Fix | Delete
# Enable transparent compression detection for the
[406] Fix | Delete
# stream interface
[407] Fix | Delete
fileobj = _StreamProxy(fileobj)
[408] Fix | Delete
comptype = fileobj.getcomptype()
[409] Fix | Delete
[410] Fix | Delete
self.name = name or ""
[411] Fix | Delete
self.mode = mode
[412] Fix | Delete
self.comptype = comptype
[413] Fix | Delete
self.fileobj = fileobj
[414] Fix | Delete
self.bufsize = bufsize
[415] Fix | Delete
self.buf = ""
[416] Fix | Delete
self.pos = 0L
[417] Fix | Delete
self.closed = False
[418] Fix | Delete
[419] Fix | Delete
try:
[420] Fix | Delete
if comptype == "gz":
[421] Fix | Delete
try:
[422] Fix | Delete
import zlib
[423] Fix | Delete
except ImportError:
[424] Fix | Delete
raise CompressionError("zlib module is not available")
[425] Fix | Delete
self.zlib = zlib
[426] Fix | Delete
self.crc = zlib.crc32("") & 0xffffffffL
[427] Fix | Delete
if mode == "r":
[428] Fix | Delete
self._init_read_gz()
[429] Fix | Delete
else:
[430] Fix | Delete
self._init_write_gz()
[431] Fix | Delete
[432] Fix | Delete
elif comptype == "bz2":
[433] Fix | Delete
try:
[434] Fix | Delete
import bz2
[435] Fix | Delete
except ImportError:
[436] Fix | Delete
raise CompressionError("bz2 module is not available")
[437] Fix | Delete
if mode == "r":
[438] Fix | Delete
self.dbuf = ""
[439] Fix | Delete
self.cmp = bz2.BZ2Decompressor()
[440] Fix | Delete
else:
[441] Fix | Delete
self.cmp = bz2.BZ2Compressor()
[442] Fix | Delete
except:
[443] Fix | Delete
if not self._extfileobj:
[444] Fix | Delete
self.fileobj.close()
[445] Fix | Delete
self.closed = True
[446] Fix | Delete
raise
[447] Fix | Delete
[448] Fix | Delete
def __del__(self):
[449] Fix | Delete
if hasattr(self, "closed") and not self.closed:
[450] Fix | Delete
self.close()
[451] Fix | Delete
[452] Fix | Delete
def _init_write_gz(self):
[453] Fix | Delete
"""Initialize for writing with gzip compression.
[454] Fix | Delete
"""
[455] Fix | Delete
self.cmp = self.zlib.compressobj(9, self.zlib.DEFLATED,
[456] Fix | Delete
-self.zlib.MAX_WBITS,
[457] Fix | Delete
self.zlib.DEF_MEM_LEVEL,
[458] Fix | Delete
0)
[459] Fix | Delete
timestamp = struct.pack("<L", long(time.time()))
[460] Fix | Delete
self.__write("\037\213\010\010%s\002\377" % timestamp)
[461] Fix | Delete
if type(self.name) is unicode:
[462] Fix | Delete
self.name = self.name.encode("iso-8859-1", "replace")
[463] Fix | Delete
if self.name.endswith(".gz"):
[464] Fix | Delete
self.name = self.name[:-3]
[465] Fix | Delete
self.__write(self.name + NUL)
[466] Fix | Delete
[467] Fix | Delete
def write(self, s):
[468] Fix | Delete
"""Write string s to the stream.
[469] Fix | Delete
"""
[470] Fix | Delete
if self.comptype == "gz":
[471] Fix | Delete
self.crc = self.zlib.crc32(s, self.crc) & 0xffffffffL
[472] Fix | Delete
self.pos += len(s)
[473] Fix | Delete
if self.comptype != "tar":
[474] Fix | Delete
s = self.cmp.compress(s)
[475] Fix | Delete
self.__write(s)
[476] Fix | Delete
[477] Fix | Delete
def __write(self, s):
[478] Fix | Delete
"""Write string s to the stream if a whole new block
[479] Fix | Delete
is ready to be written.
[480] Fix | Delete
"""
[481] Fix | Delete
self.buf += s
[482] Fix | Delete
while len(self.buf) > self.bufsize:
[483] Fix | Delete
self.fileobj.write(self.buf[:self.bufsize])
[484] Fix | Delete
self.buf = self.buf[self.bufsize:]
[485] Fix | Delete
[486] Fix | Delete
def close(self):
[487] Fix | Delete
"""Close the _Stream object. No operation should be
[488] Fix | Delete
done on it afterwards.
[489] Fix | Delete
"""
[490] Fix | Delete
if self.closed:
[491] Fix | Delete
return
[492] Fix | Delete
[493] Fix | Delete
self.closed = True
[494] Fix | Delete
try:
[495] Fix | Delete
if self.mode == "w" and self.comptype != "tar":
[496] Fix | Delete
self.buf += self.cmp.flush()
[497] Fix | Delete
[498] Fix | Delete
if self.mode == "w" and self.buf:
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function