#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import pygrep
from optparse import OptionParser


parser = OptionParser()
parser.add_option('-l', '--list', action='store_true',
                  help='only gather the list of entrypoints')
(options, args) = parser.parse_args()
ident = args[0]
files_or_dirs = args[1:]
if options.list:
    entrypoints = set()
    def handler(path, context, node, ident):
        entrypoints.add('.'.join(ident))
else:
    def handler(path, context, node, ident):
        fd = ''
        if context:
            fd = '(%s)' % '.'.join(map(lambda t: t[0], context))
        print '%s%s:%s\t%s' % (path, fd, node.lineno, '.'.join(ident))

pygrep.handleIdent(ident, files_or_dirs, handler)
if options.list:
    print '\n'.join(sorted(entrypoints))
