Codebase list debian-goodies / 2a2b412
Improve and fix the use of dpkg-query: - Store in a dictionary all of the result of dpkg-query calls to prevent calling dpkg-query for the same file twice, this speeds up the test a lot if there are many deleted libraries and they all point to the same file (i.e. libc6 update) - do not process dpkg-query until the process has finished properly - send the STDERR output to the pipe for processing and to prevent showing errors in console Javier Fernandez-Sanguino 12 years ago
1 changed file(s) with 40 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
6161 sys.stderr.write('usage: checkrestart [-vhpa]\n')
6262
6363 def main():
64 global lc_all_c_env
64 global lc_all_c_env, file_query_check
6565 process = None
6666 toRestart = {}
6767
6868 lc_all_c_env = os.environ
6969 lc_all_c_env['LC_ALL'] = 'C'
70 file_query_check = {}
7071 blacklistFiles = []
7172 blacklist = []
7273
149150 packages = {}
150151 diverted = None
151152 dpkgQuery = ["dpkg-query", "--search"] + programs.keys()
152 dpkgProc = subprocess.Popen(dpkgQuery, stdout=subprocess.PIPE, stderr=None,
153 dpkgProc = subprocess.Popen(dpkgQuery, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
153154 env = lc_all_c_env)
155 if verbose:
156 print "Running:%s" % dpkgQuery
154157 for line in dpkgProc.stdout.readlines():
155158 if line.startswith('local diversion'):
156159 continue
185188 if package == 'util-linux':
186189 continue
187190 dpkgQuery = ["dpkg-query", "--listfiles", package.name]
188 dpkgProc = subprocess.Popen(dpkgQuery, stdout=subprocess.PIPE, stderr=None,
191 dpkgProc = subprocess.Popen(dpkgQuery, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
189192 env = lc_all_c_env)
190193 for line in dpkgProc.stdout.readlines():
191194 path = line[:-1]
267270 processes.values())
268271 return toRestart
269272
273 # Tells if a file is part of a package
274 # Returns:
275 # - False - file does not exist in the system or cannot be found when querying the package database
276 # - True - file is found in an operating system package
277 def ispackagedFile (f):
278 file_in_package = False
279 # First check if the file exists, do not call dpkg-query
280 # if the file simply does not exist in the file system
281 if os.path.exists(f):
282 # If it exists, run dpkg-query
283 dpkgQuery = ["dpkg-query", "--search", f]
284 dpkgProc = subprocess.Popen(dpkgQuery, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
285 env = lc_all_c_env, close_fds=True)
286 dpkgProc.wait()
287 if verbose:
288 print "Running:%s" % dpkgQuery
289 for line in dpkgProc.stdout.readlines():
290 line = line.strip()
291 if line.find('no path found matching pattern ' + f) > 0:
292 file_in_package = False
293 break
294 if line.endswith(f):
295 file_in_package = True
296 break
297
298 return file_in_package
299
270300 # Tells if a file has to be considered a deleted file
271301 # Returns:
272302 # - 0 (NO) for known locations of files which might be deleted
273303 # - 1 (YES) for valid deleted files we are interested in
274304 def isdeletedFile (f, blacklist = None):
275305
276 global lc_all_c_env
306 global lc_all_c_env, file_query_check
277307
278308 if allFiles:
279309 return 1
329359 return 0
330360 # Skip, if asked to, files that do not belong to any package
331361 if onlyPackageFiles:
332 file_in_package = False
333362 # Remove some lsof information from the file to ensure that it is
334363 # a proper filename
335364 file_name = re.sub(r'\(.*\)','', f)
336365 file_name = re.sub(r'\s+$','', file_name)
337 dpkgQuery = ["dpkg-query", "--search", file_name]
338 dpkgProc = subprocess.Popen(dpkgQuery, stdout=subprocess.PIPE, stderr=None,
339 env = lc_all_c_env)
340 for line in dpkgProc.stdout.readlines():
341 if line.endswith(f):
342 file_in_package = True
343 break
344 if not file_in_package:
345 return 0
366 # First check: have we checked this file before? If we have not then make the check
367 if not file_name in file_query_check:
368 file_query_check[file_name] = ispackagedFile(file_name)
369 # Once we have the result then check if the file belongs to a package
370 if not file_query_check[file_name]:
371 return 0
346372
347373 # TODO: it should only care about library files (i.e. /lib, /usr/lib and the like)
348374 # build that check with a regexp to exclude others