check_deprecated_kconfigs.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python
  2. #
  3. # SPDX-FileCopyrightText: 2019-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 sys
  9. from io import open
  10. from typing import Set, Tuple
  11. from idf_ci_utils import get_submodule_dirs
  12. # FILES_TO_CHECK used as "startswith" pattern to match sdkconfig.defaults variants
  13. FILES_TO_CHECK = ('sdkconfig.ci', 'sdkconfig.defaults')
  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. )
  19. def _parse_path(path: 'os.PathLike[str]', sep: str=None) -> Set:
  20. ret = set()
  21. with open(path, 'r', encoding='utf-8') as f:
  22. for line in f:
  23. line = line.strip()
  24. if not line.startswith('#') and len(line) > 0:
  25. ret.add(line.split(sep)[0])
  26. return ret
  27. def valid_directory(path: str) -> str:
  28. if not os.path.isdir(path):
  29. raise argparse.ArgumentTypeError('{} is not a valid directory!'.format(path))
  30. return path
  31. def check() -> int:
  32. parser = argparse.ArgumentParser(description='Kconfig options checker')
  33. parser.add_argument('files', nargs='*',
  34. help='Kconfig files')
  35. parser.add_argument('--includes', '-d', nargs='*',
  36. help='Extra paths for recursively searching Kconfig files. (for example $IDF_PATH)',
  37. type=valid_directory)
  38. parser.add_argument('--exclude-submodules', action='store_true',
  39. help='Exclude submodules')
  40. args = parser.parse_args()
  41. success_counter = 0
  42. failure_counter = 0
  43. ignore_counter = 0
  44. deprecated_options = set()
  45. ignore_dirs = IGNORE_DIRS
  46. if args.exclude_submodules:
  47. for submodule in get_submodule_dirs(full_path=True):
  48. ignore_dirs = ignore_dirs + tuple(submodule)
  49. files = [os.path.abspath(file_path) for file_path in args.files]
  50. if args.includes:
  51. for directory in args.includes:
  52. for root, dirnames, filenames in os.walk(directory):
  53. for filename in filenames:
  54. full_path = os.path.join(root, filename)
  55. if filename.startswith(FILES_TO_CHECK):
  56. files.append(full_path)
  57. elif filename == 'sdkconfig.rename':
  58. deprecated_options |= _parse_path(full_path)
  59. for full_path in files:
  60. if full_path.startswith(ignore_dirs):
  61. print('{}: Ignored'.format(full_path))
  62. ignore_counter += 1
  63. continue
  64. used_options = _parse_path(full_path, '=')
  65. used_deprecated_options = deprecated_options & used_options
  66. if len(used_deprecated_options) > 0:
  67. print('{}: The following options are deprecated: {}'
  68. .format(full_path, ', '.join(used_deprecated_options)))
  69. failure_counter += 1
  70. else:
  71. print('{}: OK'.format(full_path))
  72. success_counter += 1
  73. if ignore_counter > 0:
  74. print('{} files have been ignored.'.format(ignore_counter))
  75. if success_counter > 0:
  76. print('{} files have been successfully checked.'.format(success_counter))
  77. if failure_counter > 0:
  78. print('{} files have errors. Please take a look at the log.'.format(failure_counter))
  79. return 1
  80. if not files:
  81. print('WARNING: no files specified. Please specify files or use '
  82. '"--includes" to search Kconfig files recursively')
  83. return 0
  84. def main() -> None:
  85. sys.exit(check())
  86. if __name__ == '__main__':
  87. main()