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
34 "include_masked": False,
42 def print_help(with_description=True):
43 """Print description, usage and a detailed help message.
45 @type with_description: bool
46 @param with_description: if true, print module's __doc__ string
50 print(__doc__.strip())
52 print(mod_usage(mod_name="which"))
54 print(pp.command("options"))
55 print(format_options((
56 (" -h, --help", "display this help message"),
57 (" -m, --include-masked", "return highest version ebuild available"),
58 (" -e, --ebuild", "print the ebuild")
61 def print_ebuild(ebuild_path):
62 """Output the ebuild to std_out"""
63 with open(ebuild_path) as f:
69 def parse_module_options(module_opts):
70 """Parse module options and update QUERY_OPTS"""
72 opts = (x[0] for x in module_opts)
74 if opt in ('-h', '--help'):
77 elif opt in ('-m', '--include-masked'):
78 QUERY_OPTS['include_masked'] = True
79 elif opt in ('-e', '--ebuild'):
80 QUERY_OPTS['ebuild'] = True
84 """Parse input and run the program"""
87 long_opts = ('help', 'include-masked', 'ebuild')
90 module_opts, queries = gnu_getopt(input_args, short_opts, long_opts)
91 except GetoptError as err:
92 sys.stderr.write(pp.error("Module %s" % err))
94 print_help(with_description=False)
97 parse_module_options(module_opts)
103 for query in (Query(x) for x in queries):
104 matches = query.find(
105 include_masked=QUERY_OPTS['include_masked'],
109 pkg = sorted(matches).pop()
110 ebuild_path = pkg.ebuild_path()
112 pp.uprint(os.path.normpath(ebuild_path))
113 if QUERY_OPTS['ebuild']:
114 print_ebuild(ebuild_path)
117 pp.warn("No ebuilds to satisfy %s" % pkg.cpv)
120 raise errors.GentoolkitNoMatches(query)
122 # vim: set ts=4 sw=4 tw=79: