|
| 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 | + |
| 20 | +import click |
| 21 | + |
| 22 | +try: |
| 23 | + import coloredlogs |
| 24 | + _has_coloredlogs = True |
| 25 | +except ImportError: |
| 26 | + _has_coloredlogs = False |
| 27 | + |
| 28 | +try: |
| 29 | + from matter_idl.matter_idl_parser import CreateParser |
| 30 | +except ImportError: |
| 31 | + import os |
| 32 | + sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'py_matter_idl'))) |
| 33 | + from matter_idl.matter_idl_parser import CreateParser |
| 34 | + |
| 35 | +from matter_idl.backwards_compatibility import is_backwards_compatible |
| 36 | + |
| 37 | +# Supported log levels, mapping string values required for argument |
| 38 | +# parsing into logging constants |
| 39 | +__LOG_LEVELS__ = { |
| 40 | + 'debug': logging.DEBUG, |
| 41 | + 'info': logging.INFO, |
| 42 | + 'warn': logging.WARN, |
| 43 | + 'fatal': logging.FATAL, |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +@click.command() |
| 48 | +@click.option( |
| 49 | + '--log-level', |
| 50 | + default='INFO', |
| 51 | + type=click.Choice(list(__LOG_LEVELS__.keys()), case_sensitive=False), |
| 52 | + help='Determines the verbosity of script output') |
| 53 | +@click.argument( |
| 54 | + 'old_idl', |
| 55 | + type=click.Path(exists=True)) |
| 56 | +@click.argument( |
| 57 | + 'new_idl', |
| 58 | + type=click.Path(exists=True)) |
| 59 | +def main(log_level, old_idl, new_idl): |
| 60 | + """ |
| 61 | + Parses MATTER IDL files (.matter) and validates that <new_idl> is backwards compatible |
| 62 | + when compared to <old_idl>. |
| 63 | +
|
| 64 | + Generally additions are safe, but not deletes or id changes. Actual set of rules |
| 65 | + defined in `backwards_compatibility` module. |
| 66 | + """ |
| 67 | + if _has_coloredlogs: |
| 68 | + coloredlogs.install(level=__LOG_LEVELS__[ |
| 69 | + log_level], fmt='%(asctime)s %(levelname)-7s %(message)s') |
| 70 | + else: |
| 71 | + logging.basicConfig( |
| 72 | + level=__LOG_LEVELS__[log_level], |
| 73 | + format='%(asctime)s %(levelname)-7s %(message)s', |
| 74 | + datefmt='%Y-%m-%d %H:%M:%S' |
| 75 | + ) |
| 76 | + |
| 77 | + logging.info("Parsing OLD idl from %s" % old_idl) |
| 78 | + old_tree = CreateParser().parse(open(old_idl, "rt").read()) |
| 79 | + |
| 80 | + logging.info("Parsing NEW idl from %s" % new_idl) |
| 81 | + new_tree = CreateParser().parse(open(new_idl, "rt").read()) |
| 82 | + |
| 83 | + if not is_backwards_compatible(original=old_tree, updated=new_tree): |
| 84 | + sys.exit(1) |
| 85 | + |
| 86 | + sys.exit(0) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + main(auto_envvar_prefix='CHIP') |
0 commit comments