5259d467349a6ff040d25fadf05a909d0ed48d07
[gentoolkit.git] / pym / gentoolkit / equery / which.py
1 # Copyright(c) 2009, Gentoo Foundation
2 #
3 # Licensed under the GNU General Public License, v2
4 #
5 # $Header: $
6
7 """Display the path to the ebuild that would be used by Portage with the current
8 configuration
9 """
10
11 from __future__ import print_function
12
13 __docformat__ = 'epytext'
14
15 # =======
16 # Imports
17 # =======
18
19 import os
20 import sys
21 from getopt import gnu_getopt, GetoptError
22
23
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
28
29 # =======
30 # Globals
31 # =======
32
33 QUERY_OPTS = {"include_masked": False}
34
35 # =========
36 # Functions
37 # =========
38
39 def print_help(with_description=True):
40         """Print description, usage and a detailed help message.
41
42         @type with_description: bool
43         @param with_description: if true, print module's __doc__ string
44         """
45
46         if with_description:
47                 print(__doc__.strip())
48                 print()
49         print(mod_usage(mod_name="which"))
50         print()
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")
55         )))
56
57
58 def parse_module_options(module_opts):
59         """Parse module options and update QUERY_OPTS"""
60
61         opts = (x[0] for x in module_opts)
62         for opt in opts:
63                 if opt in ('-h', '--help'):
64                         print_help()
65                         sys.exit(0)
66                 elif opt in ('-m', '--include-masked'):
67                         QUERY_OPTS['include_masked'] = True
68
69
70 def main(input_args):
71         """Parse input and run the program"""
72
73         short_opts = "hm"
74         long_opts = ('help', 'include-masked')
75
76         try:
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))
80                 print()
81                 print_help(with_description=False)
82                 sys.exit(2)
83
84         parse_module_options(module_opts)
85
86         if not queries:
87                 print_help()
88                 sys.exit(2)
89
90         for query in (Query(x) for x in queries):
91                 matches = query.find(
92                         include_masked=QUERY_OPTS['include_masked'],
93                         in_installed=False
94                 )
95                 if matches:
96                         pkg = sorted(matches).pop()
97                         ebuild_path = pkg.ebuild_path()
98                         if ebuild_path:
99                                 pp.uprint(os.path.normpath(ebuild_path))
100                         else:
101                                 sys.stderr.write(
102                                         pp.warn("No ebuilds to satisfy %s" % pkg.cpv)
103                                 )
104                 else:
105                         raise errors.GentoolkitNoMatches(query)
106
107 # vim: set ts=4 sw=4 tw=79: