Skip to content

Instantly share code, notes, and snippets.

@agroszer
Forked from mgedmin/buildout-tags.sh
Created October 13, 2015 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agroszer/7489546232bac9927c69 to your computer and use it in GitHub Desktop.
Save agroszer/7489546232bac9927c69 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=
extra_path=
for arg; do
case "$arg" in
-h|--help)
echo "Usage: $progname [-v|--verbose] [-n|--dry-run] [-s|--system-python] [-e=extra_path] [buildout-generated-script-filename]"
exit 0
;;
-v|--verbose)
verbose=1
;;
-n|--dry-run)
dry_run=1
;;
-s|--system-python)
system_python=1
;;
-e=*)
extra_path="${arg#*=}"
shift
;;
-*)
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"
test $verbose -ne 0 && echo "$extra_path"
if [ $dry_run -eq 0 ]; then
ctags-exuberant -R -f .tags.new $paths $extra_path && 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