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 * |
Scott Baker | d0d3566 | 2018-03-26 09:58:07 -0700 | [diff] [blame] | 21 | from version import __version__ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 22 | |
| 23 | parse = argparse.ArgumentParser(description='XOS Generative Toolchain') |
| 24 | 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] | 25 | parse.add_argument('--output', dest='output', action='store',default=None, help='Destination dir') |
| 26 | parse.add_argument('--attic', dest='attic', action='store',default=None, help='The location at which static files are stored') |
| 27 | parse.add_argument('--kvpairs', dest='kv', action='store',default=None, help='Key value pairs to make available to the target') |
| 28 | 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)') |
Scott Baker | d0d3566 | 2018-03-26 09:58:07 -0700 | [diff] [blame] | 29 | parse.add_argument('--version', action='version', version=__version__) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 30 | |
| 31 | group = parse.add_mutually_exclusive_group() |
| 32 | group.add_argument('--dest-file', dest='dest_file', action='store',default=None, help='Output file name (if write-to-file is set to single)') |
| 33 | group.add_argument('--dest-extension', dest='dest_extension', action='store',default=None, help='Output file extension (if write-to-file is set to single)') |
| 34 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 35 | group = parse.add_mutually_exclusive_group(required=True) |
| 36 | group.add_argument('--target', dest='target', action='store',default=None, help='Output format, corresponding to <output>.yaml file') |
| 37 | group.add_argument('--checkers', dest='checkers', action='store', default=None, help='Comma-separated list of static checkers') |
| 38 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 39 | parse.add_argument('files', metavar='<input file>', nargs='+', action='store', help='xproto files to compile') |
| 40 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 41 | CHECK = 1 |
| 42 | GEN = 2 |
| 43 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 44 | class XosGen: |
| 45 | |
| 46 | @staticmethod |
| 47 | def init(args=None): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 48 | if not args: |
| 49 | args = parse.parse_args() |
| 50 | |
| 51 | args.quiet = False |
| 52 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 53 | if args.target: |
| 54 | op = GEN |
| 55 | subdir = '/targets/' |
| 56 | elif args.checkers: |
| 57 | op = CHECK |
| 58 | subdir = '/checkers/' |
| 59 | else: |
| 60 | parse.error("At least one of --target and --checkers is required") |
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 | 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] | 63 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 64 | for i in xrange(len(operators)): |
| 65 | if not '/' in operators[i]: |
| 66 | # if the target is not a path, it refer to a library included one |
| 67 | 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] | 68 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 69 | if not os.path.isabs(operators[i]): |
| 70 | operators[i] = os.path.abspath(os.getcwd() + '/' + operators[i]) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 71 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 72 | if op == GEN: |
| 73 | # convert output to absolute path |
| 74 | if args.output is not None and not os.path.isabs(args.output): |
| 75 | args.output = os.path.abspath(os.getcwd() + '/' + args.output) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 76 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 77 | operator = operators[0] |
| 78 | |
| 79 | # check if there's a line that starts with +++ in the target |
| 80 | # if so, then the output file names are left to the target to decide |
| 81 | # also, if dest-file or dest-extension are supplied, then an error is generated. |
| 82 | plusplusplus = reduce(lambda acc, line: True if line.startswith('+++') else acc, open(operator).read().splitlines(), False) |
| 83 | |
| 84 | if plusplusplus and args.write_to_file != 'target': |
| 85 | parse.error('%s chooses the names of the files that it generates, you must set --write-to-file to "target"' % operator) |
| 86 | |
| 87 | if args.write_to_file != 'single' and (args.dest_file): |
| 88 | parse.error('--dest-file requires --write-to-file to be set to "single"') |
| 89 | |
| 90 | if args.write_to_file != 'model' and (args.dest_extension): |
| 91 | parse.error('--dest-extension requires --write-to-file to be set to "model"') |
| 92 | |
| 93 | else: |
| 94 | if args.write_to_file or args.dest_extension: |
| 95 | parse.error('Checkers cannot write to files') |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 96 | |
| 97 | inputs = [] |
| 98 | |
| 99 | for fname in args.files: |
| 100 | if not os.path.isabs(fname): |
| 101 | inputs.append(os.path.abspath(os.getcwd() + '/' + fname)) |
| 102 | else: |
| 103 | inputs.append(fname) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 104 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 105 | args.files = inputs |
| 106 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 107 | if op==GEN: |
| 108 | generated = XOSProcessor.process(args, operators[0]) |
| 109 | if not args.output and not args.write_to_file: |
| 110 | print generated |
| 111 | elif op==CHECK: |
| 112 | for o in operators: |
| 113 | verdict_str = XOSProcessor.process(args, o) |
| 114 | vlst = verdict_str.split('\n') |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 115 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 116 | try: |
| 117 | verdict = next(v for v in vlst if v.strip()) |
| 118 | status_code, status_string = verdict.split(' ', 1) |
| 119 | status_code = int(status_code) |
| 120 | except: |
| 121 | print "Checker %s returned mangled output" % o |
| 122 | exit(1) |
| 123 | |
| 124 | if status_code != 200: |
| 125 | print '%s: %s - %s' % (o, status_code, status_string) |
| 126 | exit(1) |
| 127 | else: |
| 128 | print '%s: OK'%o |