tools/espressif: Improve version checking by subprocess

Summary:
- Replace version checking logic using pkg_resources and importlib with a subprocess call to `esptool.py version`
- This change enhances compatibility with esptool installed via pipx and simplifies the version retrieval process

Impact:
- No functional changes; the script continues to prompt for installation if esptool is not found
- Increases maintainability by reducing dependency on Python version checks

Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
This commit is contained in:
Huang Qi
2025-02-18 14:29:43 +08:00
committed by Xiang Xiao
parent f13e66adcb
commit 0873d88f11
+15 -29
View File
@@ -21,19 +21,9 @@
############################################################################ ############################################################################
import argparse import argparse
import re import re
import subprocess
import sys import sys
# Different package required if Python 3.8+
if sys.version_info.minor < 8:
import pkg_resources
PYTHON_OLDER = True
else:
from importlib.metadata import PackageNotFoundError, version
PYTHON_OLDER = False
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog="check_esptool", prog="check_esptool",
description="Checks esptool version and returns true or \ description="Checks esptool version and returns true or \
@@ -55,29 +45,25 @@ def parse_version(version_string) -> list:
return [] return []
def check_version(min_esptool_version: str) -> int: def check_version(min_esptool_version: str) -> bool:
"""Attempts to read 'esptool' version using pkg_resources (for """Attempts to read 'esptool' version using subprocess command
Python <3.8) or importlib. Compare current version with which improves compatibility with esptool installed by pipx like tools.
'min_esptool_version' and returns. Compare current version with 'min_esptool_version' and returns.
Returns: Returns:
True: packages does not exist or outdated True: packages does not exist or outdated
False: package installed and up-to-date False: package installed and up-to-date
""" """
if PYTHON_OLDER: try:
try: # Run esptool.py version command
version_str = pkg_resources.get_distribution("esptool").version result = subprocess.run(
except pkg_resources.DistributionNotFound: ["esptool.py", "version"], capture_output=True, text=True, check=True
print("esptool.py not found. Please run: 'pip install esptool'") )
print("Run make again to create the nuttx.bin image.") version_str = result.stdout.strip()
return True except (subprocess.CalledProcessError, FileNotFoundError):
else: print("esptool.py not found. Please run: 'pip install esptool'")
try: print("Run make again to create the nuttx.bin image.")
version_str = version("esptool") return True
except PackageNotFoundError:
print("esptool.py not found. Please run: 'pip install esptool'")
print("Run make again to create the nuttx.bin image.")
return True
esptool_version = parse_version(version_str) esptool_version = parse_version(version_str)
min_esptool_version = parse_version(parser.version) min_esptool_version = parse_version(parser.version)