Fix repo atom breakage in getmaskingreason().
[portage.git] / pym / portage / package / ebuild / getmaskingreason.py
1 # Copyright 2010 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 __all__ = ['getmaskingreason']
5
6 import portage
7 from portage import os
8 from portage.const import USER_CONFIG_PATH
9 from portage.dep import Atom, match_from_list, _slot_separator, _repo_separator
10 from portage.exception import InvalidAtom
11 from portage.localization import _
12 from portage.util import grablines, normalize_path
13 from portage.versions import catpkgsplit
14
15 def getmaskingreason(mycpv, metadata=None, settings=None, portdb=None, return_location=False):
16         if settings is None:
17                 settings = portage.settings
18         if portdb is None:
19                 portdb = portage.portdb
20         mysplit = catpkgsplit(mycpv)
21         if not mysplit:
22                 raise ValueError(_("invalid CPV: %s") % mycpv)
23         if metadata is None:
24                 db_keys = list(portdb._aux_cache_keys)
25                 try:
26                         metadata = dict(zip(db_keys, portdb.aux_get(mycpv, db_keys)))
27                 except KeyError:
28                         if not portdb.cpv_exists(mycpv):
29                                 raise
30         if metadata is None:
31                 # Can't access SLOT due to corruption.
32                 cpv_slot_list = [mycpv]
33         else:
34                 pkg = "".join((mycpv, _slot_separator, metadata["SLOT"]))
35                 if 'repository' in metadata:
36                         pkg = "".join((pkg, _repo_separator, metadata['repository']))
37                 cpv_slot_list = [pkg]
38         mycp=mysplit[0]+"/"+mysplit[1]
39
40         # XXX- This is a temporary duplicate of code from the config constructor.
41         locations = [os.path.join(settings["PORTDIR"], "profiles")]
42         locations.extend(settings.profiles)
43         for ov in settings["PORTDIR_OVERLAY"].split():
44                 profdir = os.path.join(normalize_path(ov), "profiles")
45                 if os.path.isdir(profdir):
46                         locations.append(profdir)
47         locations.append(os.path.join(settings["PORTAGE_CONFIGROOT"],
48                 USER_CONFIG_PATH))
49         locations.reverse()
50         pmasklists = [(x, grablines(os.path.join(x, "package.mask"), recursive=1)) for x in locations]
51
52         pmaskdict = settings._mask_manager._pmaskdict
53         if mycp in pmaskdict:
54                 for x in pmaskdict[mycp]:
55                         if match_from_list(x, cpv_slot_list):
56                                 x = x.without_repo
57                                 for pmask in pmasklists:
58                                         comment = ""
59                                         comment_valid = -1
60                                         pmask_filename = os.path.join(pmask[0], "package.mask")
61                                         for i in range(len(pmask[1])):
62                                                 l = pmask[1][i].strip()
63                                                 try:
64                                                         l_atom = Atom(l, allow_repo=True,
65                                                                 allow_wildcard=True).without_repo
66                                                 except InvalidAtom:
67                                                         l_atom = None
68                                                 if l == "":
69                                                         comment = ""
70                                                         comment_valid = -1
71                                                 elif l[0] == "#":
72                                                         comment += (l+"\n")
73                                                         comment_valid = i + 1
74                                                 elif l_atom == x:
75                                                         if comment_valid != i:
76                                                                 comment = ""
77                                                         if return_location:
78                                                                 return (comment, pmask_filename)
79                                                         else:
80                                                                 return comment
81                                                 elif comment_valid != -1:
82                                                         # Apparently this comment applies to muliple masks, so
83                                                         # it remains valid until a blank line is encountered.
84                                                         comment_valid += 1
85         if return_location:
86                 return (None, None)
87         else:
88                 return None