1#!/usr/bin/env python 2# 3# SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD 4# SPDX-License-Identifier: Apache-2.0 5 6import argparse 7import os 8import re 9import sys 10 11try: 12 import pkg_resources 13except Exception: 14 print('pkg_resources cannot be imported probably because the pip package is not installed and/or using a ' 15 'legacy Python interpreter. Please refer to the Get Started section of the ESP-IDF Programming Guide for ' 16 'setting up the required packages.') 17 sys.exit(1) 18 19 20def escape_backslash(path): 21 if sys.platform == 'win32': 22 # escaped backslashes are necessary in order to be able to copy-paste the printed path 23 return path.replace('\\', '\\\\') 24 else: 25 return path 26 27 28if __name__ == '__main__': 29 idf_path = os.getenv('IDF_PATH') 30 31 default_requirements_path = os.path.join(idf_path, 'requirements.txt') # type: ignore 32 33 parser = argparse.ArgumentParser(description='ESP-IDF Python package dependency checker') 34 parser.add_argument('--requirements', '-r', 35 help='Path to the requirements file', 36 default=default_requirements_path) 37 args = parser.parse_args() 38 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 if line.startswith('--only-binary'): 48 continue 49 if line.startswith('-e') and '#egg=' in line: # version control URLs, take the egg= part at the end only 50 line = re.search(r'#egg=([^\s]+)', line).group(1) # type: ignore 51 try: 52 pkg_resources.require(line) 53 except Exception: 54 not_satisfied.append(line) 55 56 if len(not_satisfied) > 0: 57 print('The following Python requirements are not satisfied:') 58 for requirement in not_satisfied: 59 print(requirement) 60 if os.path.realpath(args.requirements) != os.path.realpath(default_requirements_path): 61 # we're using this script to check non-default requirements.txt, so tell the user to run pip 62 print('Please check the documentation for the feature you are using, or run "%s -m pip install -r %s"' % (sys.executable, args.requirements)) 63 elif os.environ.get('IDF_PYTHON_ENV_PATH'): 64 # We are running inside a private virtual environment under IDF_TOOLS_PATH, 65 # ask the user to run install.bat again. 66 if sys.platform == 'win32' and not os.environ.get('MSYSTEM'): 67 install_script = 'install.bat' 68 else: 69 install_script = 'install.sh' 70 print('To install the missing packages, please run "%s"' % os.path.join(idf_path, install_script)) # type: ignore 71 elif sys.platform == 'win32' and os.environ.get('MSYSTEM', None) == 'MINGW32' and '/mingw32/bin/python' in sys.executable: 72 print("The recommended way to install a packages is via \"pacman\". Please run \"pacman -Ss <package_name>\" for" 73 ' searching the package database and if found then ' 74 "\"pacman -S mingw-w64-i686-python-<package_name>\" for installing it.") 75 print("NOTE: You may need to run \"pacman -Syu\" if your package database is older and run twice if the " 76 "previous run updated \"pacman\" itself.") 77 print('Please read https://github.com/msys2/msys2/wiki/Using-packages for further information about using ' 78 "\"pacman\"") 79 # Special case for MINGW32 Python, needs some packages 80 # via MSYS2 not via pip or system breaks... 81 for requirement in not_satisfied: 82 if requirement.startswith('cryptography'): 83 print('WARNING: The cryptography package have dependencies on system packages so please make sure ' 84 "you run \"pacman -Syu\" followed by \"pacman -S mingw-w64-i686-python{}-cryptography\"." 85 ''.format(sys.version_info[0],)) 86 continue 87 elif requirement.startswith('setuptools'): 88 print("Please run the following command to install MSYS2's MINGW Python setuptools package:") 89 print('pacman -S mingw-w64-i686-python-setuptools') 90 continue 91 else: 92 print('Please follow the instructions found in the "Set up the tools" section of ' 93 'ESP-IDF Getting Started Guide') 94 95 print('Diagnostic information:') 96 idf_python_env_path = os.environ.get('IDF_PYTHON_ENV_PATH') 97 print(' IDF_PYTHON_ENV_PATH: {}'.format(idf_python_env_path or '(not set)')) 98 print(' Python interpreter used: {}'.format(sys.executable)) 99 if not idf_python_env_path or idf_python_env_path not in sys.executable: 100 print(' Warning: python interpreter not running from IDF_PYTHON_ENV_PATH') 101 print(' PATH: {}'.format(os.getenv('PATH'))) 102 sys.exit(1) 103 104 print('Python requirements from {} are satisfied.'.format(args.requirements)) 105