Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python3....
File: tarfile.py
#! /usr/libexec/platform-python -s
[0] Fix | Delete
#-------------------------------------------------------------------
[1] Fix | Delete
# tarfile.py
[2] Fix | Delete
#-------------------------------------------------------------------
[3] Fix | Delete
# Copyright (C) 2002 Lars Gustaebel <lars@gustaebel.de>
[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 = "0.9.0"
[31] Fix | Delete
__author__ = "Lars Gust\u00e4bel (lars@gustaebel.de)"
[32] Fix | Delete
__credits__ = "Gustavo Niemeyer, Niels Gust\u00e4bel, Richard Townsend."
[33] Fix | Delete
[34] Fix | Delete
#---------
[35] Fix | Delete
# Imports
[36] Fix | Delete
#---------
[37] Fix | Delete
from builtins import open as bltn_open
[38] Fix | Delete
import sys
[39] Fix | Delete
import os
[40] Fix | Delete
import io
[41] Fix | Delete
import shutil
[42] Fix | Delete
import stat
[43] Fix | Delete
import time
[44] Fix | Delete
import struct
[45] Fix | Delete
import copy
[46] Fix | Delete
import re
[47] Fix | Delete
[48] Fix | Delete
try:
[49] Fix | Delete
import pwd
[50] Fix | Delete
except ImportError:
[51] Fix | Delete
pwd = None
[52] Fix | Delete
try:
[53] Fix | Delete
import grp
[54] Fix | Delete
except ImportError:
[55] Fix | Delete
grp = None
[56] Fix | Delete
[57] Fix | Delete
# os.symlink on Windows prior to 6.0 raises NotImplementedError
[58] Fix | Delete
symlink_exception = (AttributeError, NotImplementedError)
[59] Fix | Delete
try:
[60] Fix | Delete
# OSError (winerror=1314) will be raised if the caller does not hold the
[61] Fix | Delete
# SeCreateSymbolicLinkPrivilege privilege
[62] Fix | Delete
symlink_exception += (OSError,)
[63] Fix | Delete
except NameError:
[64] Fix | Delete
pass
[65] Fix | Delete
[66] Fix | Delete
# from tarfile import *
[67] Fix | Delete
__all__ = ["TarFile", "TarInfo", "is_tarfile", "TarError", "ReadError",
[68] Fix | Delete
"CompressionError", "StreamError", "ExtractError", "HeaderError",
[69] Fix | Delete
"ENCODING", "USTAR_FORMAT", "GNU_FORMAT", "PAX_FORMAT",
[70] Fix | Delete
"DEFAULT_FORMAT", "open"]
[71] Fix | Delete
[72] Fix | Delete
#---------------------------------------------------------
[73] Fix | Delete
# tar constants
[74] Fix | Delete
#---------------------------------------------------------
[75] Fix | Delete
NUL = b"\0" # the null character
[76] Fix | Delete
BLOCKSIZE = 512 # length of processing blocks
[77] Fix | Delete
RECORDSIZE = BLOCKSIZE * 20 # length of records
[78] Fix | Delete
GNU_MAGIC = b"ustar \0" # magic gnu tar string
[79] Fix | Delete
POSIX_MAGIC = b"ustar\x0000" # magic posix tar string
[80] Fix | Delete
[81] Fix | Delete
LENGTH_NAME = 100 # maximum length of a filename
[82] Fix | Delete
LENGTH_LINK = 100 # maximum length of a linkname
[83] Fix | Delete
LENGTH_PREFIX = 155 # maximum length of the prefix field
[84] Fix | Delete
[85] Fix | Delete
REGTYPE = b"0" # regular file
[86] Fix | Delete
AREGTYPE = b"\0" # regular file
[87] Fix | Delete
LNKTYPE = b"1" # link (inside tarfile)
[88] Fix | Delete
SYMTYPE = b"2" # symbolic link
[89] Fix | Delete
CHRTYPE = b"3" # character special device
[90] Fix | Delete
BLKTYPE = b"4" # block special device
[91] Fix | Delete
DIRTYPE = b"5" # directory
[92] Fix | Delete
FIFOTYPE = b"6" # fifo special device
[93] Fix | Delete
CONTTYPE = b"7" # contiguous file
[94] Fix | Delete
[95] Fix | Delete
GNUTYPE_LONGNAME = b"L" # GNU tar longname
[96] Fix | Delete
GNUTYPE_LONGLINK = b"K" # GNU tar longlink
[97] Fix | Delete
GNUTYPE_SPARSE = b"S" # GNU tar sparse file
[98] Fix | Delete
[99] Fix | Delete
XHDTYPE = b"x" # POSIX.1-2001 extended header
[100] Fix | Delete
XGLTYPE = b"g" # POSIX.1-2001 global header
[101] Fix | Delete
SOLARIS_XHDTYPE = b"X" # Solaris extended header
[102] Fix | Delete
[103] Fix | Delete
USTAR_FORMAT = 0 # POSIX.1-1988 (ustar) format
[104] Fix | Delete
GNU_FORMAT = 1 # GNU tar format
[105] Fix | Delete
PAX_FORMAT = 2 # POSIX.1-2001 (pax) format
[106] Fix | Delete
DEFAULT_FORMAT = PAX_FORMAT
[107] Fix | Delete
[108] Fix | Delete
#---------------------------------------------------------
[109] Fix | Delete
# tarfile constants
[110] Fix | Delete
#---------------------------------------------------------
[111] Fix | Delete
# File types that tarfile supports:
[112] Fix | Delete
SUPPORTED_TYPES = (REGTYPE, AREGTYPE, LNKTYPE,
[113] Fix | Delete
SYMTYPE, DIRTYPE, FIFOTYPE,
[114] Fix | Delete
CONTTYPE, CHRTYPE, BLKTYPE,
[115] Fix | Delete
GNUTYPE_LONGNAME, GNUTYPE_LONGLINK,
[116] Fix | Delete
GNUTYPE_SPARSE)
[117] Fix | Delete
[118] Fix | Delete
# File types that will be treated as a regular file.
[119] Fix | Delete
REGULAR_TYPES = (REGTYPE, AREGTYPE,
[120] Fix | Delete
CONTTYPE, GNUTYPE_SPARSE)
[121] Fix | Delete
[122] Fix | Delete
# File types that are part of the GNU tar format.
[123] Fix | Delete
GNU_TYPES = (GNUTYPE_LONGNAME, GNUTYPE_LONGLINK,
[124] Fix | Delete
GNUTYPE_SPARSE)
[125] Fix | Delete
[126] Fix | Delete
# Fields from a pax header that override a TarInfo attribute.
[127] Fix | Delete
PAX_FIELDS = ("path", "linkpath", "size", "mtime",
[128] Fix | Delete
"uid", "gid", "uname", "gname")
[129] Fix | Delete
[130] Fix | Delete
# Fields from a pax header that are affected by hdrcharset.
[131] Fix | Delete
PAX_NAME_FIELDS = {"path", "linkpath", "uname", "gname"}
[132] Fix | Delete
[133] Fix | Delete
# Fields in a pax header that are numbers, all other fields
[134] Fix | Delete
# are treated as strings.
[135] Fix | Delete
PAX_NUMBER_FIELDS = {
[136] Fix | Delete
"atime": float,
[137] Fix | Delete
"ctime": float,
[138] Fix | Delete
"mtime": float,
[139] Fix | Delete
"uid": int,
[140] Fix | Delete
"gid": int,
[141] Fix | Delete
"size": int
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
#---------------------------------------------------------
[145] Fix | Delete
# initialization
[146] Fix | Delete
#---------------------------------------------------------
[147] Fix | Delete
if os.name == "nt":
[148] Fix | Delete
ENCODING = "utf-8"
[149] Fix | Delete
else:
[150] Fix | Delete
ENCODING = sys.getfilesystemencoding()
[151] Fix | Delete
[152] Fix | Delete
#---------------------------------------------------------
[153] Fix | Delete
# Some useful functions
[154] Fix | Delete
#---------------------------------------------------------
[155] Fix | Delete
[156] Fix | Delete
def stn(s, length, encoding, errors):
[157] Fix | Delete
"""Convert a string to a null-terminated bytes object.
[158] Fix | Delete
"""
[159] Fix | Delete
s = s.encode(encoding, errors)
[160] Fix | Delete
return s[:length] + (length - len(s)) * NUL
[161] Fix | Delete
[162] Fix | Delete
def nts(s, encoding, errors):
[163] Fix | Delete
"""Convert a null-terminated bytes object to a string.
[164] Fix | Delete
"""
[165] Fix | Delete
p = s.find(b"\0")
[166] Fix | Delete
if p != -1:
[167] Fix | Delete
s = s[:p]
[168] Fix | Delete
return s.decode(encoding, errors)
[169] Fix | Delete
[170] Fix | Delete
def nti(s):
[171] Fix | Delete
"""Convert a number field to a python number.
[172] Fix | Delete
"""
[173] Fix | Delete
# There are two possible encodings for a number field, see
[174] Fix | Delete
# itn() below.
[175] Fix | Delete
if s[0] in (0o200, 0o377):
[176] Fix | Delete
n = 0
[177] Fix | Delete
for i in range(len(s) - 1):
[178] Fix | Delete
n <<= 8
[179] Fix | Delete
n += s[i + 1]
[180] Fix | Delete
if s[0] == 0o377:
[181] Fix | Delete
n = -(256 ** (len(s) - 1) - n)
[182] Fix | Delete
else:
[183] Fix | Delete
try:
[184] Fix | Delete
s = nts(s, "ascii", "strict")
[185] Fix | Delete
n = int(s.strip() or "0", 8)
[186] Fix | Delete
except ValueError:
[187] Fix | Delete
raise InvalidHeaderError("invalid header")
[188] Fix | Delete
return n
[189] Fix | Delete
[190] Fix | Delete
def itn(n, digits=8, format=DEFAULT_FORMAT):
[191] Fix | Delete
"""Convert a python number to a number field.
[192] Fix | Delete
"""
[193] Fix | Delete
# POSIX 1003.1-1988 requires numbers to be encoded as a string of
[194] Fix | Delete
# octal digits followed by a null-byte, this allows values up to
[195] Fix | Delete
# (8**(digits-1))-1. GNU tar allows storing numbers greater than
[196] Fix | Delete
# that if necessary. A leading 0o200 or 0o377 byte indicate this
[197] Fix | Delete
# particular encoding, the following digits-1 bytes are a big-endian
[198] Fix | Delete
# base-256 representation. This allows values up to (256**(digits-1))-1.
[199] Fix | Delete
# A 0o200 byte indicates a positive number, a 0o377 byte a negative
[200] Fix | Delete
# number.
[201] Fix | Delete
n = int(n)
[202] Fix | Delete
if 0 <= n < 8 ** (digits - 1):
[203] Fix | Delete
s = bytes("%0*o" % (digits - 1, n), "ascii") + NUL
[204] Fix | Delete
elif format == GNU_FORMAT and -256 ** (digits - 1) <= n < 256 ** (digits - 1):
[205] Fix | Delete
if n >= 0:
[206] Fix | Delete
s = bytearray([0o200])
[207] Fix | Delete
else:
[208] Fix | Delete
s = bytearray([0o377])
[209] Fix | Delete
n = 256 ** digits + n
[210] Fix | Delete
[211] Fix | Delete
for i in range(digits - 1):
[212] Fix | Delete
s.insert(1, n & 0o377)
[213] Fix | Delete
n >>= 8
[214] Fix | Delete
else:
[215] Fix | Delete
raise ValueError("overflow in number field")
[216] Fix | Delete
[217] Fix | Delete
return s
[218] Fix | Delete
[219] Fix | Delete
def calc_chksums(buf):
[220] Fix | Delete
"""Calculate the checksum for a member's header by summing up all
[221] Fix | Delete
characters except for the chksum field which is treated as if
[222] Fix | Delete
it was filled with spaces. According to the GNU tar sources,
[223] Fix | Delete
some tars (Sun and NeXT) calculate chksum with signed char,
[224] Fix | Delete
which will be different if there are chars in the buffer with
[225] Fix | Delete
the high bit set. So we calculate two checksums, unsigned and
[226] Fix | Delete
signed.
[227] Fix | Delete
"""
[228] Fix | Delete
unsigned_chksum = 256 + sum(struct.unpack_from("148B8x356B", buf))
[229] Fix | Delete
signed_chksum = 256 + sum(struct.unpack_from("148b8x356b", buf))
[230] Fix | Delete
return unsigned_chksum, signed_chksum
[231] Fix | Delete
[232] Fix | Delete
def copyfileobj(src, dst, length=None, exception=OSError, bufsize=None):
[233] Fix | Delete
"""Copy length bytes from fileobj src to fileobj dst.
[234] Fix | Delete
If length is None, copy the entire content.
[235] Fix | Delete
"""
[236] Fix | Delete
bufsize = bufsize or 16 * 1024
[237] Fix | Delete
if length == 0:
[238] Fix | Delete
return
[239] Fix | Delete
if length is None:
[240] Fix | Delete
shutil.copyfileobj(src, dst, bufsize)
[241] Fix | Delete
return
[242] Fix | Delete
[243] Fix | Delete
blocks, remainder = divmod(length, bufsize)
[244] Fix | Delete
for b in range(blocks):
[245] Fix | Delete
buf = src.read(bufsize)
[246] Fix | Delete
if len(buf) < bufsize:
[247] Fix | Delete
raise exception("unexpected end of data")
[248] Fix | Delete
dst.write(buf)
[249] Fix | Delete
[250] Fix | Delete
if remainder != 0:
[251] Fix | Delete
buf = src.read(remainder)
[252] Fix | Delete
if len(buf) < remainder:
[253] Fix | Delete
raise exception("unexpected end of data")
[254] Fix | Delete
dst.write(buf)
[255] Fix | Delete
return
[256] Fix | Delete
[257] Fix | Delete
def _safe_print(s):
[258] Fix | Delete
encoding = getattr(sys.stdout, 'encoding', None)
[259] Fix | Delete
if encoding is not None:
[260] Fix | Delete
s = s.encode(encoding, 'backslashreplace').decode(encoding)
[261] Fix | Delete
print(s, end=' ')
[262] Fix | Delete
[263] Fix | Delete
[264] Fix | Delete
class TarError(Exception):
[265] Fix | Delete
"""Base exception."""
[266] Fix | Delete
pass
[267] Fix | Delete
class ExtractError(TarError):
[268] Fix | Delete
"""General exception for extract errors."""
[269] Fix | Delete
pass
[270] Fix | Delete
class ReadError(TarError):
[271] Fix | Delete
"""Exception for unreadable tar archives."""
[272] Fix | Delete
pass
[273] Fix | Delete
class CompressionError(TarError):
[274] Fix | Delete
"""Exception for unavailable compression methods."""
[275] Fix | Delete
pass
[276] Fix | Delete
class StreamError(TarError):
[277] Fix | Delete
"""Exception for unsupported operations on stream-like TarFiles."""
[278] Fix | Delete
pass
[279] Fix | Delete
class HeaderError(TarError):
[280] Fix | Delete
"""Base exception for header errors."""
[281] Fix | Delete
pass
[282] Fix | Delete
class EmptyHeaderError(HeaderError):
[283] Fix | Delete
"""Exception for empty headers."""
[284] Fix | Delete
pass
[285] Fix | Delete
class TruncatedHeaderError(HeaderError):
[286] Fix | Delete
"""Exception for truncated headers."""
[287] Fix | Delete
pass
[288] Fix | Delete
class EOFHeaderError(HeaderError):
[289] Fix | Delete
"""Exception for end of file headers."""
[290] Fix | Delete
pass
[291] Fix | Delete
class InvalidHeaderError(HeaderError):
[292] Fix | Delete
"""Exception for invalid headers."""
[293] Fix | Delete
pass
[294] Fix | Delete
class SubsequentHeaderError(HeaderError):
[295] Fix | Delete
"""Exception for missing and invalid extended headers."""
[296] Fix | Delete
pass
[297] Fix | Delete
[298] Fix | Delete
#---------------------------
[299] Fix | Delete
# internal stream interface
[300] Fix | Delete
#---------------------------
[301] Fix | Delete
class _LowLevelFile:
[302] Fix | Delete
"""Low-level file object. Supports reading and writing.
[303] Fix | Delete
It is used instead of a regular file object for streaming
[304] Fix | Delete
access.
[305] Fix | Delete
"""
[306] Fix | Delete
[307] Fix | Delete
def __init__(self, name, mode):
[308] Fix | Delete
mode = {
[309] Fix | Delete
"r": os.O_RDONLY,
[310] Fix | Delete
"w": os.O_WRONLY | os.O_CREAT | os.O_TRUNC,
[311] Fix | Delete
}[mode]
[312] Fix | Delete
if hasattr(os, "O_BINARY"):
[313] Fix | Delete
mode |= os.O_BINARY
[314] Fix | Delete
self.fd = os.open(name, mode, 0o666)
[315] Fix | Delete
[316] Fix | Delete
def close(self):
[317] Fix | Delete
os.close(self.fd)
[318] Fix | Delete
[319] Fix | Delete
def read(self, size):
[320] Fix | Delete
return os.read(self.fd, size)
[321] Fix | Delete
[322] Fix | Delete
def write(self, s):
[323] Fix | Delete
os.write(self.fd, s)
[324] Fix | Delete
[325] Fix | Delete
class _Stream:
[326] Fix | Delete
"""Class that serves as an adapter between TarFile and
[327] Fix | Delete
a stream-like object. The stream-like object only
[328] Fix | Delete
needs to have a read() or write() method and is accessed
[329] Fix | Delete
blockwise. Use of gzip or bzip2 compression is possible.
[330] Fix | Delete
A stream-like object could be for example: sys.stdin,
[331] Fix | Delete
sys.stdout, a socket, a tape device etc.
[332] Fix | Delete
[333] Fix | Delete
_Stream is intended to be used only internally.
[334] Fix | Delete
"""
[335] Fix | Delete
[336] Fix | Delete
def __init__(self, name, mode, comptype, fileobj, bufsize):
[337] Fix | Delete
"""Construct a _Stream object.
[338] Fix | Delete
"""
[339] Fix | Delete
self._extfileobj = True
[340] Fix | Delete
if fileobj is None:
[341] Fix | Delete
fileobj = _LowLevelFile(name, mode)
[342] Fix | Delete
self._extfileobj = False
[343] Fix | Delete
[344] Fix | Delete
if comptype == '*':
[345] Fix | Delete
# Enable transparent compression detection for the
[346] Fix | Delete
# stream interface
[347] Fix | Delete
fileobj = _StreamProxy(fileobj)
[348] Fix | Delete
comptype = fileobj.getcomptype()
[349] Fix | Delete
[350] Fix | Delete
self.name = name or ""
[351] Fix | Delete
self.mode = mode
[352] Fix | Delete
self.comptype = comptype
[353] Fix | Delete
self.fileobj = fileobj
[354] Fix | Delete
self.bufsize = bufsize
[355] Fix | Delete
self.buf = b""
[356] Fix | Delete
self.pos = 0
[357] Fix | Delete
self.closed = False
[358] Fix | Delete
[359] Fix | Delete
try:
[360] Fix | Delete
if comptype == "gz":
[361] Fix | Delete
try:
[362] Fix | Delete
import zlib
[363] Fix | Delete
except ImportError:
[364] Fix | Delete
raise CompressionError("zlib module is not available")
[365] Fix | Delete
self.zlib = zlib
[366] Fix | Delete
self.crc = zlib.crc32(b"")
[367] Fix | Delete
if mode == "r":
[368] Fix | Delete
self._init_read_gz()
[369] Fix | Delete
self.exception = zlib.error
[370] Fix | Delete
else:
[371] Fix | Delete
self._init_write_gz()
[372] Fix | Delete
[373] Fix | Delete
elif comptype == "bz2":
[374] Fix | Delete
try:
[375] Fix | Delete
import bz2
[376] Fix | Delete
except ImportError:
[377] Fix | Delete
raise CompressionError("bz2 module is not available")
[378] Fix | Delete
if mode == "r":
[379] Fix | Delete
self.dbuf = b""
[380] Fix | Delete
self.cmp = bz2.BZ2Decompressor()
[381] Fix | Delete
self.exception = OSError
[382] Fix | Delete
else:
[383] Fix | Delete
self.cmp = bz2.BZ2Compressor()
[384] Fix | Delete
[385] Fix | Delete
elif comptype == "xz":
[386] Fix | Delete
try:
[387] Fix | Delete
import lzma
[388] Fix | Delete
except ImportError:
[389] Fix | Delete
raise CompressionError("lzma module is not available")
[390] Fix | Delete
if mode == "r":
[391] Fix | Delete
self.dbuf = b""
[392] Fix | Delete
self.cmp = lzma.LZMADecompressor()
[393] Fix | Delete
self.exception = lzma.LZMAError
[394] Fix | Delete
else:
[395] Fix | Delete
self.cmp = lzma.LZMACompressor()
[396] Fix | Delete
[397] Fix | Delete
elif comptype != "tar":
[398] Fix | Delete
raise CompressionError("unknown compression type %r" % comptype)
[399] Fix | Delete
[400] Fix | Delete
except:
[401] Fix | Delete
if not self._extfileobj:
[402] Fix | Delete
self.fileobj.close()
[403] Fix | Delete
self.closed = True
[404] Fix | Delete
raise
[405] Fix | Delete
[406] Fix | Delete
def __del__(self):
[407] Fix | Delete
if hasattr(self, "closed") and not self.closed:
[408] Fix | Delete
self.close()
[409] Fix | Delete
[410] Fix | Delete
def _init_write_gz(self):
[411] Fix | Delete
"""Initialize for writing with gzip compression.
[412] Fix | Delete
"""
[413] Fix | Delete
self.cmp = self.zlib.compressobj(9, self.zlib.DEFLATED,
[414] Fix | Delete
-self.zlib.MAX_WBITS,
[415] Fix | Delete
self.zlib.DEF_MEM_LEVEL,
[416] Fix | Delete
0)
[417] Fix | Delete
timestamp = struct.pack("<L", int(time.time()))
[418] Fix | Delete
self.__write(b"\037\213\010\010" + timestamp + b"\002\377")
[419] Fix | Delete
if self.name.endswith(".gz"):
[420] Fix | Delete
self.name = self.name[:-3]
[421] Fix | Delete
# Honor "directory components removed" from RFC1952
[422] Fix | Delete
self.name = os.path.basename(self.name)
[423] Fix | Delete
# RFC1952 says we must use ISO-8859-1 for the FNAME field.
[424] Fix | Delete
self.__write(self.name.encode("iso-8859-1", "replace") + NUL)
[425] Fix | Delete
[426] Fix | Delete
def write(self, s):
[427] Fix | Delete
"""Write string s to the stream.
[428] Fix | Delete
"""
[429] Fix | Delete
if self.comptype == "gz":
[430] Fix | Delete
self.crc = self.zlib.crc32(s, self.crc)
[431] Fix | Delete
self.pos += len(s)
[432] Fix | Delete
if self.comptype != "tar":
[433] Fix | Delete
s = self.cmp.compress(s)
[434] Fix | Delete
self.__write(s)
[435] Fix | Delete
[436] Fix | Delete
def __write(self, s):
[437] Fix | Delete
"""Write string s to the stream if a whole new block
[438] Fix | Delete
is ready to be written.
[439] Fix | Delete
"""
[440] Fix | Delete
self.buf += s
[441] Fix | Delete
while len(self.buf) > self.bufsize:
[442] Fix | Delete
self.fileobj.write(self.buf[:self.bufsize])
[443] Fix | Delete
self.buf = self.buf[self.bufsize:]
[444] Fix | Delete
[445] Fix | Delete
def close(self):
[446] Fix | Delete
"""Close the _Stream object. No operation should be
[447] Fix | Delete
done on it afterwards.
[448] Fix | Delete
"""
[449] Fix | Delete
if self.closed:
[450] Fix | Delete
return
[451] Fix | Delete
[452] Fix | Delete
self.closed = True
[453] Fix | Delete
try:
[454] Fix | Delete
if self.mode == "w" and self.comptype != "tar":
[455] Fix | Delete
self.buf += self.cmp.flush()
[456] Fix | Delete
[457] Fix | Delete
if self.mode == "w" and self.buf:
[458] Fix | Delete
self.fileobj.write(self.buf)
[459] Fix | Delete
self.buf = b""
[460] Fix | Delete
if self.comptype == "gz":
[461] Fix | Delete
self.fileobj.write(struct.pack("<L", self.crc))
[462] Fix | Delete
self.fileobj.write(struct.pack("<L", self.pos & 0xffffFFFF))
[463] Fix | Delete
finally:
[464] Fix | Delete
if not self._extfileobj:
[465] Fix | Delete
self.fileobj.close()
[466] Fix | Delete
[467] Fix | Delete
def _init_read_gz(self):
[468] Fix | Delete
"""Initialize for reading a gzip compressed fileobj.
[469] Fix | Delete
"""
[470] Fix | Delete
self.cmp = self.zlib.decompressobj(-self.zlib.MAX_WBITS)
[471] Fix | Delete
self.dbuf = b""
[472] Fix | Delete
[473] Fix | Delete
# taken from gzip.GzipFile with some alterations
[474] Fix | Delete
if self.__read(2) != b"\037\213":
[475] Fix | Delete
raise ReadError("not a gzip file")
[476] Fix | Delete
if self.__read(1) != b"\010":
[477] Fix | Delete
raise CompressionError("unsupported compression method")
[478] Fix | Delete
[479] Fix | Delete
flag = ord(self.__read(1))
[480] Fix | Delete
self.__read(6)
[481] Fix | Delete
[482] Fix | Delete
if flag & 4:
[483] Fix | Delete
xlen = ord(self.__read(1)) + 256 * ord(self.__read(1))
[484] Fix | Delete
self.read(xlen)
[485] Fix | Delete
if flag & 8:
[486] Fix | Delete
while True:
[487] Fix | Delete
s = self.__read(1)
[488] Fix | Delete
if not s or s == NUL:
[489] Fix | Delete
break
[490] Fix | Delete
if flag & 16:
[491] Fix | Delete
while True:
[492] Fix | Delete
s = self.__read(1)
[493] Fix | Delete
if not s or s == NUL:
[494] Fix | Delete
break
[495] Fix | Delete
if flag & 2:
[496] Fix | Delete
self.__read(2)
[497] Fix | Delete
[498] Fix | Delete
def tell(self):
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function