Merge genscripts rev 137, this revision makes the output of equery meta compatible...
[gentoolkit.git] / pym / gentoolkit / equery / __init__.py
1 # Copyright(c) 2009, Gentoo Foundation
2 #
3 # Licensed under the GNU General Public License, v2
4 #
5 # $Header: $
6
7 """Gentoo package query tool"""
8
9 # Move to Imports section after Python 2.6 is stable
10 from __future__ import with_statement
11
12 __all__ = (
13         'format_options',
14         'format_package_names',
15         'mod_usage'
16 )
17 __docformat__ = 'epytext'
18 # version is dynamically set by distutils sdist
19 __version__ = "svn"
20
21 # =======
22 # Imports
23 # =======
24
25 import errno
26 import os
27 import sys
28 import time
29 from getopt import getopt, GetoptError
30
31 import portage
32
33 import gentoolkit
34 from gentoolkit import CONFIG
35 from gentoolkit import errors
36 from gentoolkit import pprinter as pp
37 from gentoolkit.textwrap_ import TextWrapper
38
39 __productname__ = "equery"
40 __authors__ = (
41         'Karl Trygve Kalleberg - Original author',
42         'Douglas Anderson - 0.3.0 author'
43 )
44
45 # =======
46 # Globals
47 # =======
48
49 NAME_MAP = {
50         'b': 'belongs',
51         'c': 'changes',
52         'k': 'check',
53         'd': 'depends',
54         'g': 'depgraph',
55         'f': 'files',
56         'h': 'hasuse',
57         'l': 'list_',
58         'm': 'meta',
59         's': 'size',
60         'u': 'uses',
61         'w': 'which'
62 }
63
64 # =========
65 # Functions
66 # =========
67
68 def print_help(with_description=True):
69         """Print description, usage and a detailed help message.
70
71         @param with_description (bool): Option to print module's __doc__ or not
72         """
73
74         if with_description:
75                 print __doc__
76         print main_usage()
77         print
78         print pp.globaloption("global options")
79         print format_options((
80                 (" -h, --help", "display this help message"),
81                 (" -q, --quiet", "minimal output"),
82                 (" -C, --no-color", "turn off colors"),
83                 (" -N, --no-pipe", "turn off pipe detection"),
84                 (" -V, --version", "display version info")
85         ))
86         print
87         print pp.command("modules") + " (" + pp.command("short name") + ")"
88         print format_options((
89                 (" (b)elongs", "list what package FILES belong to"),
90                 (" (c)hanges", "list changelog entries for ATOM"),
91                 (" chec(k)", "verify checksums and timestamps for PKG"),
92                 (" (d)epends", "list all packages directly depending on ATOM"),
93                 (" dep(g)raph", "display a tree of all dependencies for PKG"),
94                 (" (f)iles", "list all files installed by PKG"),
95                 (" (h)asuse", "list all packages that have USE flag"),
96                 (" (l)ist", "list package matching PKG"),
97                 (" (m)eta", "display metadata about PKG"),
98                 (" (s)ize", "display total size of all files owned by PKG"),
99                 (" (u)ses", "display USE flags for PKG"),
100                 (" (w)hich", "print full path to ebuild for PKG")
101         ))
102
103
104 def expand_module_name(module_name):
105         """Returns one of the values of NAME_MAP or raises KeyError"""
106
107         if module_name == 'list':
108                 # list is a Python builtin type, so we must rename our module
109                 return 'list_'
110         elif module_name in NAME_MAP.values():
111                 return module_name
112         else:
113                 return NAME_MAP[module_name]
114
115
116 def format_options(options):
117         """Format module options.
118
119         @type options: list
120         @param options: [('option 1', 'description 1'), ('option 2', 'des... )]
121         @rtype: str
122         @return: formatted options string
123         """
124
125         result = []
126         twrap = TextWrapper(width=CONFIG['termWidth'])
127         opts = (x[0] for x in options)
128         descs = (x[1] for x in options)
129         for opt, desc in zip(opts, descs):
130                 twrap.initial_indent = pp.emph(opt.ljust(25))
131                 twrap.subsequent_indent = " " * 25
132                 result.append(twrap.fill(desc))
133
134         return '\n'.join(result)
135
136
137 def format_filetype(path, fdesc, show_type=False, show_md5=False,
138                 show_timestamp=False):
139         """Format a path for printing.
140
141         @type path: str
142         @param path: the path
143         @type fdesc: list
144         @param fdesc: [file_type, timestamp, MD5 sum/symlink target]
145                 file_type is one of dev, dir, obj, sym.
146                 If file_type is dir, there is no timestamp or MD5 sum.
147                 If file_type is sym, fdesc[2] is the target of the symlink.
148         @type show_type: bool
149         @param show_type: if True, prepend the file's type to the formatted string
150         @type show_md5: bool
151         @param show_md5: if True, append MD5 sum to the formatted string
152         @type show_timestamp: bool
153         @param show_timestamp: if True, append time-of-creation after pathname
154         @rtype: str
155         @return: formatted pathname with optional added information
156         """
157
158         ftype = fpath = stamp = md5sum = ""
159
160         if fdesc[0] == "obj":
161                 ftype = "file"
162                 fpath = path
163                 stamp = format_timestamp(fdesc[1])
164                 md5sum = fdesc[2]
165         elif fdesc[0] == "dir":
166                 ftype = "dir"
167                 fpath = pp.path(path)
168         elif fdesc[0] == "sym":
169                 ftype = "sym"
170                 stamp = format_timestamp(fdesc[1])
171                 tgt = fdesc[2].split()[0]
172                 if CONFIG["piping"]:
173                         fpath = path
174                 else:
175                         fpath = pp.path_symlink(path + " -> " + tgt)
176         elif fdesc[0] == "dev":
177                 ftype = "dev"
178                 fpath = path
179         else:
180                 sys.stderr.write(
181                         pp.error("%s has unknown type: %s" % (path, fdesc[0]))
182                 )
183
184         result = ""
185         if show_type:
186                 result += "%4s " % ftype
187         result += fpath
188         if show_timestamp:
189                 result += "  " + stamp
190         if show_md5:
191                 result += "  " + md5sum
192
193         return result
194
195
196 def format_timestamp(timestamp):
197         """Format a timestamp into, e.g., '2009-01-31 21:19:44' format"""
198
199         return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(timestamp)))
200
201
202 def initialize_configuration():
203         """Setup the standard equery config"""
204
205         # Get terminal size
206         term_width = pp.output.get_term_size()[1]
207         if term_width == -1:
208                 # get_term_size() failed. Set a sane default width:
209                 term_width = 80
210
211         # Terminal size, minus a 1-char margin for text wrapping
212         CONFIG['termWidth'] = term_width - 1
213
214         # Guess color output
215         if (CONFIG['color'] == -1 and (not sys.stdout.isatty() or
216                 os.getenv("NOCOLOR") in ("yes", "true")) or CONFIG['color'] == 0):
217                 pp.output.nocolor()
218
219         CONFIG['verbose'] = not CONFIG['piping']
220
221
222 def main_usage():
223         """Return the main usage message for equery"""
224
225         return "%(usage)s %(product)s [%(g_opts)s] %(mod_name)s [%(mod_opts)s]" % {
226                 'usage': pp.emph("Usage:"),
227                 'product': pp.productname(__productname__),
228                 'g_opts': pp.globaloption("global-options"),
229                 'mod_name': pp.command("module-name"),
230                 'mod_opts': pp.localoption("module-options")
231         }
232
233
234 def mod_usage(mod_name="module", arg="pkgspec", optional=False):
235         """Provide a consistent usage message to the calling module.
236
237         @type arg: string
238         @param arg: what kind of argument the module takes (pkgspec, filename, etc)
239         @type optional: bool
240         @param optional: is the argument optional?
241         """
242
243         return "%(usage)s: %(mod_name)s [%(opts)s] %(arg)s" % {
244                 'usage': pp.emph("Usage"),
245                 'mod_name': pp.command(mod_name),
246                 'opts': pp.localoption("options"),
247                 'arg': ("[%s]" % pp.emph(arg)) if optional else pp.emph(arg)
248         }
249
250
251 def parse_global_options(global_opts, args):
252         """Parse global input args and return True if we should display help for
253         the called module, else False (or display help and exit from here).
254         """
255
256         need_help = False
257         opts = (opt[0] for opt in global_opts)
258         for opt in opts:
259                 if opt in ('-h', '--help'):
260                         if args:
261                                 need_help = True
262                         else:
263                                 print_help()
264                                 sys.exit(0)
265                 elif opt in ('-q','--quiet'):
266                         CONFIG['quiet'] = True
267                 elif opt in ('-C', '--no-color', '--nocolor'):
268                         CONFIG['color'] = 0
269                         pp.output.nocolor()
270                 elif opt in ('-N', '--no-pipe'):
271                         CONFIG['piping'] = False
272                 elif opt in ('-V', '--version'):
273                         print_version()
274                         sys.exit(0)
275                 elif opt in ('--debug'):
276                         CONFIG['debug'] = True
277
278         return need_help
279
280
281 def print_version():
282         """Print the version of this tool to the console."""
283
284         print "%(product)s (%(version)s) - %(docstring)s" % {
285                 "product": pp.productname(__productname__),
286                 "version": __version__,
287                 "docstring": __doc__
288         }
289
290
291 def split_arguments(args):
292         """Separate module name from module arguments"""
293
294         return args.pop(0), args
295
296
297 def main():
298         """Parse input and run the program."""
299
300         short_opts = "hqCNV"
301         long_opts = (
302                 'help', 'quiet', 'nocolor', 'no-color', 'no-pipe', 'version', 'debug'
303         )
304
305         initialize_configuration()
306
307         try:
308                 global_opts, args = getopt(sys.argv[1:], short_opts, long_opts)
309         except GetoptError, err:
310                 sys.stderr.write(pp.error("Global %s" % err))
311                 print_help(with_description=False)
312                 sys.exit(2)
313
314         # Parse global options
315         need_help = parse_global_options(global_opts, args)
316
317         # FIXME: There are a few places that make use of both quiet and verbose.
318         #        Consider combining.
319         if CONFIG['quiet']:
320                 CONFIG['verbose'] = False
321
322         try:
323                 module_name, module_args = split_arguments(args)
324         except IndexError:
325                 print_help()
326                 sys.exit(2)
327
328         if need_help:
329                 module_args.append('--help')
330
331         try:
332                 expanded_module_name = expand_module_name(module_name)
333         except KeyError:
334                 sys.stderr.write(pp.error("Unknown module '%s'" % module_name))
335                 print_help(with_description=False)
336                 sys.exit(2)
337
338         try:
339                 loaded_module = __import__(
340                         expanded_module_name, globals(), locals(), [], -1
341                 )
342                 loaded_module.main(module_args)
343         except portage.exception.AmbiguousPackageName, err:
344                 raise errors.GentoolkitAmbiguousPackage(err)
345         except IOError, err:
346                 if err.errno != errno.EPIPE:
347                         raise
348
349 if __name__ == '__main__':
350         main()