Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 115 lines (100 sloc) 3.55 KB
#! /usr/bin/env python
# langgrep: grep for a pattern but only in files written in the
# specified language (as specified by the shebang line).
# Copyright 2009 by Akkana Peck.
# Please share, modify and enjoy under the terms of the GPL v2
# or, at your option, any later GPL version.
# Bugs: it isn't smart about parsing the grep flags.
# Anything beginning with a - will be considered a flag
# and passed on to grep; the first argument not starting with -
# is taken to be the search pattern, and everything after that
# is the file list.
import string, os, sys
import subprocess
import shlex
def Usage():
print("langgrep lang [grepflags] pattern files")
print(" e.g. langgrep python -w find *")
def check_file_lang(filename, lang):
if lang.startswith("python") and filename.endswith(".py"):
return True
if os.path.isdir(filename) or not os.path.exists(filename):
return False
f = open(filename)
firstline = f.readline(80)
except IOError as e:
print("IOError", e)
#print "exc_info is", sys.exc_info()
return False
if firstline[0:2] == "#!" and string.find(firstline, lang) >= 0:
return True
return False
# main()
if len(sys.argv) < 3:
lang = sys.argv[1]
# After the language, any flag argument plus the grep pattern
# gets appended to args -- these will be the grep args.
patindex = 0
for i in range(2, len(sys.argv)):
if sys.argv[i][0] != '-':
patindex = i+1
args = sys.argv[2:patindex]
# Now args are the arguments passed to grep;
# the file list, if any, is sys.argv[patindex+1:]
# If no file list, use ~/bin/*
if patindex >= len(sys.argv):
files = set()
# Look nonrecursively at ~/bin/*
bindir = os.path.join(os.getenv("HOME"), "bin")
for f in os.listdir(bindir):
files.add(os.path.join(bindir, f))
# If language is python, also look in $PYTHONPATH, recursively.
if lang == "python":
pypathdir = os.path.join(bindir, "pythonpath")
if os.path.exists(pypathdir):
for root, dirs, fs in os.walk(pypathdir, followlinks=True):
for f in fs:
if f.endswith(".py"):
files.add(os.path.join(root, f))
files = sys.argv[patindex:]
filelist = list(files)
for fil in filelist:
if check_file_lang(fil, lang):
arglist = ['grep', '-H']
proc = subprocess.Popen(arglist,
shell=False, stdout=subprocess.PIPE)
pout = proc.communicate()[0]
# Go through the output removing all but the last dir of the path.
# Otherwise lines are so long they're confusing.
if not pout:
for line in pout.split('\n'):
# For some reason the split is giving us every other line empty
if not line:
colon = line.find(":")
pathparts = line[:colon].split('/')
if len(pathparts) < 2 or pathparts[-2] == "bin":
path = pathparts[-1]
path = '/'.join(pathparts[-2:])
print("%s%s" % (path, line[colon:]))
# Try to catch ctrl-C and print a nicer message. This doesn't work, though:
# just terminates the whole process.
except KeyboardInterrupt as e: