Skip to content

Commit

Permalink
cleanup and simplification
Browse files Browse the repository at this point in the history
reworked to try and make compatible with fish, attempt unsuccessful

code has instead received a refresh and cleanup and appears to do
better in corner cases of the supported shells

turned off automated tests for the default shell simply because it
turned out to be much too complicated to predict the types of corner
cases that needs to be address given the indirect way the unittest are
forced to test
  • Loading branch information
Odegard, Ken (Flamingo Technologies) committed Aug 26, 2016
1 parent 58d407d commit 6bee5d1
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 178 deletions.
1 change: 1 addition & 0 deletions conda.recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ build:
- bin/deactivate [unix]
- bin/deactivate.sh [unix]
- bin/deactivate.csh [unix]
- bin/whichshell_args.bash [unix]
- bin/whichshell.awk [unix]
- bin/envvar_cleanup.awk [unix]
- Scripts/activate.bat [win]
Expand Down
18 changes: 11 additions & 7 deletions conda/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,16 @@ def human_bytes(n):
pathsep=":",
printdefaultenv='echo "${CONDA_DEFAULT_ENV}"',
printpath='echo "${PATH}"',
printps1='echo "${PS1}"',
printprompt='echo "${PS1}"',
setprompt='export PS1="{value}"',
unsetprompt='unset PS1',
promptvar='PS1',
sep="/",
setenvvar='export {variable}="{value}"',
unsetenvvar='unset {variable}',
setvar='{variable}="{value}"',
unsetvar='unset {variable}',
shell_args=["-l", "-c"],
shell_args="-l",
shell_suffix="",
slash_convert=("\\", "/"),
source=".",
Expand All @@ -221,7 +223,7 @@ def human_bytes(n):
# setenvvar='export {variable}="{value}"',
# shell_suffix=".ps",
# env_script_suffix=".ps",
# printps1='echo "${PS1}"',
# printprompt='echo "${PS1}"',
# printdefaultenv='echo $CONDA_DEFAULT_ENV',
# printpath="echo %PATH%",
# exe="powershell.exe",
Expand All @@ -242,15 +244,15 @@ def human_bytes(n):
unsetenvvar='set {variable}=',
shell_suffix=".bat",
env_script_suffix=".bat",
printps1="@echo %PROMPT%",
printprompt="@echo %PROMPT%",
promptvar="PROMPT",
# parens mismatched intentionally. See http://stackoverflow.com/questions/20691060/how-do-i-echo-a-blank-empty-line-to-the-console-from-a-windows-batch-file # NOQA
printdefaultenv='IF NOT "%CONDA_DEFAULT_ENV%" == "" (\n'
'echo %CONDA_DEFAULT_ENV% ) ELSE (\n'
'echo()',
printpath="@echo %PATH%",
exe="cmd.exe",
shell_args=["/d", "/c"],
shell_args="/d /c",
path_from=path_identity,
path_to=path_identity,
slash_convert=("/", "\\"),
Expand Down Expand Up @@ -289,13 +291,15 @@ def human_bytes(n):
unix_shell_base,
env_script_suffix=".csh",
nul='>&/dev/null',
printps1='echo "${prompt}"',
printprompt='echo "${prompt}"',
promptvar='prompt',
setprompt='set prompt="{value}"',
unsetprompt='unset prompt',
setenvvar='setenv {variable} "{value}"',
unsetenvvar='unsetenv {variable}',
setvar='set {variable}="{value}"',
unsetvar='unset {variable}',
shell_args=["-c"],
shell_args="",
source="source",

)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
'shell/deactivate',
'shell/deactivate.sh',
'shell/deactivate.csh',
'shell/whichshell_args.bash',
'shell/whichshell.awk',
'shell/envvar_cleanup.sh',
]
Expand Down
24 changes: 3 additions & 21 deletions shell/activate
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,10 @@
# > ps:display.c:66: please report this bug

# error clause
echo `ps -p $$ | sed 's|.*csh|THIS-IS-CSH|' | grep CSH || eval echo '$0'` \
"`ps -p $$ | tr '\n' ' '`" \
"`test -x /usr/bin/lsb_release && lsb_release -si | sed 's| ||' || uname -s`" \
"$SHLVL" \
"activate" \
"error-clause" | awk -f "`which whichshell.awk`" &&\
bash -c "echo '[ACTIVATE]: ERROR: Only supports sourcing from tcsh/csh and bash/zsh/dash/posh.' 1>&2"
whichshell_args.bash "activate" "error-clause" | awk -f "`which whichshell.awk`"

# bourne shell clause (bash/zsh/dash/posh)
echo `ps -p $$ | sed 's|.*csh|THIS-IS-CSH|' | grep CSH || eval echo '$0'` \
"`ps -p $$ | tr '\n' ' '`" \
"`test -x /usr/bin/lsb_release && lsb_release -si | sed 's| ||' || uname -s`" \
"$SHLVL" \
"activate" \
"bourne-shell-clause" | awk -f "`which whichshell.awk`" &&\
. "`which activate.sh`" && return
whichshell_args.bash "activate" "bourne-shell-clause" | awk -f "`which whichshell.awk`" && . activate.sh

# c-shell clause (tcsh/csh)
echo `ps -p $$ | sed 's|.*csh|THIS-IS-CSH|' | grep CSH || eval echo '$0'` \
"`ps -p $$ | tr '\n' ' '`" \
"`test -x /usr/bin/lsb_release && lsb_release -si | sed 's| ||' || uname -s`" \
"$SHLVL" \
"activate" \
"c-shell-clause" | awk -f "`which whichshell.awk`" &&\
source "`which activate.csh`" && exit $status
whichshell_args.bash "activate" "c-shell-clause" | awk -f "`which whichshell.awk`" && source "`which activate.csh`"
53 changes: 40 additions & 13 deletions shell/activate.csh
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ endsw
# inherit whatever the user set
# this is important for dash where you cannot pass parameters to sourced scripts
# since this script is exclusively for csh/tcsh this is just for consistency/a bonus feature
if ( ! $?CONDA_HELP ) set CONDA_HELP=false
if ( ! $?CONDA_HELP ) then
set CONDA_HELP=false
endif
set UNKNOWN=""
if ( ! $?CONDA_VERBOSE ) set CONDA_VERBOSE=false
if ( ! $?CONDA_ENVNAME ) set CONDA_ENVNAME=""
if ( ! $?CONDA_VERBOSE ) then
set CONDA_VERBOSE=false
endif
if ( ! $?CONDA_ENVNAME ) then
set CONDA_ENVNAME=""
endif

###############################################################################
# parse command line, perform command line error checking
Expand All @@ -37,7 +43,9 @@ while ( $num != -1 )
@ num = ($num + 1)
set arg=`eval eval echo '\${$num}'`

if ( -z `echo "${arg}" | sed 's| ||g'` ) then
# use == "" instead of -z test for robust support across
# different platforms (especially Ubuntu)
if ( $status != 0 || "`echo ${arg} | sed 's| ||g'`" == "" ) then
set num=-1
else
switch ( "${arg}" )
Expand All @@ -54,7 +62,9 @@ while ( $num != -1 )
set CONDA_ENVNAME="${arg}"
set is_envname_set=true
else
if ( -z "${UNKNOWN}" ) then
# use == "" instead of -z test for robust support across
# different platforms (especially Ubuntu)
if ( "${UNKNOWN}" == "" ) then
set UNKNOWN="${arg}"
else
set UNKNOWN="${UNKNOWN} ${arg}"
Expand All @@ -70,16 +80,27 @@ unset arg
unset is_envname_set

# if any of these variables are undefined (i.e. unbounded) set them to a default
if ( -z `echo "${CONDA_HELP}" | sed 's| ||g'` ) set CONDA_HELP=false
if ( -z `echo "${CONDA_VERBOSE}" | sed 's| ||g'` ) set CONDA_VERBOSE=false
if ( -z `echo "${CONDA_ENVNAME}" | sed 's| ||g'` ) set CONDA_ENVNAME="root"
#
# use == "" instead of -z test for robust support across
# different platforms (especially Ubuntu)
if ( "`echo ${CONDA_HELP} | sed 's| ||g'`" == "" ) then
set CONDA_HELP=false
endif
if ( "`echo ${CONDA_VERBOSE} | sed 's| ||g'`" == "" ) then
set CONDA_VERBOSE=false
endif
if ( "`echo ${CONDA_ENVNAME} | sed 's| ||g'`" == "" ) then
set CONDA_ENVNAME="root"
endif

######################################################################
# help dialog
######################################################################
if ( "${CONDA_HELP}" == true ) then
if ( -n "${UNKNOWN}" ) then
sh -c "echo '[ACTIVATE]: ERROR: Unknown/Invalid flag/parameter (${UNKNOWN})' 1>&2"
# use != "" instead of -n test for robust support across
# different platforms (especially Ubuntu)
if ( "${UNKNOWN}" != "" ) then
bash -c "echo '[ACTIVATE]: ERROR: Unknown/Invalid flag/parameter (${UNKNOWN})' 1>&2"
endif
conda ..activate ${_SHELL}${EXT} -h

Expand All @@ -88,7 +109,9 @@ if ( "${CONDA_HELP}" == true ) then
unset CONDA_ENVNAME
unset CONDA_HELP
unset CONDA_VERBOSE
if ( -n "${UNKNOWN}" ) then
# use != "" instead of -n test for robust support across
# different platforms (especially Ubuntu)
if ( "${UNKNOWN}" != "" ) then
unset UNKNOWN
exit 1
else
Expand Down Expand Up @@ -130,7 +153,9 @@ set _CONDA_BIN=`conda ..activate ${_CONDA_BIN} "${CONDA_ENVNAME}" | sed 's| |\ |
if ( $status == 0 ) then
# CONDA_PS1_BACKUP
# export these to restore upon deactivation
if ( $?prompt ) setenv CONDA_PS1_BACKUP "${prompt}"
if ( $?prompt ) then
setenv CONDA_PS1_BACKUP "${prompt}"
endif

# PATH
# update path with the new conda environment
Expand Down Expand Up @@ -183,7 +208,9 @@ if ( $status == 0 ) then
set _CONDA_DIR="${CONDA_PREFIX}/etc/conda/activate.d"
if ( -d "${_CONDA_DIR}" ) then
foreach f ( `ls "${_CONDA_DIR}" | grep \\.csh$` )
if ( "${CONDA_VERBOSE}" == true ) echo "[ACTIVATE]: Sourcing ${_CONDA_DIR}/${f}."
if ( "${CONDA_VERBOSE}" == true ) then
echo "[ACTIVATE]: Sourcing ${_CONDA_DIR}/${f}."
endif
source "${_CONDA_DIR}/${f}"
end
endif
Expand Down
24 changes: 3 additions & 21 deletions shell/deactivate
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,10 @@
# > ps:display.c:66: please report this bug

# error clause
echo `ps -p $$ | sed 's|.*csh|THIS-IS-CSH|' | grep CSH || eval echo '$0'` \
"`ps -p $$ | tr '\n' ' '`" \
"`test -x /usr/bin/lsb_release && lsb_release -si | sed 's| ||' || uname -s`" \
"$SHLVL" \
"deactivate" \
"error-clause" | awk -f "`which whichshell.awk`" &&\
bash -c "echo '[DEACTIVATE]: ERROR: Only supports sourcing from tcsh/csh and bash/zsh/dash/posh.' 1>&2"
whichshell_args.bash "deactivate" "error-clause" | awk -f "`which whichshell.awk`"

# bourne shell clause (bash/zsh/dash/posh)
echo `ps -p $$ | sed 's|.*csh|THIS-IS-CSH|' | grep CSH || eval echo '$0'` \
"`ps -p $$ | tr '\n' ' '`" \
"`test -x /usr/bin/lsb_release && lsb_release -si | sed 's| ||' || uname -s`" \
"$SHLVL" \
"deactivate" \
"bourne-shell-clause" | awk -f "`which whichshell.awk`" &&\
. "`which deactivate.sh`" && return
whichshell_args.bash "deactivate" "bourne-shell-clause" | awk -f "`which whichshell.awk`" && . deactivate.sh

# c-shell clause (tcsh/csh)
echo `ps -p $$ | sed 's|.*csh|THIS-IS-CSH|' | grep CSH || eval echo '$0'` \
"`ps -p $$ | tr '\n' ' '`" \
"`test -x /usr/bin/lsb_release && lsb_release -si | sed 's| ||' || uname -s`" \
"$SHLVL" \
"deactivate" \
"c-shell-clause" | awk -f "`which whichshell.awk`" &&\
source "`which deactivate.csh`" && exit $status
whichshell_args.bash "deactivate" "c-shell-clause" | awk -f "`which whichshell.awk`" && source "`which deactivate.csh`"
41 changes: 33 additions & 8 deletions shell/deactivate.csh
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ endsw
# inherit whatever the user set
# this is important for dash where you cannot pass parameters to sourced scripts
# since this script is exclusively for csh/tcsh this is just for consistency/a bonus feature
if ( ! $?CONDA_HELP ) set CONDA_HELP=false
if ( ! $?CONDA_HELP ) then
set CONDA_HELP=false
endif
set UNKNOWN=""
if ( ! $?CONDA_VERBOSE ) set CONDA_VERBOSE=false
if ( ! $?CONDA_VERBOSE ) then
set CONDA_VERBOSE=false
endif

###############################################################################
# parse command line, perform command line error checking
Expand All @@ -35,7 +39,9 @@ while ( $num != -1 )
@ num = ($num + 1)
set arg=`eval eval echo '\$$num'`

if ( `echo "${arg}" | sed 's| ||g'` == "" ) then
# use == "" instead of -z test for robust support across
# different platforms (especially Ubuntu)
if ( $status != 0 || "`echo ${arg} | sed 's| ||g'`" == "" ) then
set num=-1
else
switch ( "${arg}" )
Expand All @@ -48,6 +54,8 @@ while ( $num != -1 )
set CONDA_VERBOSE=true
breaksw
default:
# use == "" instead of -z test for robust support across
# different platforms (especially Ubuntu)
if ( "${UNKNOWN}" == "" ) then
set UNKNOWN="${arg}"
else
Expand All @@ -62,22 +70,33 @@ unset num
unset arg

# if any of these variables are undefined (i.e. unbounded) set them to a default
if ( `echo "${CONDA_HELP}" | sed 's| ||g'` == "" ) set CONDA_HELP=false
if ( `echo "${CONDA_VERBOSE}" | sed 's| ||g'` == "" ) set CONDA_VERBOSE=false
#
# use == "" instead of -z test for robust support across
# different platforms (especially Ubuntu)
if ( "`echo ${CONDA_HELP} | sed 's| ||g'`" == "" ) then
set CONDA_HELP=false
endif
if ( "`echo ${CONDA_VERBOSE} | sed 's| ||g'`" == "" ) then
set CONDA_VERBOSE=false
endif

######################################################################
# help dialog
######################################################################
if ( "${CONDA_HELP}" == true ) then
# use != "" instead of -n test for robust support across
# different platforms (especially Ubuntu)
if ( "${UNKNOWN}" != "" ) then
sh -c "echo '[DEACTIVATE]: ERROR: Unknown/Invalid flag/parameter (${UNKNOWN})' 1>&2"
bash -c "echo '[DEACTIVATE]: ERROR: Unknown/Invalid flag/parameter (${UNKNOWN})' 1>&2"
endif
conda ..deactivate ${_SHELL}${EXT} -h

unset _SHELL
unset EXT
unset CONDA_HELP
unset CONDA_VERBOSE
# use != "" instead of -n test for robust support across
# different platforms (especially Ubuntu)
if ( "${UNKNOWN}" != "" ) then
unset UNKNOWN
exit 1
Expand All @@ -96,20 +115,26 @@ unset UNKNOWN
# accordingly
######################################################################
if ( $?CONDA_DEFAULT_ENV ) then
# use != "" instead of -n test for robust support across
# different platforms (especially Ubuntu)
if ( "${CONDA_DEFAULT_ENV}" != "" ) then
# unload post-activate scripts
# scripts found in $CONDA_PREFIX/etc/conda/deactivate.d
set _CONDA_DIR="${CONDA_PREFIX}/etc/conda/deactivate.d"
if ( -d "${_CONDA_DIR}" ) then
foreach f ( `ls "${_CONDA_DIR}" | grep \\.csh$` )
if ( "${CONDA_VERBOSE}" == true ) echo "[DEACTIVATE]: Sourcing ${_CONDA_DIR}/${f}."
if ( "${CONDA_VERBOSE}" == true ) then
echo "[DEACTIVATE]: Sourcing ${_CONDA_DIR}/${f}."
endif
source "${_CONDA_DIR}/${f}"
end
endif
unset _CONDA_DIR

# restore PROMPT
if ( $?prompt ) set prompt="${CONDA_PS1_BACKUP}"
if ( $?prompt ) then
set prompt="${CONDA_PS1_BACKUP}"
endif

# remove CONDA_DEFAULT_ENV
unsetenv CONDA_DEFAULT_ENV
Expand Down
Loading

0 comments on commit 6bee5d1

Please sign in to comment.