@@ -2722,56 +2722,6 @@ def get_constraints(idf_version: str, online: bool = True) -> str:
27222722 raise SystemExit (1 )
27232723
27242724
2725- def install_legacy_python_virtualenv (path : str ) -> None :
2726- """
2727- Checks if pip is installed (and installs it if not),
2728- checks whether virtualenv is already installed (and in which version),
2729- and finally creates virtual environment with python -m virtualenv <virtualenv_options>.
2730- """
2731- # Before creating the virtual environment, check if pip is installed.
2732- try :
2733- subprocess .check_call ([sys .executable , '-m' , 'pip' , '--version' ])
2734- except subprocess .CalledProcessError :
2735- fatal (
2736- f"Python interpreter at { sys .executable } doesn't have pip installed. "
2737- 'Please check the Getting Started Guides for the steps to install prerequisites for your OS.'
2738- )
2739- raise SystemExit (1 )
2740-
2741- virtualenv_installed_via_pip = False
2742- try :
2743- import virtualenv # noqa: F401
2744- except ImportError :
2745- info ('Installing virtualenv' )
2746- subprocess .check_call (
2747- [sys .executable , '-m' , 'pip' , 'install' , '--user' , 'virtualenv' ], stdout = sys .stdout , stderr = sys .stderr
2748- )
2749- virtualenv_installed_via_pip = True
2750- # since we just installed virtualenv via pip, we know that version is recent enough
2751- # so the version check below is not necessary.
2752-
2753- with_seeder_option = True
2754- if not virtualenv_installed_via_pip :
2755- # virtualenv is already present in the system and may have been installed via OS package manager
2756- # check the version to determine if we should add --seeder option
2757- try :
2758- major_ver = int (virtualenv .__version__ .split ('.' )[0 ])
2759- if major_ver < 20 :
2760- warn (f'Virtualenv version { virtualenv .__version__ } is old, please consider upgrading it' )
2761- with_seeder_option = False
2762- except (ValueError , NameError , AttributeError , IndexError ):
2763- pass
2764-
2765- info (f'Creating a new Python environment using virtualenv in { path } ' )
2766- virtualenv_options = ['--python' , sys .executable ]
2767- if with_seeder_option :
2768- virtualenv_options += ['--seeder' , 'pip' ]
2769-
2770- subprocess .check_call (
2771- [sys .executable , '-m' , 'virtualenv' , * virtualenv_options , path ], stdout = sys .stdout , stderr = sys .stderr
2772- )
2773-
2774-
27752725def action_install_python_env (args ): # type: ignore
27762726 """
27772727 (Re)installs python virtual environment.
@@ -2782,16 +2732,6 @@ def action_install_python_env(args): # type: ignore
27822732 reinstall = args .reinstall
27832733 idf_python_env_path , _ , virtualenv_python , idf_version = get_python_env_path ()
27842734
2785- nix_store = os .environ .get ('NIX_STORE' )
2786- is_nix = nix_store is not None and sys .base_prefix .startswith (nix_store ) and sys .prefix .startswith (nix_store )
2787-
2788- is_virtualenv = not is_nix and (
2789- hasattr (sys , 'real_prefix' ) or (hasattr (sys , 'base_prefix' ) and sys .base_prefix != sys .prefix )
2790- )
2791- if is_virtualenv and (not os .path .exists (idf_python_env_path ) or reinstall ):
2792- fatal ('This script was called from a virtual environment, can not create a virtual environment again' )
2793- raise SystemExit (1 )
2794-
27952735 if os .path .exists (virtualenv_python ):
27962736 try :
27972737 subprocess .check_call ([virtualenv_python , '--version' ], stdout = sys .stdout , stderr = sys .stderr )
@@ -2821,69 +2761,53 @@ def action_install_python_env(args): # type: ignore
28212761 if os .path .exists (virtualenv_python ):
28222762 check_python_venv_compatibility (idf_python_env_path , idf_version )
28232763 else :
2824- if (
2825- subprocess .run (
2826- [sys .executable , '-m' , 'venv' , '-h' ], check = False , stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL
2827- ).returncode
2828- == 0
2829- ):
2830- # venv available
2831- virtualenv_options = []
2832-
2833- info (f'Creating a new Python environment in { idf_python_env_path } ' )
2834-
2835- try :
2836- environ_idf_python_env_path = os .environ ['IDF_PYTHON_ENV_PATH' ]
2837- correct_env_path = environ_idf_python_env_path .endswith (
2838- PYTHON_VENV_DIR_TEMPLATE .format (idf_version , PYTHON_VER_MAJOR_MINOR )
2839- )
2840- if not correct_env_path and re .search (
2841- PYTHON_VENV_DIR_TEMPLATE .format (r'\d+\.\d+' , r'\d+\.\d+' ), environ_idf_python_env_path
2842- ):
2843- warn (
2844- f'IDF_PYTHON_ENV_PATH is set to { environ_idf_python_env_path } but it does not match '
2845- f'the detected { idf_version } ESP-IDF version and/or the used { PYTHON_VER_MAJOR_MINOR } '
2846- 'version of Python. If you have not set IDF_PYTHON_ENV_PATH intentionally then it is '
2847- 'recommended to re-run this script from a clean shell where an ESP-IDF environment is '
2848- 'not active.'
2849- )
2850-
2851- # Verify if IDF_PYTHON_ENV_PATH is a valid ESP-IDF Python virtual environment directory
2852- # to decide if content should be removed
2853- if os .path .exists (os .path .join (environ_idf_python_env_path , VENV_VER_FILE )) or re .search (
2854- PYTHON_VENV_DIR_TEMPLATE .format (r'\d+\.\d+' , r'\d+\.\d+' ), environ_idf_python_env_path
2855- ):
2856- virtualenv_options .append ('--clear' ) # delete environment if already exists
2857- elif os .listdir (environ_idf_python_env_path ): # show the message only if the directory is not empty
2858- info (
2859- f'IDF_PYTHON_ENV_PATH is set to { environ_idf_python_env_path } , '
2860- 'but it does not appear to be an ESP-IDF Python virtual environment directory. '
2861- 'Existing data in this folder will be preserved to prevent unintentional data loss.'
2862- )
2863-
2864- except KeyError :
2865- # if IDF_PYTHON_ENV_PATH not defined then the above checks can be skipped
2866- pass
2867-
2868- subprocess .check_call (
2869- [sys .executable , '-m' , 'venv' , * virtualenv_options , idf_python_env_path ],
2870- stdout = sys .stdout ,
2871- stderr = sys .stderr ,
2764+ virtualenv_options = []
2765+ info (f'Creating a new Python environment in { idf_python_env_path } ' )
2766+ try :
2767+ environ_idf_python_env_path = os .environ ['IDF_PYTHON_ENV_PATH' ]
2768+ correct_env_path = environ_idf_python_env_path .endswith (
2769+ PYTHON_VENV_DIR_TEMPLATE .format (idf_version , PYTHON_VER_MAJOR_MINOR )
28722770 )
2873-
2874- try :
2875- with open (os .path .join (idf_python_env_path , VENV_VER_FILE ), 'w' , encoding = 'utf-8' ) as f :
2876- f .write (idf_version )
2877- except OSError :
2771+ if not correct_env_path and re .search (
2772+ PYTHON_VENV_DIR_TEMPLATE .format (r'\d+\.\d+' , r'\d+\.\d+' ), environ_idf_python_env_path
2773+ ):
28782774 warn (
2879- 'The following issue occurred while generating the '
2880- 'ESP-IDF version file in the Python environment: {e}. '
2881- '(Diagnostic information. It can be ignored.)'
2775+ f'IDF_PYTHON_ENV_PATH is set to { environ_idf_python_env_path } but it does not match '
2776+ f'the detected { idf_version } ESP-IDF version and/or the used { PYTHON_VER_MAJOR_MINOR } '
2777+ 'version of Python. If you have not set IDF_PYTHON_ENV_PATH intentionally then it is '
2778+ 'recommended to re-run this script from a clean shell where an ESP-IDF environment is '
2779+ 'not active.'
2780+ )
2781+ # Verify if IDF_PYTHON_ENV_PATH is a valid ESP-IDF Python virtual environment directory
2782+ # to decide if content should be removed
2783+ if os .path .exists (os .path .join (environ_idf_python_env_path , VENV_VER_FILE )) or re .search (
2784+ PYTHON_VENV_DIR_TEMPLATE .format (r'\d+\.\d+' , r'\d+\.\d+' ), environ_idf_python_env_path
2785+ ):
2786+ virtualenv_options .append ('--clear' ) # delete environment if already exists
2787+ elif os .listdir (environ_idf_python_env_path ): # show the message only if the directory is not empty
2788+ info (
2789+ f'IDF_PYTHON_ENV_PATH is set to { environ_idf_python_env_path } , '
2790+ 'but it does not appear to be an ESP-IDF Python virtual environment directory. '
2791+ 'Existing data in this folder will be preserved to prevent unintentional data loss.'
28822792 )
28832793
2884- else :
2885- # The embeddable Python for Windows doesn't have the built-in venv module
2886- install_legacy_python_virtualenv (idf_python_env_path )
2794+ except KeyError :
2795+ # if IDF_PYTHON_ENV_PATH not defined then the above checks can be skipped
2796+ pass
2797+ subprocess .check_call (
2798+ [sys .executable , '-m' , 'venv' , * virtualenv_options , idf_python_env_path ],
2799+ stdout = sys .stdout ,
2800+ stderr = sys .stderr ,
2801+ )
2802+ try :
2803+ with open (os .path .join (idf_python_env_path , VENV_VER_FILE ), 'w' , encoding = 'utf-8' ) as f :
2804+ f .write (idf_version )
2805+ except OSError :
2806+ warn (
2807+ 'The following issue occurred while generating the '
2808+ 'ESP-IDF version file in the Python environment: {e}. '
2809+ '(Diagnostic information. It can be ignored.)'
2810+ )
28872811
28882812 env_copy = os .environ .copy ()
28892813 # Enforce disabling possible pip 'user' option to prevent installation error with virtual environment
0 commit comments