8159cc35f946d9fbc4eaf702fa93b6df0f431ea1
[portage.git] / pym / portage / sets / libs.py
1 # Copyright 2007 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 from portage.sets.base import PackageSet
6 from portage.dbapi.vartree import dblink
7 from portage.versions import catsplit, catpkgsplit
8
9 import os
10
11 class LibraryConsumerSet(PackageSet):
12         _operations = ["merge", "unmerge"]
13
14         def __init__(self, vardbapi, debug=False):
15                 super(LibraryConsumerSet, self).__init__()
16                 self.dbapi = vardbapi
17                 self.debug = debug
18
19         def mapPathsToAtoms(self, paths):
20                 rValue = set()
21                 for cpv in self.dbapi.cpv_all():
22                         mysplit = catsplit(cpv)
23                         link = dblink(mysplit[0], mysplit[1], myroot=self.dbapi.root, \
24                                         mysettings=self.dbapi.settings, treetype='vartree', \
25                                         vartree=self.dbapi.vartree)
26                         if paths.intersection(link.getcontents()):
27                                 cat, pn = catpkgsplit(cpv)[:2]
28                                 slot = self.dbapi.aux_get(cpv, ["SLOT"])[0]
29                                 rValue.add("%s/%s:%s" % (cat, pn, slot))
30                 return rValue
31         
32
33 class PreservedLibraryConsumerSet(LibraryConsumerSet):
34         def load(self):
35                 reg = self.dbapi.plib_registry
36                 consumers = set()
37                 if reg:
38                         for libs in reg.getPreservedLibs().values():
39                                 for lib in libs:
40                                         if self.debug:
41                                                 print lib
42                                                 for x in sorted(self.dbapi.linkmap.findConsumers(lib)):
43                                                         print "    ", x
44                                                 print "-"*40
45                                         consumers.update(self.dbapi.linkmap.findConsumers(lib))
46                 else:
47                         return
48                 if not consumers:
49                         return
50                 self._setAtoms(self.mapPathsToAtoms(consumers))
51
52         def singleBuilder(cls, options, settings, trees):
53                 return PreservedLibraryConsumerSet(trees["vartree"].dbapi, True)
54         singleBuilder = classmethod(singleBuilder)