master
py 109 lines 3.57 KB
Raw
1 # This work is licensed under the terms of the GNU GPL, version 2 or later.
2 # See the COPYING file in the top-level directory.
3
4 """
5 QAPI Generator
6
7 This is the main entry point for generating C code from the QAPI schema.
8 """
9
10 import argparse
11 from importlib import import_module
12 import sys
13 from typing import Optional
14
15 from .backend import QAPIBackend, QAPICBackend
16 from .common import must_match
17 from .error import QAPIError
18 from .schema import QAPISchema
19
20
21 def invalid_prefix_char(prefix: str) -> Optional[str]:
22 match = must_match(r'([A-Za-z_.-][A-Za-z0-9_.-]*)?', prefix)
23 if match.end() != len(prefix):
24 return prefix[match.end()]
25 return None
26
27
28 def create_backend(path: str) -> QAPIBackend:
29 if path is None:
30 return QAPICBackend()
31
32 module_path, dot, class_name = path.rpartition('.')
33 if not dot:
34 raise QAPIError("argument of -B must be of the form MODULE.CLASS")
35
36 try:
37 mod = import_module(module_path)
38 except Exception as ex:
39 raise QAPIError(f"unable to import '{module_path}': {ex}") from ex
40
41 try:
42 klass = getattr(mod, class_name)
43 except AttributeError as ex:
44 raise QAPIError(
45 f"module '{module_path}' has no class '{class_name}'") from ex
46
47 try:
48 backend = klass()
49 except Exception as ex:
50 raise QAPIError(
51 f"backend '{path}' cannot be instantiated: {ex}") from ex
52
53 if not isinstance(backend, QAPIBackend):
54 raise QAPIError(
55 f"backend '{path}' must be an instance of QAPIBackend")
56
57 return backend
58
59
60 def main() -> int:
61 """
62 gapi-gen executable entry point.
63 Expects arguments via sys.argv, see --help for details.
64
65 :return: int, 0 on success, 1 on failure.
66 """
67 parser = argparse.ArgumentParser(
68 description='Generate code from a QAPI schema')
69 parser.add_argument('-b', '--builtins', action='store_true',
70 help="generate code for built-in types")
71 parser.add_argument('-o', '--output-dir', action='store',
72 default='',
73 help="write output to directory OUTPUT_DIR")
74 parser.add_argument('-p', '--prefix', action='store',
75 default='',
76 help="prefix for symbols")
77 parser.add_argument('-u', '--unmask-non-abi-names', action='store_true',
78 dest='unmask',
79 help="expose non-ABI names in introspection")
80 parser.add_argument('-B', '--backend', default=None,
81 help="Python module name for code generator")
82
83 # Option --suppress-tracing exists so we can avoid solving build system
84 # problems. TODO Drop it when we no longer need it.
85 parser.add_argument('--suppress-tracing', action='store_true',
86 help="suppress adding trace events to qmp marshals")
87
88 parser.add_argument('schema', action='store')
89 args = parser.parse_args()
90
91 funny_char = invalid_prefix_char(args.prefix)
92 if funny_char:
93 msg = f"funny character '{funny_char}' in argument of --prefix"
94 print(f"{sys.argv[0]}: {msg}", file=sys.stderr)
95 return 1
96
97 try:
98 schema = QAPISchema(args.schema)
99 backend = create_backend(args.backend)
100 backend.generate(schema,
101 output_dir=args.output_dir,
102 prefix=args.prefix,
103 unmask=args.unmask,
104 builtins=args.builtins,
105 gen_tracing=not args.suppress_tracing)
106 except QAPIError as err:
107 print(err, file=sys.stderr)
108 return 1
109 return 0