-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathros_conda_wrapper_rc
1172 lines (956 loc) · 35.5 KB
/
ros_conda_wrapper_rc
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
## -- ROS Conda wrapper --
# This wrapper was added to solve for the conflicts caused by sourcing a ROS setup.bash file inside a
# Conda environment .
# Set environment variables
export ROS_CONDA_WRAPPER=true
# Set Global variables
ROS_CONDA_WRAPPER_PROGNAME="ROS Conda wrapper"
ROS_CONDA_WRAPPER_ERROR=false
ROS_CONDA_WRAPPER_VERSION="1.0.7"
ROS_CONDA_CONFIG_FILE_NAME="${HOME}/.ros_conda_wrapper_rc_cfg"
ROS_CONDA_ROS_PYTHONPATH_BACKUP=()
# Bash echo colours
ORANGE_CC='\033[0;33m'
NC_CC='\033[0m' # No colour
# Required ROS packages
ROS_CONDA_DEPS=()
ROS_CONDA_FORGE_DEPS=(
"ros-core"
"ros-actionlib"
"ros-dynamic-reconfigure"
)
ROS_CONDA_PIP_DEPS=()
# Perform some clean-up actions
unalias conda 2> /dev/null
unset -f _ros_conda_load_config_vars 2> /dev/null
unset -f _ros_conda_save_config_vars 2> /dev/null
unset -f _ros_conda_usage 2> /dev/null
unset -f _ros_conda_path_fix_outside 2> /dev/null
unset -f _ros_conda_path_fix_inside 2> /dev/null
unset -f _ros_conda_pythonpath_fix_outside 2> /dev/null
unset -f _ros_conda_pythonpath_fix_inside 2> /dev/null
unset -f _ros_conda_is_conda_env 2> /dev/null
unset -f _ros_conda_init 2> /dev/null
unset -f _ros_conda_deinit 2> /dev/null
unset -f _ros_conda_wrapper 2> /dev/null
unset -f _ros_conda_source_wrapper 2> /dev/null
unset -f _ros_conda_dot_source_wrapper 2> /dev/null
###############################################
## Ros Conda wrapper member functions #########
###############################################
_ros_conda_load_config_vars() {
# Load conda wrapper configuration values
if [[ -a "$ROS_CONDA_CONFIG_FILE_NAME" ]]; then
# Read variables from file
while IFS="=" read -r key val; do
case "$key" in
'#'*)
;; # Skip comments
'ROS_CONDA_WRAPPER_ACTIVE')
eval "$key=\"$val\""
;;
esac
done < "$ROS_CONDA_CONFIG_FILE_NAME"
# Parse variables to make sure they are alright
case "$ROS_CONDA_WRAPPER_ACTIVE" in
"true"|"True"|"TRUE"|"false"|"False"|"FALSE")
;;
*)
# Value was not valid set to default
ROS_CONDA_WRAPPER_ACTIVE=true
;;
esac
# Export variable
export ROS_CONDA_WRAPPER_ACTIVE
else
# Create file
{
cat > "$ROS_CONDA_CONFIG_FILE_NAME" <<- EOL
# Ros Conda wrapper settings
ROS_CONDA_WRAPPER_ACTIVE=true
EOL
} || {
printf "%s:${ORANGE}warning:${NC} Configuration file could not be loaded. Default values were used instead. %s\n" "$PROGNAME"
}
# Use defaults
export ROS_CONDA_WRAPPER_ACTIVE=true
fi
}
_ros_conda_save_config_vars() {
# Save the Conda wrapper configuration values
{
cat > "$ROS_CONDA_CONFIG_FILE_NAME" <<- EOL
# Ros Conda wrapper settings
ROS_CONDA_WRAPPER_ACTIVE=$ROS_CONDA_WRAPPER_ACTIVE
EOL
} || {
printf "%s:${ORANGE}warning:${NC} Pathmod configuration values could not be saved. %s\n" "$PROGNAME"
}
}
_ros_conda_usage() {
# Script that prints usage information
cat <<- EOF
usage: $ROS_CONDA_WRAPPER_PROGNAME options
This wrapper solves some problems people have while trying to use ROS Kinetic/Melodic
inside a Conda environment.
OPTIONS:
activate Activate the ROS Conda wrapper
deactivate Deactivate the ROS Conda wrapper
init <CONDA_ENVIRONMENT> Initialize a given <CONDA_ENVIRONMENT> to work with ROS
deinit <CONDA_ENVIRONMENT> Deinitailize a given ROS <CONDA_ENVIRONMENT> to work
-h, --help Displays usage information
-v, --version ROS Conda wrapper version
EOF
}
function _ros_conda_path_fix_outside() {
# This function makes sure the anaconda bin and condabin folders
# are further on the path than the /usr/bin, /bin and
# /opt/ros/<ROSVERSION>/bin when outside an anaconda environment.
# Create local variables
local path_changed=false
# Split PATH into array and get path length
local defaultIFS="$IFS"; IFS=$' \t\n'; local path_array=(${PATH//:/ }); IFS="$defaultIFS"
local arr_len="${#path_array[@]}"
# Make sure the Conda paths are at the end of the PATH variable
ros_conda_path_array=(
"${ROS_CONDA_CONDA_PATH}/condabin"
"${ROS_CONDA_CONDA_PATH}/bin"
"${ROS_CONDA_CONDA_PATH}/envs/${CONDA_DEFAULT_ENV}/bin"
)
for path in "${ros_conda_path_array[@]}"; do
if [[ :${PATH}: == *:"${path}":* ]] ; then
# Remove all occurrences from PATH variable
while [[ ":${PATH}:" == *:"${path}":* ]] ; do
# Remove path from PATH variable
if [[ "${arr_len}" -eq 1 ]]; then # If Path length is 1
PATH=${PATH//"${path}"/} # delete instance
else
PATH=${PATH//":${path}:"/":"} # delete any instances in the middle
PATH=${PATH/#"${path}:"/} # delete any instance at the beginning
PATH=${PATH/%":${path}"/} # delete any instance in the at the end
fi
done
# Add path to the end
PATH="${PATH:+${PATH}:}${path}"
path_changed=true
fi
done
# Export PATH
if [[ "$path_changed" = true ]]; then
export PATH
fi
}
function _ros_conda_path_fix_inside() {
# This function makes sure the anaconda bin and condabin folders
# are earlier on the path than the /usr/bin, /bin and
# /opt/ros/<ROSVERSION>/bin when inside an anaconda environment.
# Create local variables
local path_changed=false
# Split PATH into array and get path length
local defaultIFS="$IFS"; IFS=$' \t\n'; local path_array=(${PATH//:/ }); IFS="$defaultIFS"
local arr_len="${#path_array[@]}"
# # Make sure the Conda bin path is present on the PATH
# # NOTE: Done since sourcing a ROS setup.bash removes the Conda bin path
# if [[ "$CONDA_DEFAULT_ENV" == "base" || "$CONDA_DEFAULT_ENV" == "root" ]]; then # If in base
# # Retrieve conda bin path
# local _conda_env_bin_path="${ROS_CONDA_CONDA_PATH}/bin"
# # Add bin path to PATH
# PATH="${_conda_env_bin_path}${PATH:+:${PATH}}"
# elif [[ -z "${CONDA_DEFAULT_ENV}" ]]; then # Not in conda environment
# return 1 # Return with error code
# else # Not in base
# # Retrieve conda bin path
# local _conda_env_bin_path="${ROS_CONDA_CONDA_PATH}/envs/${CONDA_DEFAULT_ENV}/bin"
# # Add bin path to PATH
# PATH="${_conda_env_bin_path}${PATH:+:${PATH}}"
# fi
# Make sure the Conda paths are at the beginning of the PATH variable
ros_conda_path_array=(
"${ROS_CONDA_CONDA_PATH}/condabin"
"${ROS_CONDA_CONDA_PATH}/bin"
"${ROS_CONDA_CONDA_PATH}/envs/${CONDA_DEFAULT_ENV}/bin"
)
for path in "${ros_conda_path_array[@]}"; do
if [[ ":${PATH}:" == *:"${path}":* ]] ; then
# Remove all occurrences from PATH variable
while [[ ":${PATH}:" == *:"${path}":* ]] ; do
# Remove path from PATH variable
if [[ "${arr_len}" -eq 1 ]]; then # If Path length is 1
PATH=${PATH//"${path}"/} # delete instance
else
PATH=${PATH//":${path}:"/":"} # delete any instances in the middle
PATH=${PATH/#"${path}:"/} # delete any instance at the beginning
PATH=${PATH/%":${path}"/} # delete any instance in the at the end
fi
done
# Prepend to PATH variable
PATH="${path}${PATH:+:${PATH}}"
path_changed=true
fi
done
# Export PATH
if [[ "$path_changed" = true ]]; then
export PATH
fi
}
function _ros_conda_pythonpath_fix_outside() {
# This function makes sure that the ROS python dist-packages path
# IS present on the PYTHONPATH when inside a Conda environment.
# Create local variables
local path_changed=false
# Restore ROS python paths if they are not yet found on the PYTHONPATH
for ros_python_path in "${ROS_CONDA_ROS_PYTHONPATH_BACKUP[@]}"; do
if [[ -d "$ros_python_path" ]]; then
if [[ ":$PYTHONPATH:" != *":${ros_python_path}:"* ]]; then
# Prepend to PYTHONPATH variable
PYTHONPATH="$ros_python_path${PYTHONPATH:+:${PYTHONPATH}}"
path_changed=true
fi
fi
done
# Export PYTHONPATH
if [[ "$path_changed" = true ]]; then
# Export PYTHONPATH
export PYTHONPATH
fi
}
function _ros_conda_pythonpath_fix_inside() {
# This function makes sure that the ROS python dist-packages path
# is NOT present on the PYTHONPATH when inside a Conda environment.
# Create local variables
local path_changed=false
local new_pythonpath_array=()
# Split PYTHONPATH or PYTHONPATH into array
local defaultIFS="$IFS"; IFS=$' \t\n'; local pythonpath_array=(${PYTHONPATH//:/ }); IFS="$defaultIFS"
# Loop through the paths and retrieve the python paths
for path in "${pythonpath_array[@]}"; do
# Test if path is ros path
if [[ "$path" == "/opt/ros/"*"/dist-packages" ]]; then
# Add to ROS_PYTHONPATH_BACKUP array
ROS_CONDA_ROS_PYTHONPATH_BACKUP+=("${path}")
path_changed=true
else
# Pass to new PYTHONPATH array
new_pythonpath_array+=("${path}")
fi
done
# Export PYTHONPATH
if [[ "$path_changed" = true ]]; then
# Convert new PYTHONPATH back to : delimited string
PYTHONPATH=$(IFS=$':'; echo "${new_pythonpath_array[*]}") # Converting bash array back into a delimited string
# Export PYTHONPATH
export PYTHONPATH
fi
}
function _ros_conda_is_conda_env() {
# This function checks if a Conda environment exists
# it returns 0 if it does and 1 if it does not.
# Try to activate conda environment in subshell (Faster than conda info --envs or conda envs --list)
if [[ ("$ROS_CONDA_CONDA_VERSION" > 4.4.0 || "$ROS_CONDA_CONDA_VERSION" = 4.4.0) ]]; then
retval="$(\conda activate $1 2>&1 > /dev/null)"
else
retval="$(\source activate $1 2>&1 > /dev/null)"
fi
# Check if environment existed
if [[ "$retval" != *"Could not find conda environment"* ]]; then
local retval="true" # Enviroment exists
else
local retval="false" # Enviroment exists
fi
# Return result
echo "$retval"
}
function _ros_conda_init() {
# This function installs the packages that are needed to enable
# ROS to work from within a Conda environment.
# Get Conda environment name and path
if [[ "$1" == "" ]]; then
# If environment is active initialize that environment otherwise initialize base
if [[ ! -z "$CONDA_DEFAULT_ENV" ]]; then
# Test if in base environment
if [[ "$CONDA_DEFAULT_ENV" == "base" ]]; then
local _conda_env_name="base"
local _conda_env_path="$ROS_CONDA_CONDA_PATH"
elif [[ "$CONDA_DEFAULT_ENV" == "root" ]]; then
local _conda_env_name="root"
local _conda_env_path="$ROS_CONDA_CONDA_PATH"
else
# Retrieve Conda env name
local _conda_env_name="$CONDA_DEFAULT_ENV"
# Retrieve Conda env path
local _conda_env_path="${ROS_CONDA_CONDA_PATH}/envs/${_conda_env_name}"
fi
else
# Check which conda version is used and assign the right name
if [[ ("$ROS_CONDA_CONDA_VERSION" > 4.4.0 || "$ROS_CONDA_CONDA_VERSION" = 4.4.0) ]]; then
# Retrieve Conda environment path
local _conda_env_name="base"
local _conda_env_path="$ROS_CONDA_CONDA_PATH"
else
# Retrieve Conda environment path
local _conda_env_name="root"
local _conda_env_path="$ROS_CONDA_CONDA_PATH"
fi
fi
elif [[ "$1" == "base" ]]; then
# Retrieve Conda environment path
local _conda_env_name="base"
local _conda_env_path="$ROS_CONDA_CONDA_PATH"
elif [[ "$1" == "root" ]]; then
# Retrieve Conda environment path
local _conda_env_name="root"
local _conda_env_path="$ROS_CONDA_CONDA_PATH"
else
# Retrieve Conda environment path
local _conda_env_name="$1"
local _conda_env_path="${ROS_CONDA_CONDA_PATH}/envs/${_conda_env_name}"
fi
# Install ROS Conda python dependencies if they are not yet installed
if [[ ! "${#ROS_CONDA_DEPS[@]}" -eq 0 ]]; then
for dep in "${ROS_CONDA_DEPS[@]}"; do
if [[ "$(\conda list -n $_conda_env_name | grep -w $dep | grep -P '(?<!-)'$dep'(?!-)' | awk '{ print $1 }')" != "$dep" ]]; then
echo "Installing conda package: $dep"
\conda install -n "$_conda_env_name" --yes "${dep}"
else
echo "Conda package requirement already satisfied: $dep"
fi
done
fi
# Install if ROS Conda forge python dependencies if they are not yet installed
if [[ ! "${#ROS_CONDA_FORGE_DEPS[@]}" -eq 0 ]]; then
for dep in "${ROS_CONDA_FORGE_DEPS[@]}"; do
if [[ "$(\conda list -n $_conda_env_name | grep -w $dep | grep -P '(?<!-)'$dep'(?!-)' | awk '{ print $1 }')" != "$dep" ]]; then
echo "Installing conda package: $dep"
\conda install -n "$_conda_env_name" --channel conda-forge --yes "${dep}"
else
echo "Conda package requirement already satisfied: $dep"
fi
done
fi
# Get pip install locations
if [[ -z "$(find ${_conda_env_path}/bin/ -iname pip)" ]]; then
# Install pip if not installed already
echo "Installing conda package: pip"
\conda install --yes "pip"
local _env_pip_path="$(find ${_conda_env_path}/bin/ -iname pip)"
else
local _env_pip_path="$(find ${_conda_env_path}/bin/ -iname pip)"
fi
# Install ROS pip python dependencies if they are not yet installed
if [[ ! "${#ROS_CONDA_PIP_DEPS[@]}" -eq 0 ]]; then
for dep in "${ROS_CONDA_PIP_DEPS[@]}"; do
if [[ "$($_env_pip_path list | grep -w $dep | grep -P '(?<!-)'$dep'(?!-)' | awk '{ print $1 }')" != "$dep" ]]; then
echo "Installing pip package: $dep"
"$_env_pip_path" install "${dep}"
else
echo "Pip package requirement already satisfied: $dep"
fi
done
fi
# Set CONDA_INIT environmental variable to true
if [[ ! -f "$_conda_env_path/etc/conda/activate.d/env_vars.sh" ]]; then # Generate env vars activation file folder
mkdir -p "$_conda_env_path/etc/conda/activate.d"
cat <<-EOF >"$_conda_env_path/etc/conda/activate.d/env_vars.sh"
#!/bin/sh
## ROS_CONDA wrapper ENV variables
# Created on $(date)
export ROS_CONDA=TRUE
EOF
elif ! grep -q "ROS_CONDA=" "$_conda_env_path/etc/conda/activate.d/env_vars.sh"; then
cat <<-EOF >>"$_conda_env_path/etc/conda/activate.d/env_vars.sh"
## ROS_CONDA wrapper ENV variables
# Created on $(date)
export ROS_CONDA=TRUE
EOF
else
sed -i -e 's/ROS_CONDA=FALSE/ROS_CONDA=TRUE/g' "$_conda_env_path/etc/conda/activate.d/env_vars.sh"
fi
# Create ROS environmental variable unset file if it does not exist
if [ ! -f "$_conda_env_path/etc/conda/deactivate.d/env_vars.sh" ]; then # Generate env vars deactivation file folder
mkdir -p "$_conda_env_path/etc/conda/deactivate.d"
cat <<-EOF >"$_conda_env_path/etc/conda/deactivate.d/env_vars.sh"
#!/bin/sh
## ROS_CONDA wrapper ENV variables
# Created on $(date)
unset ROS_CONDA
EOF
elif ! grep -q "unset ROS_CONDA" "$_conda_env_path/etc/conda/deactivate.d/env_vars.sh"; then
cat <<-EOF >>"$_conda_env_path/etc/conda/deactivate.d/env_vars.sh"
## ROS_CONDA wrapper ENV variables
# Created on $(date)
unset ROS_CONDA
EOF
fi
}
function _ros_conda_deinit() {
# This function removes the packages that are needed to enable
# ROS to work from within a Conda environment.
# Get Conda environment name and path
if [[ "$1" == "" ]]; then
# If environment is active initialize that environment otherwise initialize base
if [[ ! -z "$CONDA_DEFAULT_ENV" ]]; then
# Test if in base environment
if [[ "$CONDA_DEFAULT_ENV" == "base" ]]; then
local _conda_env_name="base"
local _conda_env_path="$ROS_CONDA_CONDA_PATH"
elif [[ "$CONDA_DEFAULT_ENV" == "root" ]]; then
local _conda_env_name="root"
local _conda_env_path="$ROS_CONDA_CONDA_PATH"
else
# Retrieve Conda env name
local _conda_env_name="$CONDA_DEFAULT_ENV"
# Retrieve Conda env path
local _conda_env_path="${ROS_CONDA_CONDA_PATH}/envs/${_conda_env_name}"
fi
else
# Check which conda version is used and assign the right name
if [[ ("$ROS_CONDA_CONDA_VERSION" > 4.4.0 || "$ROS_CONDA_CONDA_VERSION" = 4.4.0) ]]; then
# Retrieve Conda environment path
local _conda_env_name="base"
local _conda_env_path="$ROS_CONDA_CONDA_PATH"
else
# Retrieve Conda environment path
local _conda_env_name="root"
local _conda_env_path="$ROS_CONDA_CONDA_PATH"
fi
fi
elif [[ "$1" == "base" ]]; then
# Retrieve Conda environment path
local _conda_env_name="base"
local _conda_env_path="$ROS_CONDA_CONDA_PATH"
elif [[ "$1" == "root" ]]; then
# Retrieve Conda environment path
local _conda_env_name="root"
local _conda_env_path="$ROS_CONDA_CONDA_PATH"
else
# Retrieve Conda environment path
local _conda_env_name="$1"
local _conda_env_path="${ROS_CONDA_CONDA_PATH}/envs/${_conda_env_name}"
fi
# Remove ROS Conda python dependencies if they are installed
if [[ ! "${#ROS_CONDA_DEPS[@]}" -eq 0 ]]; then
for dep in "${ROS_CONDA_DEPS[@]}"; do
if [[ ! "$(\conda list -n $_conda_env_name | grep -w $dep | grep -P '(?<!-)'$dep'(?!-)' | awk '{ print $1 }')" != "$dep" ]]; then
echo "Removing conda package: $dep"
\conda remove -n "$_conda_env_name" --yes "${dep}"
else
echo "Conda package requirement already removed: $dep"
fi
done
fi
# Remove ROS Conda Forge python dependencies if they are installed
if [[ ! "${#ROS_CONDA_FORGE_DEPS[@]}" -eq 0 ]]; then
for dep in "${ROS_CONDA_FORGE_DEPS[@]}"; do
if [[ ! "$(\conda list -n $_conda_env_name | grep -w $dep | grep -P '(?<!-)'$dep'(?!-)' | awk '{ print $1 }')" != "$dep" ]]; then
echo "Removing conda package: $dep"
\conda remove -n "$_conda_env_name" --channel conda-forge --yes "${dep}"
else
echo "Conda package requirement already removed: $dep"
fi
done
fi
# Get pip install locations
if [[ -z "$(find ${_conda_env_path}/bin/ -iname pip)" ]]; then
# Install pip if not installed already
echo "Installing conda package: pip"
\conda install --yes "pip"
local _env_pip_path="$(find ${_conda_env_path}/bin/ -iname pip)"
else
local _env_pip_path="$(find ${_conda_env_path}/bin/ -iname pip)"
fi
# Remove ROS pip python dependencies
if [[ ! "${#ROS_CONDA_PIP_DEPS[@]}" -eq 0 ]]; then
for dep in "${ROS_CONDA_PIP_DEPS[@]}"; do
if [[ ! "$($_env_pip_path list | grep -w $dep | grep -P '(?<!-)'$dep'(?!-)' | awk '{ print $1 }')" != "$dep" ]]; then
echo "Removing pip package: $dep"
"$_env_pip_path" uninstall --yes "$dep"
else
echo "Pip package already removed: $dep"
fi
done
fi
# Set CONDA_INIT environmental variable to true
if [[ ! -f "$_conda_env_path/etc/conda/activate.d/env_vars.sh" ]]; then # Generate env vars activation file folder
mkdir -p "$_conda_env_path/etc/conda/activate.d"
cat <<-EOF >"$_conda_env_path/etc/conda/activate.d/env_vars.sh"
#!/bin/sh
## ROS_CONDA wrapper ENV variables
# Created on $(date)
export ROS_CONDA=FALSE
EOF
elif ! grep -q "ROS_CONDA=" "$_conda_env_path/etc/conda/activate.d/env_vars.sh"; then
cat <<-EOF >>"$_conda_env_path/etc/conda/activate.d/env_vars.sh"
## ROS_CONDA wrapper ENV variables
# Created on $(date)
export ROS_CONDA=FALSE
EOF
else
sed -i -e 's/ROS_CONDA=TRUE/ROS_CONDA=FALSE/g' "$_conda_env_path/etc/conda/activate.d/env_vars.sh"
fi
# Create ROS environmental variable unset file if it does not exist
if [[ ! -f "$_conda_env_path/etc/conda/deactivate.d/env_vars.sh" ]]; then # Generate env vars deactivation file folder
mkdir -p "$_conda_env_path/etc/conda/deactivate.d"
cat <<-EOF >"$_conda_env_path/etc/conda/deactivate.d/env_vars.sh"
#!/bin/sh
## ROS_CONDA wrapper ENV variables
# Created on $(date)
unset ROS_CONDA
EOF
elif ! grep -q "unset ROS_CONDA" "$_conda_env_path/etc/conda/deactivate.d/env_vars.sh"; then
cat <<-EOF >>"$_conda_env_path/etc/conda/deactivate.d/env_vars.sh"
## ROS_CONDA wrapper ENV variables
# Created on $(date)
unset ROS_CONDA
EOF
fi
}
###############################################
## ros_conda_wrapper main wrapper functions ###
###############################################
function _ros_conda_wrapper() {
# This function serves a wrapper around the original Conda command. It is used
# for Conda versions > 4.4.
if [[ "$1" == "activate" && "$ROS_CONDA_WRAPPER_ACTIVE" == "true" && ("$ROS_CONDA_CONDA_VERSION" > 4.4.0 || "$ROS_CONDA_CONDA_VERSION" = 4.4.0) ]]; then # Warp Conda deactivate command
# Set error variable
local had_error=false
# Apply path modifications if environment exists
_ros_conda_path_fix_inside
# Make sure the activation Conda command is executed
\conda "$@" || had_error=true
# Restore path if source command failed
if [[ "$had_error" == true ]]; then
if [[ -z "$CONDA_DEFAULT_ENV" ]]; then
# Fix PATH and PYTHONPATH
_ros_conda_path_fix_outside
_ros_conda_pythonpath_fix_outside
fi
else
# Fix PATH and PYTHONPATH
_ros_conda_path_fix_inside
_ros_conda_pythonpath_fix_inside
fi
elif [[ "$1" == "deactivate" && "$ROS_CONDA_WRAPPER_ACTIVE" == "true" && ("$ROS_CONDA_CONDA_VERSION" > 4.4.0 || "$ROS_CONDA_CONDA_VERSION" = 4.4.0) ]]; then # Warp Conda deactivate command
# Make sure the deactivation Conda command is executed
\conda "$@"
# Fix PATH and PYTHONPATH
if [[ ! -z "$CONDA_DEFAULT_ENV" ]]; then # If still inside Conda environment
# Fix PATH and PYTHONPATH
_ros_conda_path_fix_inside
_ros_conda_pythonpath_fix_inside
elif [[ -z "$CONDA_DEFAULT_ENV" ]]; then
# # Fix PATH and PYTHONPATH
_ros_conda_path_fix_outside
_ros_conda_pythonpath_fix_outside
fi
elif [[ "$1" == "--ros-wrapper" ]]; then # Add ROS Conda wrapper cli
# ROS Conda wrapper commands
if [[ "$2" == "activate" ]]; then
# Print the result message
if $ROS_CONDA_WRAPPER_ACTIVE; then
echo "Conda wrapper already active."
else
echo "Activated conda wrapper. "
fi
# Set ROS_CONDA_WRAPPER_ACTIVE environmental variable
export ROS_CONDA_WRAPPER_ACTIVE=true
# Save values to config file
_ros_conda_save_config_vars
elif [[ "$2" == "deactivate" ]]; then
# Print the result message
if ! $ROS_CONDA_WRAPPER_ACTIVE; then
echo "Conda wrapper already deactivated."
else
echo "Deactivated conda wrapper. "
fi
# Set ROS_CONDA_WRAPPER_ACTIVE environmental variable
export ROS_CONDA_WRAPPER_ACTIVE=false
# Save values to config file
_ros_conda_save_config_vars
elif [[ "$2" == "init" ]]; then # Initialize BASE Conda environment to work with ROS
# Get Conda environment name
if [[ "$3" == "" ]]; then
if [[ ! -z "$CONDA_DEFAULT_ENV" ]]; then
# Test if in base environment
if [[ "$CONDA_DEFAULT_ENV" == "base" ]]; then
local _conda_env_name="base"
elif [[ "$CONDA_DEFAULT_ENV" == "root" ]]; then
local _conda_env_name="root"
else
# Retrieve Conda env name
local _conda_env_name="$CONDA_DEFAULT_ENV"
fi
else
# Check which conda version is used and assign the right name
if [[ ("$ROS_CONDA_CONDA_VERSION" > 4.4.0 || "$ROS_CONDA_CONDA_VERSION" = 4.4.0) ]]; then
# Retrieve Conda environment path
local _conda_env_name="base"
else
# Retrieve Conda environment path
local _conda_env_name="root"
fi
fi
elif [[ "$1" == "base" ]]; then
# Retrieve Conda environment path
local _conda_env_name="base"
elif [[ "$1" == "root" ]]; then
# Retrieve Conda environment path
local _conda_env_name="root"
else
# Retrieve Conda environment path
local _conda_env_name="$3"
fi
# Check whether environment that user specified is valid
if [[ "$(_ros_conda_is_conda_env $_conda_env_name)" == "false" ]]; then
echo "Could not find Conda environment: $_conda_env_name" 1>&2
echo "You can list all discoverable environments with \`conda info --envs\`."
return 1
fi
# Ask user if he/she wants to continue with the initialization
while true; do
read -r -p "You are about to initialize your \"$_conda_env_name\" Conda environment to work with ROS. Are you sure? [y/n] " response
case "$response" in
[yY][eE][sS] | [yY])
_ros_conda_init "$3"
break
;;
[nN][oO] | [nN])
echo "Canceling Conda ROS initialization."
break
;;
*)
echo "Please enter a valid response."
;;
esac
done
elif [[ "$2" == "deinit" ]]; then # Deinitialize main environment ros_conda wrapper
# Get Conda environment name
if [[ "$3" == "" ]]; then
if [[ ! -z "$CONDA_DEFAULT_ENV" ]]; then
# Test if in base environment
if [[ "$CONDA_DEFAULT_ENV" == "base" ]]; then
local _conda_env_name="base"
elif [[ "$CONDA_DEFAULT_ENV" == "root" ]]; then
local _conda_env_name="root"
else
# Retrieve Conda env name
local _conda_env_name="$CONDA_DEFAULT_ENV"
fi
else
# Check which conda version is used and assign the right name
if [[ ("$ROS_CONDA_CONDA_VERSION" > 4.4.0 || "$ROS_CONDA_CONDA_VERSION" = 4.4.0) ]]; then
# Retrieve Conda environment path
local _conda_env_name="base"
else
# Retrieve Conda environment path
local _conda_env_name="root"
fi
fi
elif [[ "$1" == "base" ]]; then
# Retrieve Conda environment path
local _conda_env_name="base"
elif [[ "$1" == "root" ]]; then
# Retrieve Conda environment path
local _conda_env_name="root"
else
# Retrieve Conda environment path
local _conda_env_name="$3"
fi
# Check whether environment that user specified is valid
if [[ "$(_ros_conda_is_conda_env $_conda_env_name)" == "false" ]]; then
echo "Could not find Conda environment: $_conda_env_name" 1>&2
echo "You can list all discoverable environments with \`conda info --envs\`."
return 1
fi
# Ask user if he/she wants to continue with the initialization
while true; do
read -r -p "You are about to deinitialize your \"$_conda_env_name\" Conda environment to work with ROS. Are you sure? [y/n] " response
case "$response" in
[yY][eE][sS] | [yY])
_ros_conda_deinit "$3"
break
;;
[nN][oO] | [nN])
echo "Canceling Conda ROS deinitialization."
break
;;
*)
echo "Please enter a valid response."
;;
esac
done
elif [[ "$2" == "-s" || "$2" == "--show-settings" ]]; then
# Print current settings
cat <<- EOM
Wrapper active: $ROS_CONDA_WRAPPER_ACTIVE
Wrapper error: $ROS_CONDA_WRAPPER_ERROR
EOM
elif [[ "$2" == "-h" || "$2" == "--help" ]]; then
# Print help menu
_ros_conda_usage
elif [[ "$2" == '-v' || "$2" == "--version" ]]; then
echo "v$ROS_CONDA_WRAPPER_VERSION"
else
# Print help menu
_ros_conda_usage
fi
else # Make sure other Conda commands are executed
\conda "$@" # Quotes since bash strips the outer quotes
fi
}
function _ros_conda_source_wrapper() {
# This function serves a wrapper around
# the original Conda command. This alias
# is used for Conda v < 4.4.
if [[ "$1" == "activate" && "$ROS_CONDA_WRAPPER_ACTIVE" == "true" && "$ROS_CONDA_CONDA_VERSION" < 4.4 ]]; then # Warp Conda deactivate command
# Set error variable
local had_error=false
# Apply path modifications if environment exists
_ros_conda_path_fix_inside
# Make sure the activation Conda command is executed
if [[ "$#" == 1 ]]; then
# Try to activate environment using root
# NOTE: This was done since `\source activate` from within a
# script gave a strange error.
\source activate "root" || had_error=true
else
\source "$@" || had_error=true
fi
# Restore path if source command failed
if [[ "$had_error" == true ]]; then
if [[ -z "$CONDA_DEFAULT_ENV" && "$ROS_CONDA_WRAPPER_ACTIVE" == "true" ]]; then
# Fix PATH and PYTHONPATH
_ros_conda_path_fix_outside
_ros_conda_pythonpath_fix_outside
fi
else
# Fix PATH and PYTHONPATH
_ros_conda_path_fix_inside
_ros_conda_pythonpath_fix_inside
fi
elif [[ "$1" == "deactivate" && "$ROS_CONDA_WRAPPER_ACTIVE" == "true" && "$ROS_CONDA_CONDA_VERSION" < 4.4 ]]; then # Warp Conda deactivate command
# Make sure the deactivation Conda command is executed
\source "$@"
# Fix PATH and PYTHONPATH
if [[ ! -z "$CONDA_DEFAULT_ENV" ]]; then # If still inside Conda environment
# Fix PATH and PYTHONPATH
_ros_conda_path_fix_inside
_ros_conda_pythonpath_fix_inside
elif [[ -z "$CONDA_DEFAULT_ENV" ]]; then
# # Fix PATH and PYTHONPATH
_ros_conda_path_fix_outside
_ros_conda_pythonpath_fix_outside
fi
elif [[ "$1" == "/opt/ros"*"/setup.bash" && "$ROS_CONDA_WRAPPER_ACTIVE" == "true" ]]; then
# Create error check variable
local had_error=false
# Execute source command
\source "$@" || had_error=true
# Fix paths if source was successful
if [[ "$had_error" == false ]]; then
# Check if your inside or outside an anaconda environment
if [[ ! -z "$CONDA_DEFAULT_ENV" ]]; then # Inside environment
# Fix PATH and PYTHONPATH
_ros_conda_path_fix_inside
_ros_conda_pythonpath_fix_inside
else
# Fix PATH and PYTHONPATH
_ros_conda_path_fix_outside
_ros_conda_pythonpath_fix_outside
fi
fi
elif [[ "$1" == *"devel/setup.bash" && "$ROS_CONDA_WRAPPER_ACTIVE" == "true" ]]; then
# Execute source command
local had_error=false
\source "$@" || had_error=true
# Fix paths if source was successful
if [[ "$had_error" == false ]]; then
# Get full path
local sourced_path="$1"
local sourced_path_absolute="$(cd $(dirname ${sourced_path}) 2>/dev/null && pwd -P)/$(basename ${sourced_path})"
if [[ -e "$sourced_path_absolute" ]]; then
# Get main catkin_ws path
local catkin_ws_path="$(dirname $(dirname ${sourced_path_absolute}))"
# Check if sourced file was inside catkin_ws
if [[ -e "${catkin_ws_path}/.catkin_workspace" || -e "${catkin_ws_path}/.catkin_tools" ]]; then
# Check if your inside or outside an anaconda environment
if [[ ! -z "$CONDA_DEFAULT_ENV" ]]; then # Inside environment
# Fix PATH and PYTHONPATH
_ros_conda_path_fix_inside
_ros_conda_pythonpath_fix_inside
else
# Fix PATH and PYTHONPATH
_ros_conda_path_fix_outside
_ros_conda_pythonpath_fix_outside
fi
fi
fi
fi
else # Make sure other Conda commands are executed
# Make sure the activation Conda command is executed
if [[ "$1" == "activate" ]]; then
if [[ "$#" == 1 && "$ROS_CONDA_CONDA_VERSION" < 4.4 ]]; then
# Try to activate environment using root
# NOTE: This was done since `\source activate` from within a
# script gave a strange error.
\source activate "root"
else
\source "$@"
fi
else
\source "$@"
fi
fi
}
function _ros_conda_dot_source_wrapper() {
# This function serves a wrapper around
# the dot source command (.) to make sure that
# the path and the PYTHONPATH stay fixed when
# ROS or a catkin workspace is sourced.
if [[ "$1" == "/opt/ros"*"/setup.bash" && "$ROS_CONDA_WRAPPER_ACTIVE" == "true" ]]; then
# Create error check variable
local had_error=false
# Execute source command
\. "$@" || had_error=true
# Fix paths if source was successful
if [[ "$had_error" == false ]]; then
# Check if your inside or outside an anaconda environment
if [[ ! -z "$CONDA_DEFAULT_ENV" ]]; then # Inside environment