check_test_pics.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. """Check that our test PICS include values for every PICS if we know about.
  18. Takes two filenames as arguments: the CI PICS values file and the PICS
  19. definition YAML file.
  20. """
  21. import re
  22. import sys
  23. import yaml
  24. value_regexp = re.compile("=.*")
  25. def main():
  26. if len(sys.argv) != 3:
  27. print('Expecting two arguments: the CI PICS values file and the YAML file defining all PICS values. Got: %r' %
  28. sys.argv[1:])
  29. return 1
  30. value_defs = sys.argv[1]
  31. pics_yaml = sys.argv[2]
  32. with open(value_defs, "r") as stream:
  33. defined_values = set(map(lambda item: re.sub(
  34. value_regexp, "", item.rstrip()), stream.readlines()))
  35. # Remove Comments w/ # and empty lines
  36. for elem in list(defined_values):
  37. if elem.startswith('#') or (elem == ""):
  38. defined_values.discard(elem)
  39. with open(pics_yaml, "r") as stream:
  40. try:
  41. yaml_data = yaml.safe_load(stream)
  42. except yaml.YAMLError as e:
  43. print(e)
  44. return 1
  45. possible_values = set(map(lambda item: item["id"], yaml_data["PICS"]))
  46. if defined_values != possible_values:
  47. for value in sorted(possible_values - defined_values):
  48. print('"%s" does not have a value defined in %s' %
  49. (value, value_defs))
  50. for value in sorted(defined_values - possible_values):
  51. print('"%s" is not a known PICS item in %s' % (value, pics_yaml))
  52. return 1
  53. return 0
  54. if __name__ == '__main__':
  55. sys.exit(main())