Bug 479178. Remove some unnecessary refcounting in XUL trees. r+sr=roc
[mozilla-central.git] / config / MozZipFile.py
bloba4b0becbb9ad97e501e2b36f477123479f8a00f8
1 # ***** BEGIN LICENSE BLOCK *****
2 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 # The contents of this file are subject to the Mozilla Public License Version
5 # 1.1 (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 # http://www.mozilla.org/MPL/
9 # Software distributed under the License is distributed on an "AS IS" basis,
10 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 # for the specific language governing rights and limitations under the
12 # License.
14 # The Original Code is Mozilla build system.
16 # The Initial Developer of the Original Code is
17 # Mozilla Foundation.
18 # Portions created by the Initial Developer are Copyright (C) 2007
19 # the Initial Developer. All Rights Reserved.
21 # Contributor(s):
22 # Axel Hecht <axel@pike.org>
24 # Alternatively, the contents of this file may be used under the terms of
25 # either the GNU General Public License Version 2 or later (the "GPL"), or
26 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 # in which case the provisions of the GPL or the LGPL are applicable instead
28 # of those above. If you wish to allow use of your version of this file only
29 # under the terms of either the GPL or the LGPL, and not to allow others to
30 # use your version of this file under the terms of the MPL, indicate your
31 # decision by deleting the provisions above and replace them with the notice
32 # and other provisions required by the GPL or the LGPL. If you do not delete
33 # the provisions above, a recipient may use your version of this file under
34 # the terms of any one of the MPL, the GPL or the LGPL.
36 # ***** END LICENSE BLOCK *****
38 import zipfile
39 import time
40 import binascii, struct
41 import zlib
42 from utils import lockFile
45 class ZipFile(zipfile.ZipFile):
46 """ Class with methods to open, read, write, close, list zip files.
48 Subclassing zipfile.ZipFile to allow for overwriting of existing
49 entries, though only for writestr, not for write.
50 """
51 def __init__(self, file, mode="r", compression=zipfile.ZIP_STORED,
52 lock = False):
53 if lock:
54 assert isinstance(file, basestring)
55 self.lockfile = lockFile(file + '.lck')
56 else:
57 self.lockfile = None
58 zipfile.ZipFile.__init__(self, file, mode, compression)
59 self._remove = []
60 self.end = self.fp.tell()
61 self.debug = 0
63 def writestr(self, zinfo_or_arcname, bytes):
64 """Write contents into the archive.
66 The contents is the argument 'bytes', 'zinfo_or_arcname' is either
67 a ZipInfo instance or the name of the file in the archive.
68 This method is overloaded to allow overwriting existing entries.
69 """
70 if not isinstance(zinfo_or_arcname, zipfile.ZipInfo):
71 zinfo = zipfile.ZipInfo(filename=zinfo_or_arcname,
72 date_time=time.localtime(time.time()))
73 zinfo.compress_type = self.compression
74 # Add some standard UNIX file access permissions (-rw-r--r--).
75 zinfo.external_attr = (0x81a4 & 0xFFFF) << 16L
76 else:
77 zinfo = zinfo_or_arcname
79 # Now to the point why we overwrote this in the first place,
80 # remember the entry numbers if we already had this entry.
81 # Optimizations:
82 # If the entry to overwrite is the last one, just reuse that.
83 # If we store uncompressed and the new content has the same size
84 # as the old, reuse the existing entry.
86 doSeek = False # store if we need to seek to the eof after overwriting
87 if self.NameToInfo.has_key(zinfo.filename):
88 # Find the last ZipInfo with our name.
89 # Last, because that's catching multiple overwrites
90 i = len(self.filelist)
91 while i > 0:
92 i -= 1
93 if self.filelist[i].filename == zinfo.filename:
94 break
95 zi = self.filelist[i]
96 if ((zinfo.compress_type == zipfile.ZIP_STORED
97 and zi.compress_size == len(bytes))
98 or (i + 1) == len(self.filelist)):
99 # make sure we're allowed to write, otherwise done by writestr below
100 self._writecheck(zi)
101 # overwrite existing entry
102 self.fp.seek(zi.header_offset)
103 if (i + 1) == len(self.filelist):
104 # this is the last item in the file, just truncate
105 self.fp.truncate()
106 else:
107 # we need to move to the end of the file afterwards again
108 doSeek = True
109 # unhook the current zipinfo, the writestr of our superclass
110 # will add a new one
111 self.filelist.pop(i)
112 self.NameToInfo.pop(zinfo.filename)
113 else:
114 # Couldn't optimize, sadly, just remember the old entry for removal
115 self._remove.append(self.filelist.pop(i))
116 zipfile.ZipFile.writestr(self, zinfo, bytes)
117 self.filelist.sort(lambda l, r: cmp(l.header_offset, r.header_offset))
118 if doSeek:
119 self.fp.seek(self.end)
120 self.end = self.fp.tell()
122 def close(self):
123 """Close the file, and for mode "w" and "a" write the ending
124 records.
126 Overwritten to compact overwritten entries.
128 if not self._remove:
129 # we don't have anything special to do, let's just call base
130 r = zipfile.ZipFile.close(self)
131 self.lockfile = None
132 return r
134 if self.fp.mode != 'r+b':
135 # adjust file mode if we originally just wrote, now we rewrite
136 self.fp.close()
137 self.fp = open(self.filename, 'r+b')
138 all = map(lambda zi: (zi, True), self.filelist) + \
139 map(lambda zi: (zi, False), self._remove)
140 all.sort(lambda l, r: cmp(l[0].header_offset, r[0].header_offset))
141 # empty _remove for multiple closes
142 self._remove = []
144 lengths = [all[i+1][0].header_offset - all[i][0].header_offset
145 for i in xrange(len(all)-1)]
146 lengths.append(self.end - all[-1][0].header_offset)
147 to_pos = 0
148 for (zi, keep), length in zip(all, lengths):
149 if not keep:
150 continue
151 oldoff = zi.header_offset
152 # python <= 2.4 has file_offset
153 if hasattr(zi, 'file_offset'):
154 zi.file_offset = zi.file_offset + to_pos - oldoff
155 zi.header_offset = to_pos
156 self.fp.seek(oldoff)
157 content = self.fp.read(length)
158 self.fp.seek(to_pos)
159 self.fp.write(content)
160 to_pos += length
161 self.fp.truncate()
162 zipfile.ZipFile.close(self)
163 self.lockfile = None