diff --git a/generators/VirtualPythonEnv.py b/generators/VirtualPythonEnv.py index 6a3fa91..84c0758 100644 --- a/generators/VirtualPythonEnv.py +++ b/generators/VirtualPythonEnv.py @@ -138,26 +138,14 @@ class VirtualPythonEnv(Generator): env = "conanrun") # Generate the Python Virtual Environment Script - with open(Path(__file__).parent.joinpath("VirtualPythonEnvResources", "activate.bat.jinja"), "r") as f: - activate_bat = Template(f.read()).render(envvars = envvars, prompt = self.conanfile.name) - - with open(Path(__file__).parent.joinpath("VirtualPythonEnvResources", "deactivate.bat.jinja"), "r") as f: - deactivate_bat = Template(f.read()).render(envvars = envvars) - - with open(Path(__file__).parent.joinpath("VirtualPythonEnvResources", "Activate.ps1.jinja"), "r") as f: - activate_ps1 = Template(f.read()).render(envvars = envvars, prompt = self.conanfile.name) - - with open(Path(__file__).parent.joinpath("VirtualPythonEnvResources", "activate.jinja"), "r") as f: - activate_sh = Template(f.read()).render(envvars = envvars, prompt = self.conanfile.name) + envvars.save_sh(Path(venv_folder, self._venv_path, "activate")) + envvars.save_bat(Path(venv_folder, self._venv_path, "activate.bat")) + envvars.save_ps1(Path(venv_folder, self._venv_path, "Activate.ps1")) with open(Path(__file__).parent.joinpath("VirtualPythonEnvResources", "activate_github_actions_buildenv.jinja"), "r") as f: env_prefix = "Env:" if self.conanfile.settings.os == "Windows" else "" activate_github_actions_buildenv = Template(f.read()).render(envvars = envvars, env_prefix = env_prefix) return { - str(Path(venv_folder, self._venv_path, "activate.bat")): activate_bat, - str(Path(venv_folder, self._venv_path, "deactivate.bat.jinja")): deactivate_bat, - str(Path(venv_folder, self._venv_path, "Activate.ps1")): activate_ps1, - str(Path(venv_folder, self._venv_path, "activate")): activate_sh, str(Path(venv_folder, self._venv_path, f"activate_github_actions_env{self._script_ext}")): activate_github_actions_buildenv } diff --git a/generators/VirtualPythonEnvResources/Activate.ps1.jinja b/generators/VirtualPythonEnvResources/Activate.ps1.jinja deleted file mode 100644 index 1c5c294..0000000 --- a/generators/VirtualPythonEnvResources/Activate.ps1.jinja +++ /dev/null @@ -1,77 +0,0 @@ -<# -.Synopsis -Activate a Python virtual environment for the current PowerShell session. - -.Notes -On Windows, it may be required to enable this Activate.ps1 script by setting the -execution policy for the user. You can do this by issuing the following PowerShell -command: - -PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser - -For more information on Execution Policies: -https://go.microsoft.com/fwlink/?LinkID=135170 - -#> - -<# -.Synopsis -Remove all shell session elements added by the Activate script, including the -addition of the virtual environment's Python executable from the beginning of -the PATH variable. - -.Parameter NonDestructive -If present, do not remove this function from the global namespace for the -session. - -#> -function global:deactivate ([switch]$NonDestructive) { - # Revert to original values - - # The prior prompt: - if (Test-Path -Path Function:_OLD_VIRTUAL_PROMPT) { - Copy-Item -Path Function:_OLD_VIRTUAL_PROMPT -Destination Function:prompt - Remove-Item -Path Function:_OLD_VIRTUAL_PROMPT - } - - if (Test-Path -Path Env:VIRTUAL_ENV_PROMPT) { - Remove-Item -Path env:VIRTUAL_ENV_PROMPT - } - - # Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether: - if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) { - Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force - } - - {% for var, value in envvars.items() %}if (Test-Path -Path Env:_OLD_{{ var }}) { - Copy-Item -Path Env:_OLD_{{ var }} -Destination Env:{{ var }} - Remove-Item -Path Env:_OLD_{{ var }} - } else { - if (Test-Path -Path Env:{{ var }}) { - Remove-Item -Path Env:{{ var }} - } - } - {% endfor %} - # Leave deactivate function in the global namespace if requested: - if (-not $NonDestructive) { - Remove-Item -Path function:deactivate - } -} - -Copy-Item -Path Env:PATH -Destination Env:_OLD_PATH - -{% for var, value in envvars.items() %}if (Test-Path -Path Env:{{ var }}) { - Copy-Item -Path Env:{{ var }} -Destination Env:_OLD_{{ var }} -} -$Env:{{ var }} = "{{ value }}" -{% endfor %} - -function global:_OLD_VIRTUAL_PROMPT { "" } -Copy-Item -Path function:prompt -Destination function:_OLD_VIRTUAL_PROMPT -New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value {{ prompt }} - -function global:prompt { - Write-Host -NoNewline -ForegroundColor Green "($_PYTHON_VENV_PROMPT_PREFIX) " - _OLD_VIRTUAL_PROMPT -} -$env:VIRTUAL_ENV_PROMPT = "{{ prompt }}" diff --git a/generators/VirtualPythonEnvResources/activate.bat.jinja b/generators/VirtualPythonEnvResources/activate.bat.jinja deleted file mode 100644 index 4b1374e..0000000 --- a/generators/VirtualPythonEnvResources/activate.bat.jinja +++ /dev/null @@ -1,19 +0,0 @@ -@echo off - -rem This file is UTF-8 encoded, so we need to update the current code page while executing it -for /f "tokens=2 delims=:." %%a in ('"%SystemRoot%\System32\chcp.com"') do ( - set _OLD_CODEPAGE=%%a -) -if defined _OLD_CODEPAGE ( - "%SystemRoot%\System32\chcp.com" 65001 > nul -) - -{% for var, value in envvars.items() %}set _OLD_{{ var }}=%PATH% -set {{ var }}={{ value }} -{% endfor %} -set PATH=%VIRTUAL_ENV%\Scripts;%PATH% -:END -if defined _OLD_CODEPAGE ( - "%SystemRoot%\System32\chcp.com" %_OLD_CODEPAGE% > nul - set _OLD_CODEPAGE= -) diff --git a/generators/VirtualPythonEnvResources/activate.jinja b/generators/VirtualPythonEnvResources/activate.jinja deleted file mode 100644 index 4abb356..0000000 --- a/generators/VirtualPythonEnvResources/activate.jinja +++ /dev/null @@ -1,27 +0,0 @@ -# This file must be used with "source bin/activate" *from bash* -# you cannot run it directly - -deactivate () { - # reset old environment variables - {% for var in envvars.keys() %}if [ -n "$_OLD_{{ var }}" ] ; then - export {{ var }}="$_OLD_{{ var }}" - unset _OLD_{{ var }} - else - unset {{ var }} - fi - {% endfor %} - - if [ -n "${_OLD_VIRTUAL_PS1}" ] ; then - export PS1="${_OLD_VIRTUAL_PS1}" - unset _OLD_VIRTUAL_PS1 - fi -} - -{% for var, value in envvars.items() %}if [ -n "${{ var }}" ] ; then - export _OLD_{{ var }}="${{ var }}" -fi -export {{ var }}="{{ value }}" -{% endfor %} - -_OLD_VIRTUAL_PS1="${PS1:-}" -export PS1="({{ prompt }}) ${PS1:-}" diff --git a/generators/VirtualPythonEnvResources/deactivate.bat.jinja b/generators/VirtualPythonEnvResources/deactivate.bat.jinja deleted file mode 100644 index 3c6a22e..0000000 --- a/generators/VirtualPythonEnvResources/deactivate.bat.jinja +++ /dev/null @@ -1,9 +0,0 @@ -@echo off - -{% for var in envvars.keys() %}if defined _OLD_{{ var }} ( - set "{{ var }}=%_OLD_{{ var }}%" -) -set _OLD_{{ var }}= -{% endfor %} - -:END