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 ###############################################################################
32 from cPickle import Unpickler, Pickler
33 from errno import EPERM
35 from apt_inst import debExtractControl
36 from apt_pkg import ParseSection
38 from utils import open_file, fubar, poolify
40 ###############################################################################
44 ###############################################################################
46 CHANGESFIELDS_MANDATORY = [ "distribution", "source", "architecture",
47 "version", "maintainer", "urgency", "fingerprint", "changedby822",
48 "changedby2047", "changedbyname", "maintainer822", "maintainer2047",
49 "maintainername", "maintaineremail", "closes", "changes" ]
51 __all__.append('CHANGESFIELDS_MANDATORY')
53 CHANGESFIELDS_OPTIONAL = [ "changed-by", "filecontents", "format",
54 "process-new note", "adv id", "distribution-version", "sponsoremail" ]
56 __all__.append('CHANGESFIELDS_OPTIONAL')
58 CHANGESFIELDS_FILES = [ "package", "version", "architecture", "type", "size",
59 "md5sum", "sha1sum", "sha256sum", "component", "location id",
60 "source package", "source version", "maintainer", "dbtype", "files id",
61 "new", "section", "priority", "othercomponents", "pool name",
62 "original component" ]
64 __all__.append('CHANGESFIELDS_FILES')
66 CHANGESFIELDS_DSC = [ "source", "version", "maintainer", "fingerprint",
67 "uploaders", "bts changelog", "dm-upload-allowed" ]
69 __all__.append('CHANGESFIELDS_DSC')
71 CHANGESFIELDS_DSCFILES_MANDATORY = [ "size", "md5sum" ]
73 __all__.append('CHANGESFIELDS_DSCFILES_MANDATORY')
75 CHANGESFIELDS_DSCFILES_OPTIONAL = [ "files id" ]
77 __all__.append('CHANGESFIELDS_DSCFILES_OPTIONAL')
79 CHANGESFIELDS_ORIGFILES = [ "id", "location" ]
81 __all__.append('CHANGESFIELDS_ORIGFILES')
83 ###############################################################################
85 class Changes(object):
86 """ Convenience wrapper to carry around all the package information """
88 def __init__(self, **kwds):
92 self.changes_file = ""
100 def file_summary(self):
101 # changes["distribution"] may not exist in corner cases
102 # (e.g. unreadable changes files)
103 if not self.changes.has_key("distribution") or not \
104 isinstance(self.changes["distribution"], dict):
105 self.changes["distribution"] = {}
110 override_summary = ""
112 for name, entry in sorted(self.files.items()):
113 if entry.has_key("byhand"):
115 summary += name + " byhand\n"
117 elif entry.has_key("new"):
119 summary += "(new) %s %s %s\n" % (name, entry["priority"], entry["section"])
121 if entry.has_key("othercomponents"):
122 summary += "WARNING: Already present in %s distribution.\n" % (entry["othercomponents"])
124 if entry["type"] == "deb":
125 deb_fh = open_file(name)
126 summary += ParseSection(debExtractControl(deb_fh))["Description"] + '\n'
130 entry["pool name"] = poolify(self.changes.get("source", ""), entry["component"])
131 destination = entry["pool name"] + name
132 summary += name + "\n to " + destination + "\n"
134 if not entry.has_key("type"):
135 entry["type"] = "unknown"
137 if entry["type"] in ["deb", "udeb", "dsc"]:
138 # (queue/unchecked), there we have override entries already, use them
139 # (process-new), there we dont have override entries, use the newly generated ones.
140 override_prio = entry.get("override priority", entry["priority"])
141 override_sect = entry.get("override section", entry["section"])
142 override_summary += "%s - %s %s\n" % (name, override_prio, override_sect)
144 return (byhand, new, summary, override_summary)
146 def check_override(self):
148 Checks override entries for validity.
150 Returns an empty string if there are no problems
151 or the text of a warning if there are
156 # Abandon the check if it's a non-sourceful upload
157 if not self.changes["architecture"].has_key("source"):
160 for name, entry in sorted(self.files.items()):
161 if not entry.has_key("new") and entry["type"] == "deb":
162 if entry["section"] != "-":
163 if entry["section"].lower() != entry["override section"].lower():
164 summary += "%s: package says section is %s, override says %s.\n" % (name,
166 entry["override section"])
168 if entry["priority"] != "-":
169 if entry["priority"] != entry["override priority"]:
170 summary += "%s: package says priority is %s, override says %s.\n" % (name,
172 entry["override priority"])
176 def remove_known_changes(self, session=None):
178 session = DBConn().session()
181 session.query(KnownChange).filter(changesfile=self.changes_file).delete()
186 def add_known_changes(self, queue, session=None):
190 session = DBConn().session()
193 dirpath = cnf["Dir::Queue::%s" % (queue) ]
194 changesfile = os.path.join(dirpath, self.changes_file)
195 filetime = datetime.datetime.fromtimestamp(os.path.getctime(changesfile))
198 """INSERT INTO known_changes
199 (changesname, seen, source, binaries, architecture, version,
200 distribution, urgency, maintainer, fingerprint, changedby, date)
201 VALUES (:changesfile,:filetime,:source,:binary, :architecture,
202 :version,:distribution,:urgency,'maintainer,:changedby,:date)""",
203 { 'changesfile':changesfile,
205 'source':self.changes["source"],
206 'binary':self.changes["binary"],
207 'architecture':self.changes["architecture"],
208 'version':self.changes["version"],
209 'distribution':self.changes["distribution"],
210 'urgency':self.changes["urgency"],
211 'maintainer':self.changes["maintainer"],
212 'fingerprint':self.changes["fingerprint"],
213 'changedby':self.changes["changed-by"],
214 'date':self.changes["date"]} )
220 def load_dot_dak(self, changesfile):
222 Update ourself by reading a previously created cPickle .dak dumpfile.
225 self.changes_file = changesfile
226 dump_filename = self.changes_file[:-8]+".dak"
227 dump_file = open_file(dump_filename)
229 p = Unpickler(dump_file)
231 self.changes.update(p.load())
232 self.dsc.update(p.load())
233 self.files.update(p.load())
234 self.dsc_files.update(p.load())
237 if isinstance(next_obj, dict):
238 self.orig_files.update(next_obj)
240 # Auto-convert old dak files to new format supporting
243 for dsc_file in self.dsc_files.keys():
244 if dsc_file.endswith(".orig.tar.gz"):
245 orig_tar_gz = dsc_file
246 self.orig_files[orig_tar_gz] = {}
248 self.orig_files[orig_tar_gz]["id"] = next_obj
250 if next_obj != None and next_obj != "":
251 self.orig_files[orig_tar_gz]["location"] = next_obj
252 if len(self.orig_files[orig_tar_gz]) == 0:
253 del self.orig_files[orig_tar_gz]
257 def sanitised_files(self):
259 for name, entry in self.files.items():
261 for i in CHANGESFIELDS_FILES:
263 ret[name][i] = entry[i]
267 def sanitised_changes(self):
269 # Mandatory changes fields
270 for i in CHANGESFIELDS_MANDATORY:
271 ret[i] = self.changes[i]
273 # Optional changes fields
274 for i in CHANGESFIELDS_OPTIONAL:
275 if self.changes.has_key(i):
276 ret[i] = self.changes[i]
280 def sanitised_dsc(self):
282 for i in CHANGESFIELDS_DSC:
283 if self.dsc.has_key(i):
288 def sanitised_dsc_files(self):
290 for name, entry in self.dsc_files.items():
292 # Mandatory dsc_files fields
293 for i in CHANGESFIELDS_DSCFILES_MANDATORY:
294 ret[name][i] = entry[i]
296 # Optional dsc_files fields
297 for i in CHANGESFIELDS_DSCFILES_OPTIONAL:
299 ret[name][i] = entry[i]
303 def sanitised_orig_files(self):
305 for name, entry in self.orig_files.items():
307 # Optional orig_files fields
308 for i in CHANGESFIELDS_ORIGFILES:
310 ret[name][i] = entry[i]
314 def write_dot_dak(self, dest_dir):
316 Dump ourself into a cPickle file.
318 @type dest_dir: string
319 @param dest_dir: Path where the dumpfile should be stored
321 @note: This could just dump the dictionaries as is, but I'd like to avoid this so
322 there's some idea of what process-accepted & process-new use from
323 process-unchecked. (JT)
327 dump_filename = os.path.join(dest_dir, self.changes_file[:-8] + ".dak")
328 dump_file = open_file(dump_filename, 'w')
331 os.chmod(dump_filename, 0664)
333 # chmod may fail when the dumpfile is not owned by the user
334 # invoking dak (like e.g. when NEW is processed by a member
337 perms = stat.S_IMODE(os.stat(dump_filename)[stat.ST_MODE])
338 # security precaution, should never happen unless a weird
339 # umask is set anywhere
340 if perms & stat.S_IWOTH:
341 fubar("%s is world writable and chmod failed." % \
343 # ignore the failed chmod otherwise as the file should
344 # already have the right privileges and is just, at worst,
345 # unreadable for world
349 p = Pickler(dump_file, 1)
351 p.dump(self.sanitised_changes())
352 p.dump(self.sanitised_dsc())
353 p.dump(self.sanitised_files())
354 p.dump(self.sanitised_dsc_files())
355 p.dump(self.sanitised_orig_files())
359 def unknown_files_fields(self, name):
360 return sorted(list( set(self.files[name].keys()) -
361 set(CHANGESFIELDS_FILES)))
363 def unknown_changes_fields(self):
364 return sorted(list( set(self.changes.keys()) -
365 set(CHANGESFIELDS_MANDATORY + CHANGESFIELDS_OPTIONAL)))
367 def unknown_dsc_fields(self):
368 return sorted(list( set(self.dsc.keys()) -
369 set(CHANGESFIELDS_DSC)))
371 def unknown_dsc_files_fields(self, name):
372 return sorted(list( set(self.dsc_files[name].keys()) -
373 set(CHANGESFIELDS_DSCFILES_MANDATORY + CHANGESFIELDS_DSCFILES_OPTIONAL)))
377 for name, entry in self.files.items():
378 r.append(" %s:" % (name))
379 for i in CHANGESFIELDS_FILES:
381 r.append(" %s: %s" % (i.capitalize(), entry[i]))
382 xfields = self.unknown_files_fields(name)
384 r.append("files[%s] still has following unrecognised keys: %s" % (name, ", ".join(xfields)))
388 def str_changes(self):
390 for i in CHANGESFIELDS_MANDATORY:
391 val = self.changes[i]
392 if isinstance(val, list):
394 elif isinstance(val, dict):
395 val = " ".join(val.keys())
396 r.append(' %s: %s' % (i.capitalize(), val))
398 for i in CHANGESFIELDS_OPTIONAL:
399 if self.changes.has_key(i):
400 r.append(' %s: %s' % (i.capitalize(), self.changes[i]))
402 xfields = self.unknown_changes_fields()
404 r.append("Warning: changes still has the following unrecognised fields: %s" % ", ".join(xfields))
410 for i in CHANGESFIELDS_DSC:
411 if self.dsc.has_key(i):
412 r.append(' %s: %s' % (i.capitalize(), self.dsc[i]))
414 xfields = self.unknown_dsc_fields()
416 r.append("Warning: dsc still has the following unrecognised fields: %s" % ", ".join(xfields))
420 def str_dsc_files(self):
422 for name, entry in self.dsc_files.items():
423 r.append(" %s:" % (name))
424 for i in CHANGESFIELDS_DSCFILES_MANDATORY:
425 r.append(" %s: %s" % (i.capitalize(), entry[i]))
426 for i in CHANGESFIELDS_DSCFILES_OPTIONAL:
428 r.append(" %s: %s" % (i.capitalize(), entry[i]))
429 xfields = self.unknown_dsc_files_fields(name)
431 r.append("dsc_files[%s] still has following unrecognised keys: %s" % (name, ", ".join(xfields)))
438 r.append(" Changes:")
439 r += self.str_changes()
449 r += self.str_files()
453 r.append(" Dsc Files:")
454 r += self.str_dsc_files()
458 __all__.append('Changes')