Skip to content

Commit

Permalink
Add linter rule for ZCL file being in sync
Browse files Browse the repository at this point in the history
  • Loading branch information
andy31415 committed Jun 9, 2022
1 parent 17823c2 commit f9748f0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
71 changes: 71 additions & 0 deletions scripts/tools/check_zcl_file_sync.py
Original file line number Diff line number Diff line change
@@ -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="<Expected extension file content>",
):
if l.endswith('\n'):
l = l[:-1]
print(l)

return 1


if __name__ == '__main__':
sys.exit(main())

0 comments on commit f9748f0

Please sign in to comment.