3 # Script to automate some parts of checking NEW packages
4 # Copyright (C) 2000, 2001, 2002 James Troup <james@nocrew.org>
5 # $Id: fernanda.py,v 1.9 2002-11-28 08:21:04 rdonald Exp $
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ################################################################################
23 # <Omnic> elmo wrote docs?!!?!?!?!?!?!
24 # <aj> as if he wasn't scary enough before!!
25 # * aj imagines a little red furry toy sitting hunched over a computer
26 # tapping furiously and giggling to himself
27 # <aj> eventually he stops, and his heads slowly spins around and you
28 # see this really evil grin and then he sees you, and picks up a
29 # knife from beside the keyboard and throws it at you, and as you
30 # breathe your last breath, he starts giggling again
31 # <aj> but i should be telling this to my psychiatrist, not you guys,
34 ################################################################################
36 import errno, os, re, sys
38 import apt_pkg, apt_inst
41 ################################################################################
43 re_package = re.compile(r"^(.+?)_.*");
44 re_doc_directory = re.compile(r".*/doc/([^/]*).*");
46 re_contrib = re.compile('^contrib/')
47 re_nonfree = re.compile('^non\-free/')
49 re_arch = re.compile("Architecture: .*")
50 re_builddep = re.compile("Build-Depends: .*")
51 re_builddepind = re.compile("Build-Depends-Indep: .*")
53 re_localhost = re.compile("localhost\.localdomain")
54 re_version = re.compile('^(.*)\((.*)\)')
56 re_newlinespace = re.compile('\n')
57 re_spacestrip = re.compile('(\s)')
59 ################################################################################
64 main_colour = "\033[36m";
66 contrib_colour = "\033[33m";
68 nonfree_colour = "\033[31m";
70 arch_colour = "\033[32m";
72 end_colour = "\033[0m";
74 bold_colour = "\033[1m";
76 maintainer_colour = arch_colour;
78 ################################################################################
83 Cnf = utils.get_conf()
84 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
85 db_access.init(Cnf, projectB);
87 ################################################################################
89 def usage (exit_code=0):
90 print """Usage: fernanda [PACKAGE]...
93 -h, --help show this help and exit
95 PACKAGE can be a .changes, .dsc, .deb or .udeb filename."""
99 ################################################################################
101 def get_depends_parts(depend) :
102 v_match = re_version.match(depend)
104 d_parts = { 'name' : v_match.group(1), 'version' : v_match.group(2) }
106 d_parts = { 'name' : depend , 'version' : '' }
109 def get_or_list(depend) :
110 or_list = depend.split("|");
113 def get_comma_list(depend) :
114 dep_list = depend.split(",");
117 def split_depends (d_str) :
118 # creates a list of lists of dictionaries of depends (package,version relation)
120 d_str = re_spacestrip.sub('',d_str);
122 # first split depends string up amongs comma delimiter
123 dep_list = get_comma_list(d_str);
125 while d < len(dep_list):
126 # put depends into their own list
127 depends_tree.append([dep_list[d]]);
130 while d < len(depends_tree):
132 # split up Or'd depends into a multi-item list
133 depends_tree[d] = get_or_list(depends_tree[d][0]);
134 while k < len(depends_tree[d]):
135 # split depends into {package, version relation}
136 depends_tree[d][k] = get_depends_parts(depends_tree[d][k]);
141 def read_control (filename):
148 deb_file = utils.open_file(filename);
150 extracts = apt_inst.debExtractControl(deb_file);
151 control = apt_pkg.ParseSection(extracts);
153 print "can't parse control info";
158 control_keys = control.keys();
160 if control.has_key("Depends"):
161 depends_str = control.Find("Depends");
162 # create list of dependancy lists
163 depends = split_depends(depends_str);
165 if control.has_key("Recommends"):
166 recommends_str = control.Find("Recommends");
167 recommends = split_depends(recommends_str);
169 if control.has_key("Section"):
170 section_str = control.Find("Section");
172 c_match = re_contrib.search(section_str)
173 nf_match = re_nonfree.search(section_str)
176 section = contrib_colour + section_str + end_colour
179 section = nonfree_colour + section_str + end_colour
182 section = main_colour + section_str + end_colour
183 if control.has_key("Architecture"):
184 arch_str = control.Find("Architecture")
185 arch = arch_colour + arch_str + end_colour
187 if control.has_key("Maintainer"):
188 maintainer = control.Find("Maintainer")
189 localhost = re_localhost.search(maintainer)
192 maintainer = maintainer_colour + maintainer + end_colour;
194 return (control, control_keys, section, depends, recommends, arch, maintainer)
196 def read_dsc (dsc_filename):
199 dsc_file = utils.open_file(dsc_filename);
201 dsc = utils.parse_changes(dsc_filename);
203 print "can't parse control info"
206 filecontents = dsc["filecontents"];
208 if dsc.has_key("build-depends"):
209 builddep = split_depends(dsc["build-depends"]);
210 builddepstr = create_depends_string(builddep);
211 filecontents = re_builddep.sub("Build-Depends: "+builddepstr, filecontents);
213 if dsc.has_key("build-depends-indep"):
214 builddepindstr = create_depends_string(split_depends(dsc["build-depends-indep"]));
215 filecontents = re_builddepind.sub("Build-Depends-Indep: "+builddepindstr, filecontents);
217 if dsc.has_key("architecture") :
218 if (dsc["architecture"] != "any"):
219 newarch = arch_colour + dsc["architecture"] + end_colour;
220 filecontents = re_arch.sub("Architecture: " + newarch, filecontents);
224 def create_depends_string (depends_tree):
225 # just look up unstable for now. possibly pull from .changes later
229 for l in depends_tree:
230 if (comma_count >= 2):
236 # doesn't do version lookup yet.
238 q = projectB.query("SELECT DISTINCT(b.package), b.version, c.name, su.suite_name FROM binaries b, files fi, location l, component c, bin_associations ba, suite su WHERE b.package='%s' AND b.file = fi.id AND fi.location = l.id AND l.component = c.id AND ba.bin=b.id AND ba.suite = su.id AND su.suite_name='%s' ORDER BY b.version desc" % (d['name'], suite));
243 if i[2] == "contrib":
244 result += contrib_colour + d['name'];
245 elif i[2] == "non-free":
246 result += nonfree_colour + d['name'];
248 result += main_colour + d['name'];
250 if d['version'] != '' :
251 result += " (%s)" % (d['version']);
252 result += end_colour;
254 result += bold_colour + d['name'];
255 if d['version'] != '' :
256 result += " (%s)" % (d['version']);
257 result += end_colour;
262 def output_deb_info(filename):
263 (control, control_keys, section, depends, recommends, arch, maintainer) = read_control(filename);
266 print "no control info"
268 for key in control_keys :
269 output = " " + key + ": "
271 output += create_depends_string(depends);
272 elif key == 'Recommends':
273 output += create_depends_string(recommends);
274 elif key == 'Section':
276 elif key == 'Architecture':
278 elif key == 'Maintainer':
279 output += maintainer;
280 elif key == 'Description':
281 desc = control.Find(key);
282 desc = re_newlinespace.sub('\n ', desc);
285 output += control.Find(key);
288 def do_command (command, filename):
289 o = os.popen("%s %s" % (command, filename));
292 def print_copyright (deb_filename):
293 package = re_package.sub(r'\1', deb_filename);
294 o = os.popen("ar p %s data.tar.gz | tar tzvf - | egrep 'usr(/share)?/doc/[^/]*/copyright' | awk '{ print $6 }' | head -n 1" % (deb_filename));
295 copyright = o.read()[:-1];
298 print "WARNING: No copyright found, please check package manually."
301 doc_directory = re_doc_directory.sub(r'\1', copyright);
302 if package != doc_directory:
303 print "WARNING: wrong doc directory (expected %s, got %s)." % (package, doc_directory);
306 o = os.popen("ar p %s data.tar.gz | tar xzOf - %s" % (deb_filename, copyright));
309 def check_dsc (dsc_filename):
310 print "---- .dsc file for %s ----" % (dsc_filename);
311 (dsc) = read_dsc(dsc_filename)
314 def check_deb (deb_filename):
315 filename = os.path.basename(deb_filename);
317 if filename.endswith(".udeb"):
322 print "---- control file for %s ----" % (filename);
323 #do_command ("dpkg -I", deb_filename);
324 output_deb_info(deb_filename)
327 print "---- skipping lintian check for µdeb ----";
330 print "---- lintian check for %s ----" % (filename);
331 do_command ("lintian", deb_filename);
332 print "---- linda check for %s ----" % (filename);
333 do_command ("linda", deb_filename);
335 print "---- contents of %s ----" % (filename);
336 do_command ("dpkg -c", deb_filename);
339 print "---- skipping copyright for µdeb ----";
341 print "---- copyright of %s ----" % (filename);
342 print_copyright(deb_filename);
344 print "---- file listing of %s ----" % (filename);
345 do_command ("ls -l", deb_filename);
347 def display_changes (changes_filename):
348 print "---- .changes file for %s ----" % (changes_filename);
349 file = utils.open_file (changes_filename);
350 for line in file.readlines():
355 def check_changes (changes_filename):
356 display_changes(changes_filename);
358 changes = utils.parse_changes (changes_filename);
359 files = utils.build_file_list(changes);
360 for file in files.keys():
361 if file.endswith(".deb") or file.endswith(".udeb"):
363 if file.endswith(".dsc"):
368 global Cnf, projectB, db_files, waste, excluded;
370 # Cnf = utils.get_conf()
372 Arguments = [('h',"help","Fernanda::Options::Help")];
374 if not Cnf.has_key("Frenanda::Options::%s" % (i)):
375 Cnf["Fernanda::Options::%s" % (i)] = "";
377 args = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
378 Options = Cnf.SubTree("Fernanda::Options")
383 stdout_fd = sys.stdout;
387 # Pipe output for each argument through less
388 less_fd = os.popen("less -R -", 'w', 0);
389 # -R added to display raw control chars for colour
390 sys.stdout = less_fd;
393 if file.endswith(".changes"):
395 elif file.endswith(".deb") or file.endswith(".udeb"):
397 elif file.endswith(".dsc"):
400 utils.fubar("Unrecognised file type: '%s'." % (file));
402 # Reset stdout here so future less invocations aren't FUBAR
404 sys.stdout = stdout_fd;
406 if errno.errorcode[e.errno] == 'EPIPE':
407 utils.warn("[fernanda] Caught EPIPE; skipping.");
411 except KeyboardInterrupt:
412 utils.warn("[fernanda] Caught C-c; skipping.");
415 #######################################################################################
417 if __name__ == '__main__':