Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

312 linhas
6.8 KiB

1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
11 anos atrás
  1. #!/usr/bin/env bash
  2. # Summary: Configure the shell environment for pyenv
  3. # Usage: eval "$(pyenv init [-|--path] [--no-push-path] [--detect-shell] [--no-rehash] [<shell>])"
  4. set -e
  5. [ -n "$PYENV_DEBUG" ] && set -x
  6. # Provide pyenv completions
  7. if [ "$1" = "--complete" ]; then
  8. echo -
  9. echo --path
  10. echo --no-push-path
  11. echo --no-rehash
  12. echo --detect-shell
  13. echo bash
  14. echo fish
  15. echo ksh
  16. echo zsh
  17. exit
  18. fi
  19. mode="help"
  20. no_rehash=""
  21. no_push_path=""
  22. for args in "$@"
  23. do
  24. if [ "$args" = "-" ]; then
  25. mode="print"
  26. shift
  27. fi
  28. if [ "$args" = "--path" ]; then
  29. mode="path"
  30. shift
  31. fi
  32. if [ "$args" = "--detect-shell" ]; then
  33. mode="detect-shell"
  34. shift
  35. fi
  36. if [ "$args" = "--no-push-path" ]; then
  37. no_push_path=1
  38. shift
  39. fi
  40. if [ "$args" = "--no-rehash" ]; then
  41. no_rehash=1
  42. shift
  43. fi
  44. done
  45. shell="$1"
  46. if [ -z "$shell" ]; then
  47. shell="$(ps -p "$PPID" -o 'args=' 2>/dev/null || true)"
  48. shell="${shell%% *}"
  49. shell="${shell##-}"
  50. shell="${shell:-$SHELL}"
  51. shell="${shell##*/}"
  52. shell="${shell%%-*}"
  53. fi
  54. root="${0%/*}/.."
  55. function main() {
  56. case "$mode" in
  57. "help")
  58. help_
  59. exit 1
  60. ;;
  61. "path")
  62. print_path
  63. print_rehash
  64. exit 0
  65. ;;
  66. "print")
  67. init_dirs
  68. print_path
  69. print_env
  70. print_completion
  71. print_rehash
  72. print_shell_function
  73. exit 0
  74. ;;
  75. "detect-shell")
  76. detect_profile 1
  77. print_detect_shell
  78. exit 0
  79. ;;
  80. esac
  81. # should never get here
  82. exit 2
  83. }
  84. function detect_profile() {
  85. local detect_for_detect_shell="$1"
  86. case "$shell" in
  87. bash )
  88. if [ -e '~/.bash_profile' ]; then
  89. profile='~/.bash_profile'
  90. else
  91. profile='~/.profile'
  92. fi
  93. profile_explain="~/.bash_profile if it exists, otherwise ~/.profile"
  94. rc='~/.bashrc'
  95. ;;
  96. zsh )
  97. profile='~/.zprofile'
  98. rc='~/.zshrc'
  99. ;;
  100. ksh | ksh93 | mksh )
  101. # There are two implementations of Korn shell: AT&T (ksh93) and Mir (mksh).
  102. # Systems may have them installed under those names, or as ksh, so those
  103. # are recognized here. The obsolete ksh88 (subsumed by ksh93) and pdksh
  104. # (subsumed by mksh) are not included, since they are unlikely to still
  105. # be in use as interactive shells anywhere.
  106. profile='~/.profile'
  107. rc='~/.profile'
  108. ;;
  109. * )
  110. if [ -n "$detect_for_detect_shell" ]; then
  111. profile=
  112. rc=
  113. else
  114. profile='your shell'\''s login startup file'
  115. rc='your shell'\''s interactive startup file'
  116. fi
  117. ;;
  118. esac
  119. }
  120. function print_detect_shell() {
  121. echo "PYENV_SHELL_DETECT=$shell"
  122. echo "PYENV_PROFILE_DETECT=$profile"
  123. echo "PYENV_RC_DETECT=$rc"
  124. }
  125. function help_() {
  126. detect_profile
  127. {
  128. case "$shell" in
  129. fish )
  130. echo "# Add pyenv executable to PATH by running"
  131. echo "# the following interactively:"
  132. echo
  133. echo 'set -Ux PYENV_ROOT $HOME/.pyenv'
  134. echo 'set -U fish_user_paths $PYENV_ROOT/bin $fish_user_paths'
  135. echo
  136. echo "# Load pyenv automatically by appending"
  137. echo "# the following to ~/.config/fish/config.fish:"
  138. echo
  139. echo 'pyenv init - | source'
  140. echo
  141. ;;
  142. * )
  143. echo '# Load pyenv automatically by appending'
  144. echo -n "# the following to "
  145. if [ "$profile" == "$rc" ]; then
  146. echo "$profile :"
  147. else
  148. echo
  149. echo "# ${profile_explain:-$profile} (for login shells)"
  150. echo "# and $rc (for interactive shells) :"
  151. fi
  152. echo
  153. echo 'export PYENV_ROOT="$HOME/.pyenv"'
  154. echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"'
  155. echo 'eval "$(pyenv init -)"'
  156. ;;
  157. esac
  158. echo
  159. echo '# Restart your shell for the changes to take effect.'
  160. echo
  161. } >&2
  162. }
  163. function init_dirs() {
  164. mkdir -p "${PYENV_ROOT}/"{shims,versions}
  165. }
  166. function print_path() {
  167. # if no_push_path is set, guard the PATH manipulation with a check on whether
  168. # the shim is already in the PATH.
  169. if [ -n "$no_push_path" ]; then
  170. case "$shell" in
  171. fish )
  172. echo 'if not contains -- "'"${PYENV_ROOT}/shims"'" $PATH'
  173. print_path_prepend_shims
  174. echo 'end'
  175. ;;
  176. * )
  177. echo 'if [[ ":$PATH:" != *'\':"${PYENV_ROOT}"/shims:\''* ]]; then'
  178. print_path_prepend_shims
  179. echo 'fi'
  180. ;;
  181. esac
  182. else
  183. case "$shell" in
  184. fish )
  185. echo 'while set pyenv_index (contains -i -- "'"${PYENV_ROOT}/shims"'" $PATH)'
  186. echo 'set -eg PATH[$pyenv_index]; end; set -e pyenv_index'
  187. print_path_prepend_shims
  188. ;;
  189. * )
  190. # Some distros (notably Debian-based) set Bash's SSH_SOURCE_BASHRC compilation option
  191. # that makes it source `bashrc` under SSH even when not interactive.
  192. # This is inhibited by a guard in Debian's stock `bashrc` but some people remove it
  193. # in order to get proper environment for noninteractive remote commands
  194. # (SSH provides /etc/ssh/sshrc and ~/.ssh/rc for that but no-one seems to use them for some reason).
  195. # This has caused an infinite `bashrc` execution loop for those people in the below nested Bash invocation (#2367).
  196. # --norc negates this behavior of such a customized Bash.
  197. echo 'PATH="$(bash --norc -ec '\''IFS=:; paths=($PATH); '
  198. echo 'for i in ${!paths[@]}; do '
  199. echo 'if [[ ${paths[i]} == "'\'\'"${PYENV_ROOT}/shims"\'\''" ]]; then unset '\'\\\'\''paths[i]'\'\\\'\''; '
  200. echo 'fi; done; '
  201. echo 'echo "${paths[*]}"'\'')"'
  202. print_path_prepend_shims
  203. ;;
  204. esac
  205. fi
  206. }
  207. function print_path_prepend_shims() {
  208. case "$shell" in
  209. fish )
  210. echo 'set -gx PATH '\'"${PYENV_ROOT}/shims"\'' $PATH'
  211. ;;
  212. * )
  213. echo 'export PATH="'"${PYENV_ROOT}"'/shims:${PATH}"'
  214. ;;
  215. esac
  216. }
  217. function print_env() {
  218. case "$shell" in
  219. fish )
  220. echo "set -gx PYENV_SHELL $shell"
  221. ;;
  222. * )
  223. echo "export PYENV_SHELL=$shell"
  224. ;;
  225. esac
  226. }
  227. function print_completion() {
  228. completion="${root}/completions/pyenv.${shell}"
  229. if [ -r "$completion" ]; then
  230. echo "source '$completion'"
  231. fi
  232. }
  233. function print_rehash() {
  234. if [ -z "$no_rehash" ]; then
  235. echo 'command pyenv rehash 2>/dev/null'
  236. fi
  237. }
  238. function print_shell_function() {
  239. commands=(`pyenv-commands --sh`)
  240. case "$shell" in
  241. fish )
  242. cat <<EOS
  243. function pyenv
  244. set command \$argv[1]
  245. set -e argv[1]
  246. switch "\$command"
  247. case ${commands[*]}
  248. source (pyenv "sh-\$command" \$argv|psub)
  249. case '*'
  250. command pyenv "\$command" \$argv
  251. end
  252. end
  253. EOS
  254. ;;
  255. ksh | ksh93 | mksh )
  256. cat <<EOS
  257. function pyenv {
  258. typeset command
  259. EOS
  260. ;;
  261. * )
  262. cat <<EOS
  263. pyenv() {
  264. local command
  265. EOS
  266. ;;
  267. esac
  268. if [ "$shell" != "fish" ]; then
  269. IFS="|"
  270. cat <<EOS
  271. command="\${1:-}"
  272. if [ "\$#" -gt 0 ]; then
  273. shift
  274. fi
  275. case "\$command" in
  276. ${commands[*]:-/})
  277. eval "\$(pyenv "sh-\$command" "\$@")"
  278. ;;
  279. *)
  280. command pyenv "\$command" "\$@"
  281. ;;
  282. esac
  283. }
  284. EOS
  285. fi
  286. }
  287. main