main.py 3.6 KB

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