Nathan Skrzypczak | 5c2f964 | 2019-09-09 16:45:06 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 2 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 3 | import ply.lex as lex |
| 4 | import ply.yacc as yacc |
| 5 | import sys |
| 6 | import argparse |
Paul Vinciguerra | ff47fb6 | 2019-08-06 19:58:24 -0400 | [diff] [blame] | 7 | import keyword |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 8 | import logging |
| 9 | import binascii |
| 10 | import os |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 11 | import sys |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 12 | from subprocess import Popen, PIPE |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 13 | |
Ole Troan | 14a6c0e | 2020-05-13 11:47:43 +0200 | [diff] [blame] | 14 | assert sys.version_info >= (3, 6), \ |
| 15 | "Not supported Python version: {}".format(sys.version) |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 16 | log = logging.getLogger('vppapigen') |
| 17 | |
Ole Troan | d6743b1 | 2018-03-07 08:40:58 +0100 | [diff] [blame] | 18 | # Ensure we don't leave temporary files around |
| 19 | sys.dont_write_bytecode = True |
| 20 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 21 | # |
| 22 | # VPP API language |
| 23 | # |
| 24 | |
| 25 | # Global dictionary of new types (including enums) |
| 26 | global_types = {} |
| 27 | |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 28 | seen_imports = {} |
| 29 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 30 | |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 31 | def global_type_add(name, obj): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 32 | '''Add new type to the dictionary of types ''' |
| 33 | type_name = 'vl_api_' + name + '_t' |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 34 | if type_name in global_types: |
| 35 | raise KeyError("Attempted redefinition of {!r} with {!r}.".format( |
| 36 | name, obj)) |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 37 | global_types[type_name] = obj |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 38 | |
| 39 | |
| 40 | # All your trace are belong to us! |
| 41 | def exception_handler(exception_type, exception, traceback): |
Ole Troan | 17225df | 2018-04-11 09:50:03 +0200 | [diff] [blame] | 42 | print("%s: %s" % (exception_type.__name__, exception)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 43 | |
| 44 | |
| 45 | # |
| 46 | # Lexer |
| 47 | # |
| 48 | class VPPAPILexer(object): |
| 49 | def __init__(self, filename): |
| 50 | self.filename = filename |
| 51 | |
| 52 | reserved = { |
| 53 | 'service': 'SERVICE', |
| 54 | 'rpc': 'RPC', |
| 55 | 'returns': 'RETURNS', |
Marek Gradzki | 51e5968 | 2018-03-06 10:05:44 +0100 | [diff] [blame] | 56 | 'null': 'NULL', |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 57 | 'stream': 'STREAM', |
| 58 | 'events': 'EVENTS', |
| 59 | 'define': 'DEFINE', |
| 60 | 'typedef': 'TYPEDEF', |
| 61 | 'enum': 'ENUM', |
| 62 | 'typeonly': 'TYPEONLY', |
| 63 | 'manual_print': 'MANUAL_PRINT', |
| 64 | 'manual_endian': 'MANUAL_ENDIAN', |
| 65 | 'dont_trace': 'DONT_TRACE', |
| 66 | 'autoreply': 'AUTOREPLY', |
| 67 | 'option': 'OPTION', |
| 68 | 'u8': 'U8', |
| 69 | 'u16': 'U16', |
| 70 | 'u32': 'U32', |
| 71 | 'u64': 'U64', |
| 72 | 'i8': 'I8', |
| 73 | 'i16': 'I16', |
| 74 | 'i32': 'I32', |
| 75 | 'i64': 'I64', |
| 76 | 'f64': 'F64', |
| 77 | 'bool': 'BOOL', |
| 78 | 'string': 'STRING', |
| 79 | 'import': 'IMPORT', |
| 80 | 'true': 'TRUE', |
| 81 | 'false': 'FALSE', |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 82 | 'union': 'UNION', |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | tokens = ['STRING_LITERAL', |
| 86 | 'ID', 'NUM'] + list(reserved.values()) |
| 87 | |
| 88 | t_ignore_LINE_COMMENT = '//.*' |
| 89 | |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 90 | def t_FALSE(self, t): |
| 91 | r'false' |
| 92 | t.value = False |
| 93 | return t |
| 94 | |
| 95 | def t_TRUE(self, t): |
| 96 | r'false' |
| 97 | t.value = True |
| 98 | return t |
| 99 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 100 | def t_NUM(self, t): |
Paul Vinciguerra | 063f374 | 2019-07-02 13:00:58 -0400 | [diff] [blame] | 101 | r'0[xX][0-9a-fA-F]+|-?\d+\.?\d*' |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 102 | base = 16 if t.value.startswith('0x') else 10 |
Paul Vinciguerra | 063f374 | 2019-07-02 13:00:58 -0400 | [diff] [blame] | 103 | if '.' in t.value: |
| 104 | t.value = float(t.value) |
| 105 | else: |
| 106 | t.value = int(t.value, base) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 107 | return t |
| 108 | |
| 109 | def t_ID(self, t): |
| 110 | r'[a-zA-Z_][a-zA-Z_0-9]*' |
| 111 | # Check for reserved words |
| 112 | t.type = VPPAPILexer.reserved.get(t.value, 'ID') |
| 113 | return t |
| 114 | |
| 115 | # C string |
| 116 | def t_STRING_LITERAL(self, t): |
| 117 | r'\"([^\\\n]|(\\.))*?\"' |
| 118 | t.value = str(t.value).replace("\"", "") |
| 119 | return t |
| 120 | |
| 121 | # C or C++ comment (ignore) |
| 122 | def t_comment(self, t): |
| 123 | r'(/\*(.|\n)*?\*/)|(//.*)' |
| 124 | t.lexer.lineno += t.value.count('\n') |
| 125 | |
| 126 | # Error handling rule |
| 127 | def t_error(self, t): |
| 128 | raise ParseError("Illegal character '{}' ({})" |
| 129 | "in {}: line {}".format(t.value[0], |
| 130 | hex(ord(t.value[0])), |
| 131 | self.filename, |
| 132 | t.lexer.lineno)) |
| 133 | t.lexer.skip(1) |
| 134 | |
| 135 | # Define a rule so we can track line numbers |
| 136 | def t_newline(self, t): |
| 137 | r'\n+' |
| 138 | t.lexer.lineno += len(t.value) |
| 139 | |
| 140 | literals = ":{}[];=.," |
| 141 | |
| 142 | # A string containing ignored characters (spaces and tabs) |
| 143 | t_ignore = ' \t' |
| 144 | |
Ole Troan | 17225df | 2018-04-11 09:50:03 +0200 | [diff] [blame] | 145 | |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 146 | def crc_block_combine(block, crc): |
Ole Troan | 5891425 | 2018-10-23 10:50:07 +0200 | [diff] [blame] | 147 | s = str(block).encode() |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 148 | return binascii.crc32(s, crc) & 0xffffffff |
Ole Troan | 5891425 | 2018-10-23 10:50:07 +0200 | [diff] [blame] | 149 | |
Paul Vinciguerra | 063f374 | 2019-07-02 13:00:58 -0400 | [diff] [blame] | 150 | |
Ole Troan | d5a78a5 | 2019-09-18 12:12:47 +0200 | [diff] [blame] | 151 | def vla_is_last_check(name, block): |
| 152 | vla = False |
| 153 | for i, b in enumerate(block): |
| 154 | if isinstance(b, Array) and b.vla: |
| 155 | vla = True |
| 156 | if i + 1 < len(block): |
| 157 | raise ValueError( |
| 158 | 'VLA field "{}" must be the last field in message "{}"' |
| 159 | .format(b.fieldname, name)) |
| 160 | elif b.fieldtype.startswith('vl_api_'): |
| 161 | if global_types[b.fieldtype].vla: |
| 162 | vla = True |
| 163 | if i + 1 < len(block): |
| 164 | raise ValueError( |
| 165 | 'VLA field "{}" must be the last ' |
| 166 | 'field in message "{}"' |
| 167 | .format(b.fieldname, name)) |
| 168 | elif b.fieldtype == 'string' and b.length == 0: |
| 169 | vla = True |
| 170 | if i + 1 < len(block): |
| 171 | raise ValueError( |
| 172 | 'VLA field "{}" must be the last ' |
| 173 | 'field in message "{}"' |
| 174 | .format(b.fieldname, name)) |
| 175 | return vla |
| 176 | |
| 177 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 178 | class Service(): |
Ole Troan | f5db371 | 2020-05-20 15:47:06 +0200 | [diff] [blame] | 179 | def __init__(self, caller, reply, events=None, stream_message=None, stream=False): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 180 | self.caller = caller |
| 181 | self.reply = reply |
| 182 | self.stream = stream |
Ole Troan | f5db371 | 2020-05-20 15:47:06 +0200 | [diff] [blame] | 183 | self.stream_message = stream_message |
Paul Vinciguerra | 7e0c48e | 2019-02-01 19:37:45 -0800 | [diff] [blame] | 184 | self.events = [] if events is None else events |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 185 | |
| 186 | |
| 187 | class Typedef(): |
| 188 | def __init__(self, name, flags, block): |
| 189 | self.name = name |
| 190 | self.flags = flags |
| 191 | self.block = block |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 192 | self.crc = str(block).encode() |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 193 | self.manual_print = False |
| 194 | self.manual_endian = False |
| 195 | for f in flags: |
| 196 | if f == 'manual_print': |
| 197 | self.manual_print = True |
| 198 | elif f == 'manual_endian': |
| 199 | self.manual_endian = True |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 200 | |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 201 | global_type_add(name, self) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 202 | |
Ole Troan | d5a78a5 | 2019-09-18 12:12:47 +0200 | [diff] [blame] | 203 | self.vla = vla_is_last_check(name, block) |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 204 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 205 | def __repr__(self): |
Vratko Polak | 7520e17 | 2019-08-01 10:31:49 +0200 | [diff] [blame] | 206 | return self.name + str(self.flags) + str(self.block) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 207 | |
| 208 | |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 209 | class Using(): |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 210 | def __init__(self, name, flags, alias): |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 211 | self.name = name |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 212 | self.vla = False |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 213 | self.block = [] |
| 214 | self.manual_print = True |
| 215 | self.manual_endian = True |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 216 | |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 217 | self.manual_print = False |
| 218 | self.manual_endian = False |
| 219 | for f in flags: |
| 220 | if f == 'manual_print': |
| 221 | self.manual_print = True |
| 222 | elif f == 'manual_endian': |
| 223 | self.manual_endian = True |
| 224 | |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 225 | if isinstance(alias, Array): |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 226 | a = {'type': alias.fieldtype, |
| 227 | 'length': alias.length} |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 228 | else: |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 229 | a = {'type': alias.fieldtype} |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 230 | self.alias = a |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 231 | self.crc = str(alias).encode() |
| 232 | global_type_add(name, self) |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 233 | |
| 234 | def __repr__(self): |
Vratko Polak | 7520e17 | 2019-08-01 10:31:49 +0200 | [diff] [blame] | 235 | return self.name + str(self.alias) |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 236 | |
| 237 | |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 238 | class Union(): |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 239 | def __init__(self, name, flags, block): |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 240 | self.type = 'Union' |
| 241 | self.manual_print = False |
| 242 | self.manual_endian = False |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 243 | self.name = name |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 244 | |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 245 | for f in flags: |
| 246 | if f == 'manual_print': |
| 247 | self.manual_print = True |
| 248 | elif f == 'manual_endian': |
| 249 | self.manual_endian = True |
| 250 | |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 251 | self.block = block |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 252 | self.crc = str(block).encode() |
Ole Troan | d5a78a5 | 2019-09-18 12:12:47 +0200 | [diff] [blame] | 253 | self.vla = vla_is_last_check(name, block) |
| 254 | |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 255 | global_type_add(name, self) |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 256 | |
| 257 | def __repr__(self): |
Vratko Polak | 7520e17 | 2019-08-01 10:31:49 +0200 | [diff] [blame] | 258 | return str(self.block) |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 259 | |
| 260 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 261 | class Define(): |
| 262 | def __init__(self, name, flags, block): |
| 263 | self.name = name |
| 264 | self.flags = flags |
| 265 | self.block = block |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 266 | self.dont_trace = False |
| 267 | self.manual_print = False |
| 268 | self.manual_endian = False |
| 269 | self.autoreply = False |
| 270 | self.singular = False |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 271 | self.options = {} |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 272 | for f in flags: |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 273 | if f == 'dont_trace': |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 274 | self.dont_trace = True |
| 275 | elif f == 'manual_print': |
| 276 | self.manual_print = True |
| 277 | elif f == 'manual_endian': |
| 278 | self.manual_endian = True |
| 279 | elif f == 'autoreply': |
| 280 | self.autoreply = True |
| 281 | |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 282 | remove = [] |
Ole Troan | d5a78a5 | 2019-09-18 12:12:47 +0200 | [diff] [blame] | 283 | for b in block: |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 284 | if isinstance(b, Option): |
| 285 | if b[1] == 'singular' and b[2] == 'true': |
| 286 | self.singular = True |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 287 | else: |
| 288 | self.options[b.option] = b.value |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 289 | remove.append(b) |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 290 | |
Ole Troan | 14a6c0e | 2020-05-13 11:47:43 +0200 | [diff] [blame] | 291 | block = [x for x in block if x not in remove] |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 292 | self.block = block |
Ole Troan | d5a78a5 | 2019-09-18 12:12:47 +0200 | [diff] [blame] | 293 | self.vla = vla_is_last_check(name, block) |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 294 | self.crc = str(block).encode() |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 295 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 296 | def __repr__(self): |
Vratko Polak | 7520e17 | 2019-08-01 10:31:49 +0200 | [diff] [blame] | 297 | return self.name + str(self.flags) + str(self.block) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 298 | |
| 299 | |
| 300 | class Enum(): |
| 301 | def __init__(self, name, block, enumtype='u32'): |
| 302 | self.name = name |
| 303 | self.enumtype = enumtype |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 304 | self.vla = False |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 305 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 306 | count = 0 |
| 307 | for i, b in enumerate(block): |
| 308 | if type(b) is list: |
| 309 | count = b[1] |
| 310 | else: |
| 311 | count += 1 |
| 312 | block[i] = [b, count] |
| 313 | |
| 314 | self.block = block |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 315 | self.crc = str(block).encode() |
| 316 | global_type_add(name, self) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 317 | |
| 318 | def __repr__(self): |
Vratko Polak | 7520e17 | 2019-08-01 10:31:49 +0200 | [diff] [blame] | 319 | return self.name + str(self.block) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 320 | |
| 321 | |
| 322 | class Import(): |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 323 | |
| 324 | def __new__(cls, *args, **kwargs): |
| 325 | if args[0] not in seen_imports: |
| 326 | instance = super().__new__(cls) |
| 327 | instance._initialized = False |
| 328 | seen_imports[args[0]] = instance |
| 329 | |
| 330 | return seen_imports[args[0]] |
| 331 | |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 332 | def __init__(self, filename, revision): |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 333 | if self._initialized: |
| 334 | return |
| 335 | else: |
| 336 | self.filename = filename |
| 337 | # Deal with imports |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 338 | parser = VPPAPI(filename=filename, revision=revision) |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 339 | dirlist = dirlist_get() |
| 340 | f = filename |
| 341 | for dir in dirlist: |
| 342 | f = os.path.join(dir, filename) |
| 343 | if os.path.exists(f): |
| 344 | break |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 345 | self.result = parser.parse_filename(f, None) |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 346 | self._initialized = True |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 347 | |
| 348 | def __repr__(self): |
Vratko Polak | 7520e17 | 2019-08-01 10:31:49 +0200 | [diff] [blame] | 349 | return self.filename |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 350 | |
| 351 | |
| 352 | class Option(): |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 353 | def __init__(self, option, value): |
| 354 | self.type = 'Option' |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 355 | self.option = option |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 356 | self.value = value |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 357 | self.crc = str(option).encode() |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 358 | |
| 359 | def __repr__(self): |
Vratko Polak | 7520e17 | 2019-08-01 10:31:49 +0200 | [diff] [blame] | 360 | return str(self.option) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 361 | |
| 362 | def __getitem__(self, index): |
| 363 | return self.option[index] |
| 364 | |
| 365 | |
| 366 | class Array(): |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 367 | def __init__(self, fieldtype, name, length, modern_vla=False): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 368 | self.type = 'Array' |
| 369 | self.fieldtype = fieldtype |
| 370 | self.fieldname = name |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 371 | self.modern_vla = modern_vla |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 372 | if type(length) is str: |
| 373 | self.lengthfield = length |
| 374 | self.length = 0 |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 375 | self.vla = True |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 376 | else: |
| 377 | self.length = length |
| 378 | self.lengthfield = None |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 379 | self.vla = False |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 380 | |
| 381 | def __repr__(self): |
Vratko Polak | 7520e17 | 2019-08-01 10:31:49 +0200 | [diff] [blame] | 382 | return str([self.fieldtype, self.fieldname, self.length, |
| 383 | self.lengthfield]) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 384 | |
| 385 | |
| 386 | class Field(): |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 387 | def __init__(self, fieldtype, name, limit=None): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 388 | self.type = 'Field' |
| 389 | self.fieldtype = fieldtype |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 390 | |
| 391 | if self.fieldtype == 'string': |
| 392 | raise ValueError("The string type {!r} is an " |
| 393 | "array type ".format(name)) |
| 394 | |
Paul Vinciguerra | ff47fb6 | 2019-08-06 19:58:24 -0400 | [diff] [blame] | 395 | if name in keyword.kwlist: |
| 396 | raise ValueError("Fieldname {!r} is a python keyword and is not " |
| 397 | "accessible via the python API. ".format(name)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 398 | self.fieldname = name |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 399 | self.limit = limit |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 400 | |
| 401 | def __repr__(self): |
Vratko Polak | 7520e17 | 2019-08-01 10:31:49 +0200 | [diff] [blame] | 402 | return str([self.fieldtype, self.fieldname]) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 403 | |
| 404 | |
| 405 | class Coord(object): |
| 406 | """ Coordinates of a syntactic element. Consists of: |
| 407 | - File name |
| 408 | - Line number |
| 409 | - (optional) column number, for the Lexer |
| 410 | """ |
| 411 | __slots__ = ('file', 'line', 'column', '__weakref__') |
| 412 | |
| 413 | def __init__(self, file, line, column=None): |
| 414 | self.file = file |
| 415 | self.line = line |
| 416 | self.column = column |
| 417 | |
| 418 | def __str__(self): |
| 419 | str = "%s:%s" % (self.file, self.line) |
| 420 | if self.column: |
| 421 | str += ":%s" % self.column |
| 422 | return str |
| 423 | |
| 424 | |
| 425 | class ParseError(Exception): |
| 426 | pass |
| 427 | |
| 428 | |
| 429 | # |
| 430 | # Grammar rules |
| 431 | # |
| 432 | class VPPAPIParser(object): |
| 433 | tokens = VPPAPILexer.tokens |
| 434 | |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 435 | def __init__(self, filename, logger, revision=None): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 436 | self.filename = filename |
| 437 | self.logger = logger |
| 438 | self.fields = [] |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 439 | self.revision = revision |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 440 | |
| 441 | def _parse_error(self, msg, coord): |
| 442 | raise ParseError("%s: %s" % (coord, msg)) |
| 443 | |
| 444 | def _parse_warning(self, msg, coord): |
| 445 | if self.logger: |
| 446 | self.logger.warning("%s: %s" % (coord, msg)) |
| 447 | |
| 448 | def _coord(self, lineno, column=None): |
| 449 | return Coord( |
| 450 | file=self.filename, |
| 451 | line=lineno, column=column) |
| 452 | |
| 453 | def _token_coord(self, p, token_idx): |
| 454 | """ Returns the coordinates for the YaccProduction object 'p' indexed |
| 455 | with 'token_idx'. The coordinate includes the 'lineno' and |
| 456 | 'column'. Both follow the lex semantic, starting from 1. |
| 457 | """ |
| 458 | last_cr = p.lexer.lexdata.rfind('\n', 0, p.lexpos(token_idx)) |
| 459 | if last_cr < 0: |
| 460 | last_cr = -1 |
| 461 | column = (p.lexpos(token_idx) - (last_cr)) |
| 462 | return self._coord(p.lineno(token_idx), column) |
| 463 | |
| 464 | def p_slist(self, p): |
| 465 | '''slist : stmt |
| 466 | | slist stmt''' |
| 467 | if len(p) == 2: |
| 468 | p[0] = [p[1]] |
| 469 | else: |
| 470 | p[0] = p[1] + [p[2]] |
| 471 | |
| 472 | def p_stmt(self, p): |
| 473 | '''stmt : define |
| 474 | | typedef |
| 475 | | option |
| 476 | | import |
| 477 | | enum |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 478 | | union |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 479 | | service''' |
| 480 | p[0] = p[1] |
| 481 | |
| 482 | def p_import(self, p): |
| 483 | '''import : IMPORT STRING_LITERAL ';' ''' |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 484 | p[0] = Import(p[2], revision=self.revision) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 485 | |
| 486 | def p_service(self, p): |
| 487 | '''service : SERVICE '{' service_statements '}' ';' ''' |
| 488 | p[0] = p[3] |
| 489 | |
| 490 | def p_service_statements(self, p): |
| 491 | '''service_statements : service_statement |
| 492 | | service_statements service_statement''' |
| 493 | if len(p) == 2: |
| 494 | p[0] = [p[1]] |
| 495 | else: |
| 496 | p[0] = p[1] + [p[2]] |
| 497 | |
| 498 | def p_service_statement(self, p): |
Marek Gradzki | 51e5968 | 2018-03-06 10:05:44 +0100 | [diff] [blame] | 499 | '''service_statement : RPC ID RETURNS NULL ';' |
| 500 | | RPC ID RETURNS ID ';' |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 501 | | RPC ID RETURNS STREAM ID ';' |
| 502 | | RPC ID RETURNS ID EVENTS event_list ';' ''' |
Marek Gradzki | fc70e3a | 2018-03-06 10:56:26 +0100 | [diff] [blame] | 503 | if p[2] == p[4]: |
| 504 | # Verify that caller and reply differ |
Ole Troan | 17225df | 2018-04-11 09:50:03 +0200 | [diff] [blame] | 505 | self._parse_error( |
| 506 | 'Reply ID ({}) should not be equal to Caller ID'.format(p[2]), |
| 507 | self._token_coord(p, 1)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 508 | if len(p) == 8: |
| 509 | p[0] = Service(p[2], p[4], p[6]) |
| 510 | elif len(p) == 7: |
| 511 | p[0] = Service(p[2], p[5], stream=True) |
| 512 | else: |
| 513 | p[0] = Service(p[2], p[4]) |
| 514 | |
Ole Troan | f5db371 | 2020-05-20 15:47:06 +0200 | [diff] [blame] | 515 | def p_service_statement2(self, p): |
| 516 | '''service_statement : RPC ID RETURNS ID STREAM ID ';' ''' |
| 517 | p[0] = Service(p[2], p[4], stream_message=p[6], stream=True) |
| 518 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 519 | def p_event_list(self, p): |
| 520 | '''event_list : events |
| 521 | | event_list events ''' |
| 522 | if len(p) == 2: |
| 523 | p[0] = [p[1]] |
| 524 | else: |
| 525 | p[0] = p[1] + [p[2]] |
| 526 | |
| 527 | def p_event(self, p): |
| 528 | '''events : ID |
| 529 | | ID ',' ''' |
| 530 | p[0] = p[1] |
| 531 | |
| 532 | def p_enum(self, p): |
| 533 | '''enum : ENUM ID '{' enum_statements '}' ';' ''' |
| 534 | p[0] = Enum(p[2], p[4]) |
| 535 | |
| 536 | def p_enum_type(self, p): |
| 537 | ''' enum : ENUM ID ':' enum_size '{' enum_statements '}' ';' ''' |
| 538 | if len(p) == 9: |
| 539 | p[0] = Enum(p[2], p[6], enumtype=p[4]) |
| 540 | else: |
| 541 | p[0] = Enum(p[2], p[4]) |
| 542 | |
| 543 | def p_enum_size(self, p): |
| 544 | ''' enum_size : U8 |
| 545 | | U16 |
| 546 | | U32 ''' |
| 547 | p[0] = p[1] |
| 548 | |
| 549 | def p_define(self, p): |
| 550 | '''define : DEFINE ID '{' block_statements_opt '}' ';' ''' |
| 551 | self.fields = [] |
| 552 | p[0] = Define(p[2], [], p[4]) |
| 553 | |
| 554 | def p_define_flist(self, p): |
| 555 | '''define : flist DEFINE ID '{' block_statements_opt '}' ';' ''' |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 556 | # Legacy typedef |
| 557 | if 'typeonly' in p[1]: |
Paul Vinciguerra | e717482 | 2019-08-07 00:05:59 -0400 | [diff] [blame] | 558 | self._parse_error('legacy typedef. use typedef: {} {}[{}];' |
| 559 | .format(p[1], p[2], p[4]), |
| 560 | self._token_coord(p, 1)) |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 561 | else: |
| 562 | p[0] = Define(p[3], p[1], p[5]) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 563 | |
| 564 | def p_flist(self, p): |
| 565 | '''flist : flag |
| 566 | | flist flag''' |
| 567 | if len(p) == 2: |
| 568 | p[0] = [p[1]] |
| 569 | else: |
| 570 | p[0] = p[1] + [p[2]] |
| 571 | |
| 572 | def p_flag(self, p): |
| 573 | '''flag : MANUAL_PRINT |
| 574 | | MANUAL_ENDIAN |
| 575 | | DONT_TRACE |
| 576 | | TYPEONLY |
| 577 | | AUTOREPLY''' |
| 578 | if len(p) == 1: |
| 579 | return |
| 580 | p[0] = p[1] |
| 581 | |
| 582 | def p_typedef(self, p): |
| 583 | '''typedef : TYPEDEF ID '{' block_statements_opt '}' ';' ''' |
| 584 | p[0] = Typedef(p[2], [], p[4]) |
| 585 | |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 586 | def p_typedef_flist(self, p): |
| 587 | '''typedef : flist TYPEDEF ID '{' block_statements_opt '}' ';' ''' |
| 588 | p[0] = Typedef(p[3], p[1], p[5]) |
| 589 | |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 590 | def p_typedef_alias(self, p): |
| 591 | '''typedef : TYPEDEF declaration ''' |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 592 | p[0] = Using(p[2].fieldname, [], p[2]) |
| 593 | |
| 594 | def p_typedef_alias_flist(self, p): |
| 595 | '''typedef : flist TYPEDEF declaration ''' |
| 596 | p[0] = Using(p[3].fieldname, p[1], p[3]) |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 597 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 598 | def p_block_statements_opt(self, p): |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 599 | '''block_statements_opt : block_statements ''' |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 600 | p[0] = p[1] |
| 601 | |
| 602 | def p_block_statements(self, p): |
| 603 | '''block_statements : block_statement |
| 604 | | block_statements block_statement''' |
| 605 | if len(p) == 2: |
| 606 | p[0] = [p[1]] |
| 607 | else: |
| 608 | p[0] = p[1] + [p[2]] |
| 609 | |
| 610 | def p_block_statement(self, p): |
| 611 | '''block_statement : declaration |
| 612 | | option ''' |
| 613 | p[0] = p[1] |
| 614 | |
| 615 | def p_enum_statements(self, p): |
| 616 | '''enum_statements : enum_statement |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 617 | | enum_statements enum_statement''' |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 618 | if len(p) == 2: |
| 619 | p[0] = [p[1]] |
| 620 | else: |
| 621 | p[0] = p[1] + [p[2]] |
| 622 | |
| 623 | def p_enum_statement(self, p): |
| 624 | '''enum_statement : ID '=' NUM ',' |
| 625 | | ID ',' ''' |
| 626 | if len(p) == 5: |
| 627 | p[0] = [p[1], p[3]] |
| 628 | else: |
| 629 | p[0] = p[1] |
| 630 | |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 631 | def p_field_options(self, p): |
| 632 | '''field_options : field_option |
| 633 | | field_options field_option''' |
| 634 | if len(p) == 2: |
| 635 | p[0] = p[1] |
| 636 | else: |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 637 | p[0] = {**p[1], **p[2]} |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 638 | |
| 639 | def p_field_option(self, p): |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 640 | '''field_option : ID |
| 641 | | ID '=' assignee ',' |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 642 | | ID '=' assignee |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 643 | |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 644 | ''' |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 645 | if len(p) == 2: |
| 646 | p[0] = {p[1]: None} |
| 647 | else: |
| 648 | p[0] = {p[1]: p[3]} |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 649 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 650 | def p_declaration(self, p): |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 651 | '''declaration : type_specifier ID ';' |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 652 | | type_specifier ID '[' field_options ']' ';' ''' |
| 653 | if len(p) == 7: |
| 654 | p[0] = Field(p[1], p[2], p[4]) |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 655 | elif len(p) == 4: |
| 656 | p[0] = Field(p[1], p[2]) |
| 657 | else: |
Paul Vinciguerra | 582eac5 | 2020-04-03 12:18:40 -0400 | [diff] [blame] | 658 | self._parse_error('ERROR', self._token_coord(p, 1)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 659 | self.fields.append(p[2]) |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 660 | |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 661 | def p_declaration_array_vla(self, p): |
| 662 | '''declaration : type_specifier ID '[' ']' ';' ''' |
| 663 | p[0] = Array(p[1], p[2], 0, modern_vla=True) |
| 664 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 665 | def p_declaration_array(self, p): |
| 666 | '''declaration : type_specifier ID '[' NUM ']' ';' |
| 667 | | type_specifier ID '[' ID ']' ';' ''' |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 668 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 669 | if len(p) != 7: |
| 670 | return self._parse_error( |
| 671 | 'array: %s' % p.value, |
| 672 | self._coord(lineno=p.lineno)) |
| 673 | |
| 674 | # Make this error later |
| 675 | if type(p[4]) is int and p[4] == 0: |
| 676 | # XXX: Line number is wrong |
| 677 | self._parse_warning('Old Style VLA: {} {}[{}];' |
| 678 | .format(p[1], p[2], p[4]), |
| 679 | self._token_coord(p, 1)) |
| 680 | |
| 681 | if type(p[4]) is str and p[4] not in self.fields: |
| 682 | # Verify that length field exists |
| 683 | self._parse_error('Missing length field: {} {}[{}];' |
| 684 | .format(p[1], p[2], p[4]), |
| 685 | self._token_coord(p, 1)) |
| 686 | p[0] = Array(p[1], p[2], p[4]) |
| 687 | |
| 688 | def p_option(self, p): |
| 689 | '''option : OPTION ID '=' assignee ';' ''' |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 690 | p[0] = Option(p[2], p[4]) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 691 | |
| 692 | def p_assignee(self, p): |
| 693 | '''assignee : NUM |
| 694 | | TRUE |
| 695 | | FALSE |
| 696 | | STRING_LITERAL ''' |
| 697 | p[0] = p[1] |
| 698 | |
| 699 | def p_type_specifier(self, p): |
| 700 | '''type_specifier : U8 |
| 701 | | U16 |
| 702 | | U32 |
| 703 | | U64 |
| 704 | | I8 |
| 705 | | I16 |
| 706 | | I32 |
| 707 | | I64 |
| 708 | | F64 |
| 709 | | BOOL |
| 710 | | STRING''' |
| 711 | p[0] = p[1] |
| 712 | |
| 713 | # Do a second pass later to verify that user defined types are defined |
| 714 | def p_typedef_specifier(self, p): |
| 715 | '''type_specifier : ID ''' |
| 716 | if p[1] not in global_types: |
| 717 | self._parse_error('Undefined type: {}'.format(p[1]), |
| 718 | self._token_coord(p, 1)) |
| 719 | p[0] = p[1] |
| 720 | |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 721 | def p_union(self, p): |
| 722 | '''union : UNION ID '{' block_statements_opt '}' ';' ''' |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 723 | p[0] = Union(p[2], [], p[4]) |
| 724 | |
| 725 | def p_union_flist(self, p): |
| 726 | '''union : flist UNION ID '{' block_statements_opt '}' ';' ''' |
| 727 | p[0] = Union(p[3], p[1], p[5]) |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 728 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 729 | # Error rule for syntax errors |
| 730 | def p_error(self, p): |
| 731 | if p: |
| 732 | self._parse_error( |
| 733 | 'before: %s' % p.value, |
| 734 | self._coord(lineno=p.lineno)) |
| 735 | else: |
| 736 | self._parse_error('At end of input', self.filename) |
| 737 | |
| 738 | |
| 739 | class VPPAPI(object): |
| 740 | |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 741 | def __init__(self, debug=False, filename='', logger=None, revision=None): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 742 | self.lexer = lex.lex(module=VPPAPILexer(filename), debug=debug) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 743 | self.parser = yacc.yacc(module=VPPAPIParser(filename, logger, |
| 744 | revision=revision), |
Ole Troan | d6743b1 | 2018-03-07 08:40:58 +0100 | [diff] [blame] | 745 | write_tables=False, debug=debug) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 746 | self.logger = logger |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 747 | self.revision = revision |
| 748 | self.filename = filename |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 749 | |
| 750 | def parse_string(self, code, debug=0, lineno=1): |
| 751 | self.lexer.lineno = lineno |
| 752 | return self.parser.parse(code, lexer=self.lexer, debug=debug) |
| 753 | |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 754 | def parse_fd(self, fd, debug=0): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 755 | data = fd.read() |
| 756 | return self.parse_string(data, debug=debug) |
| 757 | |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 758 | def parse_filename(self, filename, debug=0): |
| 759 | if self.revision: |
| 760 | git_show = f'git show {self.revision}:{filename}' |
Ole Troan | deecc93 | 2020-05-19 12:33:00 +0200 | [diff] [blame] | 761 | proc = Popen(git_show.split(), stdout=PIPE, encoding='utf-8') |
| 762 | try: |
| 763 | data, errs = proc.communicate() |
| 764 | if proc.returncode != 0: |
| 765 | print(f'File not found: {self.revision}:{filename}', file=sys.stderr) |
| 766 | sys.exit(2) |
| 767 | return self.parse_string(data, debug=debug) |
| 768 | except Exception as e: |
| 769 | sys.exit(3) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 770 | else: |
| 771 | try: |
| 772 | with open(filename, encoding='utf-8') as fd: |
| 773 | return self.parse_fd(fd, None) |
| 774 | except FileNotFoundError: |
| 775 | print(f'File not found: {filename}', file=sys.stderr) |
| 776 | sys.exit(2) |
| 777 | |
| 778 | def autoreply_block(self, name, parent): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 779 | block = [Field('u32', 'context'), |
| 780 | Field('i32', 'retval')] |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 781 | # inherhit the parent's options |
Ole Troan | 14a6c0e | 2020-05-13 11:47:43 +0200 | [diff] [blame] | 782 | for k, v in parent.options.items(): |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 783 | block.append(Option(k, v)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 784 | return Define(name + '_reply', [], block) |
| 785 | |
| 786 | def process(self, objs): |
| 787 | s = {} |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 788 | s['Option'] = {} |
| 789 | s['Define'] = [] |
| 790 | s['Service'] = [] |
| 791 | s['types'] = [] |
| 792 | s['Import'] = [] |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 793 | crc = 0 |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 794 | for o in objs: |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 795 | tname = o.__class__.__name__ |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 796 | try: |
Mark Nelson | ea2abba | 2020-03-04 15:32:09 -0500 | [diff] [blame] | 797 | crc = binascii.crc32(o.crc, crc) & 0xffffffff |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 798 | except AttributeError: |
| 799 | pass |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 800 | if isinstance(o, Define): |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 801 | s[tname].append(o) |
| 802 | if o.autoreply: |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 803 | s[tname].append(self.autoreply_block(o.name, o)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 804 | elif isinstance(o, Option): |
Ole Troan | 59b6c0c | 2020-02-04 09:12:00 +0100 | [diff] [blame] | 805 | s[tname][o.option] = o.value |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 806 | elif type(o) is list: |
| 807 | for o2 in o: |
| 808 | if isinstance(o2, Service): |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 809 | s['Service'].append(o2) |
Ole Troan | 5891425 | 2018-10-23 10:50:07 +0200 | [diff] [blame] | 810 | elif (isinstance(o, Enum) or |
| 811 | isinstance(o, Typedef) or |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 812 | isinstance(o, Using) or |
Ole Troan | 5891425 | 2018-10-23 10:50:07 +0200 | [diff] [blame] | 813 | isinstance(o, Union)): |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 814 | s['types'].append(o) |
| 815 | else: |
| 816 | if tname not in s: |
Ole Troan | 5891425 | 2018-10-23 10:50:07 +0200 | [diff] [blame] | 817 | raise ValueError('Unknown class type: {} {}' |
| 818 | .format(tname, o)) |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 819 | s[tname].append(o) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 820 | |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 821 | msgs = {d.name: d for d in s['Define']} |
| 822 | svcs = {s.caller: s for s in s['Service']} |
| 823 | replies = {s.reply: s for s in s['Service']} |
Marek Gradzki | 51e5968 | 2018-03-06 10:05:44 +0100 | [diff] [blame] | 824 | seen_services = {} |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 825 | |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 826 | s['file_crc'] = crc |
| 827 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 828 | for service in svcs: |
| 829 | if service not in msgs: |
Ole Troan | 17225df | 2018-04-11 09:50:03 +0200 | [diff] [blame] | 830 | raise ValueError( |
| 831 | 'Service definition refers to unknown message' |
| 832 | ' definition: {}'.format(service)) |
| 833 | if svcs[service].reply != 'null' and \ |
| 834 | svcs[service].reply not in msgs: |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 835 | raise ValueError('Service definition refers to unknown message' |
| 836 | ' definition in reply: {}' |
| 837 | .format(svcs[service].reply)) |
Marek Gradzki | b533f3f | 2018-03-06 11:10:56 +0100 | [diff] [blame] | 838 | if service in replies: |
| 839 | raise ValueError('Service definition refers to message' |
| 840 | ' marked as reply: {}'.format(service)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 841 | for event in svcs[service].events: |
| 842 | if event not in msgs: |
| 843 | raise ValueError('Service definition refers to unknown ' |
| 844 | 'event: {} in message: {}' |
| 845 | .format(event, service)) |
Marek Gradzki | 51e5968 | 2018-03-06 10:05:44 +0100 | [diff] [blame] | 846 | seen_services[event] = True |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 847 | |
Marek Gradzki | 51e5968 | 2018-03-06 10:05:44 +0100 | [diff] [blame] | 848 | # Create services implicitly |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 849 | for d in msgs: |
Marek Gradzki | 51e5968 | 2018-03-06 10:05:44 +0100 | [diff] [blame] | 850 | if d in seen_services: |
| 851 | continue |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 852 | if msgs[d].singular is True: |
| 853 | continue |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 854 | if d.endswith('_reply'): |
| 855 | if d[:-6] in svcs: |
| 856 | continue |
| 857 | if d[:-6] not in msgs: |
Marek Gradzki | cc13471 | 2018-03-06 12:25:02 +0100 | [diff] [blame] | 858 | raise ValueError('{} missing calling message' |
| 859 | .format(d)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 860 | continue |
| 861 | if d.endswith('_dump'): |
| 862 | if d in svcs: |
| 863 | continue |
| 864 | if d[:-5]+'_details' in msgs: |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 865 | s['Service'].append(Service(d, d[:-5]+'_details', |
Ole Troan | 5891425 | 2018-10-23 10:50:07 +0200 | [diff] [blame] | 866 | stream=True)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 867 | else: |
Marek Gradzki | cc13471 | 2018-03-06 12:25:02 +0100 | [diff] [blame] | 868 | raise ValueError('{} missing details message' |
| 869 | .format(d)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 870 | continue |
| 871 | |
| 872 | if d.endswith('_details'): |
| 873 | if d[:-8]+'_dump' not in msgs: |
Marek Gradzki | cc13471 | 2018-03-06 12:25:02 +0100 | [diff] [blame] | 874 | raise ValueError('{} missing dump message' |
| 875 | .format(d)) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 876 | continue |
| 877 | |
| 878 | if d in svcs: |
| 879 | continue |
| 880 | if d+'_reply' in msgs: |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 881 | s['Service'].append(Service(d, d+'_reply')) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 882 | else: |
Ole Troan | 17225df | 2018-04-11 09:50:03 +0200 | [diff] [blame] | 883 | raise ValueError( |
| 884 | '{} missing reply message ({}) or service definition' |
| 885 | .format(d, d+'_reply')) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 886 | |
| 887 | return s |
| 888 | |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 889 | def process_imports(self, objs, in_import, result): |
Marek Gradzki | 51e5968 | 2018-03-06 10:05:44 +0100 | [diff] [blame] | 890 | imported_objs = [] |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 891 | for o in objs: |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 892 | # Only allow the following object types from imported file |
| 893 | if in_import and not (isinstance(o, Enum) or |
| 894 | isinstance(o, Union) or |
Ole Troan | 10a0989 | 2018-06-29 11:32:33 +0200 | [diff] [blame] | 895 | isinstance(o, Typedef) or |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 896 | isinstance(o, Import) or |
| 897 | isinstance(o, Using)): |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 898 | continue |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 899 | if isinstance(o, Import): |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 900 | result.append(o) |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 901 | result = self.process_imports(o.result, True, result) |
Ole Troan | 10a0989 | 2018-06-29 11:32:33 +0200 | [diff] [blame] | 902 | else: |
| 903 | result.append(o) |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 904 | return result |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 905 | |
Ole Troan | 5891425 | 2018-10-23 10:50:07 +0200 | [diff] [blame] | 906 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 907 | # Add message ids to each message. |
| 908 | def add_msg_id(s): |
| 909 | for o in s: |
| 910 | o.block.insert(0, Field('u16', '_vl_msg_id')) |
| 911 | return s |
| 912 | |
| 913 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 914 | dirlist = [] |
| 915 | |
| 916 | |
| 917 | def dirlist_add(dirs): |
| 918 | global dirlist |
| 919 | if dirs: |
| 920 | dirlist = dirlist + dirs |
| 921 | |
| 922 | |
| 923 | def dirlist_get(): |
| 924 | return dirlist |
| 925 | |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 926 | |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 927 | def foldup_blocks(block, crc): |
| 928 | for b in block: |
| 929 | # Look up CRC in user defined types |
| 930 | if b.fieldtype.startswith('vl_api_'): |
| 931 | # Recursively |
| 932 | t = global_types[b.fieldtype] |
| 933 | try: |
| 934 | crc = crc_block_combine(t.block, crc) |
| 935 | return foldup_blocks(t.block, crc) |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 936 | except AttributeError: |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 937 | pass |
| 938 | return crc |
| 939 | |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 940 | |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 941 | def foldup_crcs(s): |
| 942 | for f in s: |
| 943 | f.crc = foldup_blocks(f.block, |
Mark Nelson | ea2abba | 2020-03-04 15:32:09 -0500 | [diff] [blame] | 944 | binascii.crc32(f.crc) & 0xffffffff) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 945 | |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 946 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 947 | # |
| 948 | # Main |
| 949 | # |
| 950 | def main(): |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 951 | if sys.version_info < (3, 5,): |
| 952 | log.exception('vppapigen requires a supported version of python. ' |
| 953 | 'Please use version 3.5 or greater. ' |
| 954 | 'Using {}'.format(sys.version)) |
| 955 | return 1 |
| 956 | |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 957 | cliparser = argparse.ArgumentParser(description='VPP API generator') |
| 958 | cliparser.add_argument('--pluginpath', default=""), |
| 959 | cliparser.add_argument('--includedir', action='append'), |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 960 | cliparser.add_argument('--outputdir', action='store'), |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 961 | cliparser.add_argument('--input') |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 962 | cliparser.add_argument('--output', nargs='?', |
| 963 | type=argparse.FileType('w', encoding='UTF-8'), |
| 964 | default=sys.stdout) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 965 | |
| 966 | cliparser.add_argument('output_module', nargs='?', default='C') |
| 967 | cliparser.add_argument('--debug', action='store_true') |
| 968 | cliparser.add_argument('--show-name', nargs=1) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 969 | cliparser.add_argument('--git-revision', |
| 970 | help="Git revision to use for opening files") |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 971 | args = cliparser.parse_args() |
| 972 | |
| 973 | dirlist_add(args.includedir) |
| 974 | if not args.debug: |
| 975 | sys.excepthook = exception_handler |
| 976 | |
| 977 | # Filename |
| 978 | if args.show_name: |
| 979 | filename = args.show_name[0] |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 980 | elif args.input: |
| 981 | filename = args.input |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 982 | else: |
| 983 | filename = '' |
| 984 | |
Marek Gradzki | 51e5968 | 2018-03-06 10:05:44 +0100 | [diff] [blame] | 985 | if args.debug: |
| 986 | logging.basicConfig(stream=sys.stdout, level=logging.WARNING) |
| 987 | else: |
| 988 | logging.basicConfig() |
Marek Gradzki | 51e5968 | 2018-03-06 10:05:44 +0100 | [diff] [blame] | 989 | |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 990 | parser = VPPAPI(debug=args.debug, filename=filename, logger=log, |
| 991 | revision=args.git_revision) |
| 992 | |
| 993 | try: |
| 994 | if not args.input: |
| 995 | parsed_objects = parser.parse_fd(sys.stdin, log) |
| 996 | else: |
| 997 | parsed_objects = parser.parse_filename(args.input, log) |
| 998 | except ParseError as e: |
| 999 | print('Parse error: ', e, file=sys.stderr) |
| 1000 | sys.exit(1) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1001 | |
| 1002 | # Build a list of objects. Hash of lists. |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 1003 | result = [] |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 1004 | |
| 1005 | if args.output_module == 'C': |
| 1006 | s = parser.process(parsed_objects) |
| 1007 | else: |
Paul Vinciguerra | 4bf8490 | 2019-07-31 00:34:05 -0400 | [diff] [blame] | 1008 | result = parser.process_imports(parsed_objects, False, result) |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 1009 | s = parser.process(result) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1010 | |
| 1011 | # Add msg_id field |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 1012 | s['Define'] = add_msg_id(s['Define']) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1013 | |
Ole Troan | 8dbfb43 | 2019-04-24 14:31:18 +0200 | [diff] [blame] | 1014 | # Fold up CRCs |
| 1015 | foldup_crcs(s['Define']) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1016 | |
| 1017 | # |
| 1018 | # Debug |
| 1019 | if args.debug: |
| 1020 | import pprint |
Ole Troan | 10a0989 | 2018-06-29 11:32:33 +0200 | [diff] [blame] | 1021 | pp = pprint.PrettyPrinter(indent=4, stream=sys.stderr) |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 1022 | for t in s['Define']: |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1023 | pp.pprint([t.name, t.flags, t.block]) |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 1024 | for t in s['types']: |
| 1025 | pp.pprint([t.name, t.block]) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1026 | |
| 1027 | # |
| 1028 | # Generate representation |
| 1029 | # |
Paul Vinciguerra | f4647ed | 2019-02-12 12:21:01 -0800 | [diff] [blame] | 1030 | from importlib.machinery import SourceFileLoader |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1031 | |
| 1032 | # Default path |
Ole Troan | 3078737 | 2018-03-01 13:33:39 +0100 | [diff] [blame] | 1033 | pluginpath = '' |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1034 | if not args.pluginpath: |
Ole Troan | 3078737 | 2018-03-01 13:33:39 +0100 | [diff] [blame] | 1035 | cand = [] |
| 1036 | cand.append(os.path.dirname(os.path.realpath(__file__))) |
Ole Troan | 17225df | 2018-04-11 09:50:03 +0200 | [diff] [blame] | 1037 | cand.append(os.path.dirname(os.path.realpath(__file__)) + |
Ole Troan | 3078737 | 2018-03-01 13:33:39 +0100 | [diff] [blame] | 1038 | '/../share/vpp/') |
| 1039 | for c in cand: |
| 1040 | c += '/' |
Ole Troan | 5891425 | 2018-10-23 10:50:07 +0200 | [diff] [blame] | 1041 | if os.path.isfile('{}vppapigen_{}.py' |
| 1042 | .format(c, args.output_module.lower())): |
Ole Troan | 3078737 | 2018-03-01 13:33:39 +0100 | [diff] [blame] | 1043 | pluginpath = c |
| 1044 | break |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1045 | else: |
| 1046 | pluginpath = args.pluginpath + '/' |
Ole Troan | 3078737 | 2018-03-01 13:33:39 +0100 | [diff] [blame] | 1047 | if pluginpath == '': |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 1048 | log.exception('Output plugin not found') |
| 1049 | return 1 |
Ole Troan | 5891425 | 2018-10-23 10:50:07 +0200 | [diff] [blame] | 1050 | module_path = '{}vppapigen_{}.py'.format(pluginpath, |
| 1051 | args.output_module.lower()) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1052 | |
| 1053 | try: |
Paul Vinciguerra | f4647ed | 2019-02-12 12:21:01 -0800 | [diff] [blame] | 1054 | plugin = SourceFileLoader(args.output_module, |
| 1055 | module_path).load_module() |
Ole Troan | 5891425 | 2018-10-23 10:50:07 +0200 | [diff] [blame] | 1056 | except Exception as err: |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 1057 | log.exception('Error importing output plugin: {}, {}' |
| 1058 | .format(module_path, err)) |
| 1059 | return 1 |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1060 | |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 1061 | result = plugin.run(args, filename, s) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1062 | if result: |
Ole Troan | 17225df | 2018-04-11 09:50:03 +0200 | [diff] [blame] | 1063 | print(result, file=args.output) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1064 | else: |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 1065 | log.exception('Running plugin failed: {} {}' |
| 1066 | .format(filename, result)) |
| 1067 | return 1 |
| 1068 | return 0 |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1069 | |
| 1070 | |
| 1071 | if __name__ == '__main__': |
Paul Vinciguerra | 2cd3cc8 | 2019-08-06 22:02:45 -0400 | [diff] [blame] | 1072 | sys.exit(main()) |