check_python_dependencies.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 os
  17. import sys
  18. import argparse
  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. try:
  44. pkg_resources.require(line)
  45. except Exception:
  46. not_satisfied.append(line)
  47. if len(not_satisfied) > 0:
  48. print('The following Python requirements are not satisfied:')
  49. for requirement in not_satisfied:
  50. print(requirement)
  51. if sys.platform == "win32" and os.environ.get("MSYSTEM", None) == "MINGW32" and "/mingw32/bin/python" in sys.executable:
  52. print("The recommended way to install a packages is via \"pacman\". Please run \"pacman -Ss <package_name>\" for"
  53. " searching the package database and if found then "
  54. "\"pacman -S mingw-w64-i686-python{}-<package_name>\" for installing it.".format(sys.version_info[0],))
  55. print("NOTE: You may need to run \"pacman -Syu\" if your package database is older and run twice if the "
  56. "previous run updated \"pacman\" itself.")
  57. print("Please read https://github.com/msys2/msys2/wiki/Using-packages for further information about using "
  58. "\"pacman\"")
  59. # Special case for MINGW32 Python, needs some packages
  60. # via MSYS2 not via pip or system breaks...
  61. for requirement in not_satisfied:
  62. if requirement.startswith('cryptography'):
  63. print("WARNING: The cryptography package have dependencies on system packages so please make sure "
  64. "you run \"pacman -Syu\" followed by \"pacman -S mingw-w64-i686-python{}-cryptography\"."
  65. "".format(sys.version_info[0],))
  66. continue
  67. elif requirement.startswith('setuptools'):
  68. print("Please run the following command to install MSYS2's MINGW Python setuptools package:")
  69. print("pacman -S mingw-w64-i686-python{}-setuptools".format(sys.version_info[0],))
  70. continue
  71. else:
  72. print('Please refer to the Get Started section of the ESP-IDF Programming Guide for setting up the required'
  73. ' packages.')
  74. print('Alternatively, you can run "{} -m pip install --user -r {}" for resolving the issue.'
  75. ''.format(escape_backslash(sys.executable), escape_backslash(args.requirements)))
  76. sys.exit(1)
  77. print('Python requirements from {} are satisfied.'.format(args.requirements))