Package portage :: Package dbapi :: Module cpv_expand
[hide private]

Source Code for Module portage.dbapi.cpv_expand

  1  # Copyright 2010-2013 Gentoo Foundation 
  2  # Distributed under the terms of the GNU General Public License v2 
  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 # this is illegal case. 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 # Since Gentoo stopped using old-style virtuals in 37 # 2011, typically it's possible to avoid getvirtuals() 38 # calls entirely. Therefore, only call getvirtuals() 39 # if the atom category is "virtual" and cp_list() 40 # returns nothing. 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 # The virtuals file can contain a versioned atom, so 51 # it may be necessary to remove the operator and 52 # version from the atom before it is passed into 53 # dbapi.cp_list(). 54 if mydb.cp_list(vkey.cp): 55 mykey = str(vkey) 56 break 57 if mykey == mykey_orig: 58 mykey = str(virts[0]) 59 #we only perform virtual expansion if we are passed a dbapi 60 else: 61 #specific cpv, no category, ie. "foo-1.0" 62 if mysplit: 63 myp=mysplit[0] 64 else: 65 # "foo" ? 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 # Assume that the non-virtual is desired. This helps 79 # avoid the ValueError for invalid deps that come from 80 # installed packages (during reverse blocker detection, 81 # for example). 82 mykey = x 83 else: 84 virtual_name_collision = True 85 if not virtual_name_collision: 86 # AmbiguousPackageName inherits from ValueError, 87 # for backward compatibility with calling code 88 # that already handles ValueError. 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 #again, we only perform virtual expansion if we have a dbapi (not a list) 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