add an ebuild listing option to equery which as requested by Calchan and ryao.
[gentoolkit.git] / pym / gentoolkit / equery / which.py
index be4f5e81122cadd3269d6aa81327fe4b5f08cd5d..da60a1bd0ae4e024495415c997571bc23c750481 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright(c) 2009-2010, Gentoo Foundation
+# Copyright(c) 2009, Gentoo Foundation
 #
 # Licensed under the GNU General Public License, v2
 #
@@ -8,6 +8,8 @@
 configuration
 """
 
+from __future__ import print_function
+
 __docformat__ = 'epytext'
 
 # =======
@@ -18,16 +20,20 @@ import os
 import sys
 from getopt import gnu_getopt, GetoptError
 
+
 import gentoolkit.pprinter as pp
 from gentoolkit import errors
 from gentoolkit.equery import format_options, mod_usage
-from gentoolkit.helpers import find_packages
+from gentoolkit.query import Query
 
 # =======
 # Globals
 # =======
 
-QUERY_OPTS = {"includeMasked": False}
+QUERY_OPTS = {
+       "include_masked": False,
+       "ebuild":False
+       }
 
 # =========
 # Functions
@@ -41,16 +47,24 @@ def print_help(with_description=True):
        """
 
        if with_description:
-               print __doc__.strip()
-               print
-       print mod_usage(mod_name="which")
-       print
-       print pp.command("options")
-       print format_options((
+               print(__doc__.strip())
+               print()
+       print(mod_usage(mod_name="which"))
+       print()
+       print(pp.command("options"))
+       print(format_options((
                (" -h, --help", "display this help message"),
-               (" -m, --include-masked", "return highest version ebuild available")
-       ))
-
+               (" -m, --include-masked", "return highest version ebuild available"),
+               (" -e, --ebuild", "print the ebuild")
+       )))
+
+def print_ebuild(ebuild_path):
+       """Output the ebuild to std_out"""
+       with open(ebuild_path) as f:
+               lines = f.readlines()
+               print("\n\n")
+               print("".join(lines))
+               print("\n")
 
 def parse_module_options(module_opts):
        """Parse module options and update QUERY_OPTS"""
@@ -61,20 +75,22 @@ def parse_module_options(module_opts):
                        print_help()
                        sys.exit(0)
                elif opt in ('-m', '--include-masked'):
-                       QUERY_OPTS['includeMasked'] = True
+                       QUERY_OPTS['include_masked'] = True
+               elif opt in ('-e', '--ebuild'):
+                       QUERY_OPTS['ebuild'] = True
 
 
 def main(input_args):
        """Parse input and run the program"""
 
-       short_opts = "hm"
-       long_opts = ('help', 'include-masked')
+       short_opts = "hme"
+       long_opts = ('help', 'include-masked', 'ebuild')
 
        try:
                module_opts, queries = gnu_getopt(input_args, short_opts, long_opts)
-       except GetoptError, err:
+       except GetoptError as err:
                sys.stderr.write(pp.error("Module %s" % err))
-               print
+               print()
                print_help(with_description=False)
                sys.exit(2)
 
@@ -84,17 +100,21 @@ def main(input_args):
                print_help()
                sys.exit(2)
 
-       for query in queries:
-
-               matches = find_packages(query, QUERY_OPTS['includeMasked'])
+       for query in (Query(x) for x in queries):
+               matches = query.find(
+                       include_masked=QUERY_OPTS['include_masked'],
+                       in_installed=False
+               )
                if matches:
                        pkg = sorted(matches).pop()
                        ebuild_path = pkg.ebuild_path()
                        if ebuild_path:
-                               print os.path.normpath(ebuild_path)
+                               pp.uprint(os.path.normpath(ebuild_path))
+                               if QUERY_OPTS['ebuild']:
+                                       print_ebuild(ebuild_path)
                        else:
                                sys.stderr.write(
-                                       pp.warn("No ebuilds to satisfy %s" % pkg.cpv.name)
+                                       pp.warn("No ebuilds to satisfy %s" % pkg.cpv)
                                )
                else:
                        raise errors.GentoolkitNoMatches(query)