Last active
August 29, 2015 13:57
-
-
Save mgedmin/9641425 to your computer and use it in GitHub Desktop.
Grep for a string in all the Python packages in the buildout/virtualenv path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Grep for a string in 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) | |
# Based on buildout-tags, which lives 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= | |
grepargs=() | |
for arg; do | |
case "$arg" in | |
-h|--help) | |
echo "Usage: $progname [-v|--verbose] [-n|--dry-run] [-s|--system-python] [--script=buildout-generated-script-filename] grep arguments" | |
exit 0 | |
;; | |
-v|--verbose) | |
verbose=1 | |
;; | |
-n|--dry-run) | |
dry_run=1 | |
;; | |
-s|--system-python) | |
system_python=1 | |
;; | |
--script=*) | |
scriptname=${arg#--script=} | |
;; | |
*) | |
grepargs+=("$arg") | |
;; | |
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} print('\\n'.join(sorted(set(p for p in sys.path if os.path.isdir(p)))))") | |
test -n "$paths" || die "The python path is empty? Confused." | |
test $verbose -ne 0 && echo "$paths" | |
if [ $dry_run -eq 0 ]; then | |
grepall.sh $paths -- "${grepargs[@]}" | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# A script to make recursive greps easier. Examples: | |
# | |
# grepall foo | |
# | |
# locates 'foo' in all files in the directory tree, recursively starting | |
# from the current directory (but ignoring .svn/.bzr subdirs). | |
# | |
# grepall -name '*.txt' -- foo | |
# | |
# locates 'foo' in all files named '*.txt' in the directory tree. | |
# | |
# Usage: grepall [[dir] [find-args ...] --] [grep-options] pattern | |
# | |
# find-args is spliced into the middle of a complicated find expression, so | |
# it might not do what you want if you attempt something more complicated | |
# than a bunch of and-ed tests. | |
findargs=() | |
grepargs=() | |
for arg in "$@"; do | |
if [ "$arg" = -- ]; then | |
findargs=("${grepargs[@]}") | |
grepargs=() | |
else | |
grepargs[${#grepargs[*]}]="$arg" | |
fi | |
done | |
paths=() | |
set -- "${findargs[@]}" | |
while true; do | |
case "$1" in | |
-*|!|"") | |
break | |
;; | |
*) | |
paths[${#paths[*]}]="$1" | |
shift | |
;; | |
esac | |
done | |
find "${paths[@]}" \ | |
'(' -name '.svn' -o \ | |
-name '.bzr' -o \ | |
-name '.git' -o \ | |
-name '.hg' -o \ | |
-name 'junk' -o \ | |
-name 'build' -o \ | |
-name 'coverage' -o \ | |
-name 'ftest-coverage' -o \ | |
-name 'htmlcov' -o \ | |
-name 'dist' ')' -prune -o \ | |
'(' -type f \ | |
'!' -name '*.min.js' \ | |
'!' -name '*.py[co]' \ | |
'!' -name '*.fs' \ | |
'!' -name 'Data.fs.*' \ | |
'!' -name '*.index' \ | |
'!' -name '*.lock' \ | |
'!' -name '*.tmp' \ | |
'!' -name '*.png' \ | |
'!' -name '*.jpg' \ | |
'!' -name '*.jpeg' \ | |
'!' -name '*.gif' \ | |
'!' -name '*.bmp' \ | |
'!' -name '*.ico' \ | |
'!' -name '*.psd' \ | |
'!' -name '*.ps' \ | |
'!' -name '*.pdf' \ | |
'!' -name '*.pickle' \ | |
'!' -name '*.zip' \ | |
'!' -name '*.gz' \ | |
'!' -name '*.so' \ | |
'!' -name '*.[od]' \ | |
'!' -name '*.sw[po]' \ | |
'!' -name '.#*' \ | |
'!' -name '*~' \ | |
'!' -name '.installed.cfg' \ | |
'!' -name 'ID' \ | |
'!' -iname 'tags' \ | |
"$@" ')' -print0 \ | |
|xargs -0 grep --color=auto -nH "${grepargs[@]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment