diff --git a/src/tools/vppapigen/vppapigen.py b/src/tools/vppapigen/vppapigen.py index 79f2da91b19..5b70d0ae329 100755 --- a/src/tools/vppapigen/vppapigen.py +++ b/src/tools/vppapigen/vppapigen.py @@ -1,6 +1,5 @@ #!/usr/bin/python3 -from __future__ import print_function import ply.lex as lex import ply.yacc as yacc import sys @@ -9,6 +8,8 @@ import logging import binascii import os +log = logging.getLogger('vppapigen') + # Ensure we don't leave temporary files around sys.dont_write_bytecode = True @@ -289,12 +290,9 @@ class Import(): f = os.path.join(dir, filename) if os.path.exists(f): break - if sys.version[0] == '2': - with open(f) as fd: - self.result = parser.parse_file(fd, None) - else: - with open(f, encoding='utf-8') as fd: - self.result = parser.parse_file(fd, None) + + with open(f, encoding='utf-8') as fd: + self.result = parser.parse_file(fd, None) def __repr__(self): return self.filename @@ -822,6 +820,7 @@ def dirlist_add(dirs): def dirlist_get(): return dirlist + def foldup_blocks(block, crc): for b in block: # Look up CRC in user defined types @@ -835,32 +834,32 @@ def foldup_blocks(block, crc): pass return crc + def foldup_crcs(s): for f in s: f.crc = foldup_blocks(f.block, binascii.crc32(f.crc)) + # # Main # def main(): + if sys.version_info < (3, 5,): + log.exception('vppapigen requires a supported version of python. ' + 'Please use version 3.5 or greater. ' + 'Using {}'.format(sys.version)) + return 1 + cliparser = argparse.ArgumentParser(description='VPP API generator') cliparser.add_argument('--pluginpath', default=""), cliparser.add_argument('--includedir', action='append'), - if sys.version[0] == '2': - cliparser.add_argument('--input', type=argparse.FileType('r'), - default=sys.stdin) - cliparser.add_argument('--output', nargs='?', - type=argparse.FileType('w'), - default=sys.stdout) - - else: - cliparser.add_argument('--input', - type=argparse.FileType('r', encoding='UTF-8'), - default=sys.stdin) - cliparser.add_argument('--output', nargs='?', - type=argparse.FileType('w', encoding='UTF-8'), - default=sys.stdout) + cliparser.add_argument('--input', + type=argparse.FileType('r', encoding='UTF-8'), + default=sys.stdin) + cliparser.add_argument('--output', nargs='?', + type=argparse.FileType('w', encoding='UTF-8'), + default=sys.stdout) cliparser.add_argument('output_module', nargs='?', default='C') cliparser.add_argument('--debug', action='store_true') @@ -883,7 +882,6 @@ def main(): logging.basicConfig(stream=sys.stdout, level=logging.WARNING) else: logging.basicConfig() - log = logging.getLogger('vppapigen') parser = VPPAPI(debug=args.debug, filename=filename, logger=log) parsed_objects = parser.parse_file(args.input, log) @@ -930,7 +928,8 @@ def main(): else: pluginpath = args.pluginpath + '/' if pluginpath == '': - raise Exception('Output plugin not found') + log.exception('Output plugin not found') + return 1 module_path = '{}vppapigen_{}.py'.format(pluginpath, args.output_module.lower()) @@ -938,16 +937,19 @@ def main(): plugin = SourceFileLoader(args.output_module, module_path).load_module() except Exception as err: - raise Exception('Error importing output plugin: {}, {}' - .format(module_path, err)) + log.exception('Error importing output plugin: {}, {}' + .format(module_path, err)) + return 1 result = plugin.run(filename, s) if result: print(result, file=args.output) else: - raise Exception('Running plugin failed: {} {}' - .format(filename, result)) + log.exception('Running plugin failed: {} {}' + .format(filename, result)) + return 1 + return 0 if __name__ == '__main__': - main() + sys.exit(main())