Skip to content

Instantly share code, notes, and snippets.

@chmouel
Last active June 4, 2017 10:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chmouel/60a374ba24ebd2e12061 to your computer and use it in GitHub Desktop.
Save chmouel/60a374ba24ebd2e12061 to your computer and use it in GitHub Desktop.
PAN: a zsh tool to switch easily with completion the tox virtualenvs and tests (demo: http://i.imgur.com/kJsJTjj.gif)
# Author: Chmouel Bodujnah <chmouel@chmouel.com>
#
# Pan a zsh tool to complete virtualenv tox targets and tests launched via
# testtools
#
# Demo: http://i.imgur.com/kJsJTjj.gif
#
# When you use zsh just source the file directly and when you are inside a
# project which has been pre-built with tox targets you can start do:
# pan <TAB>
# It will complete you with all the available tox virtualenv targets
# and activate that virtualenv no matter where you are in your project.
#
# If you press an another tab after you specified the environement like this :
# pan py34 <TAB>
# It will run a :
# .tox/py34/bin/python -mtesttools.run discover -l
# and give you the tests to complete and run it in that venv context.
#
# Will do a proper project in itself if it proves to be useful :)
# PS: It's tailored to OpenStack workflow but can be adapted to whatever you want.
function _testtools_tests() {
local _gitdir _target
_gitdir=$(git rev-parse --show-toplevel 2>/dev/null)
_target=${_gitdir}/.tox/${1}/bin/python
[[ -d ${_target} ]] && return 1
pushd ${_gitdir} >/dev/null && \
${_target} -m testtools.run discover -l && \
popd >/dev/null
}
function pan() {
local _gitdir _target
_gitdir=$(git rev-parse --show-toplevel 2>/dev/null)
_target=${_gitdir}/.tox/${1}/bin
[[ -d ${_target} ]] || { echo "${_target} does not exists"; return 1;}
# We already did all the checking in completion
if [[ -n $2 ]];then
${_target}/python -mtesttools.run ${2}
else
source ${_gitdir}/.tox/${1}/bin/activate
fi
}
_pan () {
local _gitdir targets
_gitdir=$(git rev-parse --show-toplevel 2>/dev/null)
[[ -z ${_gitdir} ]] && return 1
[[ -d ${_gitdir}/.tox ]] || { return 1 ;}
targets=()
for x in ${_gitdir}/.tox/*;do
if [[ -d ${x} && -e ${x}/bin/activate ]];then
targets+=(${x##*/})
fi
done
[[ -z ${targets} ]] && return 1
if (( CURRENT == 2 )); then
compadd ${targets}
fi
if (( CURRENT == 3 )); then
compadd $(_testtools_tests ${words[2]})
fi
}
compdef _pan pan
@blueyed
Copy link

blueyed commented Apr 18, 2015

Cool! (I am looking for completion of tox, mainly to provide the list for tox -e and found this).
Is it still maintained and/or do you plan to create a separate project for it?

Instead of using git to find the .tox dir, you can do the following:

local toxroot
toxroot=((../)#.tox(N))
if (( $#toxroot )); then
  ...
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment