add an ebuild listing option to equery which as requested by Calchan and ryao.
[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 = {
34         "include_masked": False,
35         "ebuild":False
36         }
37
38 # =========
39 # Functions
40 # =========
41
42 def print_help(with_description=True):
43         """Print description, usage and a detailed help message.
44
45         @type with_description: bool
46         @param with_description: if true, print module's __doc__ string
47         """
48
49         if with_description:
50                 print(__doc__.strip())
51                 print()
52         print(mod_usage(mod_name="which"))
53         print()
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")
59         )))
60
61 def print_ebuild(ebuild_path):
62         """Output the ebuild to std_out"""
63         with open(ebuild_path) as f:
64                 lines = f.readlines()
65                 print("\n\n")
66                 print("".join(lines))
67                 print("\n")
68
69 def parse_module_options(module_opts):
70         """Parse module options and update QUERY_OPTS"""
71
72         opts = (x[0] for x in module_opts)
73         for opt in opts:
74                 if opt in ('-h', '--help'):
75                         print_help()
76                         sys.exit(0)
77                 elif opt in ('-m', '--include-masked'):
78                         QUERY_OPTS['include_masked'] = True
79                 elif opt in ('-e', '--ebuild'):
80                         QUERY_OPTS['ebuild'] = True
81
82
83 def main(input_args):
84         """Parse input and run the program"""
85
86         short_opts = "hme"
87         long_opts = ('help', 'include-masked', 'ebuild')
88
89         try:
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))
93                 print()
94                 print_help(with_description=False)
95                 sys.exit(2)
96
97         parse_module_options(module_opts)
98
99         if not queries:
100                 print_help()
101                 sys.exit(2)
102
103         for query in (Query(x) for x in queries):
104                 matches = query.find(
105                         include_masked=QUERY_OPTS['include_masked'],
106                         in_installed=False
107                 )
108                 if matches:
109                         pkg = sorted(matches).pop()
110                         ebuild_path = pkg.ebuild_path()
111                         if ebuild_path:
112                                 pp.uprint(os.path.normpath(ebuild_path))
113                                 if QUERY_OPTS['ebuild']:
114                                         print_ebuild(ebuild_path)
115                         else:
116                                 sys.stderr.write(
117                                         pp.warn("No ebuilds to satisfy %s" % pkg.cpv)
118                                 )
119                 else:
120                         raise errors.GentoolkitNoMatches(query)
121
122 # vim: set ts=4 sw=4 tw=79: