2
2
3
3
"""Memory Map File Analyser for ARM mbed"""
4
4
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
8
9
import re
9
10
import csv
10
11
import json
11
- import argparse
12
+ from argparse import ArgumentParser
12
13
from copy import deepcopy
13
14
from prettytable import PrettyTable
14
15
18
19
19
20
class _Parser (object ):
20
21
"""Internal interface for parsing"""
21
- __metaclass__ = abc . ABCMeta
22
+ __metaclass__ = ABCMeta
22
23
SECTIONS = ('.text' , '.data' , '.bss' , '.heap' , '.stack' )
23
24
MISC_FLASH_SECTIONS = ('.interrupts' , '.flash_config' )
24
25
OTHER_SECTIONS = ('.interrupts_ram' , '.init' , '.ARM.extab' ,
@@ -45,7 +46,7 @@ def module_add(self, object_name, size, section):
45
46
self .modules [object_name ][section ] += size
46
47
return
47
48
48
- obj_split = os . sep + os . path . basename (object_name )
49
+ obj_split = sep + basename (object_name )
49
50
for module_path , contents in self .modules .items ():
50
51
if module_path .endswith (obj_split ) or module_path == object_name :
51
52
contents .setdefault (section , 0 )
@@ -62,7 +63,7 @@ def module_replace(self, old_object, new_object):
62
63
self .modules [new_object ] = self .modules [old_object ]
63
64
del self .modules [old_object ]
64
65
65
- @abc . abstractmethod
66
+ @abstractmethod
66
67
def parse_mapfile (self , mapfile ):
67
68
"""Parse a given file object pointing to a map file
68
69
@@ -77,7 +78,7 @@ def parse_mapfile(self, mapfile):
77
78
78
79
class _GccParser (_Parser ):
79
80
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)\))$' )
81
82
RE_STD_SECTION = re .compile (r'^\s+.*0x(\w{8,16})\s+0x(\w+)\s(.+)$' )
82
83
RE_FILL_SECTION = re .compile (r'^\s*\*fill\*\s+0x(\w{8,16})\s+0x(\w+).*$' )
83
84
@@ -119,16 +120,15 @@ def parse_object_name(self, line):
119
120
120
121
# corner case: certain objects are provided by the GCC toolchain
121
122
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 ))
123
124
return object_name
124
125
125
126
else :
126
127
test_re_obj_name = re .match (self .RE_LIBRARY_OBJECT , line )
127
128
128
129
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 ))
132
132
else :
133
133
print "Unknown object name found in GCC map file: %s" % line
134
134
return '[misc]'
@@ -183,14 +183,14 @@ def parse_mapfile(self, file_desc):
183
183
object_name , object_size = self .parse_section (line )
184
184
self .module_add (object_name , object_size , current_section )
185
185
186
- common_prefix = os . path . dirname (os . path . commonprefix ([
186
+ common_prefix = dirname (commonprefix ([
187
187
o for o in self .modules .keys () if (o .endswith (".o" ) and not o .startswith ("[lib]" ))]))
188
188
new_modules = {}
189
189
for name , stats in self .modules .items ():
190
190
if name .startswith ("[lib]" ):
191
191
new_modules [name ] = stats
192
192
elif name .endswith (".o" ):
193
- new_modules [os . path . relpath (name , common_prefix )] = stats
193
+ new_modules [relpath (name , common_prefix )] = stats
194
194
else :
195
195
new_modules [name ] = stats
196
196
return new_modules
@@ -213,9 +213,7 @@ def parse_object_name(self, line):
213
213
else :
214
214
is_obj = re .match (self .RE_OBJECT , line )
215
215
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 ))
219
217
else :
220
218
print "Malformed input found when parsing ARMCC map: %s" % line
221
219
return '[misc]'
@@ -276,14 +274,14 @@ def parse_mapfile(self, file_desc):
276
274
for line in infile :
277
275
self .module_add (* self .parse_section (line ))
278
276
279
- common_prefix = os . path . dirname (os . path . commonprefix ([
277
+ common_prefix = dirname (commonprefix ([
280
278
o for o in self .modules .keys () if (o .endswith (".o" ) and o != "anon$$obj.o" and not o .startswith ("[lib]" ))]))
281
279
new_modules = {}
282
280
for name , stats in self .modules .items ():
283
281
if name == "anon$$obj.o" or name .startswith ("[lib]" ):
284
282
new_modules [name ] = stats
285
283
elif name .endswith (".o" ):
286
- new_modules [os . path . relpath (name , common_prefix )] = stats
284
+ new_modules [relpath (name , common_prefix )] = stats
287
285
else :
288
286
new_modules [name ] = stats
289
287
return new_modules
@@ -405,10 +403,10 @@ def parse_command_line(self, lines):
405
403
for arg in line .split (" " ):
406
404
arg = arg .rstrip (" \n " )
407
405
if (not arg .startswith ("-" )) and arg .endswith (".o" ):
408
- self .cmd_modules [os . path . basename (arg )] = arg
406
+ self .cmd_modules [basename (arg )] = arg
409
407
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 )
412
410
for s , f in self .cmd_modules .items ()}
413
411
414
412
def parse_mapfile (self , file_desc ):
@@ -440,7 +438,7 @@ def parse_mapfile(self, file_desc):
440
438
object_name = self .check_new_object_lib (line )
441
439
442
440
if object_name and current_library :
443
- temp = os . path . join ('[lib]' , current_library , object_name )
441
+ temp = join ('[lib]' , current_library , object_name )
444
442
self .module_replace (object_name , temp )
445
443
return self .modules
446
444
@@ -497,10 +495,10 @@ def reduce_depth(self, depth):
497
495
else :
498
496
self .short_modules = dict ()
499
497
for module_name , v in self .modules .items ():
500
- split_name = module_name .split (os . sep )
498
+ split_name = module_name .split (sep )
501
499
if split_name [0 ] == '' :
502
500
split_name = split_name [1 :]
503
- new_name = os . path . join (* split_name [:depth ])
501
+ new_name = join (* split_name [:depth ])
504
502
self .short_modules .setdefault (new_name , {})
505
503
for section_idx , value in v .items ():
506
504
self .short_modules [new_name ].setdefault (section_idx , 0 )
@@ -526,7 +524,7 @@ def generate_output(self, export_format, depth, file_output=None):
526
524
if file_output :
527
525
file_desc = open (file_output , 'wb' )
528
526
else :
529
- file_desc = sys . stdout
527
+ file_desc = stdout
530
528
except IOError as error :
531
529
print "I/O error({0}): {1}" .format (error .errno , error .strerror )
532
530
return False
@@ -536,7 +534,7 @@ def generate_output(self, export_format, depth, file_output=None):
536
534
'table' : self .generate_table }[export_format ]
537
535
output = to_call (file_desc )
538
536
539
- if file_desc is not sys . stdout :
537
+ if file_desc is not stdout :
540
538
file_desc .close ()
541
539
542
540
return output
@@ -678,7 +676,7 @@ def main():
678
676
version = '0.4.0'
679
677
680
678
# Parser handling
681
- parser = argparse . ArgumentParser (
679
+ parser = ArgumentParser (
682
680
description = "Memory Map File Analyser for ARM mbed\n version %s" %
683
681
version )
684
682
@@ -709,9 +707,9 @@ def main():
709
707
parser .add_argument ('-v' , '--version' , action = 'version' , version = version )
710
708
711
709
# Parse/run command
712
- if len (sys . argv ) <= 1 :
710
+ if len (argv ) <= 1 :
713
711
parser .print_help ()
714
- sys . exit (1 )
712
+ exit (1 )
715
713
716
714
args = parser .parse_args ()
717
715
@@ -721,7 +719,7 @@ def main():
721
719
# Parse and decode a map file
722
720
if args .file and args .toolchain :
723
721
if memap .parse (args .file , args .toolchain ) is False :
724
- sys . exit (0 )
722
+ exit (0 )
725
723
726
724
if args .depth is None :
727
725
depth = 2 # default depth level
@@ -739,7 +737,7 @@ def main():
739
737
if args .export == 'table' and returned_string :
740
738
print returned_string
741
739
742
- sys . exit (0 )
740
+ exit (0 )
743
741
744
742
if __name__ == "__main__" :
745
743
main ()
0 commit comments