Skip to content

Commit

Permalink
bincopy command line script with info subcommand to print general inf…
Browse files Browse the repository at this point in the history
…ormation about given file(s).
  • Loading branch information
eerimoq committed Jul 25, 2017
1 parent 24bccfe commit 49ee843
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 17 deletions.
58 changes: 52 additions & 6 deletions bincopy.py
Expand Up @@ -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
Expand All @@ -15,7 +17,7 @@


__author__ = 'Erik Moqvist'
__version__ = '7.2.0'
__version__ = '7.3.0'


DEFAULT_WORD_SIZE_BITS = 8
Expand Down Expand Up @@ -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

Expand All @@ -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))
5 changes: 4 additions & 1 deletion setup.py
Expand Up @@ -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']
})
21 changes: 11 additions & 10 deletions tests/test_bincopy.py
Expand Up @@ -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):
Expand Down

0 comments on commit 49ee843

Please sign in to comment.