From f9748f00d128ec779dc6b52beaf08d8f13f7a215 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Thu, 9 Jun 2022 11:22:15 -0400 Subject: [PATCH] Add linter rule for ZCL file being in sync --- .github/workflows/lint.yml | 4 ++ scripts/tools/check_zcl_file_sync.py | 71 ++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100755 scripts/tools/check_zcl_file_sync.py diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index b5c296573021d7..ce0a12059f6916 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -85,6 +85,10 @@ jobs: if: always() run: scripts/tools/check_includes.sh + - name: Check for zcl.json and extension sync status + if: always() + run: scripts/tools/check_zcl_file_sync.py . + - name: Ensure all PICS are set for tests (to true or false) if: always() run: | diff --git a/scripts/tools/check_zcl_file_sync.py b/scripts/tools/check_zcl_file_sync.py new file mode 100755 index 00000000000000..cef9516f720891 --- /dev/null +++ b/scripts/tools/check_zcl_file_sync.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 +# +# Copyright (c) 2022 Project CHIP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Validates that the json zcl files that are used by the app are in sync. + +Current rule: + - all-clusters app uses an extension on top of the "standard" zcl file. + Ensure that the two fies are in sync EXCEPT the extension. +""" + +import json +import sys +import os +import difflib + +def main(): + if len(sys.argv) != 2: + print('Please pass CHIP_ROOT as an argument (and only chip root)'); + return 1 + + base_name = os.path.join(sys.argv[1], "src", "app", "zap-templates", "zcl", "zcl.json") + ext_name = os.path.join(sys.argv[1], "src", "app", "zap-templates", "zcl", "zcl-with-test-extensions.json") + + base_data = json.load(open(base_name)) + ext_data = json.load(open(ext_name)) + + # ext should be IDENTICAL with base if we add a few things to base: + base_data["xmlRoot"].append("./data-model/test") + base_data["xmlFile"].append("mode-select-extensions.xml") + + # do not care about sorting. mainly do not check if extension xml + # is at the end or in the middle + base_data["xmlFile"].sort() + ext_data["xmlFile"].sort() + + if base_data == ext_data: + return 0 + + print("%s and %s have unexpected differences." % (base_name, ext_name)) + print("Differences between expected and actual:") + + for l in difflib.unified_diff( + json.dumps(ext_data, indent=2).split('\n'), + json.dumps(base_data, indent=2).split('\n'), + fromfile=ext_name, + tofile="", + ): + if l.endswith('\n'): + l = l[:-1] + print(l) + + return 1 + + +if __name__ == '__main__': + sys.exit(main())