Skip to content

Commit 70bb47f

Browse files
theotherjimmyadbridge
authored andcommitted
Use non-scoped imports in memap
1 parent 94f4fdc commit 70bb47f

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

tools/memap.py

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
"""Memory Map File Analyser for ARM mbed"""
44

5-
import abc
6-
import sys
7-
import os
5+
from abc import abstractmethod, ABCMeta
6+
from sys import stdout, exit, argv
7+
from os import sep
8+
from os.path import basename, dirname, join, relpath, commonprefix
89
import re
910
import csv
1011
import json
11-
import argparse
12+
from argparse import ArgumentParser
1213
from copy import deepcopy
1314
from prettytable import PrettyTable
1415

@@ -18,7 +19,7 @@
1819

1920
class _Parser(object):
2021
"""Internal interface for parsing"""
21-
__metaclass__ = abc.ABCMeta
22+
__metaclass__ = ABCMeta
2223
SECTIONS = ('.text', '.data', '.bss', '.heap', '.stack')
2324
MISC_FLASH_SECTIONS = ('.interrupts', '.flash_config')
2425
OTHER_SECTIONS = ('.interrupts_ram', '.init', '.ARM.extab',
@@ -45,7 +46,7 @@ def module_add(self, object_name, size, section):
4546
self.modules[object_name][section] += size
4647
return
4748

48-
obj_split = os.sep + os.path.basename(object_name)
49+
obj_split = sep + basename(object_name)
4950
for module_path, contents in self.modules.items():
5051
if module_path.endswith(obj_split) or module_path == object_name:
5152
contents.setdefault(section, 0)
@@ -62,7 +63,7 @@ def module_replace(self, old_object, new_object):
6263
self.modules[new_object] = self.modules[old_object]
6364
del self.modules[old_object]
6465

65-
@abc.abstractmethod
66+
@abstractmethod
6667
def parse_mapfile(self, mapfile):
6768
"""Parse a given file object pointing to a map file
6869
@@ -77,7 +78,7 @@ def parse_mapfile(self, mapfile):
7778

7879
class _GccParser(_Parser):
7980
RE_OBJECT_FILE = re.compile(r'^(.+\/.+\.o)$')
80-
RE_LIBRARY_OBJECT = re.compile(r'^.+' + os.sep + r'lib((.+\.a)\((.+\.o)\))$')
81+
RE_LIBRARY_OBJECT = re.compile(r'^.+' + sep + r'lib((.+\.a)\((.+\.o)\))$')
8182
RE_STD_SECTION = re.compile(r'^\s+.*0x(\w{8,16})\s+0x(\w+)\s(.+)$')
8283
RE_FILL_SECTION = re.compile(r'^\s*\*fill\*\s+0x(\w{8,16})\s+0x(\w+).*$')
8384

@@ -119,16 +120,15 @@ def parse_object_name(self, line):
119120

120121
# corner case: certain objects are provided by the GCC toolchain
121122
if 'arm-none-eabi' in line:
122-
return os.path.join('[lib]', 'misc', os.path.basename(object_name))
123+
return join('[lib]', 'misc', basename(object_name))
123124
return object_name
124125

125126
else:
126127
test_re_obj_name = re.match(self.RE_LIBRARY_OBJECT, line)
127128

128129
if test_re_obj_name:
129-
object_name = os.path.join(test_re_obj_name.group(1),
130-
test_re_obj_name.group(2))
131-
return os.path.join('[lib]', object_name)
130+
return join('[lib]', test_re_obj_name.group(2),
131+
test_re_obj_name.group(3))
132132
else:
133133
print "Unknown object name found in GCC map file: %s" % line
134134
return '[misc]'
@@ -183,14 +183,14 @@ def parse_mapfile(self, file_desc):
183183
object_name, object_size = self.parse_section(line)
184184
self.module_add(object_name, object_size, current_section)
185185

186-
common_prefix = os.path.dirname(os.path.commonprefix([
186+
common_prefix = dirname(commonprefix([
187187
o for o in self.modules.keys() if (o.endswith(".o") and not o.startswith("[lib]"))]))
188188
new_modules = {}
189189
for name, stats in self.modules.items():
190190
if name.startswith("[lib]"):
191191
new_modules[name] = stats
192192
elif name.endswith(".o"):
193-
new_modules[os.path.relpath(name, common_prefix)] = stats
193+
new_modules[relpath(name, common_prefix)] = stats
194194
else:
195195
new_modules[name] = stats
196196
return new_modules
@@ -213,9 +213,7 @@ def parse_object_name(self, line):
213213
else:
214214
is_obj = re.match(self.RE_OBJECT, line)
215215
if is_obj:
216-
object_name = os.path.join(os.path.basename(is_obj.group(1)),
217-
is_obj.group(3))
218-
return os.path.join('[lib]', object_name)
216+
return join('[lib]', basename(is_obj.group(1)), is_obj.group(3))
219217
else:
220218
print "Malformed input found when parsing ARMCC map: %s" % line
221219
return '[misc]'
@@ -276,14 +274,14 @@ def parse_mapfile(self, file_desc):
276274
for line in infile:
277275
self.module_add(*self.parse_section(line))
278276

279-
common_prefix = os.path.dirname(os.path.commonprefix([
277+
common_prefix = dirname(commonprefix([
280278
o for o in self.modules.keys() if (o.endswith(".o") and o != "anon$$obj.o" and not o.startswith("[lib]"))]))
281279
new_modules = {}
282280
for name, stats in self.modules.items():
283281
if name == "anon$$obj.o" or name.startswith("[lib]"):
284282
new_modules[name] = stats
285283
elif name.endswith(".o"):
286-
new_modules[os.path.relpath(name, common_prefix)] = stats
284+
new_modules[relpath(name, common_prefix)] = stats
287285
else:
288286
new_modules[name] = stats
289287
return new_modules
@@ -405,10 +403,10 @@ def parse_command_line(self, lines):
405403
for arg in line.split(" "):
406404
arg = arg.rstrip(" \n")
407405
if (not arg.startswith("-")) and arg.endswith(".o"):
408-
self.cmd_modules[os.path.basename(arg)] = arg
406+
self.cmd_modules[basename(arg)] = arg
409407

410-
common_prefix = os.path.dirname(os.path.commonprefix(self.cmd_modules.values()))
411-
self.cmd_modules = {s: os.path.relpath(f, common_prefix)
408+
common_prefix = dirname(commonprefix(self.cmd_modules.values()))
409+
self.cmd_modules = {s: relpath(f, common_prefix)
412410
for s, f in self.cmd_modules.items()}
413411

414412
def parse_mapfile(self, file_desc):
@@ -440,7 +438,7 @@ def parse_mapfile(self, file_desc):
440438
object_name = self.check_new_object_lib(line)
441439

442440
if object_name and current_library:
443-
temp = os.path.join('[lib]', current_library, object_name)
441+
temp = join('[lib]', current_library, object_name)
444442
self.module_replace(object_name, temp)
445443
return self.modules
446444

@@ -497,10 +495,10 @@ def reduce_depth(self, depth):
497495
else:
498496
self.short_modules = dict()
499497
for module_name, v in self.modules.items():
500-
split_name = module_name.split(os.sep)
498+
split_name = module_name.split(sep)
501499
if split_name[0] == '':
502500
split_name = split_name[1:]
503-
new_name = os.path.join(*split_name[:depth])
501+
new_name = join(*split_name[:depth])
504502
self.short_modules.setdefault(new_name, {})
505503
for section_idx, value in v.items():
506504
self.short_modules[new_name].setdefault(section_idx, 0)
@@ -526,7 +524,7 @@ def generate_output(self, export_format, depth, file_output=None):
526524
if file_output:
527525
file_desc = open(file_output, 'wb')
528526
else:
529-
file_desc = sys.stdout
527+
file_desc = stdout
530528
except IOError as error:
531529
print "I/O error({0}): {1}".format(error.errno, error.strerror)
532530
return False
@@ -536,7 +534,7 @@ def generate_output(self, export_format, depth, file_output=None):
536534
'table': self.generate_table}[export_format]
537535
output = to_call(file_desc)
538536

539-
if file_desc is not sys.stdout:
537+
if file_desc is not stdout:
540538
file_desc.close()
541539

542540
return output
@@ -678,7 +676,7 @@ def main():
678676
version = '0.4.0'
679677

680678
# Parser handling
681-
parser = argparse.ArgumentParser(
679+
parser = ArgumentParser(
682680
description="Memory Map File Analyser for ARM mbed\nversion %s" %
683681
version)
684682

@@ -709,9 +707,9 @@ def main():
709707
parser.add_argument('-v', '--version', action='version', version=version)
710708

711709
# Parse/run command
712-
if len(sys.argv) <= 1:
710+
if len(argv) <= 1:
713711
parser.print_help()
714-
sys.exit(1)
712+
exit(1)
715713

716714
args = parser.parse_args()
717715

@@ -721,7 +719,7 @@ def main():
721719
# Parse and decode a map file
722720
if args.file and args.toolchain:
723721
if memap.parse(args.file, args.toolchain) is False:
724-
sys.exit(0)
722+
exit(0)
725723

726724
if args.depth is None:
727725
depth = 2 # default depth level
@@ -739,7 +737,7 @@ def main():
739737
if args.export == 'table' and returned_string:
740738
print returned_string
741739

742-
sys.exit(0)
740+
exit(0)
743741

744742
if __name__ == "__main__":
745743
main()

0 commit comments

Comments
 (0)