7 @contact: Debian FTP Master <ftpmaster@debian.org>
8 @copyright: 2001 - 2006 James Troup <james@nocrew.org>
9 @copyright: 2009 Joerg Jaspert <joerg@debian.org>
10 @copyright: 2009 Mark Hymers <mhy@debian.org>
11 @license: GNU General Public License version 2 or later
14 # This program is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 ###############################################################################
34 from cPickle import Unpickler, Pickler
35 from errno import EPERM
37 from apt_inst import debExtractControl
38 from apt_pkg import ParseSection
40 from utils import open_file, fubar, poolify
44 ###############################################################################
48 ###############################################################################
50 CHANGESFIELDS_MANDATORY = [ "distribution", "source", "architecture",
51 "version", "maintainer", "urgency", "fingerprint", "changedby822",
52 "changedby2047", "changedbyname", "maintainer822", "maintainer2047",
53 "maintainername", "maintaineremail", "closes", "changes" ]
55 __all__.append('CHANGESFIELDS_MANDATORY')
57 CHANGESFIELDS_OPTIONAL = [ "changed-by", "filecontents", "format",
58 "process-new note", "adv id", "distribution-version", "sponsoremail" ]
60 __all__.append('CHANGESFIELDS_OPTIONAL')
62 CHANGESFIELDS_FILES = [ "package", "version", "architecture", "type", "size",
63 "md5sum", "sha1sum", "sha256sum", "component", "location id",
64 "source package", "source version", "maintainer", "dbtype", "files id",
65 "new", "section", "priority", "othercomponents", "pool name",
66 "original component" ]
68 __all__.append('CHANGESFIELDS_FILES')
70 CHANGESFIELDS_DSC = [ "source", "version", "maintainer", "fingerprint",
71 "uploaders", "bts changelog", "dm-upload-allowed" ]
73 __all__.append('CHANGESFIELDS_DSC')
75 CHANGESFIELDS_DSCFILES_MANDATORY = [ "size", "md5sum" ]
77 __all__.append('CHANGESFIELDS_DSCFILES_MANDATORY')
79 CHANGESFIELDS_DSCFILES_OPTIONAL = [ "files id" ]
81 __all__.append('CHANGESFIELDS_DSCFILES_OPTIONAL')
83 CHANGESFIELDS_ORIGFILES = [ "id", "location" ]
85 __all__.append('CHANGESFIELDS_ORIGFILES')
87 ###############################################################################
89 class Changes(object):
90 """ Convenience wrapper to carry around all the package information """
92 def __init__(self, **kwds):
96 self.changes_file = ""
104 def file_summary(self):
105 # changes["distribution"] may not exist in corner cases
106 # (e.g. unreadable changes files)
107 if not self.changes.has_key("distribution") or not \
108 isinstance(self.changes["distribution"], dict):
109 self.changes["distribution"] = {}
114 override_summary = ""
116 for name, entry in sorted(self.files.items()):
117 if entry.has_key("byhand"):
119 summary += name + " byhand\n"
121 elif entry.has_key("new"):
123 summary += "(new) %s %s %s\n" % (name, entry["priority"], entry["section"])
125 if entry.has_key("othercomponents"):
126 summary += "WARNING: Already present in %s distribution.\n" % (entry["othercomponents"])
128 if entry["type"] == "deb":
129 deb_fh = open_file(name)
130 summary += ParseSection(debExtractControl(deb_fh))["Description"] + '\n'
134 entry["pool name"] = poolify(self.changes.get("source", ""), entry["component"])
135 destination = entry["pool name"] + name
136 summary += name + "\n to " + destination + "\n"
138 if not entry.has_key("type"):
139 entry["type"] = "unknown"
141 if entry["type"] in ["deb", "udeb", "dsc"]:
142 # (queue/unchecked), there we have override entries already, use them
143 # (process-new), there we dont have override entries, use the newly generated ones.
144 override_prio = entry.get("override priority", entry["priority"])
145 override_sect = entry.get("override section", entry["section"])
146 override_summary += "%s - %s %s\n" % (name, override_prio, override_sect)
148 return (byhand, new, summary, override_summary)
150 def check_override(self):
152 Checks override entries for validity.
154 Returns an empty string if there are no problems
155 or the text of a warning if there are
160 # Abandon the check if it's a non-sourceful upload
161 if not self.changes["architecture"].has_key("source"):
164 for name, entry in sorted(self.files.items()):
165 if not entry.has_key("new") and entry["type"] == "deb":
166 if entry["section"] != "-":
167 if entry["section"].lower() != entry["override section"].lower():
168 summary += "%s: package says section is %s, override says %s.\n" % (name,
170 entry["override section"])
172 if entry["priority"] != "-":
173 if entry["priority"] != entry["override priority"]:
174 summary += "%s: package says priority is %s, override says %s.\n" % (name,
176 entry["override priority"])
181 def remove_known_changes(self, session=None):
182 session.delete(get_knownchange(self.changes_file, session))
184 def mark_missing_fields(self):
185 """add "missing" in fields which we will require for the known_changes table"""
186 for key in ['urgency', 'maintainer', 'fingerprint', 'changed-by' ]:
187 if (not self.changes.has_key(key)) or (not self.changes[key]):
188 self.changes[key]='missing'
191 def add_known_changes(self, dirpath, session=None):
192 """add "missing" in fields which we will require for the known_changes table"""
195 changesfile = os.path.join(dirpath, self.changes_file)
196 filetime = datetime.datetime.fromtimestamp(os.path.getctime(changesfile))
198 self.mark_missing_fields()
201 """INSERT INTO known_changes
202 (changesname, seen, source, binaries, architecture, version,
203 distribution, urgency, maintainer, fingerprint, changedby, date)
204 VALUES (:changesfile,:filetime,:source,:binary, :architecture,
205 :version,:distribution,:urgency,:maintainer,:fingerprint,:changedby,:date)""",
206 { 'changesfile':self.changes_file,
208 'source':self.changes["source"],
209 'binary':self.changes["binary"],
210 'architecture':self.changes["architecture"],
211 'version':self.changes["version"],
212 'distribution':self.changes["distribution"],
213 'urgency':self.changes["urgency"],
214 'maintainer':self.changes["maintainer"],
215 'fingerprint':self.changes["fingerprint"],
216 'changedby':self.changes["changed-by"],
217 'date':self.changes["date"]} )
223 def load_dot_dak(self, changesfile):
225 Update ourself by reading a previously created cPickle .dak dumpfile.
228 self.changes_file = changesfile
229 dump_filename = self.changes_file[:-8]+".dak"
230 dump_file = open_file(dump_filename)
232 p = Unpickler(dump_file)
234 self.changes.update(p.load())
235 self.dsc.update(p.load())
236 self.files.update(p.load())
237 self.dsc_files.update(p.load())
240 if isinstance(next_obj, dict):
241 self.orig_files.update(next_obj)
243 # Auto-convert old dak files to new format supporting
246 for dsc_file in self.dsc_files.keys():
247 if dsc_file.endswith(".orig.tar.gz"):
248 orig_tar_gz = dsc_file
249 self.orig_files[orig_tar_gz] = {}
251 self.orig_files[orig_tar_gz]["id"] = next_obj
253 if next_obj != None and next_obj != "":
254 self.orig_files[orig_tar_gz]["location"] = next_obj
255 if len(self.orig_files[orig_tar_gz]) == 0:
256 del self.orig_files[orig_tar_gz]
260 def sanitised_files(self):
262 for name, entry in self.files.items():
264 for i in CHANGESFIELDS_FILES:
266 ret[name][i] = entry[i]
270 def sanitised_changes(self):
272 # Mandatory changes fields
273 for i in CHANGESFIELDS_MANDATORY:
274 ret[i] = self.changes[i]
276 # Optional changes fields
277 for i in CHANGESFIELDS_OPTIONAL:
278 if self.changes.has_key(i):
279 ret[i] = self.changes[i]
283 def sanitised_dsc(self):
285 for i in CHANGESFIELDS_DSC:
286 if self.dsc.has_key(i):
291 def sanitised_dsc_files(self):
293 for name, entry in self.dsc_files.items():
295 # Mandatory dsc_files fields
296 for i in CHANGESFIELDS_DSCFILES_MANDATORY:
297 ret[name][i] = entry[i]
299 # Optional dsc_files fields
300 for i in CHANGESFIELDS_DSCFILES_OPTIONAL:
302 ret[name][i] = entry[i]
306 def sanitised_orig_files(self):
308 for name, entry in self.orig_files.items():
310 # Optional orig_files fields
311 for i in CHANGESFIELDS_ORIGFILES:
313 ret[name][i] = entry[i]
317 def write_dot_dak(self, dest_dir):
319 Dump ourself into a cPickle file.
321 @type dest_dir: string
322 @param dest_dir: Path where the dumpfile should be stored
324 @note: This could just dump the dictionaries as is, but I'd like to avoid this so
325 there's some idea of what process-accepted & process-new use from
326 process-unchecked. (JT)
330 dump_filename = os.path.join(dest_dir, self.changes_file[:-8] + ".dak")
331 dump_file = open_file(dump_filename, 'w')
334 os.chmod(dump_filename, 0664)
336 # chmod may fail when the dumpfile is not owned by the user
337 # invoking dak (like e.g. when NEW is processed by a member
340 perms = stat.S_IMODE(os.stat(dump_filename)[stat.ST_MODE])
341 # security precaution, should never happen unless a weird
342 # umask is set anywhere
343 if perms & stat.S_IWOTH:
344 fubar("%s is world writable and chmod failed." % \
346 # ignore the failed chmod otherwise as the file should
347 # already have the right privileges and is just, at worst,
348 # unreadable for world
352 p = Pickler(dump_file, 1)
354 p.dump(self.sanitised_changes())
355 p.dump(self.sanitised_dsc())
356 p.dump(self.sanitised_files())
357 p.dump(self.sanitised_dsc_files())
358 p.dump(self.sanitised_orig_files())
362 def unknown_files_fields(self, name):
363 return sorted(list( set(self.files[name].keys()) -
364 set(CHANGESFIELDS_FILES)))
366 def unknown_changes_fields(self):
367 return sorted(list( set(self.changes.keys()) -
368 set(CHANGESFIELDS_MANDATORY + CHANGESFIELDS_OPTIONAL)))
370 def unknown_dsc_fields(self):
371 return sorted(list( set(self.dsc.keys()) -
372 set(CHANGESFIELDS_DSC)))
374 def unknown_dsc_files_fields(self, name):
375 return sorted(list( set(self.dsc_files[name].keys()) -
376 set(CHANGESFIELDS_DSCFILES_MANDATORY + CHANGESFIELDS_DSCFILES_OPTIONAL)))
380 for name, entry in self.files.items():
381 r.append(" %s:" % (name))
382 for i in CHANGESFIELDS_FILES:
384 r.append(" %s: %s" % (i.capitalize(), entry[i]))
385 xfields = self.unknown_files_fields(name)
387 r.append("files[%s] still has following unrecognised keys: %s" % (name, ", ".join(xfields)))
391 def str_changes(self):
393 for i in CHANGESFIELDS_MANDATORY:
394 val = self.changes[i]
395 if isinstance(val, list):
397 elif isinstance(val, dict):
398 val = " ".join(val.keys())
399 r.append(' %s: %s' % (i.capitalize(), val))
401 for i in CHANGESFIELDS_OPTIONAL:
402 if self.changes.has_key(i):
403 r.append(' %s: %s' % (i.capitalize(), self.changes[i]))
405 xfields = self.unknown_changes_fields()
407 r.append("Warning: changes still has the following unrecognised fields: %s" % ", ".join(xfields))
413 for i in CHANGESFIELDS_DSC:
414 if self.dsc.has_key(i):
415 r.append(' %s: %s' % (i.capitalize(), self.dsc[i]))
417 xfields = self.unknown_dsc_fields()
419 r.append("Warning: dsc still has the following unrecognised fields: %s" % ", ".join(xfields))
423 def str_dsc_files(self):
425 for name, entry in self.dsc_files.items():
426 r.append(" %s:" % (name))
427 for i in CHANGESFIELDS_DSCFILES_MANDATORY:
428 r.append(" %s: %s" % (i.capitalize(), entry[i]))
429 for i in CHANGESFIELDS_DSCFILES_OPTIONAL:
431 r.append(" %s: %s" % (i.capitalize(), entry[i]))
432 xfields = self.unknown_dsc_files_fields(name)
434 r.append("dsc_files[%s] still has following unrecognised keys: %s" % (name, ", ".join(xfields)))
441 r.append(" Changes:")
442 r += self.str_changes()
452 r += self.str_files()
456 r.append(" Dsc Files:")
457 r += self.str_dsc_files()
461 __all__.append('Changes')