3 #Copyright (C) 2012 jaseg <s@jaseg.de>
5 #This program is free software; you can redistribute it and/or
6 #modify it under the terms of the GNU General Public License
7 #version 3 as published by the Free Software Foundation.
16 """Generate firmware for Cerebrum devices according to a json-formatted device configuration passed on the command line."""
19 parser
= argparse
.ArgumentParser(description
='Generate firmware for Cerebrum devices according to a json-formatted device configuration passed on the command line.', epilog
='To program the device upon creating the firmware image, supply either -p or -s.')
20 parser
.add_argument('template', type=argparse
.FileType('r'), default
='-', help='The build template .json file')
21 parser
.add_argument('device', type=argparse
.FileType('r'), help='The device type of this build. For available types, have a look at the "devices" directory.')
22 parser
.add_argument('-p', '--port', type=str, help='The tty where the device may be found to be programmed')
23 parser
.add_argument('-b', '--baudrate', type=int, help='The baud rate of the device')
24 parser
.add_argument('-s', '--usbserial', type=str, help='The USB serial number by which the device can be identified in order to be programmed')
25 parser
.add_argument('-n', '--buildname', type=str, help='An optional name for the build. This is used to name the build config files.')
26 args
= parser
.parse_args()
28 # Decode json device descriptor and device config and initialize build variables
29 desc
= json
.JSONDecoder().decode('\n'.join(args
.template
.readlines()))
30 device
= json
.JSONDecoder().decode('\n'.join(args
.device
.readlines()))
31 st
= datetime
.datetime
.utcnow().timetuple()
32 builddate
= str(datetime
.datetime(st
[0], st
[1], st
[2], st
[3], st
[4], st
[5]))
33 buildsource
= 'stdin' if args
.template
.name
is '-' else args
.template
.name
35 print('Generating firmware from ', buildsource
)
37 # Generate code and write generated build config
38 # FIXME there are two different but similar things called "build config" here.
39 build_path
= os
.path
.join(os
.path
.dirname(__file__
), desc
["type"])
40 buildconfig
= generator
.generate(desc
, device
, build_path
, builddate
)
41 with
open(os
.path
.join(os
.path
.dirname(__file__
), "builds", builddate
+ "-" + args
.buildname
if args
.buildname
else os
.path
.splitext(os
.path
.basename(buildsource
))[0] + ".config.json"), "w") as f
:
42 f
.write(json
.JSONEncoder(indent
=4).encode(buildconfig
))
43 print('Wrote build config to ', f
.name
)
45 # Flash the device if requested
46 if args
.port
or args
.usbserial
:
47 print('Programming device')
48 generator
.commit(device
, device
['type'], args
)