merged patch files.
[revdep-rebuild-reimplementation.git] / libs.py.2.2_rc6.patch
blobcc452afc9ec65135c0ee56ce1800919e31fc8f26
1 --- libs.py.2.2_rc6 2008-08-01 15:41:14.000000000 -0500
2 +++ pym/portage/sets/libs.py 2008-08-11 22:43:15.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,311 @@
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')
234 + return (dirMask, libMask)
236 + def findAtomsOfLibraryConsumers(self, searchString):
237 + """
238 + Return atoms containing consumers of libraries matching the argument.
240 + @param searchString: a string used to search for libraries
241 + @type searchString: string to be compiled as a regular expression
242 + (example: 'libfoo.*')
243 + @rtype: set of strings
244 + @return: the returned set of atoms are valid to be used by package sets
246 + """
247 + atoms = set()
248 + consumers = set()
249 + matchedLibraries = set()
250 + libraryObjects = []
251 + _librarySearch_re = re.compile(searchString)
253 + # Find libraries matching searchString.
254 + libraryObjects = self.linkmap.listLibraryObjects()
255 + for library in libraryObjects:
256 + m = _librarySearch_re.search(library)
257 + if m:
258 + matchedLibraries.add(library)
259 + consumers.update(self.linkmap.findConsumers(library))
261 + if self.debug:
262 + print
263 + print "Consumers of the following libraries will be emerged:"
264 + for x in matchedLibraries:
265 + print x
267 + if consumers:
268 + # The following prevents emerging the packages that own the matched
269 + # libraries. Note that this will prevent updating the packages owning
270 + # the libraries if there are newer versions available in the installed
271 + # slot. See bug #30095
272 + atoms = self.mapPathsToAtoms(consumers)
273 + libraryOwners = self.mapPathsToAtoms(matchedLibraries)
274 + atoms.difference_update(libraryOwners)
276 + return atoms
278 + def listBrokenLibtoolLibraries(self):
279 + """
280 + Find broken libtool libraries and their missing dependencies.
282 + @rtype: dict (example: {'/lib/libfoo.la': set(['/lib/libbar.la'])})
283 + @return: The return value is a library -> set-of-libraries mapping, where
284 + library is a broken library and the set consists of dependencies
285 + needed by library that do not exist on the filesystem.
287 + """
288 + rValue = {}
289 + lines = []
290 + dependencies = []
291 + _la_re = re.compile(r".*\.la$")
292 + _dependency_libs_re = re.compile(r"^dependency_libs\s*=\s*'(.*)'")
294 + # Loop over the contents of all packages.
295 + for cpv in self.dbapi.cpv_all():
296 + mysplit = catsplit(cpv)
297 + link = dblink(mysplit[0], mysplit[1], myroot=self.dbapi.root, \
298 + mysettings=self.dbapi.settings, treetype='vartree', \
299 + vartree=self.dbapi.vartree)
300 + for file in link.getcontents():
301 + # Check if the file ends with '.la'.
302 + matchLib = _la_re.match(file)
303 + if matchLib:
304 + # Read the lines from the library.
305 + lines = []
306 + try:
307 + f = open(file, "r")
308 + try:
309 + lines.extend(f.readlines())
310 + finally:
311 + f.close()
312 + except IOError:
313 + continue
314 + # Find the line listing the dependencies.
315 + for line in lines:
316 + matchLine = _dependency_libs_re.match(line)
317 + if matchLine:
318 + dependencies = matchLine.group(1).split()
319 + # For each dependency that is a pathname (begins with
320 + # os.sep), check that it exists on the filesystem. If it
321 + # does not exist, then add the library and the missing
322 + # dependency to rValue.
323 + for dependency in dependencies:
324 + if dependency[0] == os.sep and \
325 + not os.path.isfile(dependency):
326 + rValue.setdefault(file, set()).add(dependency)
328 + return rValue
330 + def singleBuilder(self, options, settings, trees):
331 + debug = get_boolean(options, "debug", False)
332 + return MissingLibraryConsumerSet(trees["vartree"].dbapi, debug)
333 + singleBuilder = classmethod(singleBuilder)