Skip to content

Commit 5a74961

Browse files
committed
Better handling of IDF virtual environment
Resolves #1525
1 parent 1442a35 commit 5a74961

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

builder/frameworks/espidf.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,11 +1343,37 @@ def get_idf_venv_dir():
13431343

13441344
def ensure_python_venv_available():
13451345

1346+
def _get_idf_venv_python_version():
1347+
try:
1348+
version = subprocess.check_output(
1349+
[
1350+
get_python_exe(),
1351+
"-c",
1352+
"import sys;print('{0}.{1}.{2}-{3}.{4}'.format(*list(sys.version_info)))"
1353+
], text=True
1354+
)
1355+
return version.strip()
1356+
except subprocess.CalledProcessError as e:
1357+
print("Failed to extract Python version from IDF virtual env!")
1358+
return None
1359+
13461360
def _is_venv_outdated(venv_data_file):
13471361
try:
13481362
with open(venv_data_file, "r", encoding="utf8") as fp:
13491363
venv_data = json.load(fp)
13501364
if venv_data.get("version", "") != IDF_ENV_VERSION:
1365+
print(
1366+
"Warning! IDF virtual environment version changed!"
1367+
)
1368+
return True
1369+
if (
1370+
venv_data.get("python_version", "")
1371+
!= _get_idf_venv_python_version()
1372+
):
1373+
print(
1374+
"Warning! Python version in the IDF virtual environment"
1375+
" differs from the current Python!"
1376+
)
13511377
return True
13521378
return False
13531379
except:
@@ -1387,8 +1413,13 @@ def _create_venv(venv_dir):
13871413
venv_data_file = os.path.join(venv_dir, "pio-idf-venv.json")
13881414
if not os.path.isfile(venv_data_file) or _is_venv_outdated(venv_data_file):
13891415
_create_venv(venv_dir)
1416+
1417+
install_python_deps()
13901418
with open(venv_data_file, "w", encoding="utf8") as fp:
1391-
venv_info = {"version": IDF_ENV_VERSION}
1419+
venv_info = {
1420+
"version": IDF_ENV_VERSION,
1421+
"python_version": _get_idf_venv_python_version()
1422+
}
13921423
json.dump(venv_info, fp, indent=2)
13931424

13941425

@@ -1407,11 +1438,10 @@ def get_python_exe():
14071438

14081439

14091440
#
1410-
# ESP-IDF requires Python packages with specific versions in a virtual environment
1441+
# Ensure Python environment contains everything required for IDF
14111442
#
14121443

14131444
ensure_python_venv_available()
1414-
install_python_deps()
14151445

14161446
# ESP-IDF package doesn't contain .git folder, instead package version is specified
14171447
# in a special file "version.h" in the root folder of the package

0 commit comments

Comments
 (0)