From 49ee84398e17b991f562169c23b4c6eefb7c8294 Mon Sep 17 00:00:00 2001 From: Erik Moqvist Date: Tue, 25 Jul 2017 15:41:49 +0200 Subject: [PATCH] bincopy command line script with info subcommand to print general information about given file(s). --- bincopy.py | 58 ++++++++++++++++++++++++++++++++++++++----- setup.py | 5 +++- tests/test_bincopy.py | 21 ++++++++-------- 3 files changed, 67 insertions(+), 17 deletions(-) diff --git a/bincopy.py b/bincopy.py index f65a68b..d1830f1 100755 --- a/bincopy.py +++ b/bincopy.py @@ -3,10 +3,12 @@ """ -from __future__ import print_function +from __future__ import print_function, division import binascii import string +import sys +import argparse try: from StringIO import StringIO @@ -15,7 +17,7 @@ __author__ = 'Erik Moqvist' -__version__ = '7.2.0' +__version__ = '7.3.0' DEFAULT_WORD_SIZE_BITS = 8 @@ -869,18 +871,20 @@ def info(self): else: header += '\\x%02x' % ord(b) - info += 'header: "%s"\n' % header + info += 'Header: "%s"\n' % header if self.execution_start_address is not None: - info += ('execution start address: 0x%08x\n' + info += ('Execution start address: 0x%08x\n' % self.execution_start_address) - info += 'data:\n' + info += 'Data address ranges:\n' for minimum_address, maximum_address, _ in self.iter_segments(): minimum_address //= self.word_size_bytes maximum_address //= self.word_size_bytes - info += ' 0x%08x - 0x%08x\n' % (minimum_address, maximum_address) + info += ' 0x%08x - 0x%08x\n' % ( + minimum_address, + maximum_address) return info @@ -899,3 +903,45 @@ def __iadd__(self, other): def __str__(self): return self.segments.__str__() + + +def _do_info(args): + for binfile in args.binfile: + f = BinFile() + f.add_file(binfile) + print(f.info()) + + +def _main(): + parser = argparse.ArgumentParser( + description='Various binary file format utilities.') + + parser.add_argument('-d', '--debug', action='store_true') + parser.add_argument('--version', + action='version', + version=__version__, + help='Print version information and exit.') + + # Workaround to make the subparser required in Python 3. + subparsers = parser.add_subparsers(title='subcommands', + dest='subcommand') + subparsers.required = True + + # The 'info' subparser. + info_parser = subparsers.add_parser( + 'info', + description='Print general information about given file(s).') + info_parser.add_argument('binfile', + nargs='+', + help='One or more binary format files.') + info_parser.set_defaults(func=_do_info) + + args = parser.parse_args() + + if args.debug: + args.func(args) + else: + try: + args.func(args) + except BaseException as e: + sys.exit(str(e)) diff --git a/setup.py b/setup.py index 7c78de1..188bf01 100755 --- a/setup.py +++ b/setup.py @@ -27,4 +27,7 @@ '.hex'], url='https://github.com/eerimoq/bincopy', py_modules=['bincopy'], - test_suite="tests") + test_suite="tests", + entry_points = { + 'console_scripts': ['bincopy=bincopy:_main'] + }) diff --git a/tests/test_bincopy.py b/tests/test_bincopy.py index 73dffb3..906891f 100644 --- a/tests/test_bincopy.py +++ b/tests/test_bincopy.py @@ -333,16 +333,17 @@ def test_info(self): binfile = bincopy.BinFile() with open('tests/files/empty_main.s19', 'r') as fin: binfile.add_srec(fin.read()) - self.assertEqual(binfile.info(), """header: "bincopy/empty_main.s19" -execution start address: 0x00400400 -data: - 0x00400238 - 0x004002b4 - 0x004002b8 - 0x0040033e - 0x00400340 - 0x004003c2 - 0x004003d0 - 0x00400572 - 0x00400574 - 0x0040057d - 0x00400580 - 0x004006ac - 0x00600e10 - 0x00601038 + self.assertEqual(binfile.info(), + """Header: "bincopy/empty_main.s19" +Execution start address: 0x00400400 +Data address ranges: + 0x00400238 - 0x004002b4 + 0x004002b8 - 0x0040033e + 0x00400340 - 0x004003c2 + 0x004003d0 - 0x00400572 + 0x00400574 - 0x0040057d + 0x00400580 - 0x004006ac + 0x00600e10 - 0x00601038 """) def test_execution_start_address(self):