Guest User

Untitled

a guest
Jan 17th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. ### This script wraps all executables in the anaconda bin folder so that they can be used without adding Anaconda
  2. ### to the path which would break some functionality of ROS (Robot Operating System)
  3. ###
  4. ### The commands e.g. jupyter notebook will cause the script to add anaconda to the path, start jupyter notebook
  5. ### and after jupyter notebook terminated remove anaconda from the path again
  6. ###
  7. ### Notable commands:
  8. ### * release-the-snake Adds conda to the path and removes all aliases defined by this script
  9. ### Conda will stay in the PATH until the end of the session (terminal is closed) or
  10. ### until "cage-the-snake" is called
  11. ### * cage-the-snake Removes conda from the path and redefines all aliases for anaconda executables
  12. ### * source activate [env] Works just like with anaconda in the PATH, will activate the environment or (root) and
  13. ### Anaconda will stay in the PATH for the remaining session or until "source deactivate" is run
  14. ### * source deactivate Deactivates the environment and if Anaconda wasn't released manually using release-the-snake
  15. ### Anaconda will be removed from the PATH again.
  16.  
  17. if [ -z ${_ROS_CONDA_ADDED+x} ]
  18. then
  19. _ROS_CONDA_PATH=~/anaconda3/bin
  20. _ROS_CONDA_ADDED=0
  21. _ROS_CONDA_ALIASES=''
  22. _ROS_CONDA_RELEASED_MANUALLY=0
  23. fi
  24.  
  25. function _ROS_CONDA_addAliases {
  26. if [[ $_ROS_CONDA_ALIASES != '' ]]
  27. then
  28. echo "ROS Conda Wrapper: Error! Aliases already defined!"
  29. _ROS_CONDA_removeAliases
  30. fi
  31.  
  32. for file in $_ROS_CONDA_PATH/*
  33. do
  34. local name
  35. name=${file##*/}
  36. if ! [ -x "$(command -v $name)" ]
  37. then
  38. alias $name='_ROS_CONDA_runWithConda '$name' $@'
  39. _ROS_CONDA_ALIASES=$_ROS_CONDA_ALIASES" "$name
  40. fi
  41. done
  42. }
  43.  
  44. function _ROS_CONDA_removeAliases {
  45. for cmd in $_ROS_CONDA_ALIASES
  46. do
  47. unalias $cmd
  48. done
  49. _ROS_CONDA_ALIASES=''
  50. }
  51.  
  52. function _ROS_CONDA_runWithConda {
  53. _ROS_CONDA_ensureCondaInPath
  54. command $@
  55. _ROS_CONDA_removeCondaFromPath
  56. }
  57.  
  58. function _ROS_CONDA_ensureCondaInPath {
  59. if [ $_ROS_CONDA_ADDED -eq 1 ]
  60. then
  61. return 1 # false
  62. fi
  63. _ROS_CONDA_ADDED=1
  64.  
  65. # Check that the path doesn't start, end or contain the ros conda path
  66. if [[ $PATH != $_ROS_CONDA_PATH":"* && $PATH != *":"$_ROS_CONDA_PATH && $PATH != *":"$_ROS_CONDA_PATH":"* ]]
  67. then
  68. export PATH=$_ROS_CONDA_PATH:$PATH
  69.  
  70. # Unalias the stuff
  71. _ROS_CONDA_removeAliases
  72.  
  73. return 0 # true
  74. fi
  75. return 1
  76. }
  77.  
  78. function _ROS_CONDA_removeCondaFromPath {
  79. _ROS_CONDA_ADDED=0
  80. if [[ $PATH = $_ROS_CONDA_PATH":"* ]]
  81. then
  82. export PATH=${PATH#$_ROS_CONDA_PATH:}
  83. _ROS_CONDA_addAliases
  84. elif [[ $PATH = *":"$_ROS_CONDA_PATH ]]
  85. then
  86. export PATH=${PATH%:$_ROS_CONDA_PATH}
  87. _ROS_CONDA_addAliases
  88. elif [[ $PATH = *":"$_ROS_CONDA_PATH":"* ]]
  89. then
  90. export PATH=${PATH//:$_ROS_CONDA_PATH:/:}
  91. _ROS_CONDA_addAliases
  92. fi
  93. }
  94.  
  95. function _ROS_CONDA_sourceWrapper {
  96. if [ $1 == "activate" ]
  97. then
  98. _ROS_CONDA_ensureCondaInPath
  99. if [ $# == 1 ]
  100. then
  101. # If only source activate call source activate root.
  102. # Otherwise it will fail. Don't know why though
  103. command source activate
  104. else
  105. command source $@
  106. fi
  107. elif [ $1 == "deactivate" ]
  108. then
  109. command source deactivate
  110. if [ $_ROS_CONDA_RELEASED_MANUALLY -eq 0 ]
  111. then
  112. _ROS_CONDA_removeCondaFromPath
  113. fi
  114. else
  115. command source $@
  116. fi
  117. }
  118.  
  119. if [ $_ROS_CONDA_ADDED -eq 0 ]
  120. then
  121. if [[ $_ROS_CONDA_ALIASES != '' ]]
  122. then
  123. _ROS_CONDA_removeAliases
  124. fi
  125. _ROS_CONDA_addAliases
  126. fi
  127.  
  128. alias source='_ROS_CONDA_sourceWrapper'
  129. 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!
  130. Jk, you released it already."; fi'
  131. alias cage-the-snake='_ROS_CONDA_RELEASED_MANUALLY=0; _ROS_CONDA_removeCondaFromPath; echo "The snake has been caged if it wasnt already."'
Add Comment
Please, Sign In to add comment