2008-08-21 18:18:58 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""\
|
2009-09-23 10:07:50 +08:00
|
|
|
py.lookup [search_directory] SEARCH_STRING [options]
|
2008-08-21 18:18:58 +08:00
|
|
|
|
|
|
|
Looks recursively at Python files for a SEARCH_STRING, starting from the
|
|
|
|
present working directory. Prints the line, with the filename and line-number
|
|
|
|
prepended."""
|
|
|
|
|
|
|
|
import sys, os
|
|
|
|
import py
|
2008-08-21 21:12:31 +08:00
|
|
|
from py.__.io.terminalwriter import ansi_print, terminal_width
|
2008-08-21 18:18:58 +08:00
|
|
|
import re
|
|
|
|
|
|
|
|
def rec(p):
|
|
|
|
return p.check(dotfile=0)
|
|
|
|
|
2009-08-28 03:12:55 +08:00
|
|
|
parser = py.std.optparse.OptionParser(usage=__doc__)
|
2008-08-21 18:18:58 +08:00
|
|
|
parser.add_option("-i", "--ignore-case", action="store_true", dest="ignorecase",
|
|
|
|
help="ignore case distinctions")
|
|
|
|
parser.add_option("-C", "--context", action="store", type="int", dest="context",
|
|
|
|
default=0, help="How many lines of output to show")
|
|
|
|
|
|
|
|
def find_indexes(search_line, string):
|
|
|
|
indexes = []
|
|
|
|
before = 0
|
|
|
|
while 1:
|
|
|
|
i = search_line.find(string, before)
|
|
|
|
if i == -1:
|
|
|
|
break
|
|
|
|
indexes.append(i)
|
|
|
|
before = i + len(string)
|
|
|
|
return indexes
|
|
|
|
|
|
|
|
def main():
|
|
|
|
(options, args) = parser.parse_args()
|
2009-09-23 10:04:25 +08:00
|
|
|
if len(args) == 2:
|
|
|
|
search_dir, string = args
|
|
|
|
search_dir = py.path.local(search_dir)
|
|
|
|
else:
|
|
|
|
search_dir = py.path.local()
|
|
|
|
string = args[0]
|
2008-08-21 18:18:58 +08:00
|
|
|
if options.ignorecase:
|
|
|
|
string = string.lower()
|
2009-09-23 10:04:25 +08:00
|
|
|
for x in search_dir.visit('*.py', rec):
|
2008-08-21 22:25:24 +08:00
|
|
|
# match filename directly
|
2009-09-23 10:04:25 +08:00
|
|
|
s = x.relto(search_dir)
|
2008-08-21 22:25:24 +08:00
|
|
|
if options.ignorecase:
|
|
|
|
s = s.lower()
|
|
|
|
if s.find(string) != -1:
|
2009-08-30 02:04:48 +08:00
|
|
|
sys.stdout.write("%s: filename matches %r" %(x, string) + "\n")
|
2008-08-21 22:25:24 +08:00
|
|
|
|
2008-08-21 18:18:58 +08:00
|
|
|
try:
|
|
|
|
s = x.read()
|
|
|
|
except py.error.ENOENT:
|
|
|
|
pass # whatever, probably broken link (ie emacs lock)
|
|
|
|
searchs = s
|
|
|
|
if options.ignorecase:
|
|
|
|
searchs = s.lower()
|
|
|
|
if s.find(string) != -1:
|
|
|
|
lines = s.splitlines()
|
|
|
|
if options.ignorecase:
|
|
|
|
searchlines = s.lower().splitlines()
|
|
|
|
else:
|
|
|
|
searchlines = lines
|
|
|
|
for i, (line, searchline) in enumerate(zip(lines, searchlines)):
|
|
|
|
indexes = find_indexes(searchline, string)
|
|
|
|
if not indexes:
|
|
|
|
continue
|
|
|
|
if not options.context:
|
2009-09-23 10:04:25 +08:00
|
|
|
sys.stdout.write("%s:%d: " %(x.relto(search_dir), i+1))
|
2008-08-21 18:18:58 +08:00
|
|
|
last_index = 0
|
|
|
|
for index in indexes:
|
|
|
|
sys.stdout.write(line[last_index: index])
|
|
|
|
ansi_print(line[index: index+len(string)],
|
|
|
|
file=sys.stdout, esc=31, newline=False)
|
|
|
|
last_index = index + len(string)
|
|
|
|
sys.stdout.write(line[last_index:] + "\n")
|
|
|
|
else:
|
|
|
|
context = (options.context)/2
|
|
|
|
for count in range(max(0, i-context), min(len(lines) - 1, i+context+1)):
|
2009-09-23 10:04:25 +08:00
|
|
|
print("%s:%d: %s" %(x.relto(search_dir), count+1, lines[count].rstrip()))
|
2009-08-30 02:04:48 +08:00
|
|
|
print("-" * terminal_width)
|