1 # Copyright(c) 2009, Gentoo Foundation
3 # Licensed under the GNU General Public License, v2
7 """Display the path to the ebuild that would be used by Portage with the current
11 from __future__ import print_function
13 __docformat__ = 'epytext'
21 from getopt import gnu_getopt, GetoptError
24 import gentoolkit.pprinter as pp
25 from gentoolkit import errors
26 from gentoolkit.equery import format_options, mod_usage
27 from gentoolkit.query import Query
33 QUERY_OPTS = {"include_masked": False}
39 def print_help(with_description=True):
40 """Print description, usage and a detailed help message.
42 @type with_description: bool
43 @param with_description: if true, print module's __doc__ string
47 print(__doc__.strip())
49 print(mod_usage(mod_name="which"))
51 print(pp.command("options"))
52 print(format_options((
53 (" -h, --help", "display this help message"),
54 (" -m, --include-masked", "return highest version ebuild available")
58 def parse_module_options(module_opts):
59 """Parse module options and update QUERY_OPTS"""
61 opts = (x[0] for x in module_opts)
63 if opt in ('-h', '--help'):
66 elif opt in ('-m', '--include-masked'):
67 QUERY_OPTS['include_masked'] = True
71 """Parse input and run the program"""
74 long_opts = ('help', 'include-masked')
77 module_opts, queries = gnu_getopt(input_args, short_opts, long_opts)
78 except GetoptError as err:
79 sys.stderr.write(pp.error("Module %s" % err))
81 print_help(with_description=False)
84 parse_module_options(module_opts)
90 for query in (Query(x) for x in queries):
92 include_masked=QUERY_OPTS['include_masked'],
96 pkg = sorted(matches).pop()
97 ebuild_path = pkg.ebuild_path()
99 pp.uprint(os.path.normpath(ebuild_path))
102 pp.warn("No ebuilds to satisfy %s" % pkg.cpv)
105 raise errors.GentoolkitNoMatches(query)
107 # vim: set ts=4 sw=4 tw=79: