Skip to content

Commit f09158f

Browse files
authored
Merge pull request #26 from xavierabellan/feature/activate-deactivate
Proposal to add tykky shell convenience function to activate and deactivate environments
2 parents ac139a6 + 423b0f0 commit f09158f

File tree

8 files changed

+243
-4
lines changed

8 files changed

+243
-4
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,25 @@ How verbosely to report program actions
241241
- 2 general (default)
242242
- `>2` debug
243243
244+
## Activating and Deactivating wrapped environments
245+
A `tykky` convenience shell function is provided to mimic the conda activate and
246+
deactivate helpers. To use it, you must load those functions into your shell with:
247+
248+
`source etc/profile.d/tykky`
249+
250+
Then you may activate a tykky installation with
251+
252+
`tykky activate <env_dir>`
253+
254+
If you define the environment variable `TYKKY_PATH`, environments may also be
255+
found by name inside that colon-separated list of paths in `TYKKY_PATH`.
256+
257+
You may deactivate the tykky environment in your shell with:
258+
259+
`tykky deactivate`
260+
261+
Alternatively, you may also manually add `<env_dir>/bin` to your `$PATH`.
262+
244263
245264
## Misc features ideas
246265
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
__tykky_completions() {
2+
if [ "${#COMP_WORDS[@]}" == "2" ]; then
3+
COMPREPLY=($(compgen -W "activate deactivate" "${COMP_WORDS[1]}"))
4+
return
5+
fi
6+
if [[ ${#COMP_WORDS[@]} -gt 2 ]]; then
7+
if [[ "${COMP_WORDS[1]}" == "activate" && "${COMP_WORDS[2]}" != *"/"* ]]; then
8+
local __tykky_env_list=""
9+
local __tykky_path __candidate
10+
local oldIFS=$IFS
11+
IFS=:
12+
for __tykky_path in ${TYKKY_PATH:-~/.tykky}; do
13+
IFS=$oldIFS
14+
for __candidate in $__tykky_path/*; do
15+
if [[ -d "$__candidate" && -f "$__candidate/common.sh" && -d "$__candidate/bin" ]]; then
16+
__tykky_env_list+="$(basename $__candidate) "
17+
fi
18+
done
19+
done
20+
COMPREPLY=($(compgen -W "$__tykky_env_list" "${COMP_WORDS[2]}"))
21+
return
22+
elif [[ "${COMP_WORDS[1]}" == "activate" && "${COMP_WORDS[2]}" == *"/"* ]]; then
23+
COMPREPLY=( $(compgen -d -- "${COMP_WORDS[2]}") )
24+
local i
25+
for i in "${!COMPREPLY[@]}"; do
26+
if [[ -d "${COMPREPLY[$i]}" && ! -f "${COMPREPLY[$i]}/common.sh" && ! -d "${COMPREPLY[$i]}/bin" ]]; then
27+
COMPREPLY[$i]="${COMPREPLY[$i]}/"
28+
fi
29+
done
30+
compopt -o nospace
31+
fi
32+
fi
33+
return
34+
}
35+
36+
complete -F __tykky_completions tykky

etc/profile.d/tykky.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Tykky shell functions to activate/deactivate environments
2+
3+
__tykky_dir=""
4+
if [ -n "${BASH_SOURCE:-}" ]; then
5+
__tykky_dir="$(dirname "${BASH_SOURCE[0]}")"
6+
elif [ -n "${ZSH_VERSION:-}" ]; then
7+
__tykky_dir="$(dirname "${(%):-%N}")"
8+
elif [ -n "${KSH_VERSION:-}" ]; then
9+
__tykky_dir="$(dirname "${.sh.file}")"
10+
else
11+
# Generic POSIX shell case or dash
12+
__tykky_dir="$(dirname "$0")"
13+
fi
14+
15+
__tykky_dir="$(realpath $(dirname $(dirname "$__tykky_dir")))"
16+
17+
# Add the tykky tools to PATH if not present
18+
if [ "${PATH#*$__tykky_dir/bin:}" = "$PATH" ]; then
19+
export PATH="$__tykky_dir/bin:$PATH"
20+
fi
21+
22+
# Make available tykky functions for this shell and subshells
23+
for __function in $__tykky_dir/share/sh_functions/*; do
24+
source $__function
25+
if [ -z "$KSH_VERSION" ]; then
26+
export -f $(basename $__function)
27+
fi
28+
done
29+
unset __function
30+
31+
# KSH does not support exporting functions
32+
if [ "${FPATH#*$__tykky_dir/share/sh_functions:}" = "$FPATH" ]; then
33+
export FPATH="$__tykky_dir/share/sh_functions:$FPATH"
34+
fi
35+
36+
# Enable BASH autocompletion
37+
if [ -n "${BASH_VERSIONi:-}" ] && [ -n "${PS1:-}" ]; then
38+
__tykky_bash_completion_file="$_tykky_dir/etc/bash_completion.d/tykky_completion"
39+
[ -f "$__tykky_bash_completion_file" ] && . "$__tykky_bash_completion_file"
40+
unset __tykky_bash_completion_file
41+
fi
42+
unset __tykky_dir

frontends/containerize

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,27 @@ fi
124124
$M_SCRIPT_DIR/../post.sh || { print_err "Failed to move to install dir"; false ; }
125125
test -f "$_usr_yaml" && rm "$_usr_yaml"
126126
end=`date +%s`
127-
print_info "Done, duration: $((end-start))s" 1
128-
print_info "Program has been installed to $CW_INSTALLATION_PREFIX
129-
\tTo use add the bin folder to your path e.g:
130-
\texport PATH=\"$_inst_path/bin:\$PATH\"" 1
131127

128+
129+
if command -v tykky &>/dev/null ; then
130+
_env_root=$(realpath $(dirname $_inst_path))
131+
_env_name=$CW_INSTALLATION_PREFIX
132+
IFS=':' read -r -a _paths <<< "${TYKKY_PATH:-$HOME/.tykky}"
133+
for _path in "${_paths[@]}"; do
134+
if [ "$_env_root" = "$(realpath $_path)" ]; then
135+
_env_name=$(basename $CW_INSTALLATION_PREFIX)
136+
break
137+
fi
138+
done
139+
info_msg="Environment has been installed to $CW_INSTALLATION_PREFIX
140+
\tTo use, activate with:
141+
\ttykky activate $_env_name
142+
\tAlternatively, add the bin folder to your path e.g:
143+
\texport PATH=\"$_inst_path/bin:\$PATH\""
144+
else
145+
info_msg="Program has been installed to $CW_INSTALLATION_PREFIX
146+
\tTo use add the bin folder to your path e.g:
147+
\texport PATH=\"$_inst_path/bin:\$PATH\""
148+
fi
149+
print_info "Done, duration: $((end-start))s" 1
150+
print_info "$info_msg" 1

share/sh_functions/__tykky_activate

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Tykky shell functions to activate/deactivate environments
2+
__tykky_activate() {
3+
# Activate a Tykky environment. Change PS1 if interactive by default,
4+
# unless TYKKY_CHANGE_PS1 is set to 0
5+
6+
if [ -z "${2:-}" ]; then
7+
echo "ERROR: You must specify a valid tykky environment to activate" >&2
8+
false
9+
return
10+
fi
11+
__candidate=$(__tykky_get_env_path $2)
12+
if [ "$?" -ne 0 ]; then
13+
false
14+
return
15+
fi
16+
if [ -n "$__candidate" ]; then
17+
__tykky_deactivate
18+
export TYKKY_PREFIX="$(realpath $__candidate)"
19+
export PATH=$TYKKY_PREFIX/bin:$PATH
20+
if [ -n "${PS1:-}" ] && [ "${TYKKY_CHANGE_PS1:-}" != "0" ]; then
21+
PS1="($(basename $(echo $TYKKY_PREFIX))) $PS1"
22+
fi
23+
fi
24+
unset __candidate
25+
}

share/sh_functions/__tykky_deactivate

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Tykky shell functions to activate/deactivate environments
2+
__tykky_deactivate() {
3+
# Deactivate a Tykky environment. Change PS1 if interactive by default,
4+
5+
if [ -n "${TYKKY_PREFIX:-}" ]; then
6+
export PATH=$(echo $PATH | sed -e "s|$TYKKY_PREFIX/bin:||g")
7+
if [ -n "${PS1:-}" ]; then
8+
PS1="$(echo "$PS1" | sed -e "s|^($(basename $TYKKY_PREFIX)) ||")"
9+
fi
10+
unset TYKKY_PREFIX
11+
fi
12+
}
13+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Tykky shell functions to activate/deactivate environments
2+
__tykky_get_env_path() {
3+
# Get and validate installation path of a tykky environment.
4+
# If a name is passed as an argument, try to find it in colon-separated
5+
# list TYKKY_PATH
6+
7+
__candidate=""
8+
case "$1" in
9+
*/*)
10+
__candidate="${1%/}"
11+
;;
12+
*)
13+
oldIFS=$IFS
14+
IFS=:
15+
for __tykky_path in ${TYKKY_PATH:-""}; do
16+
IFS=$oldIFS
17+
if [ -d "$__tykky_path/$1" ]; then
18+
if [ -n "$__candidate" ]; then
19+
echo "WARNING: Additional candidate tykky environments found in TYKKY_PATH for $1, only the first one will be considered" >&2
20+
echo -e "\tfirst candidate: $__candidate" >&2
21+
echo -e "\tnew candidate: $__tykky_path/$1" >&2
22+
else
23+
__candidate="${__tykky_path%/}/${1%/}"
24+
fi
25+
#break
26+
fi
27+
done
28+
if [ -d "$1" ];then
29+
# If there is a matching foldername in the current directory
30+
# and the name does not match anyhing in tykky_path, also test that for convinience
31+
# this is to be consistent with that the first case allows relative paths
32+
# so then relative paths in the current directory withouth / should also work
33+
if [ -z "$__candidate" ];then
34+
__candidate=$PWD/$1
35+
36+
# Ambiguous reference as we might have multiple matches
37+
else
38+
_tp_candidate=$(readlink -f $__candidate)
39+
_d_candidate=$(readlink -f $1)
40+
# if same folder let's not emit a warning
41+
# following pseudo standard -> command line arguments override environment settings
42+
if [ ! "$_tp_candidate" = "$_d_candidate" ];then
43+
echo "WARNING: Multiple candidate tykky environments for $1, activating the one in the current directory" >&2
44+
echo -e "\tfrom TYKKY_PATH: $_tp_candidate" >&2
45+
echo -e "\tfrom current directory: $_d_candidate" >&2
46+
fi
47+
48+
fi
49+
fi
50+
51+
;;
52+
esac
53+
54+
# Validation of genunine tykky installation
55+
if [ -f "$__candidate/common.sh" ] && [ -d "$__candidate/bin" ]; then
56+
echo "$__candidate"
57+
unset __candidate __tykky_path
58+
else
59+
unset __candidate __tykky_path
60+
echo "ERROR: $1 is not a valid tykky environment" >&2
61+
if [ ! -d "$1" ] ; then
62+
echo -e "\t$1 does not exists or parent folder is not in TYKKY_PATH" >&2
63+
echo -e "\tCurrent value: TYKKY_PATH=$TYKKY_PATH" >&2
64+
fi
65+
false
66+
fi
67+
68+
}

share/sh_functions/tykky

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Tykky shell functions to activate/deactivate environments
2+
tykky() {
3+
# Top level shell function to activate and deactivate Tykky environments
4+
5+
case "${1:-}" in
6+
activate)
7+
__tykky_activate "$@"
8+
;;
9+
deactivate)
10+
__tykky_deactivate "$@"
11+
;;
12+
*)
13+
echo "Usage: tykky activate <env_name_or_dir>"
14+
echo " tykky deactivate"
15+
;;
16+
esac
17+
}

0 commit comments

Comments
 (0)