check_python_dependencies.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. parser = argparse.ArgumentParser(description='ESP32 Python package dependency checker')
  36. parser.add_argument('--requirements', '-r',
  37. help='Path to the requrements file',
  38. default=os.path.join(idf_path, 'requirements.txt'))
  39. args = parser.parse_args()
  40. not_satisfied = []
  41. with open(args.requirements) as f:
  42. for line in f:
  43. line = line.strip()
  44. # pkg_resources.require() cannot handle the full requirements file syntax so we need to make
  45. # adjustments for options which we use.
  46. if line.startswith('file://'):
  47. line = os.path.basename(line)
  48. if line.startswith('-e') and '#egg=' in line: # version control URLs, take the egg= part at the end only
  49. line = re.search(r'#egg=([^\s]+)', line).group(1)
  50. try:
  51. pkg_resources.require(line)
  52. except Exception:
  53. not_satisfied.append(line)
  54. if len(not_satisfied) > 0:
  55. print('The following Python requirements are not satisfied:')
  56. for requirement in not_satisfied:
  57. print(requirement)
  58. if os.environ.get('IDF_PYTHON_ENV_PATH'):
  59. # We are running inside a private virtual environment under IDF_TOOLS_PATH,
  60. # ask the user to run install.bat again.
  61. if sys.platform == "win32" and not os.environ.get("MSYSTEM"):
  62. install_script = 'install.bat'
  63. else:
  64. install_script = 'install.sh'
  65. print('To install the missing packages, please run "%s"' % os.path.join(idf_path, install_script))
  66. elif sys.platform == "win32" and os.environ.get("MSYSTEM", None) == "MINGW32" and "/mingw32/bin/python" in sys.executable:
  67. print("The recommended way to install a packages is via \"pacman\". Please run \"pacman -Ss <package_name>\" for"
  68. " searching the package database and if found then "
  69. "\"pacman -S mingw-w64-i686-python{}-<package_name>\" for installing it.".format(sys.version_info[0],))
  70. print("NOTE: You may need to run \"pacman -Syu\" if your package database is older and run twice if the "
  71. "previous run updated \"pacman\" itself.")
  72. print("Please read https://github.com/msys2/msys2/wiki/Using-packages for further information about using "
  73. "\"pacman\"")
  74. # Special case for MINGW32 Python, needs some packages
  75. # via MSYS2 not via pip or system breaks...
  76. for requirement in not_satisfied:
  77. if requirement.startswith('cryptography'):
  78. print("WARNING: The cryptography package have dependencies on system packages so please make sure "
  79. "you run \"pacman -Syu\" followed by \"pacman -S mingw-w64-i686-python{}-cryptography\"."
  80. "".format(sys.version_info[0],))
  81. continue
  82. elif requirement.startswith('setuptools'):
  83. print("Please run the following command to install MSYS2's MINGW Python setuptools package:")
  84. print("pacman -S mingw-w64-i686-python{}-setuptools".format(sys.version_info[0],))
  85. continue
  86. else:
  87. print('Please follow the instructions found in the "Set up the tools" section of '
  88. 'ESP-IDF Getting Started Guide')
  89. sys.exit(1)
  90. print('Python requirements from {} are satisfied.'.format(args.requirements))