diff --git a/builder/frameworks/espidf.py b/builder/frameworks/espidf.py index 2d3241a28..a808cbd74 100644 --- a/builder/frameworks/espidf.py +++ b/builder/frameworks/espidf.py @@ -2097,8 +2097,20 @@ def _get_uv_exe(): return get_executable_path(str(Path(PLATFORMIO_DIR) / "penv"), "uv") -def install_python_deps(): +def _get_python_deps(): + """Get the required Python dependencies for ESP-IDF""" + return { + # https://github.com/platformio/platform-espressif32/issues/635 + "cryptography": "~=44.0.0", + "pyparsing": ">=3.1.0,<4", + "idf-component-manager": "~=2.4.8", + "esp-idf-kconfig": "~=3.7.0" + } + + +def install_python_deps(deps=None): UV_EXE = _get_uv_exe() + deps = deps or _get_python_deps() def _get_installed_uv_packages(python_exe_path): result = {} @@ -2120,14 +2132,6 @@ def _get_installed_uv_packages(python_exe_path): if os.path.isfile(skip_python_packages): return - deps = { - # https://github.com/platformio/platform-espressif32/issues/635 - "cryptography": "~=44.0.0", - "pyparsing": ">=3.1.0,<4", - "idf-component-manager": "~=2.4.6", - "esp-idf-kconfig": "~=2.5.0" - } - python_exe_path = get_python_exe() installed_packages = _get_installed_uv_packages(python_exe_path) packages_to_install = [] @@ -2185,7 +2189,12 @@ def _get_idf_venv_python_version(): print("Failed to extract Python version from IDF virtual env!") return None - def _is_venv_outdated(venv_data_file): + def _get_deps_hash(deps_dict): + import hashlib + deps_str = json.dumps(deps_dict, sort_keys=True) + return hashlib.sha256(deps_str.encode()).hexdigest() + + def _is_venv_outdated(venv_data_file, current_deps): try: with open(venv_data_file, "r", encoding="utf8") as fp: venv_data = json.load(fp) @@ -2203,6 +2212,11 @@ def _is_venv_outdated(venv_data_file): " differs from the current Python!" ) return True + if venv_data.get("deps_hash", "") != _get_deps_hash(current_deps): + print( + "Warning! Python dependencies have changed!" + ) + return True return False except (OSError, ValueError): return True @@ -2235,15 +2249,19 @@ def _create_venv(venv_dir): sys.stderr.write("Error: Failed to create a proper virtual environment. Missing the Python executable!\n") env.Exit(1) + # Define deps here so we can track changes + deps = _get_python_deps() + venv_dir = get_idf_venv_dir() venv_data_file = str(Path(venv_dir) / "pio-idf-venv.json") - if not os.path.isfile(venv_data_file) or _is_venv_outdated(venv_data_file): + if not os.path.isfile(venv_data_file) or _is_venv_outdated(venv_data_file, deps): _create_venv(venv_dir) - install_python_deps() + install_python_deps(deps) with open(venv_data_file, "w", encoding="utf8") as fp: venv_info = { "version": IDF_ENV_VERSION, - "python_version": _get_idf_venv_python_version() + "python_version": _get_idf_venv_python_version(), + "deps_hash": _get_deps_hash(deps) } json.dump(venv_info, fp, indent=2)