annotate python/hexutil.py @ 260:b2f94b4951f1

Nice-up
author Windel Bouwman
date Tue, 06 Aug 2013 18:29:53 +0200
parents f254b87258e6
children 7b38782ed496
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
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
21 p = subparsers.add_parser('new', help='create a hexfile')
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
22 p.add_argument('hexfile', type=argparse.FileType('w'))
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
23 p.add_argument('address', type=hex2int, help="hex address of the data")
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
24 p.add_argument('datafile', type=argparse.FileType('rb'), help='binary file to add')
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
25
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
26 p = subparsers.add_parser('merge', help='merge two hexfiles into a third')
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
27 p.add_argument('hexfile1', type=argparse.FileType('r'), help="hexfile 1")
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
28 p.add_argument('hexfile2', type=argparse.FileType('r'), help="hexfile 2")
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
29 p.add_argument('rhexfile', type=argparse.FileType('w'), help="resulting hexfile")
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
30
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
31 def main(args):
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
32 if args.command == 'info':
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
33 hf = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
34 hf.load(args.hexfile)
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
35 print(hf)
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
36 for region in hf.regions:
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
37 print(region)
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
38 elif args.command == 'new':
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
39 hf = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
40 data = args.datafile.read()
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
41 hf.addRegion(args.address, data)
246
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
42 hf.save(args.hexfile)
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
43 elif args.command == 'merge':
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
44 hf = HexFile()
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
45 hf.load(args.hexfile1)
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
46 hf2 = HexFile()
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
47 hf2.load(args.hexfile2)
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
48 hf.merge(hf2)
f254b87258e6 Added hexfile to zcc
Windel Bouwman
parents: 244
diff changeset
49 hf.save(args.rhexfile)
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
50 else:
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
51 raise NotImplementedError()
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
52
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
53 if __name__ == '__main__':
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
54 args = parser.parse_args()
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
55 if not args.command:
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
56 parser.print_usage()
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
57 sys.exit(1)
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
58 main(args)
58155c7c4a8e Add hexutil
Windel Bouwman
parents:
diff changeset
59