Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activation scripts for next-gen virtualenv #1454

Merged
merged 15 commits into from
Dec 15, 2019
4 changes: 1 addition & 3 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ jobs:
image: [linux, windows, macOs]
py35:
image: [linux, windows, macOs]
py34:
image: [linux, windows]
py27:
image: [linux, windows, macOs]
fix_lint:
Expand All @@ -67,7 +65,7 @@ jobs:
displayName: install fish and csh via brew
coverage:
with_toxenv: 'coverage' # generate .tox/.coverage, .tox/coverage.xml after tests run
for_envs: [py37, py36, py35, py34, py27]
for_envs: [py38, py37, py36, py35, py27]

- ${{ if startsWith(variables['Build.SourceBranch'], 'refs/tags/') }}:
- template: publish-pypi.yml@tox
Expand Down
16 changes: 15 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,21 @@ where = src
testing =
pytest >= 4.0.0, <6
coverage >= 4.5.0, < 5
pytest-mock
pytest-mock >= 1.12.1, <2
xonsh >= 0.9.13, <1; python_version > '3.4'
docs =
sphinx >= 2.0.0, < 3
towncrier >= 18.5.0
sphinx_rtd_theme >= 0.4.2, < 1

[options.package_data]
virtualenv.seed.embed.wheels = *.whl
virtualenv.activation.bash = *.sh
virtualenv.activation.cshell = *.csh
virtualenv.activation.batch = *.bat
virtualenv.activation.fish = *.fish
virtualenv.activation.powershell = *.ps1
virtualenv.activation.xonosh = *.xsh

[options.entry_points]
console_scripts =
Expand All @@ -78,6 +85,13 @@ virtualenv.seed =
pip = virtualenv.seed.embed.pip_invoke:PipInvoke
link-app-data = virtualenv.seed.embed.link_app_data:LinkFromAppData
virtualenv.activate =
bash = virtualenv.activation.bash:BashActivator
cshell = virtualenv.activation.cshell:CShellActivator
batch = virtualenv.activation.batch:BatchActivator
fish = virtualenv.activation.fish:FishActivator
power-shell = virtualenv.activation.powershell:PowerShellActivator
python = virtualenv.activation.python:PythonActivator
xonosh = virtualenv.activation.xonosh:XonoshActivator
[sdist]
formats = gztar

Expand Down
18 changes: 18 additions & 0 deletions src/virtualenv/activation/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
from __future__ import absolute_import, unicode_literals

from .bash import BashActivator
from .batch import BatchActivator
from .cshell import CShellActivator
from .fish import FishActivator
from .powershell import PowerShellActivator
from .python import PythonActivator
from .xonosh import XonoshActivator

__all__ = [
BashActivator,
PowerShellActivator,
XonoshActivator,
CShellActivator,
PythonActivator,
BatchActivator,
FishActivator,
]
11 changes: 5 additions & 6 deletions src/virtualenv/activation/activator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ def __init__(self, options):

@classmethod
def add_parser_arguments(cls, parser):
pass
"""add activator options"""

def run(self, creator):
self.generate()
if self.flag_prompt is not None:
creator.pyenv_cfg["prompt"] = self.flag_prompt
@classmethod
def supports(cls, interpreter):
return True

@abstractmethod
def generate(self):
def generate(self, creator):
raise NotImplementedError
14 changes: 14 additions & 0 deletions src/virtualenv/activation/bash/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from __future__ import absolute_import, unicode_literals

from pathlib2 import Path

from ..via_template import ViaTemplateActivator


class BashActivator(ViaTemplateActivator):
@classmethod
def supports(cls, interpreter):
return interpreter.os != "nt"

def templates(self):
yield Path("activate.sh")
84 changes: 84 additions & 0 deletions src/virtualenv/activation/bash/activate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly


if [ "${BASH_SOURCE-}" = "$0" ]; then
echo "You must source this script: \$ source $0" >&2
exit 33
fi

deactivate () {
unset -f pydoc >/dev/null 2>&1

# reset old environment variables
# ! [ -z ${VAR+_} ] returns true if VAR is declared at all
if ! [ -z "${_OLD_VIRTUAL_PATH:+_}" ] ; then
PATH="$_OLD_VIRTUAL_PATH"
export PATH
unset _OLD_VIRTUAL_PATH
fi
if ! [ -z "${_OLD_VIRTUAL_PYTHONHOME+_}" ] ; then
PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME"
export PYTHONHOME
unset _OLD_VIRTUAL_PYTHONHOME
fi

# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then
hash -r 2>/dev/null
fi

if ! [ -z "${_OLD_VIRTUAL_PS1+_}" ] ; then
PS1="$_OLD_VIRTUAL_PS1"
export PS1
unset _OLD_VIRTUAL_PS1
fi

unset VIRTUAL_ENV
if [ ! "${1-}" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
fi
}

# unset irrelevant variables
deactivate nondestructive

VIRTUAL_ENV="__VIRTUAL_ENV__"
export VIRTUAL_ENV

_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/__BIN_NAME__:$PATH"
export PATH

# unset PYTHONHOME if set
if ! [ -z "${PYTHONHOME+_}" ] ; then
_OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME"
unset PYTHONHOME
fi

if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
_OLD_VIRTUAL_PS1="${PS1-}"
if [ "x__VIRTUAL_PROMPT__" != x ] ; then
PS1="__VIRTUAL_PROMPT__${PS1-}"
else
PS1="(`basename \"$VIRTUAL_ENV\"`) ${PS1-}"
fi
export PS1
fi

# Make sure to unalias pydoc if it's already there
alias pydoc 2>/dev/null >/dev/null && unalias pydoc || true

pydoc () {
python -m pydoc "$@"
}

# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then
hash -r 2>/dev/null
fi
16 changes: 16 additions & 0 deletions src/virtualenv/activation/batch/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import absolute_import, unicode_literals

from pathlib2 import Path

from ..via_template import ViaTemplateActivator


class BatchActivator(ViaTemplateActivator):
@classmethod
def supports(cls, interpreter):
return interpreter.os == "nt"

def templates(self):
yield Path("activate.bat")
yield Path("deactivate.bat")
yield Path("pydoc.bat")
35 changes: 35 additions & 0 deletions src/virtualenv/activation/batch/activate.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@echo off

set "VIRTUAL_ENV=__VIRTUAL_ENV__"

if defined _OLD_VIRTUAL_PROMPT (
set "PROMPT=%_OLD_VIRTUAL_PROMPT%"
) else (
if not defined PROMPT (
set "PROMPT=$P$G"
)
if not defined VIRTUAL_ENV_DISABLE_PROMPT (
set "_OLD_VIRTUAL_PROMPT=%PROMPT%"
)
)
if not defined VIRTUAL_ENV_DISABLE_PROMPT (
set "PROMPT=__VIRTUAL_PROMPT__%PROMPT%"
)

REM Don't use () to avoid problems with them in %PATH%
if defined _OLD_VIRTUAL_PYTHONHOME goto ENDIFVHOME
set "_OLD_VIRTUAL_PYTHONHOME=%PYTHONHOME%"
:ENDIFVHOME

set PYTHONHOME=

REM if defined _OLD_VIRTUAL_PATH (
if not defined _OLD_VIRTUAL_PATH goto ENDIFVPATH1
set "PATH=%_OLD_VIRTUAL_PATH%"
:ENDIFVPATH1
REM ) else (
if defined _OLD_VIRTUAL_PATH goto ENDIFVPATH2
set "_OLD_VIRTUAL_PATH=%PATH%"
:ENDIFVPATH2

set "PATH=%VIRTUAL_ENV%\__BIN_NAME__;%PATH%"
19 changes: 19 additions & 0 deletions src/virtualenv/activation/batch/deactivate.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@echo off

set VIRTUAL_ENV=

REM Don't use () to avoid problems with them in %PATH%
if not defined _OLD_VIRTUAL_PROMPT goto ENDIFVPROMPT
set "PROMPT=%_OLD_VIRTUAL_PROMPT%"
set _OLD_VIRTUAL_PROMPT=
:ENDIFVPROMPT

if not defined _OLD_VIRTUAL_PYTHONHOME goto ENDIFVHOME
set "PYTHONHOME=%_OLD_VIRTUAL_PYTHONHOME%"
set _OLD_VIRTUAL_PYTHONHOME=
:ENDIFVHOME

if not defined _OLD_VIRTUAL_PATH goto ENDIFVPATH
set "PATH=%_OLD_VIRTUAL_PATH%"
set _OLD_VIRTUAL_PATH=
:ENDIFVPATH
1 change: 1 addition & 0 deletions src/virtualenv/activation/batch/pydoc.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python.exe -m pydoc %*
14 changes: 14 additions & 0 deletions src/virtualenv/activation/cshell/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from __future__ import absolute_import, unicode_literals

from pathlib2 import Path

from ..via_template import ViaTemplateActivator


class CShellActivator(ViaTemplateActivator):
@classmethod
def supports(cls, interpreter):
return interpreter.os != "nt"

def templates(self):
yield Path("activate.csh")
55 changes: 55 additions & 0 deletions src/virtualenv/activation/cshell/activate.csh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# This file must be used with "source bin/activate.csh" *from csh*.
# You cannot run it directly.
# Created by Davide Di Blasi <[email protected]>.

set newline='\
'

alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH:q" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT:q" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; test "\!:*" != "nondestructive" && unalias deactivate && unalias pydoc'

# Unset irrelevant variables.
deactivate nondestructive

setenv VIRTUAL_ENV "__VIRTUAL_ENV__"

set _OLD_VIRTUAL_PATH="$PATH:q"
setenv PATH "$VIRTUAL_ENV:q/__BIN_NAME__:$PATH:q"



if ("__VIRTUAL_PROMPT__" != "") then
set env_name = "__VIRTUAL_PROMPT__"
else
set env_name = '('"$VIRTUAL_ENV:t:q"') '
endif

if ( $?VIRTUAL_ENV_DISABLE_PROMPT ) then
if ( $VIRTUAL_ENV_DISABLE_PROMPT == "" ) then
set do_prompt = "1"
else
set do_prompt = "0"
endif
else
set do_prompt = "1"
endif

if ( $do_prompt == "1" ) then
# Could be in a non-interactive environment,
# in which case, $prompt is undefined and we wouldn't
# care about the prompt anyway.
if ( $?prompt ) then
set _OLD_VIRTUAL_PROMPT="$prompt:q"
if ( "$prompt:q" =~ *"$newline:q"* ) then
:
else
set prompt = "$env_name:q$prompt:q"
endif
endif
endif

unset env_name
unset do_prompt

alias pydoc python -m pydoc

rehash
14 changes: 14 additions & 0 deletions src/virtualenv/activation/fish/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from __future__ import absolute_import, unicode_literals

from pathlib2 import Path

from ..via_template import ViaTemplateActivator


class FishActivator(ViaTemplateActivator):
def templates(self):
yield Path("activate.fish")

@classmethod
def supports(cls, interpreter):
return interpreter.os != "nt"
Loading