Merge branch 'rc2'
[revdep-rebuild-reimplementation.git] / libs.py.2.2_rc6.patch
blob80ab2b0d9fbdf821c64f0f580f494bf0c6546249
1 --- libs.py.2.2_rc6 2008-08-16 20:34:44.000000000 -0500
2 +++ pym/portage/sets/libs.py 2008-08-17 02:43:00.000000000 -0500
3 @@ -2,10 +2,18 @@
4 # Distributed under the terms of the GNU General Public License v2
5 # $Id: libs.py 10759 2008-06-22 04:04:50Z zmedico $
7 +import os
8 +import re
9 +import time
10 +from portage.dbapi.vartree import dblink
11 +from portage.versions import catsplit
12 from portage.sets.base import PackageSet
13 from portage.sets import get_boolean
14 from portage.versions import catpkgsplit
16 +__all__ = ["LibraryConsumerSet", "PreservedLibraryConsumerSet",
17 + "MissingLibraryConsumerSet"]
19 class LibraryConsumerSet(PackageSet):
20 _operations = ["merge", "unmerge"]
22 @@ -45,3 +53,317 @@
23 debug = get_boolean(options, "debug", False)
24 return PreservedLibraryConsumerSet(trees["vartree"].dbapi, debug)
25 singleBuilder = classmethod(singleBuilder)
28 +class MissingLibraryConsumerSet(LibraryConsumerSet):
30 + """
31 + This class is the set of packages to emerge due to missing libraries.
33 + This class scans binaries for missing and broken shared library dependencies
34 + and fixes them by emerging the packages containing the broken binaries.
36 + The user may also emerge packages containing consumers of specified
37 + libraries by passing the name or a python regular expression through the
38 + environment variable, LIBRARY. Due to a limitation in passing flags to
39 + package sets through the portage cli, the user must set environment
40 + variables to modify the behaviour of this package set. So if the
41 + environment variable LIBRARY is set, the behaviour of this set changes.
43 + """
45 + description = "The set of packages to emerge due to missing libraries."
46 + _operations = ["merge"]
48 + def __init__(self, vardbapi, debug=False):
49 + super(MissingLibraryConsumerSet, self).__init__(vardbapi, debug)
50 + # FIXME Since we can't get command line arguments from the user, the
51 + # soname can be passed through an environment variable for now.
52 + self.libraryRegexp = os.getenv("LIBRARY")
53 + self.root = self.dbapi.root
54 + self.linkmap = self.dbapi.linkmap
56 + def load(self):
57 + # brokenDependencies: object -> set-of-unsatisfied-dependencies, where
58 + # object is an installed binary/library and
59 + # set-of-unsatisfied-dependencies are sonames or libraries required by
60 + # the object but have no corresponding libraries to fulfill the
61 + # dependency.
62 + brokenDependencies = {}
63 + atoms = set()
65 + # If the LIBRARY environment variable is set, the resulting package set
66 + # will be packages containing consumers of the libraries matched by the
67 + # variable.
68 + if self.libraryRegexp:
69 + atoms = self.findAtomsOfLibraryConsumers(self.libraryRegexp)
70 + self._setAtoms(atoms)
71 + if self.debug:
72 + print
73 + print "atoms to be emerged:"
74 + for x in sorted(atoms):
75 + print x
76 + return
78 + # Get the list of broken dependencies from LinkageMap.
79 + if self.debug:
80 + timeStart = time.time()
81 + brokenDependencies = self.linkmap.listBrokenBinaries()
82 + if self.debug:
83 + timeListBrokenBinaries = time.time() - timeStart
85 + # Add broken libtool libraries into the brokenDependencies dict.
86 + if self.debug:
87 + timeStart = time.time()
88 + brokenDependencies.update(self.listBrokenLibtoolLibraries())
89 + if self.debug:
90 + timeLibtool = time.time() - timeStart
92 + # FIXME Too many atoms may be emerged because libraries in binary
93 + # packages are not being handled properly eg openoffice, nvidia-drivers,
94 + # sun-jdk. Certain binaries are run in an environment where additional
95 + # library paths are added via LD_LIBRARY_PATH. Since these paths aren't
96 + # registered in _obj_properties, they appear broken (and are if not run
97 + # in the correct environment). I have to determine if libraries and lib
98 + # paths should be masked using /etc/revdep-rebuild/* as done in
99 + # revdep-rebuild or if there is a better way to identify and deal with
100 + # these problematic packages (or if something entirely different should
101 + # be done). For now directory and library masks are used.
103 + # Remove masked directories and libraries.
104 + if self.debug:
105 + timeStart = time.time()
106 + if brokenDependencies:
107 + brokenDependencies = self.removeMaskedDependencies(brokenDependencies)
108 + if self.debug:
109 + timeMask = time.time() - timeStart
111 + # Determine atoms to emerge based on broken objects in
112 + # brokenDependencies.
113 + if self.debug:
114 + timeStart = time.time()
115 + if brokenDependencies:
116 + atoms = self.mapPathsToAtoms(set(brokenDependencies.keys()))
117 + if self.debug:
118 + timeAtoms = time.time() - timeStart
120 + # Debug output
121 + if self.debug:
122 + print
123 + print len(brokenDependencies), "brokenDependencies:"
124 + for x in sorted(brokenDependencies.keys()):
125 + print
126 + print x, "->"
127 + print '\t', brokenDependencies[x]
128 + print
129 + print "atoms to be emerged:"
130 + for x in sorted(atoms):
131 + print x
132 + print
133 + print "Broken binaries time:", timeListBrokenBinaries
134 + print "Broken libtool time:", timeLibtool
135 + print "Remove mask time:", timeMask
136 + print "mapPathsToAtoms time:", timeAtoms
137 + print
139 + self._setAtoms(atoms)
141 + def removeMaskedDependencies(self, dependencies):
142 + """
143 + Remove all masked dependencies and return the updated mapping.
145 + @param dependencies: dependencies from which to removed masked
146 + dependencies
147 + @type dependencies: dict (example: {'/usr/bin/foo': set(['libfoo.so'])})
148 + @rtype: dict
149 + @return: shallow copy of dependencies with masked items removed
151 + """
152 + rValue = dependencies.copy()
153 + dirMask, libMask = self.getDependencyMasks()
155 + # Remove entries that are masked.
156 + if dirMask or libMask:
157 + if self.debug:
158 + print "The following are masked:"
159 + for binary, libSet in rValue.items():
160 + for directory in dirMask:
161 + # Check if the broken binary lies within the masked directory or
162 + # its subdirectories.
163 + # XXX Perhaps we should allow regexps as masks.
164 + if binary.startswith(directory):
165 + del rValue[binary]
166 + if self.debug:
167 + print "dirMask:",binary
168 + break
169 + # Check if all the required libraries are masked.
170 + if binary in rValue and libSet.issubset(libMask):
171 + del rValue[binary]
172 + if self.debug:
173 + print "libMask:", binary, libSet & libMask
175 + if self.debug:
176 + print
177 + print "Directory mask:", dirMask
178 + print
179 + print "Library mask:", libMask
181 + return rValue
183 + def getDependencyMasks(self):
184 + """
185 + Return all dependency masks as a tuple.
187 + @rtype: 2-tuple of sets of strings
188 + @return: 2-tuple in which the first component is a set of directory
189 + masks and the second component is a set of library masks
191 + """
192 + dirMask = set()
193 + libMask = set()
194 + _dirMask_re = re.compile(r'SEARCH_DIRS_MASK\s*=\s*"([^"]*)"')
195 + _libMask_re = re.compile(r'LD_LIBRARY_MASK\s*=\s*"([^"]*)"')
196 + lines = []
198 + # Reads the contents of /etc/revdep-rebuild/*
199 + libMaskDir = os.path.join(self.root, "etc", "revdep-rebuild")
200 + if os.path.exists(libMaskDir):
201 + for file in os.listdir(libMaskDir):
202 + try:
203 + f = open(os.path.join(libMaskDir, file), "r")
204 + try:
205 + lines.extend(f.readlines())
206 + finally:
207 + f.close()
208 + except IOError: # OSError?
209 + continue
210 + # The following parses SEARCH_DIRS_MASK and LD_LIBRARY_MASK variables
211 + # from /etc/revdep-rebuild/*
212 + for line in lines:
213 + matchDir = _dirMask_re.match(line)
214 + matchLib = _libMask_re.match(line)
215 + if matchDir:
216 + dirMask.update(set(matchDir.group(1).split()))
217 + if matchLib:
218 + libMask.update(set(matchLib.group(1).split()))
220 + # These directories contain specially evaluated libraries.
221 + # app-emulation/vmware-workstation-6.0.1.55017
222 + dirMask.add('/opt/vmware/workstation/lib')
223 + # app-emulation/vmware-server-console-1.0.6.91891
224 + dirMask.add('/opt/vmware/server/console/lib')
225 + # www-client/mozilla-firefox-2.0.0.15
226 + dirMask.add('/usr/lib/mozilla-firefox/plugins')
227 + dirMask.add('/usr/lib64/mozilla-firefox/plugins')
228 + # app-office/openoffice-2.4.1
229 + dirMask.add('/opt/OpenOffice')
230 + dirMask.add('/usr/lib/openoffice')
231 + # dev-libs/libmix-2.05 libmix.so is missing soname entry
232 + libMask.add('libmix.so')
233 + # app-accessibility/speech-tools-1.2.96_beta missing sonames
234 + libMask.add('libestools.so')
235 + libMask.add('libestbase.so')
236 + libMask.add('libeststring.so')
237 + # app-emulation/emul-linux-x86-soundlibs-20080418
238 + dirMask.add('/usr/kde/3.5/lib32')
240 + return (dirMask, libMask)
242 + def findAtomsOfLibraryConsumers(self, searchString):
243 + """
244 + Return atoms containing consumers of libraries matching the argument.
246 + @param searchString: a string used to search for libraries
247 + @type searchString: string to be compiled as a regular expression
248 + (example: 'libfoo.*')
249 + @rtype: set of strings
250 + @return: the returned set of atoms are valid to be used by package sets
252 + """
253 + atoms = set()
254 + consumers = set()
255 + matchedLibraries = set()
256 + libraryObjects = []
257 + _librarySearch_re = re.compile(searchString)
259 + # Find libraries matching searchString.
260 + libraryObjects = self.linkmap.listLibraryObjects()
261 + for library in libraryObjects:
262 + m = _librarySearch_re.search(library)
263 + if m:
264 + matchedLibraries.add(library)
265 + consumers.update(self.linkmap.findConsumers(library))
267 + if self.debug:
268 + print
269 + print "Consumers of the following libraries will be emerged:"
270 + for x in matchedLibraries:
271 + print x
273 + if consumers:
274 + # The following prevents emerging the packages that own the matched
275 + # libraries. Note that this will prevent updating the packages owning
276 + # the libraries if there are newer versions available in the installed
277 + # slot. See bug #30095
278 + atoms = self.mapPathsToAtoms(consumers)
279 + libraryOwners = self.mapPathsToAtoms(matchedLibraries)
280 + atoms.difference_update(libraryOwners)
282 + return atoms
284 + def listBrokenLibtoolLibraries(self):
285 + """
286 + Find broken libtool libraries and their missing dependencies.
288 + @rtype: dict (example: {'/lib/libfoo.la': set(['/lib/libbar.la'])})
289 + @return: The return value is a library -> set-of-libraries mapping, where
290 + library is a broken library and the set consists of dependencies
291 + needed by library that do not exist on the filesystem.
293 + """
294 + rValue = {}
295 + lines = []
296 + dependencies = []
297 + _la_re = re.compile(r".*\.la$")
298 + _dependency_libs_re = re.compile(r"^dependency_libs\s*=\s*'(.*)'")
300 + # Loop over the contents of all packages.
301 + for cpv in self.dbapi.cpv_all():
302 + mysplit = catsplit(cpv)
303 + link = dblink(mysplit[0], mysplit[1], myroot=self.dbapi.root, \
304 + mysettings=self.dbapi.settings, treetype='vartree', \
305 + vartree=self.dbapi.vartree)
306 + for file in link.getcontents():
307 + # Check if the file ends with '.la'.
308 + matchLib = _la_re.match(file)
309 + if matchLib:
310 + # Read the lines from the library.
311 + lines = []
312 + try:
313 + f = open(file, "r")
314 + try:
315 + lines.extend(f.readlines())
316 + finally:
317 + f.close()
318 + except IOError:
319 + continue
320 + # Find the line listing the dependencies.
321 + for line in lines:
322 + matchLine = _dependency_libs_re.match(line)
323 + if matchLine:
324 + dependencies = matchLine.group(1).split()
325 + # For each dependency that is a pathname (begins with
326 + # os.sep), check that it exists on the filesystem. If it
327 + # does not exist, then add the library and the missing
328 + # dependency to rValue.
329 + for dependency in dependencies:
330 + if dependency[0] == os.sep and \
331 + not os.path.isfile(dependency):
332 + rValue.setdefault(file, set()).add(dependency)
334 + return rValue
336 + def singleBuilder(self, options, settings, trees):
337 + debug = get_boolean(options, "debug", False)
338 + return MissingLibraryConsumerSet(trees["vartree"].dbapi, debug)
339 + singleBuilder = classmethod(singleBuilder)