check_python_dependencies.py 4.5 KB

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