2012-05-21 15:12:37 +04:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2008-09-19 20:19:52 +04:00
|
|
|
|
2020-01-01 03:16:01 +03:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2009-03-31 20:47:38 +04:00
|
|
|
import os
|
2020-01-01 03:16:01 +03:00
|
|
|
import six
|
2013-10-11 19:23:18 +04:00
|
|
|
import time
|
|
|
|
import zipfile
|
|
|
|
|
|
|
|
from mozbuild.util import lock_file
|
|
|
|
|
2008-09-19 20:19:52 +04:00
|
|
|
|
|
|
|
class ZipFile(zipfile.ZipFile):
|
2020-10-24 03:36:18 +03:00
|
|
|
"""Class with methods to open, read, write, close, list zip files.
|
2018-05-22 01:01:01 +03:00
|
|
|
|
|
|
|
Subclassing zipfile.ZipFile to allow for overwriting of existing
|
|
|
|
entries, though only for writestr, not for write.
|
2008-09-19 20:19:52 +04:00
|
|
|
"""
|
2018-05-22 01:01:01 +03:00
|
|
|
|
2020-10-24 03:36:18 +03:00
|
|
|
def __init__(self, file, mode="r", compression=zipfile.ZIP_STORED, lock=False):
|
2018-05-22 01:01:01 +03:00
|
|
|
if lock:
|
2020-01-01 03:16:01 +03:00
|
|
|
assert isinstance(file, six.text_type)
|
2020-10-24 03:36:18 +03:00
|
|
|
self.lockfile = lock_file(file + ".lck")
|
2008-09-19 20:19:52 +04:00
|
|
|
else:
|
2018-05-22 01:01:01 +03:00
|
|
|
self.lockfile = None
|
|
|
|
|
2020-10-24 03:36:18 +03:00
|
|
|
if mode == "a" and lock:
|
2018-05-22 01:01:01 +03:00
|
|
|
# appending to a file which doesn't exist fails, but we can't check
|
|
|
|
# existence util we hold the lock
|
|
|
|
if (not os.path.isfile(file)) or os.path.getsize(file) == 0:
|
2020-10-24 03:36:18 +03:00
|
|
|
mode = "w"
|
2018-05-22 01:01:01 +03:00
|
|
|
|
|
|
|
zipfile.ZipFile.__init__(self, file, mode, compression)
|
|
|
|
self._remove = []
|
|
|
|
self.end = self.fp.tell()
|
|
|
|
self.debug = 0
|
|
|
|
|
|
|
|
def writestr(self, zinfo_or_arcname, bytes):
|
|
|
|
"""Write contents into the archive.
|
|
|
|
|
|
|
|
The contents is the argument 'bytes', 'zinfo_or_arcname' is either
|
|
|
|
a ZipInfo instance or the name of the file in the archive.
|
|
|
|
This method is overloaded to allow overwriting existing entries.
|
|
|
|
"""
|
|
|
|
if not isinstance(zinfo_or_arcname, zipfile.ZipInfo):
|
2020-10-24 03:36:18 +03:00
|
|
|
zinfo = zipfile.ZipInfo(
|
|
|
|
filename=zinfo_or_arcname, date_time=time.localtime(time.time())
|
2020-10-26 21:34:53 +03:00
|
|
|
)
|
2018-05-22 01:01:01 +03:00
|
|
|
zinfo.compress_type = self.compression
|
|
|
|
# Add some standard UNIX file access permissions (-rw-r--r--).
|
2020-10-24 03:36:18 +03:00
|
|
|
zinfo.external_attr = (0x81A4 & 0xFFFF) << 16
|
2018-05-22 01:01:01 +03:00
|
|
|
else:
|
|
|
|
zinfo = zinfo_or_arcname
|
|
|
|
|
|
|
|
# Now to the point why we overwrote this in the first place,
|
|
|
|
# remember the entry numbers if we already had this entry.
|
|
|
|
# Optimizations:
|
|
|
|
# If the entry to overwrite is the last one, just reuse that.
|
|
|
|
# If we store uncompressed and the new content has the same size
|
|
|
|
# as the old, reuse the existing entry.
|
|
|
|
|
|
|
|
doSeek = False # store if we need to seek to the eof after overwriting
|
2018-05-22 16:22:46 +03:00
|
|
|
if zinfo.filename in self.NameToInfo:
|
2018-05-22 01:01:01 +03:00
|
|
|
# Find the last ZipInfo with our name.
|
|
|
|
# Last, because that's catching multiple overwrites
|
|
|
|
i = len(self.filelist)
|
|
|
|
while i > 0:
|
|
|
|
i -= 1
|
|
|
|
if self.filelist[i].filename == zinfo.filename:
|
|
|
|
break
|
|
|
|
zi = self.filelist[i]
|
2020-10-24 03:36:18 +03:00
|
|
|
if (
|
|
|
|
zinfo.compress_type == zipfile.ZIP_STORED
|
|
|
|
and zi.compress_size == len(bytes)
|
|
|
|
) or (i + 1) == len(self.filelist):
|
2018-05-22 01:01:01 +03:00
|
|
|
# make sure we're allowed to write, otherwise done by writestr below
|
|
|
|
self._writecheck(zi)
|
|
|
|
# overwrite existing entry
|
|
|
|
self.fp.seek(zi.header_offset)
|
|
|
|
if (i + 1) == len(self.filelist):
|
|
|
|
# this is the last item in the file, just truncate
|
|
|
|
self.fp.truncate()
|
|
|
|
else:
|
|
|
|
# we need to move to the end of the file afterwards again
|
|
|
|
doSeek = True
|
|
|
|
# unhook the current zipinfo, the writestr of our superclass
|
|
|
|
# will add a new one
|
|
|
|
self.filelist.pop(i)
|
|
|
|
self.NameToInfo.pop(zinfo.filename)
|
|
|
|
else:
|
|
|
|
# Couldn't optimize, sadly, just remember the old entry for removal
|
|
|
|
self._remove.append(self.filelist.pop(i))
|
|
|
|
zipfile.ZipFile.writestr(self, zinfo, bytes)
|
2020-01-01 03:16:01 +03:00
|
|
|
self.filelist.sort(key=lambda l: l.header_offset)
|
2018-05-22 01:01:01 +03:00
|
|
|
if doSeek:
|
|
|
|
self.fp.seek(self.end)
|
|
|
|
self.end = self.fp.tell()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""Close the file, and for mode "w" and "a" write the ending
|
|
|
|
records.
|
|
|
|
|
|
|
|
Overwritten to compact overwritten entries.
|
|
|
|
"""
|
|
|
|
if not self._remove:
|
|
|
|
# we don't have anything special to do, let's just call base
|
|
|
|
r = zipfile.ZipFile.close(self)
|
|
|
|
self.lockfile = None
|
|
|
|
return r
|
|
|
|
|
2020-10-24 03:36:18 +03:00
|
|
|
if self.fp.mode != "r+b":
|
2018-05-22 01:01:01 +03:00
|
|
|
# adjust file mode if we originally just wrote, now we rewrite
|
|
|
|
self.fp.close()
|
2020-10-24 03:36:18 +03:00
|
|
|
self.fp = open(self.filename, "r+b")
|
|
|
|
all = map(lambda zi: (zi, True), self.filelist) + map(
|
|
|
|
lambda zi: (zi, False), self._remove
|
|
|
|
)
|
2020-01-01 03:16:01 +03:00
|
|
|
all.sort(key=lambda l: l[0].header_offset)
|
2018-05-22 01:01:01 +03:00
|
|
|
# empty _remove for multiple closes
|
|
|
|
self._remove = []
|
|
|
|
|
2020-10-24 03:36:18 +03:00
|
|
|
lengths = [
|
|
|
|
all[i + 1][0].header_offset - all[i][0].header_offset
|
|
|
|
for i in xrange(len(all) - 1)
|
|
|
|
]
|
2018-05-22 01:01:01 +03:00
|
|
|
lengths.append(self.end - all[-1][0].header_offset)
|
|
|
|
to_pos = 0
|
|
|
|
for (zi, keep), length in zip(all, lengths):
|
|
|
|
if not keep:
|
|
|
|
continue
|
|
|
|
oldoff = zi.header_offset
|
|
|
|
# python <= 2.4 has file_offset
|
2020-10-24 03:36:18 +03:00
|
|
|
if hasattr(zi, "file_offset"):
|
2018-05-22 01:01:01 +03:00
|
|
|
zi.file_offset = zi.file_offset + to_pos - oldoff
|
|
|
|
zi.header_offset = to_pos
|
|
|
|
self.fp.seek(oldoff)
|
|
|
|
content = self.fp.read(length)
|
|
|
|
self.fp.seek(to_pos)
|
|
|
|
self.fp.write(content)
|
|
|
|
to_pos += length
|
|
|
|
self.fp.truncate()
|
|
|
|
zipfile.ZipFile.close(self)
|
|
|
|
self.lockfile = None
|