python_version_checker.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python
  2. #
  3. # SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  4. #
  5. # SPDX-License-Identifier: Apache-2.0
  6. #
  7. # Script for checking the compatibility of the Python interpreter with ESP-IDF.
  8. #
  9. # There are related tools/detect_python.{sh,fish} scripts which are called earlier when the paths are not properly
  10. # set-up and they only intend to prefer the use of Python 3 over Python 2. Why not more? All possible executables
  11. # (python3.7, python3.8, ...) cannot be hardcoded there and at the end, the user is responsible to set-up a system
  12. # where "python" or "python3" of compatible version is available.
  13. import sys
  14. try:
  15. # Python 2 is not supported anymore but still the old way of typing is used here in order to give a nice Python
  16. # version failure and not a typing exception.
  17. from typing import Iterable
  18. except ImportError:
  19. pass
  20. OLDEST_PYTHON_SUPPORTED = (3, 7) # keep it as tuple for comparison with sys.version_info
  21. def _ver_to_str(it): # type: (Iterable) -> str
  22. return '.'.join(str(x) for x in it)
  23. def is_supported(): # type: () -> bool
  24. return sys.version_info[:2] >= OLDEST_PYTHON_SUPPORTED[:2]
  25. def check(): # type: () -> None
  26. if not is_supported():
  27. raise RuntimeError(
  28. 'ESP-IDF supports Python {} or newer but you are using Python {}. Please upgrade your '
  29. 'installation as described in the documentation.'.format(
  30. _ver_to_str(OLDEST_PYTHON_SUPPORTED), _ver_to_str(sys.version_info[:3])
  31. )
  32. )
  33. if __name__ == '__main__':
  34. check()