Codebase list debian-goodies / fdcd8ed
- Introduce a new ignore option - Handle properly reading from the spawned dpkg-query processes to prevent blocking Javier Fernandez-Sanguino 11 years ago
1 changed file(s) with 61 addition(s) and 26 deletion(s). Raw diff Collapse all Expand all
5858 return 1
5959
6060 def usage():
61 sys.stderr.write('usage: checkrestart [-vhpa]\n')
61 sys.stderr.write('usage: checkrestart [-vhpa] [-bblacklist] [-iignore]\n')
6262
6363 def main():
6464 global lc_all_c_env, file_query_check
7070 file_query_check = {}
7171 blacklistFiles = []
7272 blacklist = []
73 ignorelist = []
7374
7475 # Process options
7576 try:
76 opts, args = getopt.getopt(sys.argv[1:], "hvpab:", ["help", "verbose", "packages", "all", "blacklist"])
77 opts, args = getopt.getopt(sys.argv[1:], "hvpab:i:", ["help", "verbose", "packages", "all", "blacklist", "ignore"])
7778 except getopt.GetoptError, err:
7879 # print help information and exit:
7980 print str(err) # will print something like "option -x not recognized"
102103 elif o in ("-b", "--blacklist"):
103104 blacklistFiles.append(a)
104105 onlyPackageFiles = False
106 elif o in ("-i", "--ignore"):
107 ignorelist.append(a)
105108 else:
106109 assert False, "unhandled option"
107110
149152
150153 packages = {}
151154 diverted = None
155
152156 dpkgQuery = ["dpkg-query", "--search"] + programs.keys()
153 dpkgProc = subprocess.Popen(dpkgQuery, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
157 dpkgProc = subprocess.Popen(dpkgQuery, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
154158 env = lc_all_c_env)
155 dpkgProc.wait()
156159 if verbose:
157160 print "Running:%s" % dpkgQuery
158 for line in dpkgProc.stdout.readlines():
159 if line.startswith('local diversion'):
160 continue
161
162 m = re.match('^diversion by (\S+) (from|to): (.*)$', line)
163 if m:
164 if m.group(2) == 'from':
165 diverted = m.group(3)
161 while True:
162 line = dpkgProc.stdout.readline()
163 if not line:
164 break
165 if verbose:
166 print "Reading line: %s" % line
167 if line.startswith('local diversion'):
166168 continue
167 if not diverted:
168 raise Exception('Weird error while handling diversion')
169 packagename, program = m.group(1), diverted
170 else:
171 packagename, program = line[:-1].split(': ')
172 if program == diverted:
173 # dpkg prints a summary line after the diversion, name both
174 # packages of the diversion, so ignore this line
175 # mutt-patched, mutt: /usr/bin/mutt
169 if not ':' in line:
176170 continue
177171
178 packages.setdefault(packagename,Package(packagename))
179 packages[packagename].processes.extend(programs[program])
172 m = re.match('^diversion by (\S+) (from|to): (.*)$', line)
173 if m:
174 if m.group(2) == 'from':
175 diverted = m.group(3)
176 continue
177 if not diverted:
178 raise Exception('Weird error while handling diversion')
179 packagename, program = m.group(1), diverted
180 else:
181 packagename, program = line[:-1].split(': ')
182 if program == diverted:
183 # dpkg prints a summary line after the diversion, name both
184 # packages of the diversion, so ignore this line
185 # mutt-patched, mutt: /usr/bin/mutt
186 continue
187 packages.setdefault(packagename,Package(packagename))
188 try:
189 packages[packagename].processes.extend(programs[program])
190 except KeyError:
191 sys.stderr.write ('checkrestart (program not found): %s: %s\n' % (packagename, program))
192 sys.stdout.flush()
193
194 # Close the pipe
195 dpkgProc.stdout.close()
180196
181197 print "(%d distinct packages)" % len(packages)
182198
186202 sys.exit(0)
187203
188204 for package in packages.values():
189 if package == 'util-linux':
205 skip = False
206 if package.name == 'util-linux':
207 skip = True
208 if ignorelist:
209 for i in ignorelist:
210 if i in package.name > 0:
211 skip = True
212 if skip:
190213 continue
191214 dpkgQuery = ["dpkg-query", "--listfiles", package.name]
192215 dpkgProc = subprocess.Popen(dpkgQuery, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
193216 env = lc_all_c_env)
194 dpkgProc.wait()
195 for line in dpkgProc.stdout.readlines():
217 while True:
218 line = dpkgProc.stdout.readline()
219 if not line:
220 break
196221 path = line[:-1]
197222 if path.startswith('/etc/init.d/'):
198223 if path.endswith('.sh'):
199224 continue
200225 package.initscripts.add(path[12:])
226 sys.stdout.flush()
227 dpkgProc.stdout.close()
228
201229 # Alternatively, find init.d scripts that match the process name
202230 if len(package.initscripts) == 0:
203231 for process in package.processes:
236264 # TODO - consider putting this in a --verbose option
237265 print "These processes do not seem to have an associated init script to restart them:"
238266 for package in nonrestartable:
267 skip = False
268 if ignorelist:
269 for i in ignorelist:
270 if i in package.name > 0:
271 skip = True
272 if skip:
273 continue
239274 print package.name + ':'
240275 for process in package.processes:
241276 print "\t%s\t%s" % (process.pid,process.program)