size_report: convert to use argparser as optparse has been deprecated

Do a straight forward conversion from optparse to argparser.  All
of our other python scripts use argparser already.

Signed-off-by: Kumar Gala <kumar.gala@gmail.com>
This commit is contained in:
Kumar Gala 2018-10-28 13:09:58 +00:00 committed by Anas Nashif
commit a4173329d4

View file

@ -9,7 +9,7 @@
import os import os
import re import re
from optparse import OptionParser import argparse
import subprocess import subprocess
import json import json
import operator import operator
@ -395,48 +395,52 @@ def print_tree(data, total, depth):
def main(): def main():
parser = OptionParser()
parser.add_option("-d", "--depth", dest="depth", type="int", parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("-d", "--depth", dest="depth", type=int,
help="How deep should we go into the tree", metavar="DEPTH") help="How deep should we go into the tree", metavar="DEPTH")
parser.add_option("-o", "--outdir", dest="outdir", parser.add_argument("-o", "--outdir", dest="outdir",
help="read files from directory OUT", metavar="OUT") help="read files from directory OUT", metavar="OUT")
parser.add_option("-k", "--kernel-name", dest="binary", default="zephyr", parser.add_argument("-k", "--kernel-name", dest="binary", default="zephyr",
help="kernel binary name") help="kernel binary name")
parser.add_option("-r", "--ram", parser.add_argument("-r", "--ram",
action="store_true", dest="ram", default=False, action="store_true", dest="ram", default=False,
help="print RAM statistics") help="print RAM statistics")
parser.add_option("-F", "--rom", parser.add_argument("-F", "--rom",
action="store_true", dest="rom", default=False, action="store_true", dest="rom", default=False,
help="print ROM statistics") help="print ROM statistics")
parser.add_option("-s", "--objdump", type="string", dest="bin_objdump", parser.add_argument("-s", "--objdump", dest="bin_objdump",
help="Path to the GNU binary utility objdump") help="Path to the GNU binary utility objdump")
parser.add_option("-c", "--objcopy", type="string", dest="bin_objcopy", parser.add_argument("-c", "--objcopy", dest="bin_objcopy",
help="Path to the GNU binary utility objcopy") help="Path to the GNU binary utility objcopy")
parser.add_option("-n", "--nm", type="string", dest="bin_nm", parser.add_argument("-n", "--nm", dest="bin_nm",
help="Path to the GNU binary utility nm") help="Path to the GNU binary utility nm")
(options, args) = parser.parse_args() args = parser.parse_args()
bin_file = os.path.join(options.outdir, options.binary + ".bin") bin_file = os.path.join(args.outdir, args.binary + ".bin")
stat_file = os.path.join(options.outdir, options.binary + ".stat") stat_file = os.path.join(args.outdir, args.binary + ".stat")
elf_file = os.path.join(options.outdir, options.binary + ".elf") elf_file = os.path.join(args.outdir, args.binary + ".elf")
if not os.path.exists(bin_file): if not os.path.exists(bin_file):
FNULL = open(os.devnull, 'w') FNULL = open(os.devnull, 'w')
subprocess.call([options.bin_objcopy,"-S", "-Obinary", "-R", ".comment", "-R", subprocess.call([args.bin_objcopy,"-S", "-Obinary", "-R", ".comment", "-R",
"COMMON", "-R", ".eh_frame", elf_file, bin_file], "COMMON", "-R", ".eh_frame", elf_file, bin_file],
stdout=FNULL, stderr=subprocess.STDOUT) stdout=FNULL, stderr=subprocess.STDOUT)
if options.outdir and os.path.exists(elf_file): if args.outdir and os.path.exists(elf_file):
fp = get_footprint_from_bin_and_statfile(bin_file, stat_file, 0, 0) fp = get_footprint_from_bin_and_statfile(bin_file, stat_file, 0, 0)
base = os.environ['ZEPHYR_BASE'] base = os.environ['ZEPHYR_BASE']
ram, data = generate_target_memory_section( ram, data = generate_target_memory_section(
options.bin_objdump, options.bin_nm, options.outdir, options.binary, args.bin_objdump, args.bin_nm, args.outdir, args.binary,
base + '/', None) base + '/', None)
if options.rom: if args.rom:
print_tree(data, fp['total_flash'], options.depth) print_tree(data, fp['total_flash'], args.depth)
if options.ram: if args.ram:
print_tree(ram, fp['total_ram'], options.depth) print_tree(ram, fp['total_ram'], args.depth)
else: else:
print("%s does not exist." % (elf_file)) print("%s does not exist." % (elf_file))