Debian Codegraph @

libexec/pyenv-init

raw symbols SCIP
1 #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash
2 # Summary: Configure the shell environment for pyenv
3 # Usage: eval "$(pyenv init [-|--path] [--no-push-path] [--no-rehash] [<shell>])"
4 # pyenv init --install [<shell>]
5 # pyenv init --detect-shell [<shell>]
6
7 set -e
8 [scip-shell system . . `[`. -n "$PYENV_DEBUG" ] && set -x
9
10 # Provide pyenv completions
11 if [scip-shell system . . `[`. "$1" = "--complete" ]; then
12 echoscip-shell system . . echo. -
13 echoscip-shell system . . echo. --path
14 echoscip-shell system . . echo. --install
15 echoscip-shell system . . echo. --no-push-path
16 echoscip-shell system . . echo. --no-rehash
17 echoscip-shell system . . echo. --detect-shell
18 echoscip-shell system . . echo. bash
19 echoscip-shell system . . echo. fish
20 echoscip-shell system . . echo. ksh
21 echoscip-shell system . . echo. pwsh
22 echoscip-shell system . . echo. zsh
23 exit
24 fi
25
26 mode="help"
27
28 while [scip-shell system . . `[`. "$#" -gt 0 ]; do
29 case "$1" in
30 -)
31 mode="print"
32 ;;
33 --path)
34 mode="path"
35 ;;
36 --install)
37 mode="install"
38 ;;
39 --detect-shell)
40 mode="detect-shell"
41 ;;
42 --no-push-path)
43 no_push_path=1
44 ;;
45 --no-rehash)
46 no_rehash=1
47 ;;
48 *)
49 shell="$1"
50 ;;
51 esac
52 shift
53 done
54
55 # If shell is not provided, detect it.
56 if [scip-shell system . . `[`. -z "$shell" ]; then
57 if shell=$(trscip-shell system . . tr. '\0' ' ' 2>/dev/nullscip-shell filesystem . . `/dev/null`. </proc/"$PPID"/cmdline);then
58 :
59 else
60 shell=$(psscip-shell system . . ps. p "$PPID" -o 'args=' 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.)
61 fi
62 shell="${shell%% *}"
63 shell="${shell##-}"
64 shell="${shell:-$SHELL}"
65 shell="${shell##*/}"
66 shell="${shell%%-*}"
67 fi
68
69 function mainscip-shell shell shell-project 0.0.0 main().() {
70 case "$mode" in
71 "help")
72 help_scip-shell shell shell-project 0.0.0 help_().
73 exit 1
74 ;;
75 "path")
76 print_pathscip-shell shell shell-project 0.0.0 print_path().
77 print_rehashscip-shell shell shell-project 0.0.0 print_rehash().
78 exit 0
79 ;;
80 "print")
81 init_dirsscip-shell shell shell-project 0.0.0 init_dirs().
82 print_pathscip-shell shell shell-project 0.0.0 print_path().
83 print_envscip-shell shell shell-project 0.0.0 print_env().
84 print_completionscip-shell shell shell-project 0.0.0 print_completion().
85 print_rehashscip-shell shell shell-project 0.0.0 print_rehash().
86 print_shell_functionscip-shell shell shell-project 0.0.0 print_shell_function().
87 exit 0
88 ;;
89 "detect-shell")
90 detect_profilescip-shell shell shell-project 0.0.0 detect_profile().
91 print_detect_shellscip-shell shell shell-project 0.0.0 print_detect_shell().
92 exit 0
93 ;;
94 "install")
95 install_shell_startup_filesscip-shell shell shell-project 0.0.0 install_shell_startup_files().
96 exit 0
97 ;;
98 esac
99 # should never get here
100 exit 2
101 }
102
103 function detect_profilescip-shell shell shell-project 0.0.0 detect_profile().() {
104 case "$shell" in
105 bash )
106 if [scip-shell system . . `[`. -e "${HOME}/.bash_profile" ]; then
107 profile='~/.bash_profile'
108 else
109 profile='~/.profile'
110 fi
111 profile_explain="~/.bash_profile if it exists, otherwise ~/.profile"
112 rc='~/.bashrc'
113 ;;
114 fish )
115 profile='~/.config/fish/config.fish'
116 rc='~/.config/fish/config.fish'
117 ;;
118 pwsh )
119 profile='~/.config/powershell/profile.ps1'
120 rc='~/.config/powershell/profile.ps1'
121 ;;
122 zsh )
123 profile='~/.zprofile'
124 rc='~/.zshrc'
125 ;;
126 ksh | ksh93 | mksh )
127 # There are two implementations of Korn shell: AT&T (ksh93) and Mir (mksh).
128 # Systems may have them installed under those names, or as ksh, so those
129 # are recognized here. The obsolete ksh88 (subsumed by ksh93) and pdksh
130 # (subsumed by mksh) are not included, since they are unlikely to still
131 # be in use as interactive shells anywhere.
132 profile='~/.profile'
133 rc='~/.profile'
134 ;;
135 * )
136 profile=
137 rc=
138 profile_explain='your shell'\''s login startup file'
139 rc_explain='your shell'\''s interactive startup file'
140 ;;
141 esac
142 }
143
144 function print_detect_shellscip-shell shell shell-project 0.0.0 print_detect_shell().() {
145 echoscip-shell system . . echo. "PYENV_SHELL_DETECT=$shell"
146 echoscip-shell system . . echo. "PYENV_PROFILE_DETECT=$profile"
147 echoscip-shell system . . echo. "PYENV_RC_DETECT=$rc"
148 }
149
150 function help_scip-shell shell shell-project 0.0.0 help_().() {
151 detect_profilescip-shell shell shell-project 0.0.0 detect_profile().
152 {
153 case "$shell" in
154 fish )
155 echoscip-shell system . . echo. "# Add pyenv executable to PATH by running"
156 echoscip-shell system . . echo. "# the following interactively:"
157 echoscip-shell system . . echo.
158 print_fish_user_path_setupscip-shell shell shell-project 0.0.0 print_fish_user_path_setup().
159 echoscip-shell system . . echo.
160 echoscip-shell system . . echo. "# Load pyenv automatically by appending"
161 echoscip-shell system . . echo. "# the following to ~/.config/fish/config.fish:"
162 echoscip-shell system . . echo.
163 print_fish_shell_setupscip-shell shell shell-project 0.0.0 print_fish_shell_setup().
164 echoscip-shell system . . echo.
165 ;;
166 pwsh )
167 echoscip-shell system . . echo. '# Load pyenv automatically by appending'
168 echoscip-shell system . . echo. "# the following to $profile :"
169 echoscip-shell system . . echo.
170 print_pwsh_shell_setupscip-shell shell shell-project 0.0.0 print_pwsh_shell_setup().
171 ;;
172 * )
173 echoscip-shell system . . echo. '# Load pyenv automatically by appending'
174 echoscip-shell system . . echo. -n "# the following to "
175 if [[ "$profile" == "$rc" && -z $rc_explain ]]; then
176 echoscip-shell system . . echo. "${profile_explain:-$profile} :"
177 else
178 echoscip-shell system . . echo.
179 echoscip-shell system . . echo. "# ${profile_explain:-$profile} (for login shells)"
180 echoscip-shell system . . echo. "# and ${rc_explain:-$rc} (for interactive shells) :"
181 fi
182 echoscip-shell system . . echo.
183 print_posix_shell_setupscip-shell shell shell-project 0.0.0 print_posix_shell_setup().
184 ;;
185 esac
186 echoscip-shell system . . echo.
187 echoscip-shell system . . echo. '# Restart your shell for the changes to take effect.'
188 echoscip-shell system . . echo.
189 } >&2
190 }
191
192 function print_posix_shell_setupscip-shell shell shell-project 0.0.0 print_posix_shell_setup().() {
193 echoscip-shell system . . echo. 'export PYENV_ROOT="$HOME/.pyenv"'
194 echoscip-shell system . . echo. '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"'
195 echoscip-shell system . . echo. 'eval "$(pyenv init - '$shell')"'
196 }
197
198 function print_fish_shell_setupscip-shell shell shell-project 0.0.0 print_fish_shell_setup().() {
199 echoscip-shell system . . echo. 'pyenv init - fish | source'
200 }
201
202 function print_fish_user_path_setupscip-shell shell shell-project 0.0.0 print_fish_user_path_setup().() {
203 echoscip-shell system . . echo. 'set -Ux PYENV_ROOT $HOME/.pyenv'
204 echoscip-shell system . . echo. 'if functions -q fish_add_path'
205 echoscip-shell system . . echo. ' test -d $PYENV_ROOT/bin; and fish_add_path $PYENV_ROOT/bin'
206 echoscip-shell system . . echo. 'else'
207 echoscip-shell system . . echo. ' test -d $PYENV_ROOT/bin; and set -U fish_user_paths $PYENV_ROOT/bin $fish_user_paths'
208 echoscip-shell system . . echo. 'end'
209 }
210
211 function print_pwsh_shell_setupscip-shell shell shell-project 0.0.0 print_pwsh_shell_setup().() {
212 echoscip-shell system . . echo. '$Env:PYENV_ROOT="$Env:HOME/.pyenv"'
213 echoscip-shell system . . echo. 'if (Test-Path -LP "$Env:PYENV_ROOT/bin" -PathType Container) {'
214 echoscip-shell system . . echo. ' $Env:PATH="$Env:PYENV_ROOT/bin:$Env:PATH" }'
215 echoscip-shell system . . echo. 'iex ((pyenv init -) -join "`n")'
216 }
217
218 function expand_home_pathscip-shell shell shell-project 0.0.0 expand_home_path().() {
219 local path="$1"
220 printfscip-shell system . . printf. '%s\n' "${path/#\~/$HOME}"
221 }
222
223 function install_shell_startup_filesscip-shell shell shell-project 0.0.0 install_shell_startup_files().() {
224 if [[ -z $HOME ]]; then
225 echoscip-shell system . . echo. "pyenv: HOME must be set to configure shell startup files" >&2
226 return 1
227 fi
228
229 detect_profilescip-shell shell shell-project 0.0.0 detect_profile().
230
231 local files=()
232 local lines=()
233 local profile_path rc_path setup
234
235 case "$shell" in
236 bash | zsh | ksh | ksh93 | mksh )
237 rc_path="$(expand_home_pathscip-shell shell shell-project 0.0.0 expand_home_path(). "$rc")"
238 profile_path="$(expand_home_pathscip-shell shell shell-project 0.0.0 expand_home_path(). "$profile")"
239 setup="$(print_posix_shell_setupscip-shell shell shell-project 0.0.0 print_posix_shell_setup().)"
240 files=("$rc_path")
241 lines=("$setup")
242 if [[ $profile_path != "$rc_path" ]]; then
243 files+=("$profile_path")
244 lines+=("$setup")
245 fi
246 ;;
247 fish )
248 rc_path="$(expand_home_pathscip-shell shell shell-project 0.0.0 expand_home_path(). "$rc")"
249 files=("$rc_path")
250 lines=("$(print_fish_shell_setupscip-shell shell shell-project 0.0.0 print_fish_shell_setup().)")
251 ;;
252 pwsh )
253 rc_path="$(expand_home_pathscip-shell shell shell-project 0.0.0 expand_home_path(). "$rc")"
254 files=("$rc_path")
255 lines=("$(print_pwsh_shell_setupscip-shell shell shell-project 0.0.0 print_pwsh_shell_setup().)")
256 ;;
257 * )
258 echoscip-shell system . . echo. "pyenv: cannot automatically configure startup files for $shell" >&2
259 return 1
260 ;;
261 esac
262
263 local index
264 for ((index = 0; index < ${#files[@]}; index++)); do
265 check_startup_file "${files[$index]}" || return 1
266 done
267
268 if [[ $shell == fish ]]; then
269 install_fish_user_pathsscip-shell shell shell-project 0.0.0 install_fish_user_paths(). || return 1
270 fi
271
272 for ((index = 0; index < ${#files[@]}; index++)); do
273 append_lines "${files[$index]}" "${lines[$index]}"
274 done
275 }
276
277 function check_startup_filescip-shell shell shell-project 0.0.0 check_startup_file().() {
278 local file="$1"
279 local grep_status
280
281 if [[ ! -e $file ]]; then
282 return 0
283 fi
284
285 if [[ ! -f $file || ! -r $file ]]; then
286 echoscip-shell system . . echo. "pyenv: failed to inspect $file" >&2
287 return 1
288 fi
289
290 if grepscip-shell system . . grep. -Fi pyenv "$file" >/dev/nullscip-shell filesystem . . `/dev/null`.; then
291 echoscip-shell system . . echo. "pyenv: cannot automatically apply changes to $file: it appears to already contain Pyenv-related code." >&2
292 echoscip-shell system . . echo. "pyenv: review the file's contents and apply changes manually if necessary." >&2
293 echoscip-shell system . . echo. "pyenv: run \`pyenv init $shell\` to see the suggested setup." >&2
294 return 1
295 else
296 grep_status=$?
297 if [[ $grep_status == 1 ]]; then
298 return 0
299 fi
300 echoscip-shell system . . echo. "pyenv: failed to inspect $file" >&2
301 return "$grep_status"
302 fi
303 }
304
305 function install_fish_user_pathsscip-shell shell shell-project 0.0.0 install_fish_user_paths().() {
306 local fish_setup
307
308 if ! command -v fish >/dev/nullscip-shell filesystem . . `/dev/null`.; then
309 echoscip-shell system . . echo. "pyenv: fish is not available to configure fish universal variables" >&2
310 return 1
311 fi
312
313 fish_setup="$(print_fish_user_path_setupscip-shell shell shell-project 0.0.0 print_fish_user_path_setup().)"
314 if ! fish -c "$fish_setup"; then
315 echoscip-shell system . . echo. "pyenv: failed to configure fish universal variables" >&2
316 return 1
317 fi
318 }
319
320 function append_linesscip-shell shell shell-project 0.0.0 append_lines().() {
321 local file="$1"
322 local lines="$2"
323 local dir last_char
324
325 dir="${file%/*}"
326 if [[ $dir != "$file" ]]; then
327 mkdirscip-shell system . . mkdir. -p "$dir"
328 fi
329
330 if [[ -s $file ]]; then
331 last_char="$(tailscip-shell system . . tail. -c 1 "$file")" || return 1
332 if [[ -n $last_char ]]; then
333 echoscip-shell system . . echo. >> "$file"
334 fi
335 fi
336
337 printfscip-shell system . . printf. '%s\n' "$lines" >> "$file"
338 }
339
340 function init_dirsscip-shell shell shell-project 0.0.0 init_dirs().() {
341 mkdirscip-shell system . . mkdir. -p "${PYENV_ROOT}/"{shims,versions}
342 }
343
344 function print_pathscip-shell shell shell-project 0.0.0 print_path().() {
345 # if no_push_path is set, guard the PATH manipulation with a check on whether
346 # the shim is already in the PATH.
347 if [scip-shell system . . `[`. -n "$no_push_path" ]; then
348 case "$shell" in
349 fish )
350 echoscip-shell system . . echo. 'if not contains -- "'"${PYENV_ROOT}/shims"'" $PATH'
351 print_path_prepend_shimsscip-shell shell shell-project 0.0.0 print_path_prepend_shims().
352 echoscip-shell system . . echo. 'end'
353 ;;
354 pwsh )
355 echoscip-shell system . . echo. 'if ( $Env:PATH -notmatch "'"${PYENV_ROOT}/shims"'" ) {'
356 print_path_prepend_shimsscip-shell shell shell-project 0.0.0 print_path_prepend_shims().
357 echoscip-shell system . . echo. '}'
358 ;;
359 * )
360 echoscip-shell system . . echo. 'if [[ ":$PATH:" != *'\':"${PYENV_ROOT}"/shims:\''* ]]; then'
361 print_path_prepend_shimsscip-shell shell shell-project 0.0.0 print_path_prepend_shims().
362 echoscip-shell system . . echo. 'fi'
363 ;;
364 esac
365 else
366 case "$shell" in
367 fish )
368 echoscip-shell system . . echo. 'while set pyenv_index (contains -i -- "'"${PYENV_ROOT}/shims"'" $PATH)'
369 echoscip-shell system . . echo. 'set -eg PATH[$pyenv_index]; end; set -e pyenv_index'
370 print_path_prepend_shimsscip-shell shell shell-project 0.0.0 print_path_prepend_shims().
371 ;;
372 pwsh )
373 echoscip-shell system . . echo. '$Env:PATH="$(($Env:PATH -split '"':'"' | where { -not ($_ -match '"'${PYENV_ROOT}/shims'"') }) -join '"':'"')"'
374 print_path_prepend_shimsscip-shell shell shell-project 0.0.0 print_path_prepend_shims().
375 ;;
376 * )
377 # Some distros (notably Debian-based) set Bash's SSH_SOURCE_BASHRC compilation option
378 # that makes it source `bashrc` under SSH even when not interactive.
379 # This is inhibited by a guard in Debian's stock `bashrc` but some people remove it
380 # in order to get proper environment for noninteractive remote commands
381 # (SSH provides /etc/ssh/sshrc and ~/.ssh/rc for that but no-one seems to use them for some reason).
382 # This has caused an infinite `bashrc` execution loop for those people in the below nested Bash invocation (#2367).
383 # --norc negates this behavior of such a customized Bash.
384 echoscip-shell system . . echo. 'PATH="$(bash --norc -ec '\''IFS=:; paths=($PATH); '
385 echoscip-shell system . . echo. 'for i in ${!paths[@]}; do '
386 echoscip-shell system . . echo. 'if [[ ${paths[i]} == "'\'\'"${PYENV_ROOT}/shims"\'\''" ]]; then unset '\'\\\'\''paths[i]'\'\\\'\''; '
387 echoscip-shell system . . echo. 'fi; done; '
388 echoscip-shell system . . echo. 'echo "${paths[*]}"'\'')"'
389 print_path_prepend_shimsscip-shell shell shell-project 0.0.0 print_path_prepend_shims().
390 ;;
391 esac
392 fi
393 }
394
395 function print_path_prepend_shimsscip-shell shell shell-project 0.0.0 print_path_prepend_shims().() {
396 case "$shell" in
397 fish )
398 echoscip-shell system . . echo. 'set -gx PATH '\'"${PYENV_ROOT}/shims"\'' $PATH'
399 ;;
400 pwsh )
401 echoscip-shell system . . echo. '$Env:PATH="'"${PYENV_ROOT}"'/shims:$Env:PATH"'
402 ;;
403 * )
404 echoscip-shell system . . echo. 'export PATH="'"${PYENV_ROOT}"'/shims:${PATH}"'
405 ;;
406 esac
407 }
408
409 function print_envscip-shell shell shell-project 0.0.0 print_env().() {
410 case "$shell" in
411 fish )
412 echoscip-shell system . . echo. "set -gx PYENV_SHELL $shell"
413 ;;
414 pwsh )
415 echoscip-shell system . . echo. '$Env:PYENV_SHELL="'"$shell"'"'
416 ;;
417 * )
418 echoscip-shell system . . echo. "export PYENV_SHELL=$shell"
419 ;;
420 esac
421 }
422
423 function print_completionscip-shell shell shell-project 0.0.0 print_completion().() {
424 completion="${_PYENV_INSTALL_PREFIX}/completions/pyenv.${shell}"
425 if [scip-shell system . . `[`. -r "$completion" ]; then
426 case "$shell" in
427 pwsh )
428 echoscip-shell system . . echo. "iex (gc $completion -Raw)"
429 ;;
430 * )
431 echoscip-shell system . . echo. "source '$completion'"
432 ;;
433 esac
434 fi
435 }
436
437 function print_rehashscip-shell shell shell-project 0.0.0 print_rehash().() {
438 if [scip-shell system . . `[`. -z "$no_rehash" ]; then
439 case "$shell" in
440 pwsh )
441 echoscip-shell system . . echo. '& pyenv rehash'
442 ;;
443 * )
444 echoscip-shell system . . echo. 'command pyenv rehash'
445 ;;
446 esac
447 fi
448 }
449
450 function print_shell_functionscip-shell shell shell-project 0.0.0 print_shell_function().() {
451 commands=(`pyenv-commands --sh`)
452 case "$shell" in
453 fish )
454 echoscip-shell system . . echo. \
455 'function pyenv
456 set command $argv[1]
457 set -e argv[1]
458
459 switch "$command"
460 case '"${commands[*]}"'
461 source (pyenv "sh-$command" $argv|psub)
462 case "*"
463 command pyenv "$command" $argv
464 end
465 end'
466 ;;
467 pwsh )
468 catscip-shell system . . cat. <<EOS
469 function pyenv {
470 \$command=""
471 if ( \$args.Count -gt 0 ) {
472 \$command, \$args = \$args
473 }
474
475 if ( ("${commands[*]}" -split ' ') -contains \$command ) {
476 \$shell_cmds = (& (get-command -commandtype application pyenv) sh-\$command \$args)
477 if ( \$shell_cmds.Count -gt 0 ) {
478 iex (\$shell_cmds -join "\`n")
479 }
480 } else {
481 & (get-command -commandtype application pyenv) \$command \$args
482 }
483 }
484 EOS
485 ;;
486 ksh | ksh93 | mksh )
487 echoscip-shell system . . echo. \
488 'function pyenv {
489 typeset command=${1:-}'
490 ;;
491 * )
492 echoscip-shell system . . echo. \
493 'pyenv() {
494 local command=${1:-}'
495 ;;
496 esac
497
498 if [scip-shell system . . `[`. "$shell" != "fish" ] && [scip-shell system . . `[`. "$shell" != "pwsh" ]; then
499 IFS="|"
500 echoscip-shell system . . echo. \
501 ' [ "$#" -gt 0 ] && shift
502 case "$command" in
503 '"${commands[*]:-/}"')
504 eval "$(pyenv "sh-$command" "$@")"
505 ;;
506 *)
507 command pyenv "$command" "$@"
508 ;;
509 esac
510 }'
511 fi
512 }
513
514 mainscip-shell shell shell-project 0.0.0 main().