backwards_compatibility_checker.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (c) 2023 Project CHIP Authors
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. #
  17. import logging
  18. import sys
  19. import click
  20. try:
  21. import coloredlogs
  22. _has_coloredlogs = True
  23. except ImportError:
  24. _has_coloredlogs = False
  25. try:
  26. from matter_idl.matter_idl_parser import CreateParser
  27. except ImportError:
  28. import os
  29. sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'py_matter_idl')))
  30. from matter_idl.matter_idl_parser import CreateParser
  31. from matter_idl.backwards_compatibility import is_backwards_compatible
  32. # Supported log levels, mapping string values required for argument
  33. # parsing into logging constants
  34. __LOG_LEVELS__ = {
  35. 'debug': logging.DEBUG,
  36. 'info': logging.INFO,
  37. 'warn': logging.WARN,
  38. 'fatal': logging.FATAL,
  39. }
  40. @click.command()
  41. @click.option(
  42. '--log-level',
  43. default='INFO',
  44. type=click.Choice(list(__LOG_LEVELS__.keys()), case_sensitive=False),
  45. help='Determines the verbosity of script output')
  46. @click.argument(
  47. 'old_idl',
  48. type=click.Path(exists=True))
  49. @click.argument(
  50. 'new_idl',
  51. type=click.Path(exists=True))
  52. def main(log_level, old_idl, new_idl):
  53. """
  54. Parses MATTER IDL files (.matter) and validates that <new_idl> is backwards compatible
  55. when compared to <old_idl>.
  56. Generally additions are safe, but not deletes or id changes. Actual set of rules
  57. defined in `backwards_compatibility` module.
  58. """
  59. if _has_coloredlogs:
  60. coloredlogs.install(level=__LOG_LEVELS__[
  61. log_level], fmt='%(asctime)s %(levelname)-7s %(message)s')
  62. else:
  63. logging.basicConfig(
  64. level=__LOG_LEVELS__[log_level],
  65. format='%(asctime)s %(levelname)-7s %(message)s',
  66. datefmt='%Y-%m-%d %H:%M:%S'
  67. )
  68. logging.info("Parsing OLD idl from %s" % old_idl)
  69. old_tree = CreateParser().parse(open(old_idl, "rt").read())
  70. logging.info("Parsing NEW idl from %s" % new_idl)
  71. new_tree = CreateParser().parse(open(new_idl, "rt").read())
  72. if not is_backwards_compatible(original=old_tree, updated=new_tree):
  73. sys.exit(1)
  74. sys.exit(0)
  75. if __name__ == '__main__':
  76. main(auto_envvar_prefix='CHIP')