aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'gentoolkit/pym/gentoolkit/equery/belongs.py')
-rw-r--r--gentoolkit/pym/gentoolkit/equery/belongs.py156
1 files changed, 156 insertions, 0 deletions
diff --git a/gentoolkit/pym/gentoolkit/equery/belongs.py b/gentoolkit/pym/gentoolkit/equery/belongs.py
new file mode 100644
index 0000000..3845b9d
--- /dev/null
+++ b/gentoolkit/pym/gentoolkit/equery/belongs.py
@@ -0,0 +1,156 @@
+# Copyright(c) 2009-2010, Gentoo Foundation
+#
+# Licensed under the GNU General Public License, v2
+#
+# $Header: $
+
+"""List all packages owning a particular file
+
+Note: Normally, only one package will own a file. If multiple packages own
+ the same file, it usually constitutes a problem, and should be reported.
+"""
+
+__docformat__ = 'epytext'
+
+# =======
+# Imports
+# =======
+
+import sys
+from getopt import gnu_getopt, GetoptError
+
+import gentoolkit.pprinter as pp
+from gentoolkit.equery import (format_filetype, format_options, mod_usage,
+ CONFIG)
+from gentoolkit.helpers import FileOwner
+
+# =======
+# Globals
+# =======
+
+QUERY_OPTS = {
+ "fullRegex": False,
+ "earlyOut": False,
+ "nameOnly": False
+}
+
+# =======
+# Classes
+# =======
+
+class BelongsPrinter(object):
+ """Outputs a formatted list of packages that claim to own a files."""
+
+ def __init__(self, verbose=True, name_only=False):
+ if verbose:
+ self.print_fn = self.print_verbose
+ else:
+ self.print_fn = self.print_quiet
+
+ self.name_only = name_only
+
+ def __call__(self, pkg, cfile):
+ self.print_fn(pkg, cfile)
+
+ # W0613: *Unused argument %r*
+ # pylint: disable-msg=W0613
+ def print_quiet(self, pkg, cfile):
+ "Format for minimal output."
+ if self.name_only:
+ name = pkg.cpv.cp
+ else:
+ name = str(pkg.cpv)
+ print name
+
+ def print_verbose(self, pkg, cfile):
+ "Format for full output."
+ file_str = pp.path(format_filetype(cfile, pkg.parsed_contents()[cfile]))
+ if self.name_only:
+ name = pkg.cpv.cp
+ else:
+ name = str(pkg.cpv)
+ print pp.cpv(name), "(" + file_str + ")"
+
+
+# =========
+# Functions
+# =========
+
+def parse_module_options(module_opts):
+ """Parse module options and update QUERY_OPTS"""
+
+ opts = (x[0] for x in module_opts)
+ for opt in opts:
+ if opt in ('-h','--help'):
+ print_help()
+ sys.exit(0)
+ elif opt in ('-e', '--early-out', '--earlyout'):
+ if opt == '--earlyout':
+ sys.stderr.write(pp.warn("Use of --earlyout is deprecated."))
+ sys.stderr.write(pp.warn("Please use --early-out."))
+ print
+ QUERY_OPTS['earlyOut'] = True
+ elif opt in ('-f', '--full-regex'):
+ QUERY_OPTS['fullRegex'] = True
+ elif opt in ('-n', '--name-only'):
+ QUERY_OPTS['nameOnly'] = True
+
+
+def print_help(with_description=True):
+ """Print description, usage and a detailed help message.
+
+ @type with_description: bool
+ @param with_description: if true, print module's __doc__ string
+ """
+
+ if with_description:
+ print __doc__.strip()
+ print
+ print mod_usage(mod_name="belongs", arg="filename")
+ print
+ print pp.command("options")
+ print format_options((
+ (" -h, --help", "display this help message"),
+ (" -f, --full-regex", "supplied query is a regex" ),
+ (" -e, --early-out", "stop when first match is found"),
+ (" -n, --name-only", "don't print the version")
+ ))
+
+
+def main(input_args):
+ """Parse input and run the program"""
+
+ short_opts = "h:fen"
+ long_opts = ('help', 'full-regex', 'early-out', 'earlyout',
+ 'name-only')
+
+ try:
+ module_opts, queries = gnu_getopt(input_args, short_opts, long_opts)
+ except GetoptError, err:
+ sys.stderr.write(pp.error("Module %s" % err))
+ print
+ print_help(with_description=False)
+ sys.exit(2)
+
+ parse_module_options(module_opts)
+
+ if not queries:
+ print_help()
+ sys.exit(2)
+
+ if CONFIG['verbose']:
+ print " * Searching for %s ... " % (pp.regexpquery(",".join(queries)))
+
+ printer_fn = BelongsPrinter(
+ verbose=CONFIG['verbose'], name_only=QUERY_OPTS['nameOnly']
+ )
+
+ find_owner = FileOwner(
+ is_regex=QUERY_OPTS['fullRegex'],
+ early_out=QUERY_OPTS['earlyOut'],
+ printer_fn=printer_fn
+ )
+
+ find_owner(queries)
+
+# vim: set ts=4 sw=4 tw=79: