return errors
+
+
+def checkDict(dict):
+ noneInstalled = []
+ noneAffected = []
+
+ # iterate over all entries in the dict
+ for entry_cp in dict.keys():
+ # find installed packages of type entry
+ iPkgs = portage.vardbapi(portage.root).match(entry_cp)
+
+ # check if packages are installed
+ if not len(iPkgs):
+ noneInstalled.append(", ".join(dict[entry_cp]))
+ continue
+
+ count = 0
+ # now check for every installed package, if it is hit by the entry
+ for i in iPkgs:
+ count += len(portage.match_to_list(i, dict[entry_cp]))
+ if count > 0:
+ break;
+ if count == 0:
+ noneAffected.append(", ".join(dict[entry_cp]))
+ continue
+
+ #check if there are entries in the file, that didn't match
+ #ie: package.keywords contains "=kde-base/kde-3.0.0 ... and =kde-base/kde-2.0.0 ..."
+ # now if kde-4.0.0 is installed, kde-3.0.0 is still not recognized as redundant, as it
+ # gives a match and count > 0.
+ #solution: do the check again for single entries like "=*" and "~*"
+ if len(dict[entry_cp]) > 1:
+ for x in dict[entry_cp]:
+ if x[0] == "=" or x[0] == "~":
+ count = 0
+ # now check for every installed package, if it is hit by the entry
+ for i in iPkgs:
+ count += len(portage.match_to_list(i, [x]))
+ if count > 0:
+ break
+ if count == 0:
+ noneAffected.append(x)
+
+ return (noneInstalled, noneAffected)
+
+def fixFile(fileName, noneInstalled, noneAffected):
+ errors = []
+ strNoneInstalled = "NONE INSTALLED"
+ strNoneAffected = "NONE AFFECTED"
+ try:
+ ifile = open(fileName)
+ lines = ifile.readlines()
+ ifile.close()
+
+ ofile = open(fileName, "w")
+
+ # now check every line if it contains a problematic entry
+ for line in lines:
+ prefix = ""
+ for x in noneInstalled:
+ if line.find(x) >= 0:
+ prefix += strNoneInstalled
+ for x in noneAffected:
+ if line.find(x) >= 0:
+ prefix += strNoneAffected
+
+ if prefix:
+ prefix = "# "+prefix+" "
+ errors.append("fixed: " + prefix + line.strip()) #remove \n
+
+ ofile.write(prefix+line)
+
+ ofile.close()
+
+ except OSError:
+ errors.append(fileName + " could not be opened for reading or writing")
+
+ return errors
+
+
+class PackageKeywordsHandler(object):
+
+ def name():
+ return "p.keywords"
+ name = staticmethod(name)
+
+
+ def __init__(self):
+ print "init"
+ self.noneInstalled = []
+ self.noneAffected = []
+
+ # cache the config object
+ cfg = portage.config(config_profile_path=portage.root, config_incrementals=portage_const.INCREMENTALS)
+
+ #transforming the format of pkeywordsdict into something like punmaskdict i.e. key : [entry1, entry2, ...], key : [...
+ dict = {}
+ for k, v in cfg.pkeywordsdict.items():
+ t = []
+ for k2, v2 in v.items():
+ t.append(k2)
+ dict[k] = t
+
+ (self.noneInstalled , self.noneAffected) = checkDict( dict )
+
+ def check(self):
+ errors = []
+ errors += map(lambda x: "'%s' has no installed packages" % x, self.noneInstalled)
+ errors += map(lambda x: "'%s' affects no installed package(-version)" % x, self.noneAffected)
+ return errors
+
+ def fix(self):
+ return fixFile("/etc/portage/package.keywords", self.noneInstalled, self.noneAffected)
+
+
+class PackageUnmaskHandler(object):
+
+ def name():
+ return "p.unmask"
+ name = staticmethod(name)
+
+ def __init__(self):
+ print "init"
+ self.noneInstalled = []
+ self.noneAffected = []
+
+ # cache the config object
+ cfg = portage.config(config_profile_path=portage.root, config_incrementals=portage_const.INCREMENTALS)
+
+ (self.noneInstalled , self.noneAffected) = checkDict( cfg.punmaskdict )
+
+ def check(self):
+ errors = []
+ errors += map(lambda x: "'%s' has no installed packages" % x, self.noneInstalled)
+ errors += map(lambda x: "'%s' affects no installed package(-version)" % x, self.noneAffected)
+ return errors
+
+ def fix(self):
+ return fixFile("/etc/portage/package.unmask", self.noneInstalled, self.noneAffected)
+
+
+
+
# this sucks, should track this in a different manner.
-#modules = {"world" : WorldHandler,
-# "vdbkeys": VdbKeyHandler}
-modules = {"world" : WorldHandler}
+modules = {"world" : WorldHandler,
+ "vdbkeys": VdbKeyHandler,
+ "p.keywords" : PackageKeywordsHandler,
+ "p.unmask" : PackageUnmaskHandler}
+
+#modules = {"world" : WorldHandler}
module_names = modules.keys()
module_names.sort()