Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 3 | # Copyright 2017-present Open Networking Foundation |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 17 | from __future__ import absolute_import, print_function |
Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 18 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 19 | import argparse |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 20 | import os |
| 21 | from functools import reduce |
| 22 | |
| 23 | from six.moves import range |
| 24 | |
| 25 | from .generator import XOSProcessor, XOSProcessorArgs |
| 26 | from .version import __version__ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 27 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 28 | parse = argparse.ArgumentParser(description="XOS Generative Toolchain") |
| 29 | parse.add_argument( |
| 30 | "--rev", |
| 31 | dest="rev", |
| 32 | action="store_true", |
| 33 | default=XOSProcessorArgs.default_rev, |
| 34 | help="Convert proto to xproto", |
| 35 | ) |
| 36 | parse.add_argument( |
| 37 | "--output", |
| 38 | dest="output", |
| 39 | action="store", |
| 40 | default=XOSProcessorArgs.default_output, |
| 41 | help="Destination dir", |
| 42 | ) |
| 43 | parse.add_argument( |
| 44 | "--attic", |
| 45 | dest="attic", |
| 46 | action="store", |
| 47 | default=XOSProcessorArgs.default_attic, |
| 48 | help="The location at which static files are stored", |
| 49 | ) |
| 50 | parse.add_argument( |
| 51 | "--kvpairs", |
| 52 | dest="kv", |
| 53 | action="store", |
| 54 | default=XOSProcessorArgs.default_kvpairs, |
| 55 | help="Key value pairs to make available to the target", |
| 56 | ) |
| 57 | parse.add_argument( |
| 58 | "--write-to-file", |
| 59 | dest="write_to_file", |
| 60 | choices=["single", "model", "target"], |
| 61 | action="store", |
| 62 | default=XOSProcessorArgs.default_write_to_file, |
| 63 | help="Single output file (single) or output file per model (model) or let target decide (target)", |
| 64 | ) |
| 65 | parse.add_argument("--version", action="version", version=__version__) |
| 66 | parse.add_argument( |
| 67 | "-v", |
| 68 | "--verbosity", |
| 69 | action="count", |
| 70 | default=XOSProcessorArgs.default_verbosity, |
| 71 | help="increase output verbosity", |
| 72 | ) |
| 73 | parse.add_argument( |
| 74 | "--include-models", |
| 75 | dest="include_models", |
| 76 | action="append", |
| 77 | default=XOSProcessorArgs.default_include_models, |
| 78 | help="list of models to include", |
| 79 | ) |
| 80 | parse.add_argument( |
| 81 | "--include-apps", |
| 82 | dest="include_apps", |
| 83 | action="append", |
| 84 | default=XOSProcessorArgs.default_include_apps, |
| 85 | help="list of models to include", |
| 86 | ) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 87 | |
| 88 | group = parse.add_mutually_exclusive_group() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 89 | group.add_argument( |
| 90 | "--dest-file", |
| 91 | dest="dest_file", |
| 92 | action="store", |
| 93 | default=XOSProcessorArgs.default_dest_file, |
| 94 | help="Output file name (if write-to-file is set to single)", |
| 95 | ) |
| 96 | group.add_argument( |
| 97 | "--dest-extension", |
| 98 | dest="dest_extension", |
| 99 | action="store", |
| 100 | default=XOSProcessorArgs.default_dest_extension, |
| 101 | help="Output file extension (if write-to-file is set to single)", |
| 102 | ) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 103 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 104 | group = parse.add_mutually_exclusive_group(required=True) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 105 | group.add_argument( |
| 106 | "--target", |
| 107 | dest="target", |
| 108 | action="store", |
| 109 | default=XOSProcessorArgs.default_target, |
| 110 | help="Output format, corresponding to <output>.yaml file", |
| 111 | ) |
| 112 | group.add_argument( |
| 113 | "--checkers", |
| 114 | dest="checkers", |
| 115 | action="store", |
| 116 | default=XOSProcessorArgs.default_checkers, |
| 117 | help="Comma-separated list of static checkers", |
| 118 | ) |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 119 | group.add_argument( |
| 120 | "--lint", |
| 121 | dest="lint", |
| 122 | action="store_true", |
| 123 | default=XOSProcessorArgs.default_lint, |
| 124 | help="Parse the xproto but don't execute any xtargets", |
| 125 | ) |
| 126 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 127 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 128 | parse.add_argument( |
| 129 | "files", |
| 130 | metavar="<input file>", |
| 131 | nargs="+", |
| 132 | action="store", |
| 133 | help="xproto files to compile", |
| 134 | ) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 135 | |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 136 | parse.add_argument( |
| 137 | "--strict-validation", |
| 138 | dest="strict_validation", |
| 139 | action="store_true", |
| 140 | default=XOSProcessorArgs.default_strict_validation, |
| 141 | help="Exit if validation fails", |
| 142 | ) |
| 143 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 144 | CHECK = 1 |
| 145 | GEN = 2 |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 146 | LINT = 3 |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 147 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 148 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 149 | class XosGen: |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 150 | @staticmethod |
| 151 | def init(args=None): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 152 | if not args: |
| 153 | args = parse.parse_args() |
| 154 | |
| 155 | args.quiet = False |
| 156 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 157 | if args.target: |
| 158 | op = GEN |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 159 | subdir = "/targets/" |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 160 | operators = [args.target] |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 161 | elif args.checkers: |
| 162 | op = CHECK |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 163 | subdir = "/checkers/" |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 164 | operators = args.checkers |
| 165 | elif args.lint: |
| 166 | op = LINT |
| 167 | subdir = None |
| 168 | operators = [] |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 169 | else: |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 170 | parse.error("At least one of --target, --checkers, or --lint is required") |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 171 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 172 | for i in range(len(operators)): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 173 | if "/" not in operators[i]: |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 174 | # if the target is not a path, it refer to a library included one |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 175 | operators[i] = os.path.abspath( |
| 176 | os.path.dirname(os.path.realpath(__file__)) + subdir + operators[i] |
| 177 | ) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 178 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 179 | if not os.path.isabs(operators[i]): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 180 | operators[i] = os.path.abspath(os.getcwd() + "/" + operators[i]) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 181 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 182 | if op == GEN: |
| 183 | # convert output to absolute path |
| 184 | if args.output is not None and not os.path.isabs(args.output): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 185 | args.output = os.path.abspath(os.getcwd() + "/" + args.output) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 186 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 187 | operator = operators[0] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 188 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 189 | # check if there's a line that starts with +++ in the target |
| 190 | # if so, then the output file names are left to the target to decide |
| 191 | # also, if dest-file or dest-extension are supplied, then an error is generated. |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 192 | plusplusplus = reduce( |
| 193 | lambda acc, line: True if line.startswith("+++") else acc, |
| 194 | open(operator).read().splitlines(), |
| 195 | False, |
| 196 | ) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 197 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 198 | if plusplusplus and args.write_to_file != "target": |
| 199 | parse.error( |
| 200 | '%s chooses the names of the files that it generates, you must set --write-to-file to "target"' |
| 201 | % operator |
| 202 | ) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 203 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 204 | if args.write_to_file != "single" and (args.dest_file): |
| 205 | parse.error( |
| 206 | '--dest-file requires --write-to-file to be set to "single"' |
| 207 | ) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 208 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 209 | if args.write_to_file != "model" and (args.dest_extension): |
| 210 | parse.error( |
| 211 | '--dest-extension requires --write-to-file to be set to "model"' |
| 212 | ) |
| 213 | |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 214 | elif op == CHECK: |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 215 | if args.write_to_file or args.dest_extension: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 216 | parse.error("Checkers cannot write to files") |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 217 | elif op == LINT: |
| 218 | # no outputs, so nothing to check |
| 219 | pass |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 220 | |
| 221 | inputs = [] |
| 222 | |
| 223 | for fname in args.files: |
| 224 | if not os.path.isabs(fname): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 225 | inputs.append(os.path.abspath(os.getcwd() + "/" + fname)) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 226 | else: |
| 227 | inputs.append(fname) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 228 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 229 | args.files = inputs |
| 230 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 231 | if op == GEN: |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 232 | generated = XOSProcessor.process(args, operators[0]) |
| 233 | if not args.output and not args.write_to_file: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 234 | print(generated) |
| 235 | elif op == CHECK: |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 236 | for o in operators: |
| 237 | verdict_str = XOSProcessor.process(args, o) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 238 | vlst = verdict_str.split("\n") |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 239 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 240 | try: |
| 241 | verdict = next(v for v in vlst if v.strip()) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 242 | status_code, status_string = verdict.split(" ", 1) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 243 | status_code = int(status_code) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 244 | except BaseException: |
| 245 | print("Checker %s returned mangled output" % o) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 246 | exit(1) |
| 247 | |
| 248 | if status_code != 200: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 249 | print("%s: %s - %s" % (o, status_code, status_string)) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 250 | exit(1) |
| 251 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 252 | print("%s: OK" % o) |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 253 | elif op == LINT: |
| 254 | XOSProcessor.process(args, None) |