python.plugin.zsh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # set python command if 'py' not installed
  2. builtin which py > /dev/null || alias py='python3'
  3. # Find python file
  4. alias pyfind='find . -name "*.py"'
  5. # Remove python compiled byte-code and mypy/pytest cache in either the current
  6. # directory or in a list of specified directories (including sub directories).
  7. function pyclean() {
  8. find "${@:-.}" -type f -name "*.py[co]" -delete
  9. find "${@:-.}" -type d -name "__pycache__" -delete
  10. find "${@:-.}" -depth -type d -name ".mypy_cache" -exec rm -r "{}" +
  11. find "${@:-.}" -depth -type d -name ".pytest_cache" -exec rm -r "{}" +
  12. }
  13. # Add the user installed site-packages paths to PYTHONPATH, only if the
  14. # directory exists. Also preserve the current PYTHONPATH value.
  15. # Feel free to autorun this when .zshrc loads.
  16. function pyuserpaths() {
  17. setopt localoptions extendedglob
  18. # Check for a non-standard install directory.
  19. local user_base="${PYTHONUSERBASE:-"${HOME}/.local"}"
  20. local python version site_pkgs
  21. for python in python2 python3; do
  22. # Check if command exists
  23. (( ${+commands[$python]} )) || continue
  24. # Get minor release version.
  25. # The patch version is variable length, truncate it.
  26. version=${(M)${"$($python -V 2>&1)":7}#[^.]##.[^.]##}
  27. # Add version specific path, if:
  28. # - it exists in the filesystem
  29. # - it isn't in $PYTHONPATH already.
  30. site_pkgs="${user_base}/lib/python${version}/site-packages"
  31. [[ -d "$site_pkgs" && ! "$PYTHONPATH" =~ (^|:)"$site_pkgs"(:|$) ]] || continue
  32. export PYTHONPATH="${site_pkgs}${PYTHONPATH+":${PYTHONPATH}"}"
  33. done
  34. }
  35. # Grep among .py files
  36. alias pygrep='grep -nr --include="*.py"'
  37. # Share local directory as a HTTP server
  38. alias pyserver="python3 -m http.server"
  39. ## venv settings
  40. : ${PYTHON_VENV_NAME:=venv}
  41. # Array of possible virtual environment names to look for, in order
  42. # -U for removing duplicates
  43. typeset -gaU PYTHON_VENV_NAMES
  44. [[ -n "$PYTHON_VENV_NAMES" ]] || PYTHON_VENV_NAMES=($PYTHON_VENV_NAME venv .venv)
  45. # Activate a the python virtual environment specified.
  46. # If none specified, use the first existing in $PYTHON_VENV_NAMES.
  47. function vrun() {
  48. if [[ -z "$1" ]]; then
  49. local name
  50. for name in $PYTHON_VENV_NAMES; do
  51. local venvpath="${name:P}"
  52. if [[ -d "$venvpath" ]]; then
  53. vrun "$name"
  54. return $?
  55. fi
  56. done
  57. echo >&2 "Error: no virtual environment found in current directory"
  58. fi
  59. local name="${1:-$PYTHON_VENV_NAME}"
  60. local venvpath="${name:P}"
  61. if [[ ! -d "$venvpath" ]]; then
  62. echo >&2 "Error: no such venv in current directory: $name"
  63. return 1
  64. fi
  65. if [[ ! -f "${venvpath}/bin/activate" ]]; then
  66. echo >&2 "Error: '${name}' is not a proper virtual environment"
  67. return 1
  68. fi
  69. . "${venvpath}/bin/activate" || return $?
  70. echo "Activated virtual environment ${name}"
  71. }
  72. # Create a new virtual environment using the specified name.
  73. # If none specified, use $PYTHON_VENV_NAME
  74. function mkv() {
  75. local name="${1:-$PYTHON_VENV_NAME}"
  76. local venvpath="${name:P}"
  77. python3 -m venv "${name}" || return
  78. echo >&2 "Created venv in '${venvpath}'"
  79. vrun "${name}"
  80. }
  81. if [[ "$PYTHON_AUTO_VRUN" == "true" ]]; then
  82. # Automatically activate venv when changing dir
  83. function auto_vrun() {
  84. # deactivate if we're on a different dir than VIRTUAL_ENV states
  85. # we don't deactivate subdirectories!
  86. if (( $+functions[deactivate] )) && [[ $PWD != ${VIRTUAL_ENV:h}* ]]; then
  87. deactivate > /dev/null 2>&1
  88. fi
  89. if [[ $PWD != ${VIRTUAL_ENV:h} ]]; then
  90. local file
  91. for file in "${^PYTHON_VENV_NAMES[@]}"/bin/activate(N.); do
  92. # make sure we're not in a venv already
  93. (( $+functions[deactivate] )) && deactivate > /dev/null 2>&1
  94. source $file > /dev/null 2>&1
  95. break
  96. done
  97. fi
  98. }
  99. add-zsh-hook chpwd auto_vrun
  100. auto_vrun
  101. fi