Merge branch 'master' of c-leuse:cerebrum
[cerebrum.git] / build.py
blobdd3bd8dd23f8416903a7558fa7523f4618a23d27
1 #!/usr/bin/env python3
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.
9 import sys
10 import os
11 import json
12 import imp
13 import datetime
14 import argparse
15 import generator
16 """Generate firmware for Cerebrum devices according to a json-formatted device configuration passed on the command line."""
18 # Parse arguments
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 print("Parsing config")
30 desc = json.JSONDecoder().decode(args.template.read())
31 print("Parsing device description")
32 device = json.JSONDecoder().decode(args.device.read())
33 st = datetime.datetime.utcnow().timetuple()
34 builddate = str(datetime.datetime(st[0], st[1], st[2], st[3], st[4], st[5]))
35 buildsource = 'stdin' if args.template.name is '-' else args.template.name
36 print(builddate)
37 print('Generating firmware from ', buildsource, "for", desc['type'])
39 # Generate code and write generated build config
40 # FIXME there are two different but similar things called "build config" here.
41 build_path = os.path.join(os.path.dirname(__file__), desc["type"])
42 buildconfig = generator.generate(desc, device, build_path, builddate, args.buildname)
43 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:
44 f.write(json.JSONEncoder(indent=4).encode(buildconfig))
45 print('Wrote build config to ', f.name)
47 # Flash the device if requested
48 if args.port or args.usbserial:
49 print('Programming device')
50 generator.commit(device, device['type'], args)
52 # ???
54 # PROFIT!!!