annotate python/hexutil.py @ 244:58155c7c4a8e

Add hexutil
author Windel Bouwman
date Wed, 24 Jul 2013 19:47:13 +0200
parents
children f254b87258e6
rev   line source
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
1 #!/usr/bin/python
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
2
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
3 import sys
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
4 import argparse
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
5 from hexfile import HexFile
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
6
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
7 def hex2int(s):
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
8 if s.startswith('0x'):
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
9 s = s[2:]
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
10 return int(s, 16)
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
11 raise ValueError('Hexadecimal value must begin with 0x')
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
12
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
13 parser = argparse.ArgumentParser(
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
14 description='hexfile manipulation tool by Windel Bouwman')
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
15 subparsers = parser.add_subparsers(title='commands',
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
16 description='possible commands', dest='command')
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
17
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
18 p = subparsers.add_parser('info', help='dump info about hexfile')
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
19 p.add_argument('hexfile', type=argparse.FileType('r'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
20
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
21 p = subparsers.add_parser('new', help='create empty hexfile')
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
22 p.add_argument('hexfile', type=argparse.FileType('x'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
23
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
24 p = subparsers.add_parser('add', help='add binary data from file to hexfile')
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
25 p.add_argument('hexfile', type=argparse.FileType('r+'), help="the hexfile to add the data to")
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
26 p.add_argument('address', type=hex2int, help="hex address")
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
27 p.add_argument('datafile', type=argparse.FileType('rb'), help='binary file to add')
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
28
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
29 def main(args):
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
30 if args.command == 'info':
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
31 hf = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
32 hf.load(args.hexfile)
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
33 print(hf)
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
34 for region in hf.regions:
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
35 print(region)
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
36 elif args.command == 'new':
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
37 hf = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
38 hf.save(args.hexfile)
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
39 elif args.command == 'add':
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
40 hf = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
41 hf.load(args.hexfile)
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
42 data = args.datafile.read()
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
43 hf.addRegion(args.address, data)
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
44 else:
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
45 raise NotImplementedError()
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
46
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
47 if __name__ == '__main__':
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
48 args = parser.parse_args()
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
49 if not args.command:
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
50 parser.print_usage()
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
51 sys.exit(1)
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
52 main(args)
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
53