diff options
Diffstat (limited to 'pym/gentoolkit/equery/__init__.py')
-rw-r--r-- | pym/gentoolkit/equery/__init__.py | 153 |
1 files changed, 77 insertions, 76 deletions
diff --git a/pym/gentoolkit/equery/__init__.py b/pym/gentoolkit/equery/__init__.py index c205938..13ff6ba 100644 --- a/pym/gentoolkit/equery/__init__.py +++ b/pym/gentoolkit/equery/__init__.py @@ -15,27 +15,51 @@ __all__ = ( 'mod_usage' ) __docformat__ = 'epytext' +# version is dynamically set by distutils sdist +__version__ = "svn" # ======= # Imports # ======= import errno +import os import sys import time from getopt import getopt, GetoptError -from portage import exception +import portage import gentoolkit -import gentoolkit.pprinter as pp -from gentoolkit import settings, Config +from gentoolkit import CONFIG +from gentoolkit import errors +from gentoolkit import pprinter as pp from gentoolkit.textwrap_ import TextWrapper __productname__ = "equery" -__authors__ = """\ -Karl Trygve Kalleberg - Original author -Douglas Anderson - Modular redesign; author of meta, changes""" +__authors__ = ( + 'Karl Trygve Kalleberg - Original author', + 'Douglas Anderson - Modular redesign; author of meta, changes' +) + +# ======= +# Globals +# ======= + +NAME_MAP = { + 'b': 'belongs', + 'c': 'changes', + 'k': 'check', + 'd': 'depends', + 'g': 'depgraph', + 'f': 'files', + 'h': 'hasuse', + 'l': 'list_', + 'm': 'meta', + 's': 'size', + 'u': 'uses', + 'w': 'which' +} # ========= # Functions @@ -48,7 +72,7 @@ def print_help(with_description=True): """ if with_description: - print __doc__ + print __doc__ print main_usage() print print pp.globaloption("global options") @@ -78,30 +102,15 @@ def print_help(with_description=True): def expand_module_name(module_name): - """Returns one of the values of name_map or raises KeyError""" - - name_map = { - 'b': 'belongs', - 'c': 'changes', - 'k': 'check', - 'd': 'depends', - 'g': 'depgraph', - 'f': 'files', - 'h': 'hasuse', - 'l': 'list_', - 'm': 'meta', - 's': 'size', - 'u': 'uses', - 'w': 'which' - } + """Returns one of the values of NAME_MAP or raises KeyError""" if module_name == 'list': # list is a Python builtin type, so we must rename our module return 'list_' - elif module_name in name_map.values(): + elif module_name in NAME_MAP.values(): return module_name else: - return name_map[module_name] + return NAME_MAP[module_name] def format_options(options): @@ -114,21 +123,21 @@ def format_options(options): """ result = [] - twrap = TextWrapper(width=Config['termWidth']) + twrap = TextWrapper(width=CONFIG['termWidth']) opts = (x[0] for x in options) descs = (x[1] for x in options) for opt, desc in zip(opts, descs): twrap.initial_indent = pp.emph(opt.ljust(25)) twrap.subsequent_indent = " " * 25 result.append(twrap.fill(desc)) - + return '\n'.join(result) def format_filetype(path, fdesc, show_type=False, show_md5=False, show_timestamp=False): """Format a path for printing. - + @type path: str @param path: the path @type fdesc: list @@ -141,7 +150,7 @@ def format_filetype(path, fdesc, show_type=False, show_md5=False, @type show_md5: bool @param show_md5: if True, append MD5 sum to the formatted string @type show_timestamp: bool - @param show_timestamp: if True, append time-of-creation after pathname + @param show_timestamp: if True, append time-of-creation after pathname @rtype: str @return: formatted pathname with optional added information """ @@ -160,7 +169,7 @@ def format_filetype(path, fdesc, show_type=False, show_md5=False, ftype = "sym" stamp = format_timestamp(fdesc[1]) tgt = fdesc[2].split()[0] - if Config["piping"]: + if CONFIG["piping"]: fpath = path else: fpath = pp.path_symlink(path + " -> " + tgt) @@ -168,7 +177,9 @@ def format_filetype(path, fdesc, show_type=False, show_md5=False, ftype = "dev" fpath = path else: - pp.print_error("%s has unknown type: %s" % (path, fdesc[0])) + sys.stderr.write( + pp.error("%s has unknown type: %s" % (path, fdesc[0])) + ) result = "" if show_type: @@ -198,24 +209,14 @@ def initialize_configuration(): term_width = 80 # Terminal size, minus a 1-char margin for text wrapping - Config['termWidth'] = term_width - 1 - - # Color handling: -1: Use Portage settings, 0: Force off, 1: Force on - Config['color'] = -1 - - Config['quiet'] = False + CONFIG['termWidth'] = term_width - 1 # Guess color output - if (Config['color'] == -1 and (not sys.stdout.isatty() or - settings["NOCOLOR"] in ("yes", "true")) or - Config['color'] == 0): + if (CONFIG['color'] == -1 and (not sys.stdout.isatty() or + os.getenv("NOCOLOR") in ("yes", "true")) or CONFIG['color'] == 0): pp.output.nocolor() - # Guess piping output - if not sys.stdout.isatty(): - Config["piping"] = True - else: - Config["piping"] = False + CONFIG['verbose'] = not CONFIG['piping'] def main_usage(): @@ -232,7 +233,7 @@ def main_usage(): def mod_usage(mod_name="module", arg="pkgspec", optional=False): """Provide a consistant usage message to the calling module. - + @type arg: string @param arg: what kind of argument the module takes (pkgspec, filename, etc) @type optional: bool @@ -262,88 +263,88 @@ def parse_global_options(global_opts, args): print_help() sys.exit(0) elif opt in ('-q','--quiet'): - Config["quiet"] = True + CONFIG['quiet'] = True elif opt in ('-C', '--no-color', '--nocolor'): - Config['color'] = 0 + CONFIG['color'] = 0 pp.output.nocolor() elif opt in ('-N', '--no-pipe'): - Config["piping"] = False + CONFIG['piping'] = False elif opt in ('-V', '--version'): print_version() sys.exit(0) - + elif opt in ('--debug'): + CONFIG['debug'] = True + return need_help - + def print_version(): """Print the version of this tool to the console.""" - try: - with open('/usr/share/gentoolkit/VERSION') as gentoolkit_version: - version = gentoolkit_version.read().strip() - except IOError, err: - pp.die(2, str(err)) - print "%(product)s (%(version)s) - %(docstring)s" % { "product": pp.productname(__productname__), - "version": version, + "version": __version__, "docstring": __doc__ } def split_arguments(args): """Separate module name from module arguments""" - + return args.pop(0), args def main(): """Parse input and run the program.""" - initialize_configuration() - short_opts = "hqCNV" - long_opts = ('help', 'quiet', 'nocolor', 'no-color', 'no-pipe', 'version') + long_opts = ( + 'help', 'quiet', 'nocolor', 'no-color', 'no-pipe', 'version', 'debug' + ) + + initialize_configuration() try: global_opts, args = getopt(sys.argv[1:], short_opts, long_opts) except GetoptError, err: - pp.print_error("Global %s" % err) + sys.stderr.write(pp.error("Global %s" % err)) print_help(with_description=False) sys.exit(2) # Parse global options need_help = parse_global_options(global_opts, args) + # FIXME: There are a few places that make use of both quiet and verbose. + # Consider combining. + if CONFIG['quiet']: + CONFIG['verbose'] = False + try: module_name, module_args = split_arguments(args) except IndexError: print_help() sys.exit(2) - + if need_help: module_args.append('--help') - if Config['piping'] or Config['quiet']: - Config['verbose'] = False - else: - Config['verbose'] = True - try: expanded_module_name = expand_module_name(module_name) except KeyError: - pp.print_error("Unknown module '%s'" % module_name) + sys.stderr.write(pp.error("Unknown module '%s'" % module_name)) print_help(with_description=False) sys.exit(2) try: - loaded_module = __import__(expanded_module_name, globals(), - locals(), [], -1) + loaded_module = __import__( + expanded_module_name, globals(), locals(), [], -1 + ) loaded_module.main(module_args) - except exception.AmbiguousPackageName, err: - pp.print_error("Ambiguous package name. Use one of: ") - while err[0]: - print " " + err[0].pop() + except portage.exception.AmbiguousPackageName, err: + raise errors.GentoolkitAmbiguousPackage(err) except IOError, err: if err.errno != errno.EPIPE: raise + +if __name__ == '__main__': + main() |