check_zcl_file_sync.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (c) 2022 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. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. """
  18. Validates that the json zcl files that are used by the app are in sync.
  19. Current rule:
  20. - all-clusters app uses an extension on top of the "standard" zcl file.
  21. Ensure that the two fies are in sync EXCEPT the extension.
  22. """
  23. import difflib
  24. import json
  25. import os
  26. import sys
  27. def main():
  28. if len(sys.argv) != 2:
  29. print('Please pass CHIP_ROOT as an argument (and only chip root)')
  30. return 1
  31. base_name = os.path.join(sys.argv[1], "src", "app", "zap-templates", "zcl", "zcl.json")
  32. ext_name = os.path.join(sys.argv[1], "src", "app", "zap-templates", "zcl", "zcl-with-test-extensions.json")
  33. base_data = json.load(open(base_name))
  34. ext_data = json.load(open(ext_name))
  35. # ext should be IDENTICAL with base if we add a few things to base:
  36. base_data["xmlRoot"].append("./data-model/test")
  37. base_data["xmlFile"].append("mode-select-extensions.xml")
  38. # do not care about sorting. mainly do not check if extension xml
  39. # is at the end or in the middle
  40. base_data["xmlFile"].sort()
  41. ext_data["xmlFile"].sort()
  42. # remove the description. That is expected to be different, so we
  43. # will completely exclude it from the comparison.
  44. del base_data["description"]
  45. del ext_data["description"]
  46. if base_data == ext_data:
  47. return 0
  48. print("%s and %s have unexpected differences." % (base_name, ext_name))
  49. print("Differences between expected and actual:")
  50. for line in difflib.unified_diff(
  51. json.dumps(ext_data, indent=2).split('\n'),
  52. json.dumps(base_data, indent=2).split('\n'),
  53. fromfile=ext_name,
  54. tofile="<Expected extension file content>",
  55. ):
  56. if line.endswith('\n'):
  57. line = line[:-1]
  58. print(line)
  59. return 1
  60. if __name__ == '__main__':
  61. sys.exit(main())