check_python_dependencies.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python
  2. #
  3. # SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD
  4. # SPDX-License-Identifier: Apache-2.0
  5. import argparse
  6. import os
  7. import re
  8. import sys
  9. try:
  10. import pkg_resources
  11. except Exception:
  12. print('pkg_resources cannot be imported probably because the pip package is not installed and/or using a '
  13. 'legacy Python interpreter. Please refer to the Get Started section of the ESP-IDF Programming Guide for '
  14. 'setting up the required packages.')
  15. sys.exit(1)
  16. def escape_backslash(path):
  17. if sys.platform == 'win32':
  18. # escaped backslashes are necessary in order to be able to copy-paste the printed path
  19. return path.replace('\\', '\\\\')
  20. else:
  21. return path
  22. if __name__ == '__main__':
  23. idf_path = os.getenv('IDF_PATH')
  24. default_requirements_path = os.path.join(idf_path, 'requirements.txt') # type: ignore
  25. parser = argparse.ArgumentParser(description='ESP-IDF Python package dependency checker')
  26. parser.add_argument('--requirements', '-r',
  27. help='Path to the requirements file',
  28. default=default_requirements_path)
  29. args = parser.parse_args()
  30. not_satisfied = []
  31. with open(args.requirements) as f:
  32. for line in f:
  33. line = line.strip()
  34. # pkg_resources.require() cannot handle the full requirements file syntax so we need to make
  35. # adjustments for options which we use.
  36. if line.startswith('file://'):
  37. line = os.path.basename(line)
  38. if line.startswith('--only-binary'):
  39. continue
  40. if line.startswith('-e') and '#egg=' in line: # version control URLs, take the egg= part at the end only
  41. line = re.search(r'#egg=([^\s]+)', line).group(1) # type: ignore
  42. try:
  43. pkg_resources.require(line)
  44. except Exception:
  45. not_satisfied.append(line)
  46. if len(not_satisfied) > 0:
  47. print('The following Python requirements are not satisfied:')
  48. for requirement in not_satisfied:
  49. print(requirement)
  50. if os.path.realpath(args.requirements) != os.path.realpath(default_requirements_path):
  51. # we're using this script to check non-default requirements.txt, so tell the user to run pip
  52. print('Please check the documentation for the feature you are using, or run "%s -m pip install -r %s"' % (sys.executable, args.requirements))
  53. elif os.environ.get('IDF_PYTHON_ENV_PATH'):
  54. # We are running inside a private virtual environment under IDF_TOOLS_PATH,
  55. # ask the user to run install.bat again.
  56. if sys.platform == 'win32':
  57. install_script = 'install.bat'
  58. else:
  59. install_script = 'install.sh'
  60. print('To install the missing packages, please run "%s"' % os.path.join(idf_path, install_script)) # type: ignore
  61. else:
  62. print('Please follow the instructions found in the "Set up the tools" section of '
  63. 'ESP-IDF Getting Started Guide')
  64. print('Diagnostic information:')
  65. idf_python_env_path = os.environ.get('IDF_PYTHON_ENV_PATH')
  66. print(' IDF_PYTHON_ENV_PATH: {}'.format(idf_python_env_path or '(not set)'))
  67. print(' Python interpreter used: {}'.format(sys.executable))
  68. if not idf_python_env_path or idf_python_env_path not in sys.executable:
  69. print(' Warning: python interpreter not running from IDF_PYTHON_ENV_PATH')
  70. print(' PATH: {}'.format(os.getenv('PATH')))
  71. sys.exit(1)
  72. print('Python requirements from {} are satisfied.'.format(args.requirements))