2016-12-05 01:50:52 +00:00
|
|
|
'''
|
|
|
|
Search for a target string within the lines of files.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
fileswith.py *.py "import random"
|
|
|
|
'''
|
|
|
|
|
2016-11-18 06:24:52 +00:00
|
|
|
import fnmatch
|
|
|
|
import glob
|
|
|
|
import re
|
|
|
|
import sys
|
2016-12-02 06:37:07 +00:00
|
|
|
|
|
|
|
from voussoirkit import spinal
|
2016-11-18 06:24:52 +00:00
|
|
|
|
|
|
|
filepattern = sys.argv[1]
|
|
|
|
searchpattern = sys.argv[2]
|
|
|
|
|
|
|
|
for filename in spinal.walk_generator():
|
|
|
|
filename = filename.absolute_path
|
|
|
|
if not fnmatch.fnmatch(filename, filepattern):
|
|
|
|
continue
|
|
|
|
matches = []
|
|
|
|
handle = open(filename, 'r', encoding='utf-8')
|
|
|
|
try:
|
|
|
|
with handle:
|
|
|
|
for (index, line) in enumerate(handle):
|
|
|
|
if re.search(searchpattern, line, flags=re.IGNORECASE):
|
|
|
|
line = '%d | %s' % (index, line.strip())
|
|
|
|
matches.append(line)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
if matches:
|
|
|
|
print(filename)
|
|
|
|
print('\n'.join(matches))
|
2016-12-05 01:50:52 +00:00
|
|
|
print()
|