Merge branch 'feat/deactivate_idf_env_every_run_v5.4' into 'release/v5.4'

feat(tools): Deactivate current ESP-IDF environment with every export run (v5.4)

See merge request espressif/esp-idf!38026
This commit is contained in:
Roland Dobai
2025-03-31 16:08:39 +08:00
2 changed files with 14 additions and 38 deletions

View File

@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
# SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
import getpass
import os
@@ -143,7 +143,6 @@ class BashShell(UnixShell):
def spawn(self) -> None:
self.init_file()
new_env = os.environ.copy()
new_env.update(self.expanded_env())
run([self.shell, '--init-file', str(self.script_file_path)], env=new_env)
@@ -188,7 +187,6 @@ class ZshShell(UnixShell):
shutil.copy(str(self.script_file_path), str(zshrc_path))
new_env = os.environ.copy()
new_env.update(self.expanded_env())
# Set new ZDOTDIR in the new environment
new_env['ZDOTDIR'] = str(tmpdir_path)
@@ -216,7 +214,6 @@ class FishShell(UnixShell):
def spawn(self) -> None:
self.init_file()
new_env = os.environ.copy()
new_env.update(self.expanded_env())
run([self.shell, f'--init-command=source {self.script_file_path}'], env=new_env)
@@ -262,7 +259,6 @@ class PowerShell(Shell):
def spawn(self) -> None:
self.init_file()
new_env = os.environ.copy()
new_env.update(self.expanded_env())
arguments = ['-NoExit', '-Command', f'{self.script_file_path}']
cmd: Union[str, List[str]] = [self.shell] + arguments
run(cmd, env=new_env)
@@ -315,7 +311,6 @@ class WinCmd(Shell):
def spawn(self) -> None:
self.init_file()
new_env = os.environ.copy()
new_env.update(self.expanded_env())
arguments = ['/k', f'{self.script_file_path}']
cmd: Union[str, List[str]] = [self.shell] + arguments
cmd = ' '.join(cmd)

View File

@@ -1,7 +1,7 @@
#!/usr/bin/env python
# coding=utf-8
#
# SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
# SPDX-FileCopyrightText: 2019-2025 Espressif Systems (Shanghai) CO LTD
#
# SPDX-License-Identifier: Apache-2.0
#
@@ -900,7 +900,8 @@ class IDFTool(object):
if v_repl != v:
v_repl = to_shell_specific_paths([v_repl])[0]
old_v = os.environ.get(k)
if old_v is None or old_v != v_repl:
# Remove condition in IDF-10292
if sys.platform != 'win32' or old_v is None or old_v != v_repl:
result[k] = v_repl
return result
@@ -1583,8 +1584,7 @@ class ENVState:
def save(self) -> str:
try:
if self.deactivate_file_path and os.path.basename(self.deactivate_file_path).endswith(f'idf_{str(os.getppid())}'):
# If exported file path/name exists and belongs to actual opened shell
if self.deactivate_file_path:
with open(self.deactivate_file_path, 'w', encoding='utf-8') as w:
json.dump(self.idf_variables, w, ensure_ascii=False, indent=4) # type: ignore
else:
@@ -1858,7 +1858,6 @@ def print_deactivate_statement(args: List[str]) -> None:
"""
env_state_obj = ENVState.get_env_state()
if not env_state_obj.idf_variables:
warn('No IDF variables to remove from environment found. Deactivation of previous esp-idf version was not successful.')
return
unset_vars = env_state_obj.idf_variables
env_path: Optional[str] = os.getenv('PATH')
@@ -1895,27 +1894,6 @@ def get_unset_format_and_separator(args: List[str]) -> Tuple[str, str]:
return {EXPORT_SHELL: ('unset {}', ';'), EXPORT_KEY_VALUE: ('{}', '\n')}[args.format] # type: ignore
def different_idf_detected() -> bool:
"""
Checks if new IDF detected.
"""
# If IDF global variable found, test if belong to different ESP-IDF version
if 'IDF_TOOLS_EXPORT_CMD' in os.environ:
if g.idf_path != os.path.dirname(os.environ['IDF_TOOLS_EXPORT_CMD']):
return True
# No previous ESP-IDF export detected, nothing to be unset
if all(s not in os.environ for s in ['IDF_PYTHON_ENV_PATH', 'OPENOCD_SCRIPTS', 'ESP_IDF_VERSION']):
return False
# User is exporting the same version as is in env
if os.getenv('ESP_IDF_VERSION') == get_idf_version():
return False
# Different version detected
return True
def active_repo_id() -> str:
"""
Function returns unique id of running ESP-IDF combining current idfpath with version.
@@ -2157,8 +2135,7 @@ def action_export(args: Any) -> None:
Exports all necessary environment variables and paths needed for tools used.
"""
if args.deactivate:
if different_idf_detected():
print_deactivate_statement(args)
print_deactivate_statement(args)
return
tools_info = load_tools_info()
@@ -2190,8 +2167,10 @@ def action_export(args: Any) -> None:
idf_python_env_path = to_shell_specific_paths([idf_python_env_path])[0]
if os.getenv('IDF_PYTHON_ENV_PATH') != idf_python_env_path:
export_vars['IDF_PYTHON_ENV_PATH'] = to_shell_specific_paths([idf_python_env_path])[0]
if current_path and idf_python_export_path not in current_path: # getenv can return None
paths_to_export.append(idf_python_export_path)
if current_path: # getenv can return None
# Remove condition in IDF-10292
if sys.platform != 'win32' or idf_python_export_path not in current_path:
paths_to_export.append(idf_python_export_path)
idf_version = get_idf_version()
if os.getenv('ESP_IDF_VERSION') != idf_version:
@@ -2201,8 +2180,10 @@ def action_export(args: Any) -> None:
idf_tools_dir = os.path.join(g.idf_path, 'tools') # type: ignore
idf_tools_dir = to_shell_specific_paths([idf_tools_dir])[0]
if current_path and idf_tools_dir not in current_path:
paths_to_export.append(idf_tools_dir)
if current_path:
# Remove condition in IDF-10292
if sys.platform != 'win32' or idf_tools_dir not in current_path:
paths_to_export.append(idf_tools_dir)
if sys.platform == 'win32':
old_path = '%PATH%'