Skip to content

Commit

Permalink
Fix powershell
Browse files Browse the repository at this point in the history
Signed-off-by: Bernat Gabor <bgabor8@bloomberg.net>
  • Loading branch information
gaborbernat committed Aug 21, 2020
1 parent 673c8c6 commit ebe560c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 69 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
*.bat text eol=crlf
*.ps1 text eol=lf
*.fish text eol=lf
*.csh text eol=lf
*.sh text eol=lf
120 changes: 60 additions & 60 deletions src/virtualenv/activation/powershell/activate.ps1
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
$script:THIS_PATH = $myinvocation.mycommand.path
$script:BASE_DIR = Split-Path (Resolve-Path "$THIS_PATH/..") -Parent

function global:deactivate([switch] $NonDestructive) {
if (Test-Path variable:_OLD_VIRTUAL_PATH) {
$env:PATH = $variable:_OLD_VIRTUAL_PATH
Remove-Variable "_OLD_VIRTUAL_PATH" -Scope global
}

if (Test-Path function:_old_virtual_prompt) {
$function:prompt = $function:_old_virtual_prompt
Remove-Item function:\_old_virtual_prompt
}

if ($env:VIRTUAL_ENV) {
Remove-Item env:VIRTUAL_ENV -ErrorAction SilentlyContinue
}

if (!$NonDestructive) {
# Self destruct!
Remove-Item function:deactivate
Remove-Item function:pydoc
}
}

function global:pydoc {
python -m pydoc $args
}

# unset irrelevant variables
deactivate -nondestructive

$VIRTUAL_ENV = $BASE_DIR
$env:VIRTUAL_ENV = $VIRTUAL_ENV

New-Variable -Scope global -Name _OLD_VIRTUAL_PATH -Value $env:PATH

$env:PATH = "$env:VIRTUAL_ENV/__BIN_NAME____PATH_SEP__" + $env:PATH
if (!$env:VIRTUAL_ENV_DISABLE_PROMPT) {
function global:_old_virtual_prompt {
""
}
$function:_old_virtual_prompt = $function:prompt

if ("__VIRTUAL_PROMPT__" -ne "") {
function global:prompt {
# Add the custom prefix to the existing prompt
$previous_prompt_value = & $function:_old_virtual_prompt
("__VIRTUAL_PROMPT__" + $previous_prompt_value)
}
}
else {
function global:prompt {
# Add a prefix to the current prompt, but don't discard it.
$previous_prompt_value = & $function:_old_virtual_prompt
$new_prompt_value = "($( Split-Path $env:VIRTUAL_ENV -Leaf )) "
($new_prompt_value + $previous_prompt_value)
}
}
}
$script:THIS_PATH = $myinvocation.mycommand.path
$script:BASE_DIR = Split-Path (Resolve-Path "$THIS_PATH/..") -Parent

function global:deactivate([switch] $NonDestructive) {
if (Test-Path variable:_OLD_VIRTUAL_PATH) {
$env:PATH = $variable:_OLD_VIRTUAL_PATH
Remove-Variable "_OLD_VIRTUAL_PATH" -Scope global
}

if (Test-Path function:_old_virtual_prompt) {
$function:prompt = $function:_old_virtual_prompt
Remove-Item function:\_old_virtual_prompt
}

if ($env:VIRTUAL_ENV) {
Remove-Item env:VIRTUAL_ENV -ErrorAction SilentlyContinue
}

if (!$NonDestructive) {
# Self destruct!
Remove-Item function:deactivate
Remove-Item function:pydoc
}
}

function global:pydoc {
python -m pydoc $args
}

# unset irrelevant variables
deactivate -nondestructive

$VIRTUAL_ENV = $BASE_DIR
$env:VIRTUAL_ENV = $VIRTUAL_ENV

New-Variable -Scope global -Name _OLD_VIRTUAL_PATH -Value $env:PATH

$env:PATH = "$env:VIRTUAL_ENV/__BIN_NAME____PATH_SEP__" + $env:PATH
if (!$env:VIRTUAL_ENV_DISABLE_PROMPT) {
function global:_old_virtual_prompt {
""
}
$function:_old_virtual_prompt = $function:prompt

if ("__VIRTUAL_PROMPT__" -ne "") {
function global:prompt {
# Add the custom prefix to the existing prompt
$previous_prompt_value = & $function:_old_virtual_prompt
("__VIRTUAL_PROMPT__" + $previous_prompt_value)
}
}
else {
function global:prompt {
# Add a prefix to the current prompt, but don't discard it.
$previous_prompt_value = & $function:_old_virtual_prompt
$new_prompt_value = "($( Split-Path $env:VIRTUAL_ENV -Leaf )) "
($new_prompt_value + $previous_prompt_value)
}
}
}
5 changes: 3 additions & 2 deletions src/virtualenv/create/via_global_ref/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from virtualenv.info import fs_supports_symlink
from virtualenv.util.path import Path
from virtualenv.util.six import ensure_text

from ..creator import Creator, CreatorMeta

Expand Down Expand Up @@ -91,10 +92,10 @@ def install_patch(self):
text = self.env_patch_text()
if text:
pth = self.purelib / "_virtualenv.pth"
logging.debug("create virtualenv import hook file %s", pth)
logging.debug("create virtualenv import hook file %s", ensure_text(str(pth)))
pth.write_text("import _virtualenv")
dest_path = self.purelib / "_virtualenv.py"
logging.debug("create %s", dest_path)
logging.debug("create %s", ensure_text(str(dest_path)))
dest_path.write_text(text)

def env_patch_text(self):
Expand Down
11 changes: 5 additions & 6 deletions tests/unit/activation/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@ def __call__(self, monkeypatch, tmp_path):

# check line endings are correct type
script_content = activate_script.read_bytes()
for line in script_content.split(b"\n"):
if line:
if self.unix_line_ending:
assert line[-1] != b"\r"
else:
assert line[-1] == b"\r"
for line in script_content.split(b"\n")[:-1]:
if self.unix_line_ending:
assert not line or line[-1] != 13, script_content.decode("utf-8")
else:
assert line[-1] == 13, script_content.decode("utf-8")

test_script = self._generate_test_script(activate_script, tmp_path)
monkeypatch.chdir(ensure_text(str(tmp_path)))
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/activation/test_python_activator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from textwrap import dedent

from virtualenv.activation import PythonActivator
from virtualenv.info import WIN_CPYTHON_2
from virtualenv.info import IS_WIN, WIN_CPYTHON_2
from virtualenv.util.six import ensure_text


Expand All @@ -21,6 +21,7 @@ def __init__(self, session):
extension="py",
non_source_fail_message="You must use exec(open(this_file).read(), {'__file__': this_file}))",
)
self.unix_line_ending = not IS_WIN

def env(self, tmp_path):
env = os.environ.copy()
Expand Down

0 comments on commit ebe560c

Please sign in to comment.