summaryrefslogtreecommitdiffstats
path: root/pym/portage/dbapi
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2012-11-24 13:56:51 -0800
committerZac Medico <zmedico@gentoo.org>2012-11-24 13:56:51 -0800
commitabd18694835b9f8a5a515f9c6333a754703a0462 (patch)
treeaa69de228de42e98f0dcd85b2196bcebf53ffab7 /pym/portage/dbapi
parent437c0f1987fa770bd2edf31f1a5a1812391e37fd (diff)
downloadportage-abd18694835b9f8a5a515f9c6333a754703a0462.tar.gz
portage-abd18694835b9f8a5a515f9c6333a754703a0462.tar.bz2
portage-abd18694835b9f8a5a515f9c6333a754703a0462.zip
depgraph: split out similar_name_search func
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.
Diffstat (limited to 'pym/portage/dbapi')
-rw-r--r--pym/portage/dbapi/_similar_name_search.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/pym/portage/dbapi/_similar_name_search.py b/pym/portage/dbapi/_similar_name_search.py
new file mode 100644
index 000000000..d569fbf89
--- /dev/null
+++ b/pym/portage/dbapi/_similar_name_search.py
@@ -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