check_python_dependencies.py 5.6 KB

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