-
Notifications
You must be signed in to change notification settings - Fork 0
/
conda_with_ros.bash
161 lines (140 loc) · 4.65 KB
/
conda_with_ros.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
## SOURCE:
## https://gist.github.com/StefanFabian/17fa715e783cd2be6a32cd5bbb98acd9#file-anaconda_with_ros_wrapper-bash
### This script wraps all executables in the anaconda bin folder so that they can be used without adding Anaconda
### to the path which would break some functionality of ROS (Robot Operating System)
###
### The commands e.g. jupyter notebook will cause the script to add anaconda to the path, start jupyter notebook
### and after jupyter notebook terminated remove anaconda from the path again
###
### Notable commands:
### * release-the-snake Adds conda to the path and removes all aliases defined by this script
### Conda will stay in the PATH until the end of the session (terminal is closed) or
### until "cage-the-snake" is called
### * cage-the-snake Removes conda from the path and redefines all aliases for anaconda executables
### * source activate [env] Works just like with anaconda in the PATH, will activate the environment or (root) and
### Anaconda will stay in the PATH for the remaining session or until "source deactivate" is run
### * source deactivate Deactivates the environment and if Anaconda wasn't released manually using release-the-snake
### Anaconda will be removed from the PATH again.
if [ -z ${_ROS_CONDA_ADDED+x} ]
then
_ROS_CONDA_PATH=~/anaconda3/bin
_ROS_CONDA_ADDED=0
_ROS_CONDA_ALIASES=''
_ROS_CONDA_RELEASED_MANUALLY=0
_ROS_CONDA_PYTHONPATH_WITHOUT_ROS=$PYTHONPATH
_PS1=$PS1
fi
function _ROS_CONDA_addAliases {
if [[ $_ROS_CONDA_ALIASES != '' ]]
then
echo "ROS Conda Wrapper: Error! Aliases already defined!"
_ROS_CONDA_removeAliases
fi
for file in $_ROS_CONDA_PATH/*
do
local name
name=${file##*/}
if ! [ -x "$(command -v $name)" ]
then
alias $name='_ROS_CONDA_runWithConda '$name' $@'
_ROS_CONDA_ALIASES=$_ROS_CONDA_ALIASES" "$name
fi
done
}
function _ROS_CONDA_removeAliases {
for cmd in $_ROS_CONDA_ALIASES
do
unalias $cmd
done
_ROS_CONDA_ALIASES=''
}
function _ROS_CONDA_runWithConda {
_ROS_CONDA_ensureCondaInPath
command $@
_ROS_CONDA_removeCondaFromPath
}
function _ROS_CONDA_ensureCondaInPath {
if [ $_ROS_CONDA_ADDED -eq 1 ]
then
return 1 # false
fi
_ROS_CONDA_ADDED=1
# Check that the path doesn't start, end or contain the ros conda path
if [[ $PATH != $_ROS_CONDA_PATH":"* && $PATH != *":"$_ROS_CONDA_PATH && $PATH != *":"$_ROS_CONDA_PATH":"* ]]
then
export PATH=$_ROS_CONDA_PATH:$PATH
# Backup and clear python path to keep ros from checking ros directories for python modules
_ROS_CONDA_PYTHONPATH_BACKUP=$PYTHONPATH
export PYTHONPATH=$_ROS_CONDA_PYTHONPATH_WITHOUT_ROS
# Unalias the stuff
_ROS_CONDA_removeAliases
_UPDATE_PROMPT
return 0 # true
fi
return 1
}
function _ROS_CONDA_removeCondaFromPath {
if [[ $PATH = $_ROS_CONDA_PATH":"* ]]
then
export PATH=${PATH#$_ROS_CONDA_PATH:}
elif [[ $PATH = *":"$_ROS_CONDA_PATH ]]
then
export PATH=${PATH%:$_ROS_CONDA_PATH}
elif [[ $PATH = *":"$_ROS_CONDA_PATH":"* ]]
then
export PATH=${PATH//:$_ROS_CONDA_PATH:/:}
fi
if [ $_ROS_CONDA_ADDED -eq 1 ]
then
# Restore ROS PYTHONPATH
export PYTHONPATH=$_ROS_CONDA_PYTHONPATH_BACKUP
_ROS_CONDA_addAliases
_UPDATE_PROMPT
fi
_ROS_CONDA_ADDED=0
}
function _ROS_CONDA_sourceWrapper {
if [ $1 == "activate" ]
then
_ROS_CONDA_ensureCondaInPath
if [ $# == 1 ]
then
# If only source activate call source activate root.
# Otherwise it will fail. Don't know why though
command source activate
else
command source $@
fi
elif [ $1 == "deactivate" ]
then
command source deactivate
if [ $_ROS_CONDA_RELEASED_MANUALLY -eq 0 ]
then
_ROS_CONDA_removeCondaFromPath
fi
else
command source $@
fi
}
if [ $_ROS_CONDA_ADDED -eq 0 ]
then
if [[ $_ROS_CONDA_ALIASES != '' ]]
then
_ROS_CONDA_removeAliases
fi
_ROS_CONDA_addAliases
fi
# add conda identifier to prompt
function _UPDATE_PROMPT {
if [[ $PATH == $_ROS_CONDA_PATH":"* || $PATH == *":"$_ROS_CONDA_PATH || $PATH == *":"$_ROS_CONDA_PATH":"* ]]
then
export PS1="\[\033[0;36m\][conda] $_PS1"
else
export PS1="$_PS1"
fi
}
_UPDATE_PROMPT
alias source='_ROS_CONDA_sourceWrapper'
alias release-the-snake='_ROS_CONDA_RELEASED_MANUALLY=1; if _ROS_CONDA_ensureCondaInPath; then echo "All hail the snake!"; else echo "The snake is in another castle!
Jk, you released it already."; fi'
alias cage-the-snake='_ROS_CONDA_RELEASED_MANUALLY=0; _ROS_CONDA_removeCondaFromPath; echo "The snake has been caged if it wasn'"'"'t already."'