1
2
3
4 from __future__ import unicode_literals
5
6 __all__ = ["cpv_expand"]
7
8 import portage
9 from portage.exception import AmbiguousPackageName
10 from portage.localization import _
11 from portage.util import writemsg
12 from portage.versions import _pkgsplit
13
14 -def cpv_expand(mycpv, mydb=None, use_cache=1, settings=None):
15 """Given a string (packagename or virtual) expand it into a valid
16 cat/package string. Virtuals use the mydb to determine which provided
17 virtual is a valid choice and defaults to the first element when there
18 are no installed/available candidates."""
19 myslash=mycpv.split("/")
20 mysplit = _pkgsplit(myslash[-1])
21 if settings is None:
22 try:
23 settings = mydb.settings
24 except AttributeError:
25 settings = portage.settings
26 if len(myslash)>2:
27
28 mysplit=[]
29 mykey=mycpv
30 elif len(myslash)==2:
31 if mysplit:
32 mykey=myslash[0]+"/"+mysplit[0]
33 else:
34 mykey=mycpv
35
36
37
38
39
40
41 if mykey.startswith("virtual/") and \
42 hasattr(mydb, "cp_list") and \
43 not mydb.cp_list(mykey, use_cache=use_cache):
44 if hasattr(mydb, "vartree"):
45 settings._populate_treeVirtuals_if_needed(mydb.vartree)
46 virts = settings.getvirtuals().get(mykey)
47 if virts:
48 mykey_orig = mykey
49 for vkey in virts:
50
51
52
53
54 if mydb.cp_list(vkey.cp):
55 mykey = str(vkey)
56 break
57 if mykey == mykey_orig:
58 mykey = str(virts[0])
59
60 else:
61
62 if mysplit:
63 myp=mysplit[0]
64 else:
65
66 myp=mycpv
67 mykey=None
68 matches=[]
69 if mydb and hasattr(mydb, "categories"):
70 for x in mydb.categories:
71 if mydb.cp_list(x+"/"+myp,use_cache=use_cache):
72 matches.append(x+"/"+myp)
73 if len(matches) > 1:
74 virtual_name_collision = False
75 if len(matches) == 2:
76 for x in matches:
77 if not x.startswith("virtual/"):
78
79
80
81
82 mykey = x
83 else:
84 virtual_name_collision = True
85 if not virtual_name_collision:
86
87
88
89 raise AmbiguousPackageName(matches)
90 elif matches:
91 mykey=matches[0]
92
93 if not mykey and not isinstance(mydb, list):
94 if hasattr(mydb, "vartree"):
95 settings._populate_treeVirtuals_if_needed(mydb.vartree)
96 virts_p = settings.get_virts_p().get(myp)
97 if virts_p:
98 mykey = virts_p[0]
99
100 if not mykey:
101 mykey="null/"+myp
102 if mysplit:
103 if mysplit[2]=="r0":
104 return mykey+"-"+mysplit[1]
105 else:
106 return mykey+"-"+mysplit[1]+"-"+mysplit[2]
107 else:
108 return mykey
109