check_kconfigs.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python
  2. #
  3. # SPDX-FileCopyrightText: 2018-2023 Espressif Systems (Shanghai) CO LTD
  4. # SPDX-License-Identifier: Apache-2.0
  5. from __future__ import print_function, unicode_literals
  6. import argparse
  7. import os
  8. import re
  9. import subprocess
  10. import sys
  11. from idf_ci_utils import IDF_PATH, get_submodule_dirs
  12. # regular expression for matching Kconfig files
  13. RE_KCONFIG = r'^Kconfig(\.projbuild)?(\.in)?$'
  14. # ignored directories (makes sense only when run on IDF_PATH)
  15. # Note: ignore_dirs is a tuple in order to be able to use it directly with the startswith() built-in function which
  16. # accepts tuples but no lists.
  17. IGNORE_DIRS: tuple = (
  18. # Kconfigs from submodules need to be ignored:
  19. os.path.join(IDF_PATH, 'components', 'mqtt', 'esp-mqtt'),
  20. # Test Kconfigs are also ignored
  21. os.path.join(IDF_PATH, 'tools', 'ldgen', 'test', 'data'),
  22. os.path.join(IDF_PATH, 'tools', 'kconfig_new', 'test'),
  23. )
  24. ignore_dirs: tuple = IGNORE_DIRS
  25. def valid_directory(path:str) -> str:
  26. if not os.path.isdir(path):
  27. raise argparse.ArgumentTypeError('{} is not a valid directory!'.format(path))
  28. return str(path)
  29. parser = argparse.ArgumentParser(description='Kconfig style checker')
  30. parser.add_argument(
  31. 'files',
  32. nargs='*',
  33. help='Kconfig files to check (full paths separated by space)',
  34. )
  35. parser.add_argument(
  36. '--exclude-submodules',
  37. action='store_true',
  38. help='Exclude submodules',
  39. )
  40. parser.add_argument(
  41. '--includes',
  42. '-d',
  43. nargs='*',
  44. help='Extra paths for recursively searching Kconfig files. (for example $IDF_PATH)',
  45. type=valid_directory
  46. )
  47. args, unknown_args = parser.parse_known_args()
  48. # if the deprecated argument '--exclude-submodules' is used
  49. if args.exclude_submodules:
  50. ignore_dirs = ignore_dirs + tuple(get_submodule_dirs(full_path=True))
  51. files_to_check: list = []
  52. # if the deprecated argument '--includes' is used all valid paths are checked for KConfigs
  53. # except IGNORE_DIRS and submodules (if exclude is given)
  54. # paths to KConfig files are passed to esp-idf-kconfig kconfcheck tool
  55. if args.includes:
  56. for directory in args.includes:
  57. for root, dirnames, filenames in os.walk(directory):
  58. for filename in filenames:
  59. full_path = os.path.join(root, filename)
  60. if full_path.startswith(ignore_dirs):
  61. continue
  62. if re.search(RE_KCONFIG, filename):
  63. files_to_check.append(f'{full_path}')
  64. elif re.search(RE_KCONFIG, filename, re.IGNORECASE):
  65. # On Windows Kconfig files are working with different cases!
  66. print(
  67. '{}: Incorrect filename. The case should be "Kconfig"!'.format(
  68. full_path
  69. )
  70. )
  71. if __name__ == '__main__':
  72. sys.exit(subprocess.run([sys.executable, '-m', 'kconfcheck'] + files_to_check + unknown_args).returncode)