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