Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 15 | from __future__ import absolute_import, print_function |
Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 16 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 17 | import os |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 18 | import jinja2 |
| 19 | import plyxproto.parser as plyxproto |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 20 | import yaml |
Sapan Bhatia | 85b7101 | 2018-01-12 12:11:19 -0500 | [diff] [blame] | 21 | from colorama import Fore |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 22 | import sys |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 23 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 24 | from . import jinja2_extensions |
| 25 | from .proto2xproto import Proto2XProto |
| 26 | from .xos2jinja import XOS2Jinja |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 27 | from .validator import XProtoValidator |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 28 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 29 | loader = jinja2.PackageLoader(__name__, "templates") |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 30 | env = jinja2.Environment(loader=loader) |
| 31 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 32 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 33 | class XOSProcessorArgs: |
| 34 | """ Helper class for use cases that want to call XOSProcessor directly, rather than executing xosgenx from the |
| 35 | command line. |
| 36 | """ |
| 37 | |
| 38 | default_rev = False |
| 39 | default_output = None |
| 40 | default_attic = None |
| 41 | default_kvpairs = None |
| 42 | default_write_to_file = None |
| 43 | default_dest_file = None |
| 44 | default_dest_extension = None |
| 45 | default_target = None |
| 46 | default_checkers = None |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 47 | default_verbosity = ( |
| 48 | 0 |
| 49 | ) # Higher numbers = more verbosity, lower numbers = less verbosity |
| 50 | default_include_models = ( |
| 51 | [] |
| 52 | ) # If neither include_models nor include_apps is specified, then all models will |
| 53 | default_include_apps = [] # be included. |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 54 | default_strict_validation = False |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 55 | default_lint = False |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 56 | |
| 57 | def __init__(self, **kwargs): |
| 58 | # set defaults |
| 59 | self.rev = XOSProcessorArgs.default_rev |
| 60 | self.output = XOSProcessorArgs.default_output |
| 61 | self.attic = XOSProcessorArgs.default_attic |
| 62 | self.kvpairs = XOSProcessorArgs.default_kvpairs |
| 63 | self.verbosity = XOSProcessorArgs.default_verbosity |
| 64 | self.write_to_file = XOSProcessorArgs.default_write_to_file |
| 65 | self.default_dest_file = XOSProcessorArgs.default_dest_file |
| 66 | self.default_dest_extension = XOSProcessorArgs.default_dest_extension |
| 67 | self.default_target = XOSProcessorArgs.default_target |
| 68 | self.default_checkers = XOSProcessorArgs.default_target |
| 69 | self.include_models = XOSProcessorArgs.default_include_models |
| 70 | self.include_apps = XOSProcessorArgs.default_include_apps |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 71 | self.strict_validation = XOSProcessorArgs.default_strict_validation |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 72 | self.lint = XOSProcessorArgs.default_lint |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 73 | |
| 74 | # override defaults with kwargs |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 75 | for (k, v) in kwargs.items(): |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 76 | setattr(self, k, v) |
| 77 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 78 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 79 | class XOSProcessor: |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 80 | @staticmethod |
| 81 | def _read_input_from_files(files): |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 82 | """ Read the files and return the combined text read. |
| 83 | |
| 84 | Also returns a list of (line_number, filename) tuples that tell which |
| 85 | starting line corresponds to each file. |
| 86 | """ |
| 87 | line_map = [] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 88 | input = "" |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 89 | for fname in files: |
| 90 | with open(fname) as infile: |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 91 | line_map.append((len(input.split("\n")), fname)) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 92 | input += infile.read() |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 93 | return (input, line_map) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 94 | |
| 95 | @staticmethod |
| 96 | def _attach_parser(ast, args): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 97 | if hasattr(args, "rev") and args.rev: |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 98 | v = Proto2XProto() |
| 99 | ast.accept(v) |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 100 | |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 101 | v = XOS2Jinja(args) |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 102 | ast.accept(v) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 103 | return v |
| 104 | |
| 105 | @staticmethod |
| 106 | def _get_template(target): |
| 107 | if not os.path.isabs(target): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 108 | return os.path.abspath( |
| 109 | os.path.dirname(os.path.realpath(__file__)) + "/targets/" + target |
| 110 | ) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 111 | return target |
| 112 | |
| 113 | @staticmethod |
| 114 | def _file_exists(attic): |
| 115 | # NOTE this method can be used in the jinja template |
| 116 | def file_exists2(name): |
| 117 | if attic is not None: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 118 | path = attic + "/" + name |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 119 | else: |
| 120 | path = name |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 121 | return os.path.exists(path) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 122 | |
| 123 | return file_exists2 |
| 124 | |
| 125 | @staticmethod |
| 126 | def _include_file(attic): |
| 127 | # NOTE this method can be used in the jinja template |
| 128 | def include_file2(name): |
| 129 | if attic is not None: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 130 | path = attic + "/" + name |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 131 | else: |
| 132 | path = name |
| 133 | return open(path).read() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 134 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 135 | return include_file2 |
| 136 | |
| 137 | @staticmethod |
| 138 | def _load_jinja2_extensions(os_template_env, attic): |
| 139 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 140 | os_template_env.globals["include_file"] = XOSProcessor._include_file( |
| 141 | attic |
| 142 | ) # Generates a function |
| 143 | os_template_env.globals["file_exists"] = XOSProcessor._file_exists( |
| 144 | attic |
| 145 | ) # Generates a function |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 146 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 147 | os_template_env.filters["yaml"] = yaml.dump |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 148 | for f in dir(jinja2_extensions): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 149 | if f.startswith("xproto"): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 150 | os_template_env.globals[f] = getattr(jinja2_extensions, f) |
| 151 | return os_template_env |
| 152 | |
| 153 | @staticmethod |
| 154 | def _add_context(args): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 155 | if not hasattr(args, "kv") or not args.kv: |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 156 | return |
| 157 | try: |
| 158 | context = {} |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 159 | for s in args.kv.split(","): |
| 160 | k, val = s.split(":") |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 161 | context[k] = val |
| 162 | return context |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 163 | except Exception as e: |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 164 | print(e) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 165 | |
| 166 | @staticmethod |
| 167 | def _write_single_file(rendered, dir, dest_file, quiet): |
| 168 | |
| 169 | file_name = "%s/%s" % (dir, dest_file) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 170 | file = open(file_name, "w") |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 171 | file.write(rendered) |
| 172 | file.close() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 173 | if not quiet: |
| 174 | print("Saved: %s" % file_name) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 175 | |
| 176 | @staticmethod |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 177 | def _write_split_target(rendered, dir, quiet): |
| 178 | |
| 179 | lines = rendered.splitlines() |
| 180 | current_buffer = [] |
| 181 | for l in lines: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 182 | if l.startswith("+++"): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 183 | |
| 184 | if dir: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 185 | path = dir + "/" + l[4:].lower() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 186 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 187 | fil = open(path, "w") |
| 188 | buf = "\n".join(current_buffer) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 189 | |
| 190 | obuf = buf |
| 191 | |
| 192 | fil.write(obuf) |
| 193 | fil.close() |
| 194 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 195 | if not quiet: |
| 196 | print("Save file to: %s" % path) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 197 | |
| 198 | current_buffer = [] |
| 199 | else: |
| 200 | current_buffer.append(l) |
| 201 | |
| 202 | @staticmethod |
| 203 | def _find_message_by_model_name(messages, model): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 204 | return next((x for x in messages if x["name"] == model), None) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 205 | |
| 206 | @staticmethod |
Sapan Bhatia | 85b7101 | 2018-01-12 12:11:19 -0500 | [diff] [blame] | 207 | def _find_last_nonempty_line(text, pointer): |
| 208 | ne_pointer = pointer |
| 209 | found = False |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 210 | while ne_pointer != 0 and not found: |
| 211 | ne_pointer = text[: (ne_pointer - 1)].rfind("\n") |
| 212 | if ne_pointer < 0: |
| 213 | ne_pointer = 0 |
| 214 | if text[ne_pointer - 1] != "\n": |
Sapan Bhatia | 85b7101 | 2018-01-12 12:11:19 -0500 | [diff] [blame] | 215 | found = True |
| 216 | |
| 217 | return ne_pointer |
| 218 | |
| 219 | @staticmethod |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 220 | def process(args, operator=None): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 221 | # Setting defaults |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 222 | if not hasattr(args, "attic"): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 223 | args.attic = None |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 224 | if not hasattr(args, "write_to_file"): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 225 | args.write_to_file = None |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 226 | if not hasattr(args, "dest_file"): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 227 | args.dest_file = None |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 228 | if not hasattr(args, "dest_extension"): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 229 | args.dest_extension = None |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 230 | if not hasattr(args, "output"): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 231 | args.output = None |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 232 | if not hasattr(args, "quiet"): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 233 | args.quiet = True |
| 234 | |
| 235 | # Validating |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 236 | if args.write_to_file == "single" and args.dest_file is None: |
| 237 | raise Exception( |
| 238 | "[XosGenX] write_to_file option is specified as 'single' but no dest_file is provided" |
| 239 | ) |
| 240 | if args.write_to_file == "model" and (args.dest_extension is None): |
| 241 | raise Exception( |
| 242 | "[XosGenX] write_to_file option is specified as 'model' but no dest_extension is provided" |
| 243 | ) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 244 | |
| 245 | if args.output is not None and not os.path.isabs(args.output): |
Scott Baker | 63c27ba | 2019-03-01 16:06:15 -0800 | [diff] [blame] | 246 | raise Exception("[XosGenX] The output dir (%s) must be an absolute path!" % args.output) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 247 | if args.output is not None and not os.path.isdir(args.output): |
Scott Baker | 63c27ba | 2019-03-01 16:06:15 -0800 | [diff] [blame] | 248 | raise Exception("[XosGenX] The output dir (%s) must be a directory!" % args.output) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 249 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 250 | if hasattr(args, "files"): |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 251 | (inputs, line_map) = XOSProcessor._read_input_from_files(args.files) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 252 | elif hasattr(args, "inputs"): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 253 | inputs = args.inputs |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 254 | line_map = [] |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 255 | else: |
| 256 | raise Exception("[XosGenX] No inputs provided!") |
| 257 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 258 | context = XOSProcessor._add_context(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 259 | |
| 260 | parser = plyxproto.ProtobufAnalyzer() |
Sapan Bhatia | 85b7101 | 2018-01-12 12:11:19 -0500 | [diff] [blame] | 261 | try: |
| 262 | ast = parser.parse_string(inputs, debug=0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 263 | except plyxproto.ParsingError as e: |
Sapan Bhatia | 85b7101 | 2018-01-12 12:11:19 -0500 | [diff] [blame] | 264 | if e.message: |
| 265 | error = e.message |
| 266 | else: |
| 267 | error = "xproto parsing error" |
Scott Baker | 115e526 | 2019-06-03 11:49:15 -0700 | [diff] [blame] | 268 | if e.error_range is None: |
| 269 | # No line number information |
| 270 | print(error + "\n") |
| 271 | else: |
| 272 | line, start, end = e.error_range |
Sapan Bhatia | 85b7101 | 2018-01-12 12:11:19 -0500 | [diff] [blame] | 273 | |
Scott Baker | 115e526 | 2019-06-03 11:49:15 -0700 | [diff] [blame] | 274 | ptr = XOSProcessor._find_last_nonempty_line(inputs, start) |
| 275 | |
| 276 | if start == 0: |
| 277 | beginning = "" |
| 278 | else: |
| 279 | beginning = inputs[ptr: start - 1] |
| 280 | |
| 281 | line_end_char = inputs[start + end:].find("\n") |
| 282 | line_end = inputs[line_end_char] |
| 283 | |
| 284 | print(error + "\n" + Fore.YELLOW + "Line %d:" % line + Fore.WHITE) |
| 285 | print( |
| 286 | beginning |
| 287 | + Fore.YELLOW |
| 288 | + inputs[start - 1: start + end] |
| 289 | + Fore.WHITE |
| 290 | + line_end |
| 291 | ) |
Sapan Bhatia | 85b7101 | 2018-01-12 12:11:19 -0500 | [diff] [blame] | 292 | exit(1) |
| 293 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 294 | v = XOSProcessor._attach_parser(ast, args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 295 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 296 | if args.include_models or args.include_apps: |
| 297 | for message in v.messages: |
| 298 | message["is_included"] = False |
| 299 | if message["name"] in args.include_models: |
| 300 | message["is_included"] = True |
| 301 | else: |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 302 | app_label = ( |
| 303 | message.get("options", {}) |
| 304 | .get("app_label") |
| 305 | .strip('"') |
| 306 | ) |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 307 | if app_label in args.include_apps: |
| 308 | message["is_included"] = True |
| 309 | else: |
| 310 | for message in v.messages: |
| 311 | message["is_included"] = True |
| 312 | |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 313 | validator = XProtoValidator(v.models, line_map) |
| 314 | validator.validate() |
| 315 | if validator.errors: |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 316 | if args.strict_validation or (args.verbosity >= 0): |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 317 | validator.print_errors() |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 318 | fatal_errors = [x for x in validator.errors if x["severity"] == "ERROR"] |
| 319 | if fatal_errors and args.strict_validation: |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 320 | sys.exit(-1) |
| 321 | |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 322 | if args.lint: |
| 323 | return "" |
| 324 | |
| 325 | if not operator: |
| 326 | operator = args.target |
| 327 | template_path = XOSProcessor._get_template(operator) |
| 328 | else: |
| 329 | template_path = operator |
| 330 | |
| 331 | [template_folder, template_name] = os.path.split(template_path) |
| 332 | os_template_loader = jinja2.FileSystemLoader(searchpath=[template_folder]) |
| 333 | os_template_env = jinja2.Environment(loader=os_template_loader) |
| 334 | os_template_env = XOSProcessor._load_jinja2_extensions( |
| 335 | os_template_env, args.attic |
| 336 | ) |
| 337 | template = os_template_env.get_template(template_name) |
| 338 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 339 | if args.output is not None and args.write_to_file == "model": |
Scott Baker | be2a517 | 2019-04-10 18:02:50 -0700 | [diff] [blame] | 340 | # Handle the case where each model is written to a separate python file. |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 341 | rendered = {} |
Scott Baker | be2a517 | 2019-04-10 18:02:50 -0700 | [diff] [blame] | 342 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 343 | for i, model in enumerate(v.models): |
Scott Baker | be2a517 | 2019-04-10 18:02:50 -0700 | [diff] [blame] | 344 | model_dict = v.models[model] |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 345 | messages = [XOSProcessor._find_message_by_model_name(v.messages, model)] |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 346 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 347 | rendered[model] = template.render( |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 348 | { |
| 349 | "proto": { |
Scott Baker | be2a517 | 2019-04-10 18:02:50 -0700 | [diff] [blame] | 350 | "message_table": {model: model_dict}, |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 351 | "messages": messages, |
| 352 | "policies": v.policies, |
| 353 | "message_names": [m["name"] for m in v.messages], |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 354 | }, |
| 355 | "context": context, |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 356 | "options": v.options, |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 357 | } |
| 358 | ) |
Scott Baker | be2a517 | 2019-04-10 18:02:50 -0700 | [diff] [blame] | 359 | if not rendered[model]: |
| 360 | print("Not saving model %s as it is empty" % model, file=sys.stderr) |
| 361 | else: |
| 362 | legacy = jinja2_extensions.base.xproto_list_evaluates_true( |
| 363 | [model_dict.get("options", {}).get("custom_python", None), |
| 364 | model_dict.get("options", {}).get("legacy", None), |
| 365 | v.options.get("custom_python", None), |
| 366 | v.options.get("legacy", None)]) |
| 367 | |
| 368 | if legacy: |
| 369 | file_name = "%s/%s_decl.%s" % (args.output, model.lower(), args.dest_extension) |
| 370 | else: |
| 371 | file_name = "%s/%s.%s" % (args.output, model.lower(), args.dest_extension) |
| 372 | |
| 373 | file = open(file_name, "w") |
| 374 | file.write(rendered[model]) |
| 375 | file.close() |
| 376 | if not args.quiet: |
| 377 | print("Saved: %s" % file_name, file=sys.stderr) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 378 | else: |
Scott Baker | be2a517 | 2019-04-10 18:02:50 -0700 | [diff] [blame] | 379 | # Handle the case where all models are written to the same python file. |
| 380 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 381 | rendered = template.render( |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 382 | { |
| 383 | "proto": { |
| 384 | "message_table": v.models, |
| 385 | "messages": v.messages, |
| 386 | "policies": v.policies, |
| 387 | "message_names": [m["name"] for m in v.messages], |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 388 | }, |
| 389 | "context": context, |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 390 | "options": v.options, |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 391 | } |
| 392 | ) |
| 393 | if args.output is not None and args.write_to_file == "target": |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 394 | XOSProcessor._write_split_target(rendered, args.output, args.quiet) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 395 | elif args.output is not None and args.write_to_file == "single": |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 396 | XOSProcessor._write_single_file( |
| 397 | rendered, args.output, args.dest_file, args.quiet |
| 398 | ) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 399 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 400 | return rendered |