check_requirement_files.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python
  2. # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. # SPDX-License-Identifier: Apache-2.0
  4. import json
  5. import os
  6. import re
  7. from typing import Any
  8. import jsonschema
  9. IDF_PATH = os.environ['IDF_PATH']
  10. JSON_PATH = os.path.join(IDF_PATH, 'tools', 'requirements.json')
  11. SCHEMA_PATH = os.path.join(IDF_PATH, 'tools', 'requirements_schema.json')
  12. REQ_DIR = os.path.join(IDF_PATH, 'tools', 'requirements')
  13. RE_FEATURE = re.compile(r'requirements\.(\w+)\.txt')
  14. def action_validate(req_obj: Any) -> None: # "Any" because we are checking this in this script
  15. '''
  16. Check that the parsed JSON object is valid according to the JSON schema provided
  17. '''
  18. with open(SCHEMA_PATH, 'r') as schema_file:
  19. schema_json = json.load(schema_file)
  20. jsonschema.validate(req_obj, schema_json)
  21. def action_check_directory(req_obj: Any) -> None: # "Any" because we are checking this in this script
  22. '''
  23. Check that all directory items are listed in the JSON file
  24. '''
  25. features = set(d['name'] for d in req_obj['features'])
  26. features_found = set()
  27. for file_name in os.listdir(REQ_DIR):
  28. m = re.match(RE_FEATURE, file_name)
  29. if m:
  30. if m.group(1) not in features:
  31. raise RuntimeError(f'Cannot find a feature for {file_name} in {JSON_PATH}')
  32. features_found.add(m.group(1))
  33. else:
  34. raise RuntimeError(f'{file_name} in {REQ_DIR} doesn\'t match the expected name')
  35. features_not_found = features - features_found
  36. if len(features_not_found) > 0:
  37. raise RuntimeError(f'There are no requirements file in {REQ_DIR} for {features_not_found}')
  38. def main() -> None:
  39. with open(JSON_PATH, 'r') as f:
  40. req_obj = json.load(f)
  41. action_validate(req_obj)
  42. action_check_directory(req_obj)
  43. if __name__ == '__main__':
  44. main()