2018-08-15 03:52:07 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2023-06-27 10:07:23 -04:00
|
|
|
# SPDX-FileCopyrightText: 2018-2023 Espressif Systems (Shanghai) CO LTD
|
2021-08-13 03:11:42 -04:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2018-08-15 03:52:07 -04:00
|
|
|
|
2019-01-08 05:40:49 -05:00
|
|
|
import argparse
|
2018-08-15 03:52:07 -04:00
|
|
|
import os
|
2019-11-28 16:56:53 -05:00
|
|
|
import re
|
2018-08-15 03:52:07 -04:00
|
|
|
import sys
|
2019-01-08 05:40:49 -05:00
|
|
|
|
2018-08-15 03:52:07 -04:00
|
|
|
try:
|
2023-06-27 10:07:23 -04:00
|
|
|
from packaging.requirements import Requirement
|
|
|
|
from packaging.version import Version
|
2021-09-16 10:48:03 -04:00
|
|
|
except ImportError:
|
2023-06-27 10:07:23 -04:00
|
|
|
print('packaging cannot be imported. '
|
|
|
|
'If you\'ve installed a custom Python then this package is provided separately and have to be installed as well. '
|
2022-07-18 04:46:38 -04:00
|
|
|
'Please refer to the Get Started section of the ESP-IDF Programming Guide for setting up the required packages.')
|
2018-08-15 03:52:07 -04:00
|
|
|
sys.exit(1)
|
|
|
|
|
2023-06-27 10:07:23 -04:00
|
|
|
try:
|
|
|
|
from importlib.metadata import PackageNotFoundError, requires
|
|
|
|
from importlib.metadata import version as get_version
|
|
|
|
except ImportError:
|
|
|
|
# compatibility for python <=3.7
|
|
|
|
from importlib_metadata import PackageNotFoundError, requires # type: ignore
|
|
|
|
from importlib_metadata import version as get_version # type: ignore
|
|
|
|
|
2022-04-26 10:32:06 -04:00
|
|
|
try:
|
|
|
|
from typing import Set
|
|
|
|
except ImportError:
|
|
|
|
# This is a script run during the early phase of setting up the environment. So try to avoid failure caused by
|
|
|
|
# Python version incompatibility. The supported Python version is checked elsewhere.
|
|
|
|
pass
|
|
|
|
|
|
|
|
PYTHON_PACKAGE_RE = re.compile(r'[^<>=~]+')
|
2018-11-30 07:31:44 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
if __name__ == '__main__':
|
2020-04-16 05:52:19 -04:00
|
|
|
parser = argparse.ArgumentParser(description='ESP-IDF Python package dependency checker')
|
2018-08-28 14:01:25 -04:00
|
|
|
parser.add_argument('--requirements', '-r',
|
2021-09-16 10:48:03 -04:00
|
|
|
help='Path to a requirements file (can be used multiple times)',
|
|
|
|
action='append', default=[])
|
|
|
|
parser.add_argument('--constraints', '-c', default=[],
|
|
|
|
help='Path to a constraints file (can be used multiple times)',
|
|
|
|
action='append')
|
2018-08-28 14:01:25 -04:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-09-16 10:48:03 -04:00
|
|
|
required_set = set()
|
|
|
|
for req_path in args.requirements:
|
|
|
|
with open(req_path) as f:
|
|
|
|
required_set |= set(i for i in map(str.strip, f.readlines()) if len(i) > 0 and not i.startswith('#'))
|
|
|
|
|
|
|
|
constr_dict = {} # for example package_name -> package_name==1.0
|
|
|
|
for const_path in args.constraints:
|
|
|
|
with open(const_path) as f:
|
|
|
|
for con in [i for i in map(str.strip, f.readlines()) if len(i) > 0 and not i.startswith('#')]:
|
|
|
|
if con.startswith('file://'):
|
|
|
|
con = os.path.basename(con)
|
|
|
|
elif con.startswith('--only-binary'):
|
|
|
|
continue
|
|
|
|
elif con.startswith('-e') and '#egg=' in con: # version control URLs, take the egg= part at the end only
|
|
|
|
con_m = re.search(r'#egg=([^\s]+)', con)
|
|
|
|
if not con_m:
|
|
|
|
print('Malformed input. Cannot find name in {}'.format(con))
|
|
|
|
sys.exit(1)
|
|
|
|
con = con_m[1]
|
|
|
|
|
|
|
|
name_m = PYTHON_PACKAGE_RE.search(con)
|
|
|
|
if not name_m:
|
|
|
|
print('Malformed input. Cannot find name in {}'.format(con))
|
|
|
|
sys.exit(1)
|
2023-06-27 10:07:23 -04:00
|
|
|
constr_dict[name_m[0]] = con.partition(' #')[0] # remove comments
|
2021-09-16 10:48:03 -04:00
|
|
|
|
2022-04-26 10:32:06 -04:00
|
|
|
not_satisfied = [] # in string form which will be printed
|
|
|
|
|
|
|
|
# already_checked set is used in order to avoid circular checks which would cause looping.
|
2023-06-27 10:07:23 -04:00
|
|
|
already_checked = set() # type: Set[Requirement]
|
2022-04-26 10:32:06 -04:00
|
|
|
|
|
|
|
# required_set contains package names in string form without version constraints. If the package has a constraint
|
|
|
|
# specification (package name + version requirement) then use that instead. new_req_list is used to store
|
|
|
|
# requirements to be checked on each level of breath-first-search of the package dependency tree. The initial
|
|
|
|
# version is the direct dependencies deduced from the requirements arguments of the script.
|
2023-06-27 10:07:23 -04:00
|
|
|
new_req_list = [Requirement(constr_dict.get(i, i)) for i in required_set]
|
|
|
|
|
|
|
|
def version_check(requirement: Requirement) -> None:
|
|
|
|
# compare installed version with required
|
|
|
|
version = Version(get_version(requirement.name))
|
|
|
|
if version.base_version not in requirement.specifier:
|
|
|
|
not_satisfied.append(f"Requirement '{requirement}' was not met. Installed version: {version}")
|
|
|
|
|
|
|
|
# evaluate markers and check versions of direct requirements
|
|
|
|
for req in new_req_list[:]:
|
|
|
|
if not req.marker or req.marker.evaluate():
|
|
|
|
version_check(req)
|
|
|
|
else:
|
|
|
|
new_req_list.remove(req)
|
2021-09-16 10:48:03 -04:00
|
|
|
|
2022-04-26 10:32:06 -04:00
|
|
|
while new_req_list:
|
|
|
|
req_list = new_req_list
|
|
|
|
new_req_list = []
|
|
|
|
already_checked.update(req_list)
|
|
|
|
for requirement in req_list: # check one level of the dependency tree
|
|
|
|
try:
|
2023-06-27 10:07:23 -04:00
|
|
|
dependency_requirements = set()
|
|
|
|
extras = list(requirement.extras) or ['']
|
|
|
|
for name in requires(requirement.name) or []:
|
|
|
|
sub_req = Requirement(name)
|
|
|
|
# check extras e.g. esptool[hsm]
|
|
|
|
for extra in extras:
|
|
|
|
# evaluate markers if present
|
|
|
|
if not sub_req.marker or sub_req.marker.evaluate(environment={'extra': extra}):
|
|
|
|
dependency_requirements.add(sub_req)
|
|
|
|
version_check(sub_req)
|
2022-04-26 10:32:06 -04:00
|
|
|
# dependency_requirements are the direct dependencies of "requirement". They belong to the next level
|
|
|
|
# of the dependency tree. They will be checked only if they haven't been already. Note that the
|
|
|
|
# version is taken into account as well because packages can have different requirements for a given
|
|
|
|
# Python package. The dependencies need to be checked for all of them because they can be different.
|
|
|
|
new_req_list.extend(dependency_requirements - already_checked)
|
2023-06-27 10:07:23 -04:00
|
|
|
except PackageNotFoundError as e:
|
|
|
|
not_satisfied.append(f"'{e}' - was not found and is required by the application")
|
2018-08-15 03:52:07 -04:00
|
|
|
|
|
|
|
if len(not_satisfied) > 0:
|
|
|
|
print('The following Python requirements are not satisfied:')
|
2021-09-16 10:48:03 -04:00
|
|
|
print(os.linesep.join(not_satisfied))
|
|
|
|
if 'IDF_PYTHON_ENV_PATH' in os.environ:
|
2019-04-17 03:32:03 -04:00
|
|
|
# We are running inside a private virtual environment under IDF_TOOLS_PATH,
|
|
|
|
# ask the user to run install.bat again.
|
2021-09-16 10:48:03 -04:00
|
|
|
install_script = 'install.bat' if sys.platform == 'win32' else 'install.sh'
|
|
|
|
print('To install the missing packages, please run "{}"'.format(install_script))
|
2018-12-19 08:04:57 -05:00
|
|
|
else:
|
2019-11-04 09:43:13 -05:00
|
|
|
print('Please follow the instructions found in the "Set up the tools" section of '
|
2021-09-16 10:48:03 -04:00
|
|
|
'ESP-IDF Getting Started Guide.')
|
2020-04-16 05:52:19 -04:00
|
|
|
|
|
|
|
print('Diagnostic information:')
|
|
|
|
idf_python_env_path = os.environ.get('IDF_PYTHON_ENV_PATH')
|
|
|
|
print(' IDF_PYTHON_ENV_PATH: {}'.format(idf_python_env_path or '(not set)'))
|
|
|
|
print(' Python interpreter used: {}'.format(sys.executable))
|
2020-06-08 12:14:39 -04:00
|
|
|
if not idf_python_env_path or idf_python_env_path not in sys.executable:
|
2020-04-16 05:52:19 -04:00
|
|
|
print(' Warning: python interpreter not running from IDF_PYTHON_ENV_PATH')
|
|
|
|
print(' PATH: {}'.format(os.getenv('PATH')))
|
2018-08-15 03:52:07 -04:00
|
|
|
sys.exit(1)
|
|
|
|
|
2021-09-16 10:48:03 -04:00
|
|
|
print('Python requirements are satisfied.')
|