check_python_dependencies.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. # Special case for MINGW32 Python, needs some packages
  40. # via MSYS2 not via pip or system breaks...
  41. if sys.platform == "win32" and \
  42. os.environ.get("MSYSTEM", None) == "MINGW32" and \
  43. "/mingw32/bin/python" in sys.executable:
  44. failed = False
  45. try:
  46. import cryptography # noqa: intentionally not used - imported for testing its availability
  47. except ImportError:
  48. print("Please run the following command to install MSYS2's MINGW Python cryptography package:")
  49. print("pacman -Sy mingw-w64-i686-python%d-cryptography" % (sys.version_info[0],))
  50. failed = True
  51. try:
  52. import setuptools # noqa: intentionally not used - imported for testing its availability
  53. except ImportError:
  54. print("Please run the following command to install MSYS2's MINGW Python setuptools package:")
  55. print("pacman -Sy mingw-w64-i686-python%d-setuptools" % (sys.version_info[0],))
  56. failed = True
  57. if failed:
  58. sys.exit(1)
  59. not_satisfied = []
  60. with open(args.requirements) as f:
  61. for line in f:
  62. line = line.strip()
  63. try:
  64. pkg_resources.require(line)
  65. except Exception:
  66. not_satisfied.append(line)
  67. if len(not_satisfied) > 0:
  68. print('The following Python requirements are not satisfied:')
  69. for requirement in not_satisfied:
  70. print(requirement)
  71. print('Please refer to the Get Started section of the ESP-IDF Programming Guide for setting up the required '
  72. 'packages. Alternatively, you can run "{} -m pip install --user -r {}" for resolving the issue.'
  73. ''.format(escape_backslash(sys.executable), escape_backslash(args.requirements)))
  74. sys.exit(1)
  75. print('Python requirements from {} are satisfied.'.format(args.requirements))