Skip to content

Instantly share code, notes, and snippets.

@mgedmin
Last active December 14, 2015 21:38
Show Gist options
  • Save mgedmin/5152189 to your computer and use it in GitHub Desktop.
Save mgedmin/5152189 to your computer and use it in GitHub Desktop.
Create ctags for the current virtualend or buildout
#!/bin/bash
# Build a vim tags file for all the Python packages in the buildout path
# Assumes . is the directory with a buildout.cfg
# Note: there is no one true buildout path -- every part has its own. So
# we look for a python interpreter in bin/python (or bin/py) and use its path.
# If that fails, we try to find a bin/test script and scrape the path from it.
# Should also work with virtualenv (just run it somewhere with a bin/python)
# Published at https://gist.github.com/mgedmin/5152189
progname=${0##*/}
die() {
echo "$progname: $@" 1>&2
exit 1
}
verbose=0
dry_run=0
system_python=0
scriptname=
for arg; do
case "$arg" in
-h|--help)
echo "Usage: $progname [-v|--verbose] [-n|--dry-run] [-s|--system-python] [buildout-generated-script-filename]"
exit 0
;;
-v|--verbose)
verbose=1
;;
-n|--dry-run)
dry_run=1
;;
-s|--system-python)
system_python=1
;;
-*)
die "Unexpected argument: $arg"
;;
*)
if [ -z "$scriptname" ]; then
scriptname=$arg
else
die "Unexpected argument: $arg"
fi
;;
esac
done
extra_work=
if [ -n "$scriptname" ] && [ -x "$scriptname" ]; then
shebang=$(head -n 1 "$scriptname")
python=${shebang:2}
extra_work="exec(open('$scriptname').read(), {});"
elif [ $system_python -ne 0 ]; then
python=python
elif [ -x bin/python ]; then
python=bin/python
elif [ -x bin/py ]; then
# some zope.foo buildouts use interpreter = bin/py
python=bin/py
elif [ -x .venv/bin/python ]; then
python=.venv/bin/python
elif [ -x .env/bin/python ]; then
python=.env/bin/python
elif [ -x env/bin/python ]; then
python=env/bin/python
elif [ -x bin/test ]; then
shebang=$(head -n 1 bin/test)
python=${shebang:2}
extra_work="exec(open('bin/test').read(), {});"
else
die "I expect to find a bin/python (or bin/py, or bin/test) here"
fi
paths=$($python -c "
import os, sys
${extra_work}
d = set(p for p in sys.path if os.path.isdir(p) and not os.path.isdir(os.path.join(p, 'dist-packages')))
for p in sys.path:
if os.path.isdir(os.path.join(p, 'dist-packages')):
d.add(os.path.join(p, '*.py'))
for s in os.listdir(p):
if os.path.exists(os.path.join(p, s, '__init__.py')):
d.add(os.path.join(p, s))
print('\\n'.join(sorted(d)))
")
test -n "$paths" || die "The python path is empty? Confused."
test $verbose -ne 0 && echo "$paths"
if [ $dry_run -eq 0 ]; then
ctags-exuberant -R -f .tags.new $paths && mv .tags.new tags || rm .tags.new
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment