Skip to content

Commit

Permalink
fix(tools/python_dep_check): replace deprecated pkg_resources with im…
Browse files Browse the repository at this point in the history
…portlib
  • Loading branch information
peterdragun committed Jul 14, 2023
1 parent 703e116 commit fd96daf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ setuptools>=21
# The setuptools package is required to install source distributions and on some systems is not installed by default.
# Please keep it as the first item of this list. Version 21 is required to handle PEP 508 environment markers.
#
# importlib_metadata: is part of python3.8 and newer as importlib.metadata
importlib_metadata; python_version < "3.8"
packaging
click>=7.0
pyserial>=3.3
future>=0.15.2
Expand Down
45 changes: 36 additions & 9 deletions tools/check_python_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@
import sys

try:
import pkg_resources
except Exception:
print('pkg_resources cannot be imported probably because the pip package is not installed and/or using a '
'legacy Python interpreter. Please refer to the Get Started section of the ESP-IDF Programming Guide for '
'setting up the required packages.')
from packaging.requirements import Requirement
from packaging.version import Version
except ImportError:
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. '
'Please refer to the Get Started section of the ESP-IDF Programming Guide for setting up the required packages.')
sys.exit(1)

try:
from importlib.metadata import requires
from importlib.metadata import version as get_version
except ImportError:
from importlib_metadata import requires # type: ignore
from importlib_metadata import version as get_version # type: ignore


def escape_backslash(path):
if sys.platform == 'win32':
Expand All @@ -35,21 +43,40 @@ def escape_backslash(path):
help='Path to the requirements file',
default=default_requirements_path)
args = parser.parse_args()

not_satisfied = []

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}")

with open(args.requirements) as f:
for line in f:
line = line.strip()
# pkg_resources.require() cannot handle the full requirements file syntax so we need to make
# requires() cannot handle the full requirements file syntax so we need to make
# adjustments for options which we use.
if line.startswith('file://'):
line = os.path.basename(line)
if line.startswith('--only-binary'):
if line.startswith('--only-binary') or line.startswith('#') or len(line) == 0:
continue
if line.startswith('-e') and '#egg=' in line: # version control URLs, take the egg= part at the end only
line = re.search(r'#egg=([^\s]+)', line).group(1) # type: ignore
# remove comments
line = line.partition(' #')[0]
try:
pkg_resources.require(line)
requirement = Requirement(line)
extras = requirement.extras
if requirement.marker and not requirement.marker.evaluate():
continue
version_check(requirement)
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}):
version_check(sub_req)
except Exception:
not_satisfied.append(line)

Expand Down

0 comments on commit fd96daf

Please sign in to comment.