check_deprecated_kconfigs.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2019 Espressif Systems (Shanghai) PTE LTD
  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. from __future__ import print_function
  17. from __future__ import unicode_literals
  18. import argparse
  19. import os
  20. import sys
  21. from io import open
  22. # FILES_TO_CHECK used as "startswith" pattern to match sdkconfig.defaults variants
  23. FILES_TO_CHECK = ('sdkconfig.ci', 'sdkconfig.defaults')
  24. # ignored directories (makes sense only when run on IDF_PATH)
  25. # Note: IGNORE_DIRS is a tuple in order to be able to use it directly with the startswith() built-in function which
  26. # accepts tuples but no lists.
  27. IGNORE_DIRS = (
  28. )
  29. def _parse_path(path, sep=None):
  30. ret = set()
  31. with open(path, 'r', encoding='utf-8') as f:
  32. for line in f:
  33. line = line.strip()
  34. if not line.startswith('#') and len(line) > 0:
  35. ret.add(line.split(sep)[0])
  36. return ret
  37. def _valid_directory(path):
  38. if not os.path.isdir(path):
  39. raise argparse.ArgumentTypeError("{} is not a valid directory!".format(path))
  40. return path
  41. def main():
  42. default_path = os.getenv('IDF_PATH', None)
  43. parser = argparse.ArgumentParser(description='Kconfig options checker')
  44. parser.add_argument('--directory', '-d', help='Path to directory to check recursively '
  45. '(for example $IDF_PATH)',
  46. type=_valid_directory,
  47. required=default_path is None,
  48. default=default_path)
  49. args = parser.parse_args()
  50. # IGNORE_DIRS makes sense when the required directory is IDF_PATH
  51. check_ignore_dirs = default_path is not None and os.path.abspath(args.directory) == os.path.abspath(default_path)
  52. ignores = 0
  53. files_to_check = []
  54. deprecated_options = set()
  55. errors = []
  56. for root, dirnames, filenames in os.walk(args.directory):
  57. for filename in filenames:
  58. full_path = os.path.join(root, filename)
  59. path_in_idf = os.path.relpath(full_path, args.directory)
  60. if filename.startswith(FILES_TO_CHECK):
  61. if check_ignore_dirs and path_in_idf.startswith(IGNORE_DIRS):
  62. print('{}: Ignored'.format(path_in_idf))
  63. ignores += 1
  64. continue
  65. files_to_check.append(full_path)
  66. elif filename == 'sdkconfig.rename':
  67. deprecated_options |= _parse_path(full_path)
  68. for path in files_to_check:
  69. used_options = _parse_path(path, '=')
  70. used_deprecated_options = deprecated_options & used_options
  71. if len(used_deprecated_options) > 0:
  72. errors.append('{}: The following options are deprecated: {}'.format(path,
  73. ', '.join(used_deprecated_options)))
  74. if ignores > 0:
  75. print('{} files have been ignored.'.format(ignores))
  76. if len(errors) > 0:
  77. print('\n\n'.join(errors))
  78. sys.exit(1)
  79. if __name__ == "__main__":
  80. main()