Bug 508760 - Remove MSVC6 support from the tree; (Iv1) /toolkit/xre/*.cpp.
[mozilla-central.git] / config / MozZipFile.py
blob3a85e4a289b682859217c016464ef400f941ec7a
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 import os
43 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
59 if mode == 'a' and lock:
60 # appending to a file which doesn't exist fails, but we can't check
61 # existence util we hold the lock
62 if (not os.path.isfile(file)) or os.path.getsize(file) == 0:
63 mode = 'w'
65 zipfile.ZipFile.__init__(self, file, mode, compression)
66 self._remove = []
67 self.end = self.fp.tell()
68 self.debug = 0
70 def writestr(self, zinfo_or_arcname, bytes):
71 """Write contents into the archive.
73 The contents is the argument 'bytes', 'zinfo_or_arcname' is either
74 a ZipInfo instance or the name of the file in the archive.
75 This method is overloaded to allow overwriting existing entries.
76 """
77 if not isinstance(zinfo_or_arcname, zipfile.ZipInfo):
78 zinfo = zipfile.ZipInfo(filename=zinfo_or_arcname,
79 date_time=time.localtime(time.time()))
80 zinfo.compress_type = self.compression
81 # Add some standard UNIX file access permissions (-rw-r--r--).
82 zinfo.external_attr = (0x81a4 & 0xFFFF) << 16L
83 else:
84 zinfo = zinfo_or_arcname
86 # Now to the point why we overwrote this in the first place,
87 # remember the entry numbers if we already had this entry.
88 # Optimizations:
89 # If the entry to overwrite is the last one, just reuse that.
90 # If we store uncompressed and the new content has the same size
91 # as the old, reuse the existing entry.
93 doSeek = False # store if we need to seek to the eof after overwriting
94 if self.NameToInfo.has_key(zinfo.filename):
95 # Find the last ZipInfo with our name.
96 # Last, because that's catching multiple overwrites
97 i = len(self.filelist)
98 while i > 0:
99 i -= 1
100 if self.filelist[i].filename == zinfo.filename:
101 break
102 zi = self.filelist[i]
103 if ((zinfo.compress_type == zipfile.ZIP_STORED
104 and zi.compress_size == len(bytes))
105 or (i + 1) == len(self.filelist)):
106 # make sure we're allowed to write, otherwise done by writestr below
107 self._writecheck(zi)
108 # overwrite existing entry
109 self.fp.seek(zi.header_offset)
110 if (i + 1) == len(self.filelist):
111 # this is the last item in the file, just truncate
112 self.fp.truncate()
113 else:
114 # we need to move to the end of the file afterwards again
115 doSeek = True
116 # unhook the current zipinfo, the writestr of our superclass
117 # will add a new one
118 self.filelist.pop(i)
119 self.NameToInfo.pop(zinfo.filename)
120 else:
121 # Couldn't optimize, sadly, just remember the old entry for removal
122 self._remove.append(self.filelist.pop(i))
123 zipfile.ZipFile.writestr(self, zinfo, bytes)
124 self.filelist.sort(lambda l, r: cmp(l.header_offset, r.header_offset))
125 if doSeek:
126 self.fp.seek(self.end)
127 self.end = self.fp.tell()
129 def close(self):
130 """Close the file, and for mode "w" and "a" write the ending
131 records.
133 Overwritten to compact overwritten entries.
135 if not self._remove:
136 # we don't have anything special to do, let's just call base
137 r = zipfile.ZipFile.close(self)
138 self.lockfile = None
139 return r
141 if self.fp.mode != 'r+b':
142 # adjust file mode if we originally just wrote, now we rewrite
143 self.fp.close()
144 self.fp = open(self.filename, 'r+b')
145 all = map(lambda zi: (zi, True), self.filelist) + \
146 map(lambda zi: (zi, False), self._remove)
147 all.sort(lambda l, r: cmp(l[0].header_offset, r[0].header_offset))
148 # empty _remove for multiple closes
149 self._remove = []
151 lengths = [all[i+1][0].header_offset - all[i][0].header_offset
152 for i in xrange(len(all)-1)]
153 lengths.append(self.end - all[-1][0].header_offset)
154 to_pos = 0
155 for (zi, keep), length in zip(all, lengths):
156 if not keep:
157 continue
158 oldoff = zi.header_offset
159 # python <= 2.4 has file_offset
160 if hasattr(zi, 'file_offset'):
161 zi.file_offset = zi.file_offset + to_pos - oldoff
162 zi.header_offset = to_pos
163 self.fp.seek(oldoff)
164 content = self.fp.read(length)
165 self.fp.seek(to_pos)
166 self.fp.write(content)
167 to_pos += length
168 self.fp.truncate()
169 zipfile.ZipFile.close(self)
170 self.lockfile = None