depgraph: split out similar_name_search func
authorZac Medico <zmedico@gentoo.org>
Sat, 24 Nov 2012 21:56:51 +0000 (13:56 -0800)
committerZac Medico <zmedico@gentoo.org>
Sat, 24 Nov 2012 21:56:51 +0000 (13:56 -0800)
This will allow the code to be re-used for bug #444596. Copyright
begins in 2011 since that's when the code for this feature was first
added in commit aa78cc8da18015b7d1e4eec277b5a7f940fe357c.

pym/_emerge/depgraph.py
pym/portage/dbapi/_similar_name_search.py [new file with mode: 0644]

index c97aa02b8683ac94a55b817c8dd29c850ab42c7e..365fbf8d37350ac18a160c7476692f1847d3967d 100644 (file)
@@ -3,7 +3,6 @@
 
 from __future__ import print_function
 
-import difflib
 import errno
 import io
 import logging
@@ -19,6 +18,7 @@ from portage import _unicode_decode, _unicode_encode, _encodings
 from portage.const import PORTAGE_PACKAGE_ATOM, USER_CONFIG_PATH
 from portage.dbapi import dbapi
 from portage.dbapi.dep_expand import dep_expand
+from portage.dbapi._similar_name_search import similar_name_search
 from portage.dep import Atom, best_match_to_list, extract_affecting_use, \
        check_required_use, human_readable_required_use, match_from_list, \
        _repo_separator
@@ -3678,57 +3678,17 @@ class depgraph(object):
                                not cp_exists and \
                                self._frozen_config.myopts.get(
                                "--misspell-suggestions", "y") != "n":
-                               cp = myparent.atom.cp.lower()
-                               cat, pkg = portage.catsplit(cp)
-                               if cat == "null":
-                                       cat = None
 
                                writemsg("\nemerge: searching for similar names..."
                                        , noiselevel=-1)
 
-                               all_cp = set()
-                               all_cp.update(vardb.cp_all())
+                               dbs = [vardb]
                                if "--usepkgonly" not in self._frozen_config.myopts:
-                                       all_cp.update(portdb.cp_all())
+                                       dbs.append(portdb)
                                if "--usepkg" in self._frozen_config.myopts:
-                                       all_cp.update(bindb.cp_all())
-                               # discard dir containing no ebuilds
-                               all_cp.discard(cp)
+                                       dbs.append(bindb)
 
-                               orig_cp_map = {}
-                               for cp_orig in all_cp:
-                                       orig_cp_map.setdefault(cp_orig.lower(), []).append(cp_orig)
-                               all_cp = set(orig_cp_map)
-
-                               if cat:
-                                       matches = difflib.get_close_matches(cp, all_cp)
-                               else:
-                                       pkg_to_cp = {}
-                                       for other_cp in list(all_cp):
-                                               other_pkg = portage.catsplit(other_cp)[1]
-                                               if other_pkg == pkg:
-                                                       # Check for non-identical package that
-                                                       # differs only by upper/lower case.
-                                                       identical = True
-                                                       for cp_orig in orig_cp_map[other_cp]:
-                                                               if portage.catsplit(cp_orig)[1] != \
-                                                                       portage.catsplit(atom.cp)[1]:
-                                                                       identical = False
-                                                                       break
-                                                       if identical:
-                                                               # discard dir containing no ebuilds
-                                                               all_cp.discard(other_cp)
-                                                               continue
-                                               pkg_to_cp.setdefault(other_pkg, set()).add(other_cp)
-                                       pkg_matches = difflib.get_close_matches(pkg, pkg_to_cp)
-                                       matches = []
-                                       for pkg_match in pkg_matches:
-                                               matches.extend(pkg_to_cp[pkg_match])
-
-                               matches_orig_case = []
-                               for cp in matches:
-                                       matches_orig_case.extend(orig_cp_map[cp])
-                               matches = matches_orig_case
+                               matches = similar_name_search(dbs, atom)
 
                                if len(matches) == 1:
                                        writemsg("\nemerge: Maybe you meant " + matches[0] + "?\n"
diff --git a/pym/portage/dbapi/_similar_name_search.py b/pym/portage/dbapi/_similar_name_search.py
new file mode 100644 (file)
index 0000000..d569fbf
--- /dev/null
@@ -0,0 +1,57 @@
+# Copyright 2011-2012 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
+import difflib
+
+from portage.versions import catsplit
+
+def similar_name_search(dbs, atom):
+
+       cp = atom.cp.lower()
+       cat, pkg = catsplit(cp)
+       if cat == "null":
+               cat = None
+
+       all_cp = set()
+       for db in dbs:
+               all_cp.update(db.cp_all())
+
+       # discard dir containing no ebuilds
+       all_cp.discard(cp)
+
+       orig_cp_map = {}
+       for cp_orig in all_cp:
+               orig_cp_map.setdefault(cp_orig.lower(), []).append(cp_orig)
+       all_cp = set(orig_cp_map)
+
+       if cat:
+               matches = difflib.get_close_matches(cp, all_cp)
+       else:
+               pkg_to_cp = {}
+               for other_cp in list(all_cp):
+                       other_pkg = catsplit(other_cp)[1]
+                       if other_pkg == pkg:
+                               # Check for non-identical package that
+                               # differs only by upper/lower case.
+                               identical = True
+                               for cp_orig in orig_cp_map[other_cp]:
+                                       if catsplit(cp_orig)[1] != \
+                                               catsplit(atom.cp)[1]:
+                                               identical = False
+                                               break
+                               if identical:
+                                       # discard dir containing no ebuilds
+                                       all_cp.discard(other_cp)
+                                       continue
+                       pkg_to_cp.setdefault(other_pkg, set()).add(other_cp)
+
+               pkg_matches = difflib.get_close_matches(pkg, pkg_to_cp)
+               matches = []
+               for pkg_match in pkg_matches:
+                       matches.extend(pkg_to_cp[pkg_match])
+
+       matches_orig_case = []
+       for cp in matches:
+               matches_orig_case.extend(orig_cp_map[cp])
+
+       return matches_orig_case