From 63050dbffe6a9be55b823cd91bccbb528b376e62 Mon Sep 17 00:00:00 2001 From: David Coghlan Date: Tue, 6 Oct 2020 18:23:10 +0100 Subject: [PATCH 1/8] Fix absolute paths for Cygwin Absolute paths on Windows take the form "c:\somePath" -- these need to be mapped to the form "/cygdrive/c/somePath" on Cygwin. Otherwise, the virtualenv path gets garbled when activated on Cygwin, which can cause the wrong Python environment to be used. --- src/virtualenv/activation/bash/activate.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/virtualenv/activation/bash/activate.sh b/src/virtualenv/activation/bash/activate.sh index 19bf552bd..cd3d03256 100644 --- a/src/virtualenv/activation/bash/activate.sh +++ b/src/virtualenv/activation/bash/activate.sh @@ -46,7 +46,7 @@ deactivate () { # unset irrelevant variables deactivate nondestructive -VIRTUAL_ENV='__VIRTUAL_ENV__' +VIRTUAL_ENV="$(if [ "$OSTYPE" = "cygwin" ]; then cygpath -u '__VIRTUAL_ENV__'; else echo '__VIRTUAL_ENV__'; fi;)" export VIRTUAL_ENV _OLD_VIRTUAL_PATH="$PATH" From 0779f409ca80f7eeffc92ea5f877d19b3fda186b Mon Sep 17 00:00:00 2001 From: David Coghlan Date: Fri, 9 Oct 2020 13:04:05 +0100 Subject: [PATCH 2/8] Remove Cygwin from the list, rely on the shell script for that --- src/virtualenv/activation/via_template.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/virtualenv/activation/via_template.py b/src/virtualenv/activation/via_template.py index 6839f0590..33a401380 100644 --- a/src/virtualenv/activation/via_template.py +++ b/src/virtualenv/activation/via_template.py @@ -34,7 +34,7 @@ def generate(self, creator): def replacements(self, creator, dest_folder): current_platform = sysconfig.get_platform() - platforms = ["mingw", "cygwin", "msys"] + platforms = ["mingw", "msys"] if any(platform in current_platform for platform in platforms): pattern = re.compile("^([A-Za-z]):(.*)") match = pattern.match(str(creator.dest)) From b6cd6ccfd2535340ed89072e52a5d9a33f7ed58c Mon Sep 17 00:00:00 2001 From: David Coghlan Date: Fri, 9 Oct 2020 13:55:57 +0100 Subject: [PATCH 3/8] Improved formatting --- src/virtualenv/activation/bash/activate.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/virtualenv/activation/bash/activate.sh b/src/virtualenv/activation/bash/activate.sh index cd3d03256..7f5bc5696 100644 --- a/src/virtualenv/activation/bash/activate.sh +++ b/src/virtualenv/activation/bash/activate.sh @@ -46,7 +46,12 @@ deactivate () { # unset irrelevant variables deactivate nondestructive -VIRTUAL_ENV="$(if [ "$OSTYPE" = "cygwin" ]; then cygpath -u '__VIRTUAL_ENV__'; else echo '__VIRTUAL_ENV__'; fi;)" +VIRTUAL_ENV='__VIRTUAL_ENV__' + +if [ "$OSTYPE" = "cygwin" ]; then + VIRTUAL_ENV=$(cygpath -u "$VIRTUAL_ENV") +fi + export VIRTUAL_ENV _OLD_VIRTUAL_PATH="$PATH" From 30f4aeb66d66f5ab6d29a75748ecd9bf8bf99c33 Mon Sep 17 00:00:00 2001 From: David Coghlan Date: Fri, 9 Oct 2020 14:26:12 +0100 Subject: [PATCH 4/8] Update tests to handle mingw + msys explicitly --- tests/unit/activation/test_activation_support.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/unit/activation/test_activation_support.py b/tests/unit/activation/test_activation_support.py index 5a234f9cc..b00996b9f 100644 --- a/tests/unit/activation/test_activation_support.py +++ b/tests/unit/activation/test_activation_support.py @@ -65,9 +65,9 @@ def __init__(self): @pytest.mark.skipif(IS_WIN, reason="Github Actions ships with WSL bash") -@pytest.mark.parametrize("activator_class", [BashActivator]) -def test_cygwin_msys2_path_conversion(mocker, activator_class): - mocker.patch("sysconfig.get_platform", return_value="mingw") +@pytest.mark.parametrize("activator_class,platform", [(BashActivator,"mingw"),(BashActivator,"msys")]) +def test_mingw_path_conversion(mocker, activator_class, platform): + mocker.patch("sysconfig.get_platform", return_value=platform) activator = activator_class(Namespace(prompt=None)) creator = Creator() mocker.stub(creator.bin_dir.relative_to) @@ -87,9 +87,9 @@ def test_win_path_no_conversion(mocker, activator_class): @pytest.mark.skipif(IS_WIN, reason="Github Actions ships with WSL bash") -@pytest.mark.parametrize("activator_class", [BashActivator]) -def test_cygwin_path_no_conversion(mocker, activator_class): - mocker.patch("sysconfig.get_platform", return_value="cygwin") +@pytest.mark.parametrize("activator_class,platform", [(BashActivator,"mingw"),(BashActivator,"msys")]) +def test_mingw_path_no_conversion(mocker, activator_class, platform): + mocker.patch("sysconfig.get_platform", return_value=platform) activator = activator_class(Namespace(prompt=None)) creator = Creator() creator.dest = "/c/tools/msys64/home" From 05e8a921b6f12b66f44f6ba5055854f79bb757e9 Mon Sep 17 00:00:00 2001 From: David Coghlan Date: Fri, 9 Oct 2020 14:32:51 +0100 Subject: [PATCH 5/8] Add changelog entry --- docs/changelog/1969.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/changelog/1969.bugfix.rst diff --git a/docs/changelog/1969.bugfix.rst b/docs/changelog/1969.bugfix.rst new file mode 100644 index 000000000..0cc9e124b --- /dev/null +++ b/docs/changelog/1969.bugfix.rst @@ -0,0 +1 @@ +Handle Cygwin path conversion in the activation script - by :user:`davidcoghlan`. \ No newline at end of file From 75a8a042f1e8dd638429f224d09e4d8f6119f86d Mon Sep 17 00:00:00 2001 From: David Coghlan Date: Fri, 9 Oct 2020 14:39:46 +0100 Subject: [PATCH 6/8] lint errors --- docs/changelog/1969.bugfix.rst | 2 +- tests/unit/activation/test_activation_support.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/changelog/1969.bugfix.rst b/docs/changelog/1969.bugfix.rst index 0cc9e124b..6e6e70677 100644 --- a/docs/changelog/1969.bugfix.rst +++ b/docs/changelog/1969.bugfix.rst @@ -1 +1 @@ -Handle Cygwin path conversion in the activation script - by :user:`davidcoghlan`. \ No newline at end of file +Handle Cygwin path conversion in the activation script - by :user:`davidcoghlan`. diff --git a/tests/unit/activation/test_activation_support.py b/tests/unit/activation/test_activation_support.py index b00996b9f..332168358 100644 --- a/tests/unit/activation/test_activation_support.py +++ b/tests/unit/activation/test_activation_support.py @@ -65,7 +65,7 @@ def __init__(self): @pytest.mark.skipif(IS_WIN, reason="Github Actions ships with WSL bash") -@pytest.mark.parametrize("activator_class,platform", [(BashActivator,"mingw"),(BashActivator,"msys")]) +@pytest.mark.parametrize("activator_class,platform", [(BashActivator, "mingw"), (BashActivator, "msys")]) def test_mingw_path_conversion(mocker, activator_class, platform): mocker.patch("sysconfig.get_platform", return_value=platform) activator = activator_class(Namespace(prompt=None)) @@ -87,7 +87,7 @@ def test_win_path_no_conversion(mocker, activator_class): @pytest.mark.skipif(IS_WIN, reason="Github Actions ships with WSL bash") -@pytest.mark.parametrize("activator_class,platform", [(BashActivator,"mingw"),(BashActivator,"msys")]) +@pytest.mark.parametrize("activator_class,platform", [(BashActivator, "mingw"), (BashActivator, "msys")]) def test_mingw_path_no_conversion(mocker, activator_class, platform): mocker.patch("sysconfig.get_platform", return_value=platform) activator = activator_class(Namespace(prompt=None)) From a34b438eed90abe6059b9ea34b86958a26c913ad Mon Sep 17 00:00:00 2001 From: David Coghlan Date: Fri, 9 Oct 2020 16:26:35 +0100 Subject: [PATCH 7/8] Add msys support to the activation script --- src/virtualenv/activation/bash/activate.sh | 4 +- src/virtualenv/activation/via_template.py | 13 +----- .../activation/test_activation_support.py | 44 ------------------- 3 files changed, 2 insertions(+), 59 deletions(-) diff --git a/src/virtualenv/activation/bash/activate.sh b/src/virtualenv/activation/bash/activate.sh index 7f5bc5696..2b3d5ffa9 100644 --- a/src/virtualenv/activation/bash/activate.sh +++ b/src/virtualenv/activation/bash/activate.sh @@ -47,11 +47,9 @@ deactivate () { deactivate nondestructive VIRTUAL_ENV='__VIRTUAL_ENV__' - -if [ "$OSTYPE" = "cygwin" ]; then +if ([ "$OSTYPE" = "cygwin" ] || [ "$OSTYPE" = "msys" ]) && $(command -v cygpath &> /dev/null) > 0 ; then VIRTUAL_ENV=$(cygpath -u "$VIRTUAL_ENV") fi - export VIRTUAL_ENV _OLD_VIRTUAL_PATH="$PATH" diff --git a/src/virtualenv/activation/via_template.py b/src/virtualenv/activation/via_template.py index 33a401380..d7a3d6247 100644 --- a/src/virtualenv/activation/via_template.py +++ b/src/virtualenv/activation/via_template.py @@ -33,20 +33,9 @@ def generate(self, creator): return generated def replacements(self, creator, dest_folder): - current_platform = sysconfig.get_platform() - platforms = ["mingw", "msys"] - if any(platform in current_platform for platform in platforms): - pattern = re.compile("^([A-Za-z]):(.*)") - match = pattern.match(str(creator.dest)) - if match: - virtual_env = "/" + match.group(1).lower() + match.group(2) - else: - virtual_env = str(creator.dest) - else: - virtual_env = str(creator.dest) return { "__VIRTUAL_PROMPT__": "" if self.flag_prompt is None else self.flag_prompt, - "__VIRTUAL_ENV__": ensure_text(virtual_env), + "__VIRTUAL_ENV__": ensure_text(str(creator.dest)), "__VIRTUAL_NAME__": creator.env_name, "__BIN_NAME__": ensure_text(str(creator.bin_dir.relative_to(creator.dest))), "__PATH_SEP__": ensure_text(os.pathsep), diff --git a/tests/unit/activation/test_activation_support.py b/tests/unit/activation/test_activation_support.py index 332168358..d493c23ae 100644 --- a/tests/unit/activation/test_activation_support.py +++ b/tests/unit/activation/test_activation_support.py @@ -13,8 +13,6 @@ PythonActivator, ) from virtualenv.discovery.py_info import PythonInfo -from virtualenv.info import IS_WIN -from virtualenv.util.path import Path @pytest.mark.parametrize( @@ -55,45 +53,3 @@ def test_activator_no_support_posix(mocker, activator_class): interpreter = mocker.Mock(spec=PythonInfo) interpreter.os = "posix" assert not activator.supports(interpreter) - - -class Creator: - def __init__(self): - self.dest = "C:/tools/msys64/home" - self.env_name = "venv" - self.bin_dir = Path("C:/tools/msys64/home/bin") - - -@pytest.mark.skipif(IS_WIN, reason="Github Actions ships with WSL bash") -@pytest.mark.parametrize("activator_class,platform", [(BashActivator, "mingw"), (BashActivator, "msys")]) -def test_mingw_path_conversion(mocker, activator_class, platform): - mocker.patch("sysconfig.get_platform", return_value=platform) - activator = activator_class(Namespace(prompt=None)) - creator = Creator() - mocker.stub(creator.bin_dir.relative_to) - resource = activator.replacements(creator, "") - assert resource["__VIRTUAL_ENV__"] == "/c/tools/msys64/home" - - -@pytest.mark.skipif(IS_WIN, reason="Github Actions ships with WSL bash") -@pytest.mark.parametrize("activator_class", [BashActivator]) -def test_win_path_no_conversion(mocker, activator_class): - mocker.patch("sysconfig.get_platform", return_value="win-amd64") - activator = activator_class(Namespace(prompt=None)) - creator = Creator() - mocker.stub(creator.bin_dir.relative_to) - resource = activator.replacements(creator, "") - assert resource["__VIRTUAL_ENV__"] == "C:/tools/msys64/home" - - -@pytest.mark.skipif(IS_WIN, reason="Github Actions ships with WSL bash") -@pytest.mark.parametrize("activator_class,platform", [(BashActivator, "mingw"), (BashActivator, "msys")]) -def test_mingw_path_no_conversion(mocker, activator_class, platform): - mocker.patch("sysconfig.get_platform", return_value=platform) - activator = activator_class(Namespace(prompt=None)) - creator = Creator() - creator.dest = "/c/tools/msys64/home" - creator.bin_dir = Path("/c/tools/msys64/home/bin") - mocker.stub(creator.bin_dir.relative_to) - resource = activator.replacements(creator, "") - assert resource["__VIRTUAL_ENV__"] == "/c/tools/msys64/home" From a27fe8b9a08a7f54c9c51925f487d071e9997088 Mon Sep 17 00:00:00 2001 From: David Coghlan Date: Fri, 9 Oct 2020 16:50:08 +0100 Subject: [PATCH 8/8] Fix script error & linting --- src/virtualenv/activation/bash/activate.sh | 2 +- src/virtualenv/activation/via_template.py | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/virtualenv/activation/bash/activate.sh b/src/virtualenv/activation/bash/activate.sh index 2b3d5ffa9..222d98204 100644 --- a/src/virtualenv/activation/bash/activate.sh +++ b/src/virtualenv/activation/bash/activate.sh @@ -47,7 +47,7 @@ deactivate () { deactivate nondestructive VIRTUAL_ENV='__VIRTUAL_ENV__' -if ([ "$OSTYPE" = "cygwin" ] || [ "$OSTYPE" = "msys" ]) && $(command -v cygpath &> /dev/null) > 0 ; then +if ([ "$OSTYPE" = "cygwin" ] || [ "$OSTYPE" = "msys" ]) && $(command -v cygpath &> /dev/null) ; then VIRTUAL_ENV=$(cygpath -u "$VIRTUAL_ENV") fi export VIRTUAL_ENV diff --git a/src/virtualenv/activation/via_template.py b/src/virtualenv/activation/via_template.py index d7a3d6247..14f097973 100644 --- a/src/virtualenv/activation/via_template.py +++ b/src/virtualenv/activation/via_template.py @@ -1,9 +1,7 @@ from __future__ import absolute_import, unicode_literals import os -import re import sys -import sysconfig from abc import ABCMeta, abstractmethod from six import add_metaclass