check_python_dependencies.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env python
  2. #
  3. # SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD
  4. # SPDX-License-Identifier: Apache-2.0
  5. import argparse
  6. import os
  7. import re
  8. import sys
  9. try:
  10. import pkg_resources
  11. except Exception:
  12. print('pkg_resources cannot be imported probably because the pip package is not installed and/or using a '
  13. 'legacy Python interpreter. Please refer to the Get Started section of the ESP-IDF Programming Guide for '
  14. 'setting up the required packages.')
  15. sys.exit(1)
  16. def escape_backslash(path):
  17. if sys.platform == 'win32':
  18. # escaped backslashes are necessary in order to be able to copy-paste the printed path
  19. return path.replace('\\', '\\\\')
  20. else:
  21. return path
  22. if __name__ == '__main__':
  23. idf_path = os.getenv('IDF_PATH')
  24. default_requirements_path = os.path.join(idf_path, 'requirements.txt') # type: ignore
  25. parser = argparse.ArgumentParser(description='ESP-IDF Python package dependency checker')
  26. parser.add_argument('--requirements', '-r',
  27. help='Path to the requirements file',
  28. default=default_requirements_path)
  29. args = parser.parse_args()
  30. not_satisfied = []
  31. with open(args.requirements) as f:
  32. for line in f:
  33. line = line.strip()
  34. # pkg_resources.require() cannot handle the full requirements file syntax so we need to make
  35. # adjustments for options which we use.
  36. if line.startswith('file://'):
  37. line = os.path.basename(line)
  38. if line.startswith('--only-binary'):
  39. continue
  40. if line.startswith('-e') and '#egg=' in line: # version control URLs, take the egg= part at the end only
  41. line = re.search(r'#egg=([^\s]+)', line).group(1) # type: ignore
  42. try:
  43. pkg_resources.require(line)
  44. except Exception:
  45. not_satisfied.append(line)
  46. if len(not_satisfied) > 0:
  47. print('The following Python requirements are not satisfied:')
  48. for requirement in not_satisfied:
  49. print(requirement)
  50. if os.path.realpath(args.requirements) != os.path.realpath(default_requirements_path):
  51. # we're using this script to check non-default requirements.txt, so tell the user to run pip
  52. print('Please check the documentation for the feature you are using, or run "%s -m pip install -r %s"' % (sys.executable, args.requirements))
  53. elif os.environ.get('IDF_PYTHON_ENV_PATH'):
  54. # We are running inside a private virtual environment under IDF_TOOLS_PATH,
  55. # ask the user to run install.bat again.
  56. if sys.platform == 'win32' and not os.environ.get('MSYSTEM'):
  57. install_script = 'install.bat'
  58. else:
  59. install_script = 'install.sh'
  60. print('To install the missing packages, please run "%s"' % os.path.join(idf_path, install_script)) # type: ignore
  61. elif sys.platform == 'win32' and os.environ.get('MSYSTEM', None) == 'MINGW32' and '/mingw32/bin/python' in sys.executable:
  62. print("The recommended way to install a packages is via \"pacman\". Please run \"pacman -Ss <package_name>\" for"
  63. ' searching the package database and if found then '
  64. "\"pacman -S mingw-w64-i686-python-<package_name>\" for installing it.")
  65. print("NOTE: You may need to run \"pacman -Syu\" if your package database is older and run twice if the "
  66. "previous run updated \"pacman\" itself.")
  67. print('Please read https://github.com/msys2/msys2/wiki/Using-packages for further information about using '
  68. "\"pacman\"")
  69. # Special case for MINGW32 Python, needs some packages
  70. # via MSYS2 not via pip or system breaks...
  71. for requirement in not_satisfied:
  72. if requirement.startswith('cryptography'):
  73. print('WARNING: The cryptography package have dependencies on system packages so please make sure '
  74. "you run \"pacman -Syu\" followed by \"pacman -S mingw-w64-i686-python{}-cryptography\"."
  75. ''.format(sys.version_info[0],))
  76. continue
  77. elif requirement.startswith('setuptools'):
  78. print("Please run the following command to install MSYS2's MINGW Python setuptools package:")
  79. print('pacman -S mingw-w64-i686-python-setuptools')
  80. continue
  81. else:
  82. print('Please follow the instructions found in the "Set up the tools" section of '
  83. 'ESP-IDF Getting Started Guide')
  84. print('Diagnostic information:')
  85. idf_python_env_path = os.environ.get('IDF_PYTHON_ENV_PATH')
  86. print(' IDF_PYTHON_ENV_PATH: {}'.format(idf_python_env_path or '(not set)'))
  87. print(' Python interpreter used: {}'.format(sys.executable))
  88. if not idf_python_env_path or idf_python_env_path not in sys.executable:
  89. print(' Warning: python interpreter not running from IDF_PYTHON_ENV_PATH')
  90. print(' PATH: {}'.format(os.getenv('PATH')))
  91. sys.exit(1)
  92. print('Python requirements from {} are satisfied.'.format(args.requirements))