Filter grep by project
Let the user specify project(s) to filter the lines being scanned by grep.
This commit is contained in:
parent
9c17b091bc
commit
85dc323561
|
@ -15,6 +15,13 @@ class Grep(Command):
|
|||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(Grep, self).get_parser(prog_name)
|
||||
parser.add_argument(
|
||||
'--project',
|
||||
action='append',
|
||||
default=[],
|
||||
dest='projects',
|
||||
help='projects to limit search',
|
||||
)
|
||||
parser.add_argument('pattern',
|
||||
action='store',
|
||||
help='regular expression',
|
||||
|
@ -24,7 +31,7 @@ class Grep(Command):
|
|||
def take_action(self, parsed_args):
|
||||
session = self.app.get_db_session()
|
||||
pm = project.ProjectManager(session)
|
||||
for r in pm.grep(parsed_args.pattern):
|
||||
for r in pm.grep(parsed_args.pattern, parsed_args.projects):
|
||||
line_num, content, filename, project_name = r
|
||||
print('%s/%s:%s:%s' %
|
||||
(project_name, filename, line_num, content.rstrip())
|
||||
|
|
|
@ -194,7 +194,7 @@ class ProjectManager(object):
|
|||
self._remove_file_data(obj, reason='file no longer exists')
|
||||
self.session.flush()
|
||||
|
||||
def grep(self, pattern):
|
||||
def grep(self, pattern, projects=[]):
|
||||
"""Given a pattern, search for lines in files in all projects that match it.
|
||||
|
||||
Returns results of the query, including the four columns line
|
||||
|
@ -209,5 +209,10 @@ class ProjectManager(object):
|
|||
Line.number, Line.content, File.name, Project.name,
|
||||
).join(File, Project).filter(
|
||||
Line.content.op('regexp')(pattern)
|
||||
).order_by(Project.name, File.name, Line.number)
|
||||
)
|
||||
if projects:
|
||||
query = query.filter(
|
||||
Project.name.in_(projects)
|
||||
)
|
||||
query = query.order_by(Project.name, File.name, Line.number)
|
||||
return query.yield_per(20).all()
|
||||
|
|
Loading…
Reference in New Issue