codegen.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2022 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import logging
  16. import sys
  17. import click
  18. try:
  19. import coloredlogs
  20. _has_coloredlogs = True
  21. except ImportError:
  22. _has_coloredlogs = False
  23. try:
  24. from matter_idl.matter_idl_parser import CreateParser
  25. except ImportError:
  26. import os
  27. sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'py_matter_idl')))
  28. from matter_idl.matter_idl_parser import CreateParser
  29. # isort: off
  30. from matter_idl.generators import FileSystemGeneratorStorage, GeneratorStorage
  31. from matter_idl.generators.registry import CodeGenerator, GENERATORS
  32. class ListGeneratedFilesStorage(GeneratorStorage):
  33. """
  34. A storage that prints out file names that would have content in them.
  35. """
  36. def __init__(self):
  37. super().__init__()
  38. def get_existing_data(self, relative_path: str):
  39. return None # stdout has no pre-existing data
  40. def write_new_data(self, relative_path: str, content: str):
  41. print(relative_path)
  42. # Supported log levels, mapping string values required for argument
  43. # parsing into logging constants
  44. __LOG_LEVELS__ = {
  45. 'debug': logging.DEBUG,
  46. 'info': logging.INFO,
  47. 'warn': logging.WARN,
  48. 'fatal': logging.FATAL,
  49. }
  50. @click.command()
  51. @click.option(
  52. '--log-level',
  53. default='INFO',
  54. type=click.Choice(__LOG_LEVELS__.keys(), case_sensitive=False),
  55. help='Determines the verbosity of script output')
  56. @click.option(
  57. '--generator',
  58. default='JAVA',
  59. help='What code generator to run. The choices are: '+'|'.join(GENERATORS.keys())+'. ' +
  60. 'When using custom, provide the plugin path using `--generator custom:<path_to_plugin>:<plugin_module_name>` syntax. ' +
  61. 'For example, `--generator custom:./my_plugin:my_plugin_module` will load `./my_plugin/my_plugin_module/__init.py__` ' +
  62. 'that defines a subclass of CodeGenerator named CustomGenerator.')
  63. @click.option(
  64. '--output-dir',
  65. type=click.Path(exists=False),
  66. default=".",
  67. help='Where to generate the code')
  68. @click.option(
  69. '--dry-run',
  70. default=False,
  71. is_flag=True,
  72. help='If to actually generate')
  73. @click.option(
  74. '--name-only',
  75. default=False,
  76. is_flag=True,
  77. help='Output just a list of file names that would be generated')
  78. @click.option(
  79. '--expected-outputs',
  80. type=click.Path(exists=True),
  81. default=None,
  82. help='A file containing all expected outputs. Script will fail if outputs do not match')
  83. @click.argument(
  84. 'idl_path',
  85. type=click.Path(exists=True))
  86. def main(log_level, generator, output_dir, dry_run, name_only, expected_outputs, idl_path):
  87. """
  88. Parses MATTER IDL files (.matter) and performs SDK code generation
  89. as set up by the program arguments.
  90. """
  91. if _has_coloredlogs:
  92. coloredlogs.install(level=__LOG_LEVELS__[
  93. log_level], fmt='%(asctime)s %(levelname)-7s %(message)s')
  94. else:
  95. logging.basicConfig(
  96. level=__LOG_LEVELS__[log_level],
  97. format='%(asctime)s %(levelname)-7s %(message)s',
  98. datefmt='%Y-%m-%d %H:%M:%S'
  99. )
  100. if name_only:
  101. storage = ListGeneratedFilesStorage()
  102. else:
  103. storage = FileSystemGeneratorStorage(output_dir)
  104. logging.info("Parsing idl from %s" % idl_path)
  105. idl_tree = CreateParser().parse(open(idl_path, "rt").read())
  106. plugin_module = None
  107. if generator.startswith('custom'):
  108. # check that the plugin path is provided
  109. if ':' not in generator:
  110. logging.fatal("Custom generator plugin path not provided. Use --generator custom:<path_to_plugin>")
  111. sys.exit(1)
  112. custom_params = generator.split(':')
  113. (generator, plugin_path, plugin_module) = custom_params
  114. logging.info("Using CustomGenerator at plugin path %s.%s" % (plugin_path, plugin_module))
  115. sys.path.append(plugin_path)
  116. generator = 'CUSTOM'
  117. logging.info("Running code generator %s" % generator)
  118. generator = CodeGenerator.FromString(generator).Create(storage, idl=idl_tree, plugin_module=plugin_module)
  119. generator.render(dry_run)
  120. if expected_outputs:
  121. with open(expected_outputs, 'rt') as fin:
  122. expected = set()
  123. for l in fin.readlines():
  124. l = l.strip()
  125. if l:
  126. expected.add(l)
  127. if expected != storage.generated_paths:
  128. logging.fatal("expected and generated files do not match.")
  129. extra = storage.generated_paths - expected
  130. missing = expected - storage.generated_paths
  131. for name in extra:
  132. logging.fatal(" '%s' was generated but not expected" % name)
  133. for name in missing:
  134. logging.fatal(" '%s' was expected but not generated" % name)
  135. sys.exit(1)
  136. logging.info("Done")
  137. if __name__ == '__main__':
  138. main(auto_envvar_prefix='CHIP')