Skip to content

Commit

Permalink
Removed the command line interface
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Mar 29, 2016
1 parent 52a9084 commit b9d9bce
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 277 deletions.
226 changes: 1 addition & 225 deletions bincopy.py
Expand Up @@ -3,13 +3,12 @@
# Mangling of various file formats that conveys binary
# information (Motorola S-Record, Intel HEX and binary files).

import sys
import binascii
import io
import string

__author__ = 'Erik Moqvist'
__version__ = '1.2.0'
__version__ = '2.0.0'

DEFAULT_WORD_SIZE = 8

Expand Down Expand Up @@ -607,226 +606,3 @@ def __iadd__(self, other):

def __str__(self):
return self.segments.__str__()


def main(args, stdout=sys.stdout, stderr=sys.stderr):
class FileArgs(object):

def __init__(self):
self.type_ = 'srec'
self.offset = None
self.output = False
self.filename = None
self.address_length = 32
self.exclude = None

def parse_file_args(fargs, args, i):
while ((i < len(args)) and
args[i].startswith('--') and
not (args[i] == '--output') and
not (args[i] == '--stdin')):
if args[i] == '--ihex':
fargs.type_ = 'ihex'
elif args[i] == '--binary':
fargs.type_ = 'binary'
elif args[i] == '--offset':
fargs.offset = int(args[i+1])
i += 1
elif args[i] == '--address-length':
fargs.address_length = int(args[i+1])
i += 1
elif args[i] == '--exclude':
minimum = int(args[i+1], 0)
maximum = int(args[i+2], 0)
fargs.exclude = (minimum, maximum)
i += 2

i += 1

return i

def help():
stdout.write('USAGE\n')
stdout.write('\n')
stdout.write(' bincopy.py { cat, info, --help } ...\n')
stdout.write(' [ --word-size <number of bits> ] ...\n')
stdout.write(' ( { <file>, --stdin } [ --ihex | --binary ] [ --offset <n> ] ...\n')
stdout.write(' [ --exclude <minimum> <maximum> ] )+ ...\n')
stdout.write(' [ --output [ <file> ] [ --ihex | --binary ] [ --offset <n> ] ...\n')
stdout.write(' [ --exclude <minimum> <maximum> ] [ --address-length <bits> ] ]\n')
stdout.write('\n')
stdout.write('DESCRIPTION\n')
stdout.write('\n')
stdout.write(' Mangling of various file formats that conveys binary information (Motorola S-Record,\n')
stdout.write(' Intel HEX and binary files).\n')
stdout.write('\n')
stdout.write('EXAMPLES\n')
stdout.write('\n')
stdout.write(' help:\n')
stdout.write(' $ bincopy.py --help\n')
stdout.write('\n')
stdout.write(' cat:\n')
stdout.write(' $ bincopy.py cat foo.s19 bar.s19\n')
stdout.write(' $ bincopy.py cat foo.s19 bar.hex --ihex fie.bin --binary --offset 512\n')
stdout.write(' $ bincopy.py cat foo.s19 bar.hex --ihex --output fie.s19\n')
stdout.write(' $ bincopy.py cat foo.s19 --exclude 0 100 bar.hex --ihex --output fie.hex --ihex\n')
stdout.write('\n')
stdout.write(' info:\n')
stdout.write(' $ bincopy.py info foo.s19\n')
stdout.write('\n')
stdout.write('AUTHOR\n\n')
stdout.write(' Erik Moqvist\n')

def parse_args(args):
i = 0
word_size = DEFAULT_WORD_SIZE

if args[i] == '--word-size':
word_size = int(args[i+1])
i += 2

file_args_list = []

while i < len(args):
file_args = FileArgs()

if args[i] == '--output':
file_args.output = True
i += 1

if (i < len(args)) and not args[i].startswith('--'):
file_args.filename = args[i]
i += 1
elif args[i] == '--stdin':
i += 1
else:
file_args.filename = args[i]
i += 1

i = parse_file_args(file_args, args, i)
file_args_list.append(file_args)

return word_size, file_args_list

def cmd_cat(args):
word_size, file_args_list = parse_args(args)
file_all = File(word_size)
outputted = False

for file_args in file_args_list:
f = File(word_size)

if not file_args.output:
if file_args.filename:
if file_args.type_ == 'srec':
with open(file_args.filename, 'r') as fin:
f.add_srec(fin)
elif file_args.type_ == 'ihex':
with open(file_args.filename, 'r') as fin:
f.add_ihex(fin)
elif file_args.type_ == 'binary':
with open(file_args.filename, 'rb') as fin:
f.add_binary(fin, (file_args.offset
if file_args.offset else 0))
else:
if file_args.type_ == 'srec':
f.add_srec(sys.stdin)
elif file_args.type_ == 'ihex':
f.add_ihex(sys.stdin)
elif file_args.type_ == 'binary':
f.add_binary(sys.stdin, (file_args.offset
if file_args.offset else 0))

if file_args.exclude:
f.exclude(file_args.exclude[0], file_args.exclude[1])

file_all += f
else:
outputted = True

if file_args.exclude:
file_all.exclude(file_args.exclude[0],
file_args.exclude[1])

if file_args.filename:
if file_args.type_ == 'srec':
data = file_all.as_srec(address_length=file_args.address_length)
with open(file_args.filename, 'w') as fout:
fout.write(data)
elif file_args.type_ == 'ihex':
data = file_all.as_ihex()
with open(file_args.filename, 'w') as fout:
fout.write(data)
elif file_args.type_ == 'binary':
data = file_all.as_binary(file_args.offset
if file_args.offset else 0)
with open(file_args.filename, 'wb') as fout:
fout.write(data)
else:
if file_args.type_ == 'srec':
stdout.write(file_all.as_srec(address_length=file_args.address_length))
elif file_args.type_ == 'ihex':
stdout.write(file_all.as_ihex())
elif file_args.type_ == 'binary':
stdout.write(file_all.as_binary(file_args.offset
if file_args.offset
else 0))

if not outputted:
stdout.write(file_all.as_srec())

def cmd_info(args):
word_size, file_args_list = parse_args(args)
info_list = []

for file_args in file_args_list:
f = File(word_size)

if file_args.output:
raise Error('bad option --output')

if file_args.filename:
if file_args.type_ == 'srec':
with open(file_args.filename, 'r') as fin:
f.add_srec(fin)
elif file_args.type_ == 'ihex':
with open(file_args.filename, 'r') as fin:
f.add_ihex(fin)
elif file_args.type_ == 'binary':
with open(file_args.filename, 'rb') as fin:
f.add_binary(fin, (file_args.offset
if file_args.offset else 0))
else:
if file_args.type_ == 'srec':
f.add_srec(sys.stdin)
elif file_args.type_ == 'ihex':
f.add_ihex(sys.stdin)
elif file_args.type_ == 'binary':
f.add_binary(sys.stdin, (file_args.offset
if file_args.offset else 0))

if file_args.exclude:
f.exclude(file_args.exclude[0], file_args.exclude[1])
info_list.append(f.info(file_args.type_, file_args.filename))

stdout.write('\n'.join(info_list))

if (len(args) == 0) or (args[0] in ['--help', 'help']):
help()
elif args[0] == 'cat':
cmd_cat(args[1:])
elif args[0] == 'info':
cmd_info(args[1:])
else:
stdout.write('error: bad argument %s\n' % args[0])
sys.exit(1)


def entry():
main(sys.argv[1:])


# See help() function for details or type 'python bincopy.py --help'
# on the command line
if __name__ == '__main__':
entry()
1 change: 0 additions & 1 deletion setup.py
Expand Up @@ -27,5 +27,4 @@
'.hex'],
url='https://github.com/eerimoq/bincopy',
py_modules=['bincopy'],
entry_points={'console_scripts': ['bincopy=bincopy:entry']},
test_suite="tests")
53 changes: 2 additions & 51 deletions tests/test_bincopy.py
@@ -1,5 +1,4 @@
import unittest
import io
import bincopy


Expand Down Expand Up @@ -120,57 +119,9 @@ def test_iter_segments(self):
f.add_srec(fin)
i = 0
for begin, end, data in f.iter_segments():
del begin, end, data
i += 1
self.assertEqual(1, 1)

def test_cmd_cat(self):
stdout = io.StringIO()
bincopy.main(['cat',
'tests/files/in.s19',
'tests/files/in.hex',
'--ihex',
'tests/files/binary1.bin',
'--binary',
'--offset', '1024',
'--output',
'--address-length', '16'], stdout)
stdout.seek(0)
with open('tests/files/out.s19') as fin:
self.assertEqual(stdout.read(), fin.read())

def test_cmd_info(self):
stdout = io.StringIO()
bincopy.main(['info',
'tests/files/in.s19',
'tests/files/in.hex',
'--ihex',
'tests/files/binary1.bin',
'--binary',
'--offset', '1024'], stdout)
stdout.seek(0)
with open('tests/files/info.txt') as fin:
self.assertEqual(stdout.read(), fin.read())

def test_cmd_info_exclude_0x0_0x103(self):
stdout = io.StringIO()
bincopy.main(['info',
'tests/files/out.s19',
'--exclude', '0', '0x103'], stdout)
stdout.seek(0)
with open('tests/files/info_exclude_0_103.txt') as fin:
self.assertEqual(stdout.read(), fin.read())

def test_cmd_info_exclude_0x107_0x401(self):
stdout = io.StringIO()
bincopy.main(['info',
'tests/files/out.s19',
'--exclude', '0x107', '0x401'], stdout)
stdout.seek(0)
with open('tests/files/info_exclude_0x107_0x401.txt') as fin:
self.assertEqual(stdout.read(), fin.read())

def test_help(self):
bincopy.main(['--help'])
self.assertEqual(i, 1)

def test_ihex_crc(self):
self.assertEqual(bincopy.crc_ihex('0300300002337a'), 0x1e)
Expand Down

0 comments on commit b9d9bce

Please sign in to comment.