Skip to content

Commit

Permalink
add activation
Browse files Browse the repository at this point in the history
Signed-off-by: Bernat Gabor <[email protected]>
  • Loading branch information
Siddhant Kumar authored and gaborbernat committed Nov 28, 2019
1 parent 7696124 commit 81aedfe
Show file tree
Hide file tree
Showing 22 changed files with 768 additions and 6 deletions.
7 changes: 7 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,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
dos = virtualenv.activation.dos:DOSActivator
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 .cshell import CShellActivator
from .dos import DOSActivator
from .fish import FishActivator
from .powershell import PowerShellActivator
from .python import PythonActivator
from .xonosh import XonoshActivator

__all__ = [
BashActivator,
PowerShellActivator,
XonoshActivator,
CShellActivator,
PythonActivator,
DOSActivator,
FishActivator,
]
32 changes: 26 additions & 6 deletions src/virtualenv/activation/activator.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
from __future__ import absolute_import, unicode_literals

from abc import ABCMeta, abstractmethod
import os
import pkgutil
from abc import ABCMeta

import six


@six.add_metaclass(ABCMeta)
class Activator(object):
def __init__(self, options):
self.flag_prompt = options.prompt
self.flag_prompt = options.prompt if options.prompt is not None else ""

@classmethod
def add_parser_arguments(cls, parser):
pass

def run(self, creator):
self.generate()
self.generate(creator)
if self.flag_prompt is not None:
creator.pyenv_cfg["prompt"] = self.flag_prompt

@abstractmethod
def generate(self):
raise NotImplementedError
@classmethod
def supports(cls, interpreter):
return True

def mappings(self, creator):
return {
"__VIRTUAL_PROMPT__": self.flag_prompt,
"__VIRTUAL_WINPROMPT__": self.flag_prompt,
"__VIRTUAL_ENV__": str(creator.env_dir),
"__VIRTUAL_NAME__": str(creator.env_name),
"__BIN_NAME__": str(creator.bin_name),
"__PATH_SEP__": os.pathsep,
}

def generate(self, creator):
mappings = self.mappings(creator)
for resource in map(str, self.files):
text = pkgutil.get_data(self.__module__, resource).decode("utf-8")
for start, end in mappings.items():
text = text.replace(start, end)
(creator.bin_dir / resource).write_text(text)
11 changes: 11 additions & 0 deletions src/virtualenv/activation/bash/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from __future__ import absolute_import, unicode_literals

from pathlib2 import Path

from ..activator import Activator
from ..utils import RequiresPosixMixin


class BashActivator(RequiresPosixMixin, Activator):

files = [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
11 changes: 11 additions & 0 deletions src/virtualenv/activation/cshell/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from __future__ import absolute_import, unicode_literals

from pathlib2 import Path

from ..activator import Activator
from ..utils import RequiresPosixMixin


class CShellActivator(RequiresPosixMixin, Activator):

files = [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
16 changes: 16 additions & 0 deletions src/virtualenv/activation/dos/__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 ..activator import Activator


class DOSActivator(Activator):

files = [Path("activate.bat"), Path("deactivate.bat")]

@classmethod
def supports(cls, interpreter):
if interpreter.os == "nt":
return True
return False
35 changes: 35 additions & 0 deletions src/virtualenv/activation/dos/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_WINPROMPT__%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/dos/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
11 changes: 11 additions & 0 deletions src/virtualenv/activation/fish/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from __future__ import absolute_import, unicode_literals

from pathlib2 import Path

from ..activator import Activator
from ..utils import RequiresPosixMixin


class FishActivator(RequiresPosixMixin, Activator):

files = [Path("activate.fish")]
Loading

0 comments on commit 81aedfe

Please sign in to comment.