Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 17 | #!/usr/bin/python |
| 18 | |
| 19 | import argparse |
| 20 | from generator import * |
| 21 | |
| 22 | parse = argparse.ArgumentParser(description='XOS Generative Toolchain') |
| 23 | parse.add_argument('--rev', dest='rev', action='store_true',default=False, help='Convert proto to xproto') |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 24 | parse.add_argument('--output', dest='output', action='store',default=None, help='Destination dir') |
| 25 | parse.add_argument('--attic', dest='attic', action='store',default=None, help='The location at which static files are stored') |
| 26 | parse.add_argument('--kvpairs', dest='kv', action='store',default=None, help='Key value pairs to make available to the target') |
| 27 | 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)') |
| 28 | |
| 29 | group = parse.add_mutually_exclusive_group() |
| 30 | group.add_argument('--dest-file', dest='dest_file', action='store',default=None, help='Output file name (if write-to-file is set to single)') |
| 31 | group.add_argument('--dest-extension', dest='dest_extension', action='store',default=None, help='Output file extension (if write-to-file is set to single)') |
| 32 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 33 | group = parse.add_mutually_exclusive_group(required=True) |
| 34 | group.add_argument('--target', dest='target', action='store',default=None, help='Output format, corresponding to <output>.yaml file') |
| 35 | group.add_argument('--checkers', dest='checkers', action='store', default=None, help='Comma-separated list of static checkers') |
| 36 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 37 | parse.add_argument('files', metavar='<input file>', nargs='+', action='store', help='xproto files to compile') |
| 38 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 39 | CHECK = 1 |
| 40 | GEN = 2 |
| 41 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 42 | class XosGen: |
| 43 | |
| 44 | @staticmethod |
| 45 | def init(args=None): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 46 | if not args: |
| 47 | args = parse.parse_args() |
| 48 | |
| 49 | args.quiet = False |
| 50 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 51 | if args.target: |
| 52 | op = GEN |
| 53 | subdir = '/targets/' |
| 54 | elif args.checkers: |
| 55 | op = CHECK |
| 56 | subdir = '/checkers/' |
| 57 | else: |
| 58 | parse.error("At least one of --target and --checkers is required") |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 59 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 60 | operators = args.checkers.split(',') if hasattr(args, 'checkers') and args.checkers else [args.target] |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 61 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 62 | for i in xrange(len(operators)): |
| 63 | if not '/' in operators[i]: |
| 64 | # if the target is not a path, it refer to a library included one |
| 65 | operators[i] = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + subdir + operators[i]) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 66 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 67 | if not os.path.isabs(operators[i]): |
| 68 | operators[i] = os.path.abspath(os.getcwd() + '/' + operators[i]) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 69 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 70 | if op == GEN: |
| 71 | # convert output to absolute path |
| 72 | if args.output is not None and not os.path.isabs(args.output): |
| 73 | args.output = os.path.abspath(os.getcwd() + '/' + args.output) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 74 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 75 | operator = operators[0] |
| 76 | |
| 77 | # check if there's a line that starts with +++ in the target |
| 78 | # if so, then the output file names are left to the target to decide |
| 79 | # also, if dest-file or dest-extension are supplied, then an error is generated. |
| 80 | plusplusplus = reduce(lambda acc, line: True if line.startswith('+++') else acc, open(operator).read().splitlines(), False) |
| 81 | |
| 82 | if plusplusplus and args.write_to_file != 'target': |
| 83 | parse.error('%s chooses the names of the files that it generates, you must set --write-to-file to "target"' % operator) |
| 84 | |
| 85 | if args.write_to_file != 'single' and (args.dest_file): |
| 86 | parse.error('--dest-file requires --write-to-file to be set to "single"') |
| 87 | |
| 88 | if args.write_to_file != 'model' and (args.dest_extension): |
| 89 | parse.error('--dest-extension requires --write-to-file to be set to "model"') |
| 90 | |
| 91 | else: |
| 92 | if args.write_to_file or args.dest_extension: |
| 93 | parse.error('Checkers cannot write to files') |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 94 | |
| 95 | inputs = [] |
| 96 | |
| 97 | for fname in args.files: |
| 98 | if not os.path.isabs(fname): |
| 99 | inputs.append(os.path.abspath(os.getcwd() + '/' + fname)) |
| 100 | else: |
| 101 | inputs.append(fname) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 102 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 103 | args.files = inputs |
| 104 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 105 | if op==GEN: |
| 106 | generated = XOSProcessor.process(args, operators[0]) |
| 107 | if not args.output and not args.write_to_file: |
| 108 | print generated |
| 109 | elif op==CHECK: |
| 110 | for o in operators: |
| 111 | verdict_str = XOSProcessor.process(args, o) |
| 112 | vlst = verdict_str.split('\n') |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 113 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 114 | try: |
| 115 | verdict = next(v for v in vlst if v.strip()) |
| 116 | status_code, status_string = verdict.split(' ', 1) |
| 117 | status_code = int(status_code) |
| 118 | except: |
| 119 | print "Checker %s returned mangled output" % o |
| 120 | exit(1) |
| 121 | |
| 122 | if status_code != 200: |
| 123 | print '%s: %s - %s' % (o, status_code, status_string) |
| 124 | exit(1) |
| 125 | else: |
| 126 | print '%s: OK'%o |