check_deprecated_kconfigs.py 3.7 KB

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