Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import argparse |
| 4 | from generator import * |
| 5 | |
| 6 | parse = argparse.ArgumentParser(description='XOS Generative Toolchain') |
| 7 | parse.add_argument('--rev', dest='rev', action='store_true',default=False, help='Convert proto to xproto') |
| 8 | parse.add_argument('--target', dest='target', action='store',default=None, help='Output format, corresponding to <output>.yaml file', required=True) |
| 9 | parse.add_argument('--output', dest='output', action='store',default=None, help='Destination dir') |
| 10 | parse.add_argument('--attic', dest='attic', action='store',default=None, help='The location at which static files are stored') |
| 11 | parse.add_argument('--kvpairs', dest='kv', action='store',default=None, help='Key value pairs to make available to the target') |
| 12 | parse.add_argument('--write-to-file', dest='write_to_file', choices = ['single', 'model', 'target'], action='store',default=None, help='Single output file (single) or output file per model (model) or let target decide (target)') |
| 13 | |
| 14 | group = parse.add_mutually_exclusive_group() |
| 15 | group.add_argument('--dest-file', dest='dest_file', action='store',default=None, help='Output file name (if write-to-file is set to single)') |
| 16 | group.add_argument('--dest-extension', dest='dest_extension', action='store',default=None, help='Output file extension (if write-to-file is set to single)') |
| 17 | |
| 18 | parse.add_argument('files', metavar='<input file>', nargs='+', action='store', help='xproto files to compile') |
| 19 | |
| 20 | class XosGen: |
| 21 | |
| 22 | @staticmethod |
| 23 | def init(args=None): |
| 24 | |
| 25 | if not args: |
| 26 | args = parse.parse_args() |
| 27 | |
| 28 | args.quiet = False |
| 29 | |
| 30 | # convert output to absolute path |
| 31 | if args.output is not None and not os.path.isabs(args.output): |
| 32 | args.output = os.path.abspath(os.getcwd() + '/' + args.output) |
| 33 | if not '/' in args.target: |
| 34 | # if the target is not a path, it refer to a library included one |
| 35 | args.target = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/targets/" + args.target) |
| 36 | if not os.path.isabs(args.target): |
| 37 | args.target = os.path.abspath(os.getcwd() + '/' + args.target) |
| 38 | |
| 39 | # check if there's a line that starts with +++ in the target |
| 40 | # if so, then the output file names are left to the target to decide |
| 41 | # also, if dest-file or dest-extension are supplied, then an error is generated. |
| 42 | plusplusplus = reduce(lambda acc, line: True if line.startswith('+++') else acc, open(args.target).read().splitlines(), False) |
| 43 | |
| 44 | if plusplusplus and args.write_to_file != 'target': |
| 45 | parse.error('%s chooses the names of the files that it generates, you must set --write-to-file to "target"' % args.target) |
| 46 | |
| 47 | |
| 48 | if args.write_to_file != 'single' and (args.dest_file): |
| 49 | parse.error('--dest-file requires --write-to-file to be set to "single"') |
| 50 | |
| 51 | if args.write_to_file != 'model' and (args.dest_extension): |
| 52 | parse.error('--dest-extension requires --write-to-file to be set to "model"') |
| 53 | |
| 54 | inputs = [] |
| 55 | |
| 56 | for fname in args.files: |
| 57 | if not os.path.isabs(fname): |
| 58 | inputs.append(os.path.abspath(os.getcwd() + '/' + fname)) |
| 59 | else: |
| 60 | inputs.append(fname) |
| 61 | args.files = inputs |
| 62 | |
| 63 | generated = XOSGenerator.generate(args) |
| 64 | |
| 65 | if not args.output and not args.write_to_file: |
| 66 | print generated |