aboutsummaryrefslogtreecommitdiff
blob: b6e4a1fbe951e0a3b168e15c9a289c2ad17cb055 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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_lower = atom.cp.lower()
	cat, pkg = catsplit(cp_lower)
	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(atom.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_lower, 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