Remove all svn $Id keywords.
[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 match_from_list
10 from portage.localization import _
11 from portage.util import grablines, normalize_path
12 from portage.versions import catpkgsplit
13
14 def getmaskingreason(mycpv, metadata=None, settings=None, portdb=None, return_location=False):
15         if settings is None:
16                 settings = portage.settings
17         if portdb is None:
18                 portdb = portage.portdb
19         mysplit = catpkgsplit(mycpv)
20         if not mysplit:
21                 raise ValueError(_("invalid CPV: %s") % mycpv)
22         if metadata is None:
23                 db_keys = list(portdb._aux_cache_keys)
24                 try:
25                         metadata = dict(zip(db_keys, portdb.aux_get(mycpv, db_keys)))
26                 except KeyError:
27                         if not portdb.cpv_exists(mycpv):
28                                 raise
29         if metadata is None:
30                 # Can't access SLOT due to corruption.
31                 cpv_slot_list = [mycpv]
32         else:
33                 cpv_slot_list = ["%s:%s" % (mycpv, metadata["SLOT"])]
34         mycp=mysplit[0]+"/"+mysplit[1]
35
36         # XXX- This is a temporary duplicate of code from the config constructor.
37         locations = [os.path.join(settings["PORTDIR"], "profiles")]
38         locations.extend(settings.profiles)
39         for ov in settings["PORTDIR_OVERLAY"].split():
40                 profdir = os.path.join(normalize_path(ov), "profiles")
41                 if os.path.isdir(profdir):
42                         locations.append(profdir)
43         locations.append(os.path.join(settings["PORTAGE_CONFIGROOT"],
44                 USER_CONFIG_PATH))
45         locations.reverse()
46         pmasklists = [(x, grablines(os.path.join(x, "package.mask"), recursive=1)) for x in locations]
47
48         if mycp in settings.pmaskdict:
49                 for x in settings.pmaskdict[mycp]:
50                         if match_from_list(x, cpv_slot_list):
51                                 for pmask in pmasklists:
52                                         comment = ""
53                                         comment_valid = -1
54                                         pmask_filename = os.path.join(pmask[0], "package.mask")
55                                         for i in range(len(pmask[1])):
56                                                 l = pmask[1][i].strip()
57                                                 if l == "":
58                                                         comment = ""
59                                                         comment_valid = -1
60                                                 elif l[0] == "#":
61                                                         comment += (l+"\n")
62                                                         comment_valid = i + 1
63                                                 elif l == x:
64                                                         if comment_valid != i:
65                                                                 comment = ""
66                                                         if return_location:
67                                                                 return (comment, pmask_filename)
68                                                         else:
69                                                                 return comment
70                                                 elif comment_valid != -1:
71                                                         # Apparently this comment applies to muliple masks, so
72                                                         # it remains valid until a blank line is encountered.
73                                                         comment_valid += 1
74         if return_location:
75                 return (None, None)
76         else:
77                 return None