python.plugin.zsh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 utilities
  40. : ${PYTHON_VENV_NAME:=venv}
  41. # Activate a the python virtual environment specified.
  42. # If none specified, use $PYTHON_VENV_NAME, else 'venv'.
  43. function vrun() {
  44. local name="${1:-$PYTHON_VENV_NAME}"
  45. local venvpath="${name:P}"
  46. if [[ ! -d "$venvpath" ]]; then
  47. echo >&2 "Error: no such venv in current directory: $name"
  48. return 1
  49. fi
  50. if [[ ! -f "${venvpath}/bin/activate" ]]; then
  51. echo >&2 "Error: '${name}' is not a proper virtual environment"
  52. return 1
  53. fi
  54. . "${venvpath}/bin/activate" || return $?
  55. echo "Activated virtual environment ${name}"
  56. }
  57. # Create a new virtual environment using the specified name.
  58. # If none specified, use $PYTHON_VENV_NAME
  59. function mkv() {
  60. local name="${1:-$PYTHON_VENV_NAME}"
  61. local venvpath="${name:P}"
  62. python3 -m venv "${name}" || return
  63. echo >&2 "Created venv in '${venvpath}'"
  64. vrun "${name}"
  65. }
  66. if [[ "$PYTHON_AUTO_VRUN" == "true" ]]; then
  67. # Automatically activate venv when changing dir
  68. function auto_vrun() {
  69. # deactivate if we're on a different dir than VIRTUAL_ENV states
  70. # we don't deactivate subdirectories!
  71. if (( $+functions[deactivate] )) && [[ $PWD != ${VIRTUAL_ENV:h}* ]]; then
  72. deactivate > /dev/null 2>&1
  73. fi
  74. if [[ $PWD != ${VIRTUAL_ENV:h} ]]; then
  75. for _file in "${PYTHON_VENV_NAME}"*/bin/activate(N.); do
  76. # make sure we're not in a venv already
  77. (( $+functions[deactivate] )) && deactivate > /dev/null 2>&1
  78. source $_file > /dev/null 2>&1
  79. break
  80. done
  81. fi
  82. }
  83. add-zsh-hook chpwd auto_vrun
  84. auto_vrun
  85. fi