Patched up LinkageMap.listBrokenBinaries
[revdep-rebuild-reimplementation.git] / libs.py.2.2_rc6.patch
blob634f441f34c949b8b73deebaf24810e018e3eea8
1 --- libs.py.2.2_rc6 2008-08-01 15:41:14.000000000 -0500
2 +++ pym/portage/sets/libs.py 2008-08-14 01:49:42.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,318 @@
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 + if self.debug:
79 + timeStart = time.time()
80 + self.linkmap.rebuild()
81 + if self.debug:
82 + timeRebuild = time.time() - timeStart
84 + # Get the list of broken dependencies from LinkageMap.
85 + if self.debug:
86 + timeStart = time.time()
87 + brokenDependencies = self.linkmap.listBrokenBinaries()
88 + if self.debug:
89 + timeListBrokenBinaries = time.time() - timeStart
91 + # Add broken libtool libraries into the brokenDependencies dict.
92 + if self.debug:
93 + timeStart = time.time()
94 + brokenDependencies.update(self.listBrokenLibtoolLibraries())
95 + if self.debug:
96 + timeLibtool = time.time() - timeStart
98 + # FIXME Too many atoms may be emerged because libraries in binary
99 + # packages are not being handled properly eg openoffice, nvidia-drivers,
100 + # sun-jdk. Certain binaries are run in an environment where additional
101 + # library paths are added via LD_LIBRARY_PATH. Since these paths aren't
102 + # registered in _obj_properties, they appear broken (and are if not run
103 + # in the correct environment). I have to determine if libraries and lib
104 + # paths should be masked using /etc/revdep-rebuild/* as done in
105 + # revdep-rebuild or if there is a better way to identify and deal with
106 + # these problematic packages (or if something entirely different should
107 + # be done). For now directory and library masks are used.
109 + # Remove masked directories and libraries.
110 + if self.debug:
111 + timeStart = time.time()
112 + if brokenDependencies:
113 + brokenDependencies = self.removeMaskedDependencies(brokenDependencies)
114 + if self.debug:
115 + timeMask = time.time() - timeStart
117 + # Determine atoms to emerge based on broken objects in
118 + # brokenDependencies.
119 + if self.debug:
120 + timeStart = time.time()
121 + if brokenDependencies:
122 + atoms = self.mapPathsToAtoms(set(brokenDependencies.keys()))
123 + if self.debug:
124 + timeAtoms = time.time() - timeStart
126 + # Debug output
127 + if self.debug:
128 + print
129 + print len(brokenDependencies), "brokenDependencies:"
130 + for x in sorted(brokenDependencies.keys()):
131 + print
132 + print x, "->"
133 + print '\t', brokenDependencies[x]
134 + print
135 + print "atoms to be emerged:"
136 + for x in sorted(atoms):
137 + print x
138 + print
139 + print "Rebuild time:", timeRebuild
140 + print "Broken binaries time:", timeListBrokenBinaries
141 + print "Broken libtool time:", timeLibtool
142 + print "Remove mask time:", timeMask
143 + print "mapPathsToAtoms time:", timeAtoms
144 + print
146 + self._setAtoms(atoms)
148 + def removeMaskedDependencies(self, dependencies):
149 + """
150 + Remove all masked dependencies and return the updated mapping.
152 + @param dependencies: dependencies from which to removed masked
153 + dependencies
154 + @type dependencies: dict (example: {'/usr/bin/foo': set(['libfoo.so'])})
155 + @rtype: dict
156 + @return: shallow copy of dependencies with masked items removed
158 + """
159 + rValue = dependencies.copy()
160 + dirMask, libMask = self.getDependencyMasks()
162 + # Remove entries that are masked.
163 + if dirMask or libMask:
164 + if self.debug:
165 + print "The following are masked:"
166 + for binary, libSet in rValue.items():
167 + for directory in dirMask:
168 + # Check if the broken binary lies within the masked directory or
169 + # its subdirectories.
170 + # XXX Perhaps we should allow regexps as masks.
171 + if binary.startswith(directory):
172 + del rValue[binary]
173 + if self.debug:
174 + print "dirMask:",binary
175 + break
176 + # Check if all the required libraries are masked.
177 + if binary in rValue and libSet.issubset(libMask):
178 + del rValue[binary]
179 + if self.debug:
180 + print "libMask:", binary, libSet & libMask
182 + if self.debug:
183 + print
184 + print "Directory mask:", dirMask
185 + print
186 + print "Library mask:", libMask
188 + return rValue
190 + def getDependencyMasks(self):
191 + """
192 + Return all dependency masks as a tuple.
194 + @rtype: 2-tuple of sets of strings
195 + @return: 2-tuple in which the first component is a set of directory
196 + masks and the second component is a set of library masks
198 + """
199 + dirMask = set()
200 + libMask = set()
201 + _dirMask_re = re.compile(r'SEARCH_DIRS_MASK\s*=\s*"([^"]*)"')
202 + _libMask_re = re.compile(r'LD_LIBRARY_MASK\s*=\s*"([^"]*)"')
203 + lines = []
205 + # Reads the contents of /etc/revdep-rebuild/*
206 + libMaskDir = os.path.join(self.root, "etc", "revdep-rebuild")
207 + if os.path.exists(libMaskDir):
208 + for file in os.listdir(libMaskDir):
209 + try:
210 + f = open(os.path.join(libMaskDir, file), "r")
211 + try:
212 + lines.extend(f.readlines())
213 + finally:
214 + f.close()
215 + except IOError: # OSError?
216 + continue
217 + # The following parses SEARCH_DIRS_MASK and LD_LIBRARY_MASK variables
218 + # from /etc/revdep-rebuild/*
219 + for line in lines:
220 + matchDir = _dirMask_re.match(line)
221 + matchLib = _libMask_re.match(line)
222 + if matchDir:
223 + dirMask.update(set(matchDir.group(1).split()))
224 + if matchLib:
225 + libMask.update(set(matchLib.group(1).split()))
227 + # These directories contain specially evaluated libraries.
228 + # app-emulation/vmware-workstation-6.0.1.55017
229 + dirMask.add('/opt/vmware/workstation/lib')
230 + # app-emulation/vmware-server-console-1.0.6.91891
231 + dirMask.add('/opt/vmware/server/console/lib')
232 + # www-client/mozilla-firefox-2.0.0.15
233 + dirMask.add('/usr/lib/mozilla-firefox/plugins')
234 + dirMask.add('/usr/lib64/mozilla-firefox/plugins')
235 + # app-office/openoffice-2.4.1
236 + dirMask.add('/opt/OpenOffice')
237 + dirMask.add('/usr/lib/openoffice')
238 + # dev-libs/libmix-2.05 libmix.so is missing soname entry
239 + libMask.add('libmix.so')
241 + return (dirMask, libMask)
243 + def findAtomsOfLibraryConsumers(self, searchString):
244 + """
245 + Return atoms containing consumers of libraries matching the argument.
247 + @param searchString: a string used to search for libraries
248 + @type searchString: string to be compiled as a regular expression
249 + (example: 'libfoo.*')
250 + @rtype: set of strings
251 + @return: the returned set of atoms are valid to be used by package sets
253 + """
254 + atoms = set()
255 + consumers = set()
256 + matchedLibraries = set()
257 + libraryObjects = []
258 + _librarySearch_re = re.compile(searchString)
260 + # Find libraries matching searchString.
261 + libraryObjects = self.linkmap.listLibraryObjects()
262 + for library in libraryObjects:
263 + m = _librarySearch_re.search(library)
264 + if m:
265 + matchedLibraries.add(library)
266 + consumers.update(self.linkmap.findConsumers(library))
268 + if self.debug:
269 + print
270 + print "Consumers of the following libraries will be emerged:"
271 + for x in matchedLibraries:
272 + print x
274 + if consumers:
275 + # The following prevents emerging the packages that own the matched
276 + # libraries. Note that this will prevent updating the packages owning
277 + # the libraries if there are newer versions available in the installed
278 + # slot. See bug #30095
279 + atoms = self.mapPathsToAtoms(consumers)
280 + libraryOwners = self.mapPathsToAtoms(matchedLibraries)
281 + atoms.difference_update(libraryOwners)
283 + return atoms
285 + def listBrokenLibtoolLibraries(self):
286 + """
287 + Find broken libtool libraries and their missing dependencies.
289 + @rtype: dict (example: {'/lib/libfoo.la': set(['/lib/libbar.la'])})
290 + @return: The return value is a library -> set-of-libraries mapping, where
291 + library is a broken library and the set consists of dependencies
292 + needed by library that do not exist on the filesystem.
294 + """
295 + rValue = {}
296 + lines = []
297 + dependencies = []
298 + _la_re = re.compile(r".*\.la$")
299 + _dependency_libs_re = re.compile(r"^dependency_libs\s*=\s*'(.*)'")
301 + # Loop over the contents of all packages.
302 + for cpv in self.dbapi.cpv_all():
303 + mysplit = catsplit(cpv)
304 + link = dblink(mysplit[0], mysplit[1], myroot=self.dbapi.root, \
305 + mysettings=self.dbapi.settings, treetype='vartree', \
306 + vartree=self.dbapi.vartree)
307 + for file in link.getcontents():
308 + # Check if the file ends with '.la'.
309 + matchLib = _la_re.match(file)
310 + if matchLib:
311 + # Read the lines from the library.
312 + lines = []
313 + try:
314 + f = open(file, "r")
315 + try:
316 + lines.extend(f.readlines())
317 + finally:
318 + f.close()
319 + except IOError:
320 + continue
321 + # Find the line listing the dependencies.
322 + for line in lines:
323 + matchLine = _dependency_libs_re.match(line)
324 + if matchLine:
325 + dependencies = matchLine.group(1).split()
326 + # For each dependency that is a pathname (begins with
327 + # os.sep), check that it exists on the filesystem. If it
328 + # does not exist, then add the library and the missing
329 + # dependency to rValue.
330 + for dependency in dependencies:
331 + if dependency[0] == os.sep and \
332 + not os.path.isfile(dependency):
333 + rValue.setdefault(file, set()).add(dependency)
335 + return rValue
337 + def singleBuilder(self, options, settings, trees):
338 + debug = get_boolean(options, "debug", False)
339 + return MissingLibraryConsumerSet(trees["vartree"].dbapi, debug)
340 + singleBuilder = classmethod(singleBuilder)