Correct all linting errors

pull/10469/head
Jimmy Brisson 2019-04-08 13:32:01 -05:00 committed by adbridge
parent 5eba40774a
commit 9efccffa02
1 changed files with 93 additions and 49 deletions

View File

@ -24,11 +24,6 @@ from sys import stdout, exit, argv, path
from os import sep from os import sep
from os.path import (basename, dirname, join, relpath, abspath, commonprefix, from os.path import (basename, dirname, join, relpath, abspath, commonprefix,
splitext) splitext)
# Be sure that the tools directory is in the search path
ROOT = abspath(join(dirname(__file__), ".."))
path.insert(0, ROOT)
import re import re
import csv import csv
import json import json
@ -39,8 +34,16 @@ from prettytable import PrettyTable, HEADER
from jinja2 import FileSystemLoader, StrictUndefined from jinja2 import FileSystemLoader, StrictUndefined
from jinja2.environment import Environment from jinja2.environment import Environment
from tools.utils import (argparse_filestring_type, argparse_lowercase_hyphen_type,
argparse_uppercase_type) # Be sure that the tools directory is in the search path
ROOT = abspath(join(dirname(__file__), ".."))
path.insert(0, ROOT)
from tools.utils import (
argparse_filestring_type,
argparse_lowercase_hyphen_type,
argparse_uppercase_type
) # noqa: E402
class _Parser(object): class _Parser(object):
@ -105,13 +108,19 @@ class _Parser(object):
class _GccParser(_Parser): class _GccParser(_Parser):
RE_OBJECT_FILE = re.compile(r'^(.+\/.+\.o(bj)?)$') RE_OBJECT_FILE = re.compile(r'^(.+\/.+\.o(bj)?)$')
RE_LIBRARY_OBJECT = re.compile(r'^.+' + r''.format(sep) + r'lib((.+\.a)\((.+\.o(bj)?)\))$') RE_LIBRARY_OBJECT = re.compile(
r'^.+' + r''.format(sep) + r'lib((.+\.a)\((.+\.o(bj)?)\))$'
)
RE_STD_SECTION = re.compile(r'^\s+.*0x(\w{8,16})\s+0x(\w+)\s(.+)$') RE_STD_SECTION = re.compile(r'^\s+.*0x(\w{8,16})\s+0x(\w+)\s(.+)$')
RE_FILL_SECTION = re.compile(r'^\s*\*fill\*\s+0x(\w{8,16})\s+0x(\w+).*$') RE_FILL_SECTION = re.compile(r'^\s*\*fill\*\s+0x(\w{8,16})\s+0x(\w+).*$')
OBJECT_EXTENSIONS = (".o", ".obj") OBJECT_EXTENSIONS = (".o", ".obj")
ALL_SECTIONS = _Parser.SECTIONS + _Parser.OTHER_SECTIONS + \ ALL_SECTIONS = (
_Parser.MISC_FLASH_SECTIONS + ('unknown', 'OUTPUT') _Parser.SECTIONS
+ _Parser.OTHER_SECTIONS
+ _Parser.MISC_FLASH_SECTIONS
+ ('unknown', 'OUTPUT')
)
def check_new_section(self, line): def check_new_section(self, line):
""" Check whether a new section in a map file has been detected """ Check whether a new section in a map file has been detected
@ -119,19 +128,16 @@ class _GccParser(_Parser):
Positional arguments: Positional arguments:
line - the line to check for a new section line - the line to check for a new section
return value - A section name, if a new section was found, False return value - A section name, if a new section was found, None
otherwise otherwise
""" """
for i in self.ALL_SECTIONS: for i in self.ALL_SECTIONS:
if line.startswith(i): if line.startswith(i):
# should name of the section (assuming it's a known one)
return i return i
if line.startswith('.'): if line.startswith('.'):
return 'unknown' # all others are classified are unknown return 'unknown'
else: else:
return False # everything else, means no change in section return None
def parse_object_name(self, line): def parse_object_name(self, line):
""" Parse a path to object file """ Parse a path to object file
@ -158,8 +164,10 @@ class _GccParser(_Parser):
return join('[lib]', test_re_obj_name.group(2), return join('[lib]', test_re_obj_name.group(2),
test_re_obj_name.group(3)) test_re_obj_name.group(3))
else: else:
if (not line.startswith("LONG") and if (
not line.startswith("linker stubs")): not line.startswith("LONG") and
not line.startswith("linker stubs")
):
print("Unknown object name found in GCC map file: %s" print("Unknown object name found in GCC map file: %s"
% line) % line)
return '[misc]' return '[misc]'
@ -168,8 +176,8 @@ class _GccParser(_Parser):
""" Parse data from a section of gcc map file """ Parse data from a section of gcc map file
examples: examples:
0x00004308 0x7c ./BUILD/K64F/GCC_ARM/mbed-os/hal/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/spi_api.o 0x00004308 0x7c ./BUILD/K64F/GCC_ARM/spi_api.o
.text 0x00000608 0x198 ./BUILD/K64F/GCC_ARM/mbed-os/core/mbed-rtos/rtx/TARGET_CORTEX_M/TARGET_RTOS_M4_M7/TOOLCHAIN/HAL_CM4.o .text 0x00000608 0x198 ./BUILD/K64F/HAL_CM4.o
Positional arguments: Positional arguments:
line - the line to parse a section from line - the line to parse a section from
@ -215,7 +223,11 @@ class _GccParser(_Parser):
self.module_add(object_name, object_size, current_section) self.module_add(object_name, object_size, current_section)
common_prefix = dirname(commonprefix([ common_prefix = dirname(commonprefix([
o for o in self.modules.keys() if (o.endswith(self.OBJECT_EXTENSIONS) and not o.startswith("[lib]"))])) o for o in self.modules.keys()
if (
o.endswith(self.OBJECT_EXTENSIONS)
and not o.startswith("[lib]")
)]))
new_modules = {} new_modules = {}
for name, stats in self.modules.items(): for name, stats in self.modules.items():
if name.startswith("[lib]"): if name.startswith("[lib]"):
@ -245,9 +257,13 @@ class _ArmccParser(_Parser):
else: else:
is_obj = re.match(self.RE_OBJECT, line) is_obj = re.match(self.RE_OBJECT, line)
if is_obj: if is_obj:
return join('[lib]', basename(is_obj.group(1)), is_obj.group(3)) return join(
'[lib]', basename(is_obj.group(1)), is_obj.group(3)
)
else: else:
print("Malformed input found when parsing ARMCC map: %s" % line) print(
"Malformed input found when parsing ARMCC map: %s" % line
)
return '[misc]' return '[misc]'
def parse_section(self, line): def parse_section(self, line):
@ -260,7 +276,7 @@ class _ArmccParser(_Parser):
Positional arguments: Positional arguments:
line - the line to parse the section data from line - the line to parse the section data from
""" """ # noqa: E501
test_re = re.match(self.RE, line) test_re = re.match(self.RE, line)
if test_re: if test_re:
@ -276,8 +292,10 @@ class _ArmccParser(_Parser):
elif test_re.group(3) == 'Code': elif test_re.group(3) == 'Code':
section = '.text' section = '.text'
else: else:
print("Malformed input found when parsing armcc map: %s, %r" print(
% (line, test_re.groups())) "Malformed input found when parsing armcc map: %s, %r"
% (line, test_re.groups())
)
return ["", 0, ""] return ["", 0, ""]
@ -307,10 +325,20 @@ class _ArmccParser(_Parser):
self.module_add(*self.parse_section(line)) self.module_add(*self.parse_section(line))
common_prefix = dirname(commonprefix([ common_prefix = dirname(commonprefix([
o for o in self.modules.keys() if (o.endswith(self.OBJECT_EXTENSIONS) and o != "anon$$obj.o" and o != "anon$$obj.obj" and not o.startswith("[lib]"))])) o for o in self.modules.keys()
if (
o.endswith(self.OBJECT_EXTENSIONS)
and o != "anon$$obj.o"
and o != "anon$$obj.obj"
and not o.startswith("[lib]")
)]))
new_modules = {} new_modules = {}
for name, stats in self.modules.items(): for name, stats in self.modules.items():
if name == "anon$$obj.o" or name == "anon$$obj.obj" or name.startswith("[lib]"): if (
name == "anon$$obj.o"
or name == "anon$$obj.obj"
or name.startswith("[lib]")
):
new_modules[name] = stats new_modules[name] = stats
elif name.endswith(self.OBJECT_EXTENSIONS): elif name.endswith(self.OBJECT_EXTENSIONS):
new_modules[relpath(name, common_prefix)] = stats new_modules[relpath(name, common_prefix)] = stats
@ -365,11 +393,13 @@ class _IarParser(_Parser):
Positional_arguments: Positional_arguments:
line - the line to parse section data from line - the line to parse section data from
""" """ # noqa: E501
test_re = re.match(self.RE, line) test_re = re.match(self.RE, line)
if test_re: if test_re:
if (test_re.group(2) == 'const' or if (
test_re.group(2) == 'ro code'): test_re.group(2) == 'const' or
test_re.group(2) == 'ro code'
):
section = '.text' section = '.text'
elif (test_re.group(2) == 'zero' or elif (test_re.group(2) == 'zero' or
test_re.group(2) == 'uninit'): test_re.group(2) == 'uninit'):
@ -409,7 +439,8 @@ class _IarParser(_Parser):
def check_new_object_lib(self, line): def check_new_object_lib(self, line):
""" """
Searches for objects within a library section and returns name. Example: Searches for objects within a library section and returns name.
Example:
rt7M_tl.a: [44] rt7M_tl.a: [44]
ABImemclr4.o 6 ABImemclr4.o 6
ABImemcpy_unaligned.o 118 ABImemcpy_unaligned.o 118
@ -435,7 +466,10 @@ class _IarParser(_Parser):
break break
for arg in line.split(" "): for arg in line.split(" "):
arg = arg.rstrip(" \n") arg = arg.rstrip(" \n")
if (not arg.startswith("-")) and arg.endswith(self.OBJECT_EXTENSIONS): if (
not arg.startswith("-")
and arg.endswith(self.OBJECT_EXTENSIONS)
):
self.cmd_modules[basename(arg)] = arg self.cmd_modules[basename(arg)] = arg
common_prefix = dirname(commonprefix(list(self.cmd_modules.values()))) common_prefix = dirname(commonprefix(list(self.cmd_modules.values())))
@ -484,7 +518,6 @@ class MemapParser(object):
print_sections = ('.text', '.data', '.bss') print_sections = ('.text', '.data', '.bss')
delta_sections = ('.text-delta', '.data-delta', '.bss-delta') delta_sections = ('.text-delta', '.data-delta', '.bss-delta')
# sections to print info (generic for all toolchains) # sections to print info (generic for all toolchains)
sections = _Parser.SECTIONS sections = _Parser.SECTIONS
misc_flash_sections = _Parser.MISC_FLASH_SECTIONS misc_flash_sections = _Parser.MISC_FLASH_SECTIONS
@ -498,7 +531,6 @@ class MemapParser(object):
# short version with specific depth # short version with specific depth
self.short_modules = dict() self.short_modules = dict()
# Memory report (sections + summary) # Memory report (sections + summary)
self.mem_report = [] self.mem_report = []
@ -528,7 +560,7 @@ class MemapParser(object):
mbed-os/drivers mbed-os/drivers
""" """
if depth == 0 or depth == None: if depth == 0 or depth is None:
self.short_modules = deepcopy(self.modules) self.short_modules = deepcopy(self.modules)
else: else:
self.short_modules = dict() self.short_modules = dict()
@ -539,8 +571,9 @@ class MemapParser(object):
new_name = join(*split_name[:depth]) new_name = join(*split_name[:depth])
self.short_modules.setdefault(new_name, defaultdict(int)) self.short_modules.setdefault(new_name, defaultdict(int))
for section_idx, value in v.items(): for section_idx, value in v.items():
self.short_modules[new_name][section_idx] += self.modules[module_name][section_idx] self.short_modules[new_name][section_idx] += value
self.short_modules[new_name][section_idx + '-delta'] += self.modules[module_name][section_idx] delta_name = section_idx + '-delta'
self.short_modules[new_name][delta_name] += value
if self.old_modules: if self.old_modules:
for module_name, v in self.old_modules.items(): for module_name, v in self.old_modules.items():
split_name = module_name.split(sep) split_name = module_name.split(sep)
@ -549,7 +582,8 @@ class MemapParser(object):
new_name = join(*split_name[:depth]) new_name = join(*split_name[:depth])
self.short_modules.setdefault(new_name, defaultdict(int)) self.short_modules.setdefault(new_name, defaultdict(int))
for section_idx, value in v.items(): for section_idx, value in v.items():
self.short_modules[new_name][section_idx + '-delta'] -= self.old_modules[module_name][section_idx] delta_name = section_idx + '-delta'
self.short_modules[new_name][delta_name] -= value
export_formats = ["json", "csv-ci", "html", "table"] export_formats = ["json", "csv-ci", "html", "table"]
@ -657,7 +691,10 @@ class MemapParser(object):
if not modules: if not modules:
break break
next_module = modules.pop(0) next_module = modules.pop(0)
if not any(cld['name'] == next_module for cld in cur_text['children']): if not any(
cld['name'] == next_module
for cld in cur_text['children']
):
break break
cur_text = self._move_up_tree(cur_text, next_module) cur_text = self._move_up_tree(cur_text, next_module)
cur_data = self._move_up_tree(cur_data, next_module) cur_data = self._move_up_tree(cur_data, next_module)
@ -759,8 +796,10 @@ class MemapParser(object):
row = [i] row = [i]
for k in self.print_sections: for k in self.print_sections:
row.append("{}({:+})".format(self.short_modules[i][k], row.append("{}({:+})".format(
self.short_modules[i][k + "-delta"])) self.short_modules[i][k],
self.short_modules[i][k + "-delta"]
))
table.add_row(row) table.add_row(row)
@ -855,6 +894,7 @@ class MemapParser(object):
print("I/O error({0}): {1}".format(error.errno, error.strerror)) print("I/O error({0}): {1}".format(error.errno, error.strerror))
return False return False
def main(): def main():
"""Entry Point""" """Entry Point"""
version = '0.4.0' version = '0.4.0'
@ -912,9 +952,12 @@ def main():
returned_string = None returned_string = None
# Write output in file # Write output in file
if args.output != None: if args.output is not None:
returned_string = memap.generate_output(args.export, \ returned_string = memap.generate_output(
depth, args.output) args.export,
depth,
args.output
)
else: # Write output in screen else: # Write output in screen
returned_string = memap.generate_output(args.export, depth) returned_string = memap.generate_output(args.export, depth)
@ -923,5 +966,6 @@ def main():
exit(0) exit(0)
if __name__ == "__main__": if __name__ == "__main__":
main() main()