Skip to content

Commit

Permalink
bug 1463425 - autopep8 on config/ r=gps
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: EaTAhH2CAee
  • Loading branch information
sylvestre committed May 21, 2018
1 parent b323297 commit 20492d3
Show file tree
Hide file tree
Showing 20 changed files with 851 additions and 757 deletions.
247 changes: 124 additions & 123 deletions config/MozZipFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,128 +10,129 @@


class ZipFile(zipfile.ZipFile):
""" Class with methods to open, read, write, close, list zip files.
Subclassing zipfile.ZipFile to allow for overwriting of existing
entries, though only for writestr, not for write.
"""
def __init__(self, file, mode="r", compression=zipfile.ZIP_STORED,
lock = False):
if lock:
assert isinstance(file, basestring)
self.lockfile = lock_file(file + '.lck')
else:
self.lockfile = None

if mode == 'a' and lock:
# 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:
mode = 'w'

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.
""" Class with methods to open, read, write, close, list zip files.
Subclassing zipfile.ZipFile to allow for overwriting of existing
entries, though only for writestr, not for write.
"""
if not isinstance(zinfo_or_arcname, zipfile.ZipInfo):
zinfo = zipfile.ZipInfo(filename=zinfo_or_arcname,
date_time=time.localtime(time.time()))
zinfo.compress_type = self.compression
# Add some standard UNIX file access permissions (-rw-r--r--).
zinfo.external_attr = (0x81a4 & 0xFFFF) << 16L
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
if self.NameToInfo.has_key(zinfo.filename):
# 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]
if ((zinfo.compress_type == zipfile.ZIP_STORED
and zi.compress_size == len(bytes))
or (i + 1) == len(self.filelist)):
# 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()

def __init__(self, file, mode="r", compression=zipfile.ZIP_STORED,
lock=False):
if lock:
assert isinstance(file, basestring)
self.lockfile = lock_file(file + '.lck')
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)
self.filelist.sort(lambda l, r: cmp(l.header_offset, r.header_offset))
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

if self.fp.mode != 'r+b':
# adjust file mode if we originally just wrote, now we rewrite
self.fp.close()
self.fp = open(self.filename, 'r+b')
all = map(lambda zi: (zi, True), self.filelist) + \
map(lambda zi: (zi, False), self._remove)
all.sort(lambda l, r: cmp(l[0].header_offset, r[0].header_offset))
# empty _remove for multiple closes
self._remove = []

lengths = [all[i+1][0].header_offset - all[i][0].header_offset
for i in xrange(len(all)-1)]
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
if hasattr(zi, 'file_offset'):
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
self.lockfile = None

if mode == 'a' and lock:
# 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:
mode = 'w'

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):
zinfo = zipfile.ZipInfo(filename=zinfo_or_arcname,
date_time=time.localtime(time.time()))
zinfo.compress_type = self.compression
# Add some standard UNIX file access permissions (-rw-r--r--).
zinfo.external_attr = (0x81a4 & 0xFFFF) << 16L
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
if self.NameToInfo.has_key(zinfo.filename):
# 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]
if ((zinfo.compress_type == zipfile.ZIP_STORED
and zi.compress_size == len(bytes))
or (i + 1) == len(self.filelist)):
# 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)
self.filelist.sort(lambda l, r: cmp(l.header_offset, r.header_offset))
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

if self.fp.mode != 'r+b':
# adjust file mode if we originally just wrote, now we rewrite
self.fp.close()
self.fp = open(self.filename, 'r+b')
all = map(lambda zi: (zi, True), self.filelist) + \
map(lambda zi: (zi, False), self._remove)
all.sort(lambda l, r: cmp(l[0].header_offset, r[0].header_offset))
# empty _remove for multiple closes
self._remove = []

lengths = [all[i+1][0].header_offset - all[i][0].header_offset
for i in xrange(len(all)-1)]
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
if hasattr(zi, 'file_offset'):
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
8 changes: 7 additions & 1 deletion config/check_js_msg_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from mozversioncontrol import get_repository_from_env


scriptname = os.path.basename(__file__);
scriptname = os.path.basename(__file__)
expected_encoding = 'ascii'

# The following files don't define JSErrorFormatString.
Expand All @@ -26,13 +26,16 @@
'js/xpconnect/src/xpc.msg',
]


def log_pass(filename, text):
print('TEST-PASS | {} | {} | {}'.format(scriptname, filename, text))


def log_fail(filename, text):
print('TEST-UNEXPECTED-FAIL | {} | {} | {}'.format(scriptname, filename,
text))


def check_single_file(filename):
with open(filename, 'rb') as f:
data = f.read()
Expand All @@ -44,6 +47,7 @@ def check_single_file(filename):
log_pass(filename, 'ok')
return True


def check_files():
result = True

Expand All @@ -58,11 +62,13 @@ def check_files():

return result


def main():
if not check_files():
sys.exit(1)

sys.exit(0)


if __name__ == '__main__':
main()
7 changes: 6 additions & 1 deletion config/check_js_opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
import os
import sys

scriptname = os.path.basename(__file__);
scriptname = os.path.basename(__file__)
topsrcdir = os.path.dirname(os.path.dirname(__file__))


def log_pass(text):
print('TEST-PASS | {} | {}'.format(scriptname, text))


def log_fail(text):
print('TEST-UNEXPECTED-FAIL | {} | {}'.format(scriptname, text))


def check_opcode():
sys.path.insert(0, os.path.join(topsrcdir, 'js', 'src', 'vm'))
import opcode
Expand All @@ -33,11 +36,13 @@ def check_opcode():
log_pass('ok')
return True


def main():
if not check_opcode():
sys.exit(1)

sys.exit(0)


if __name__ == '__main__':
main()
18 changes: 12 additions & 6 deletions config/check_macroassembler_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
reArgName = "(?P<name>\s\w+)"
reArgDefault = "(?P<default>(?:\s=[^,)]+)?)"
reAfterArg = "(?=[,)])"
reMatchArg = re.compile(reBeforeArg + reArgType + reArgName + reArgDefault + reAfterArg)
reMatchArg = re.compile(reBeforeArg + reArgType +
reArgName + reArgDefault + reAfterArg)


def get_normalized_signatures(signature, fileAnnot=None):
Expand All @@ -58,7 +59,8 @@ def get_normalized_signatures(signature, fileAnnot=None):
archs = [fileAnnot['arch']]

if 'DEFINED_ON(' in signature:
archs = re.sub(r'.*DEFINED_ON\((?P<archs>[^()]*)\).*', '\g<archs>', signature).split(',')
archs = re.sub(
r'.*DEFINED_ON\((?P<archs>[^()]*)\).*', '\g<archs>', signature).split(',')
archs = [a.strip() for a in archs]
signature = re.sub(r'\s+DEFINED_ON\([^()]*\)', '', signature)

Expand Down Expand Up @@ -157,7 +159,8 @@ def get_macroassembler_definitions(filename):
line = re.sub(r'//.*', '', line)
if line.startswith('{') or line.strip() == "{}":
if 'MacroAssembler::' in lines:
signatures.extend(get_normalized_signatures(lines, fileAnnot))
signatures.extend(
get_normalized_signatures(lines, fileAnnot))
if line.strip() != "{}": # Empty declaration, no need to declare
# a new code section
code_section = True
Expand Down Expand Up @@ -244,7 +247,8 @@ def generate_file_content(signatures):
elif len(archs.symmetric_difference(all_shared_architecture_names)) == 0:
output.append(s + ' PER_SHARED_ARCH;\n')
else:
output.append(s + ' DEFINED_ON(' + ', '.join(sorted(archs)) + ');\n')
output.append(
s + ' DEFINED_ON(' + ', '.join(sorted(archs)) + ');\n')
for a in sorted(archs):
a = a.replace('_', '-')
masm = '%s/MacroAssembler-%s' % (a, a)
Expand All @@ -271,8 +275,10 @@ def check_style():
filepath = os.path.join(dirpath, filename).replace('\\', '/')

if filepath.endswith('MacroAssembler.h'):
decls = append_signatures(decls, get_macroassembler_declaration(filepath))
defs = append_signatures(defs, get_macroassembler_definitions(filepath))
decls = append_signatures(
decls, get_macroassembler_declaration(filepath))
defs = append_signatures(
defs, get_macroassembler_definitions(filepath))

if not decls or not defs:
raise Exception("Did not find any definitions or declarations")
Expand Down
Loading

0 comments on commit 20492d3

Please sign in to comment.