Mercurial > lcfOS
view python/hexutil.py @ 244:58155c7c4a8e
Add hexutil
author | Windel Bouwman |
---|---|
date | Wed, 24 Jul 2013 19:47:13 +0200 |
parents | |
children | f254b87258e6 |
line wrap: on
line source
#!/usr/bin/python import sys import argparse from hexfile import HexFile def hex2int(s): if s.startswith('0x'): s = s[2:] return int(s, 16) raise ValueError('Hexadecimal value must begin with 0x') parser = argparse.ArgumentParser( description='hexfile manipulation tool by Windel Bouwman') subparsers = parser.add_subparsers(title='commands', description='possible commands', dest='command') p = subparsers.add_parser('info', help='dump info about hexfile') p.add_argument('hexfile', type=argparse.FileType('r')) p = subparsers.add_parser('new', help='create empty hexfile') p.add_argument('hexfile', type=argparse.FileType('x')) p = subparsers.add_parser('add', help='add binary data from file to hexfile') p.add_argument('hexfile', type=argparse.FileType('r+'), help="the hexfile to add the data to") p.add_argument('address', type=hex2int, help="hex address") p.add_argument('datafile', type=argparse.FileType('rb'), help='binary file to add') def main(args): if args.command == 'info': hf = HexFile() hf.load(args.hexfile) print(hf) for region in hf.regions: print(region) elif args.command == 'new': hf = HexFile() hf.save(args.hexfile) elif args.command == 'add': hf = HexFile() hf.load(args.hexfile) data = args.datafile.read() hf.addRegion(args.address, data) else: raise NotImplementedError() if __name__ == '__main__': args = parser.parse_args() if not args.command: parser.print_usage() sys.exit(1) main(args)