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_dbchange(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'
190 def __get_file_from_pool(self, filename, entry, session, logger):
193 if cnf.has_key("Dinstall::SuiteSuffix"):
194 component = cnf["Dinstall::SuiteSuffix"] + entry["component"]
196 component = entry["component"]
198 poolname = poolify(entry["source"], component)
199 l = get_location(cnf["Dir::Pool"], component, session=session)
201 found, poolfile = check_poolfile(os.path.join(poolname, filename),
208 if logger is not None:
209 logger.log(["E: Found multiple files for pool (%s) for %s" % (filename, component)])
211 elif found is False and poolfile is not None:
212 if logger is not None:
213 logger.log(["E: md5sum/size mismatch for %s in pool" % (filename)])
217 if logger is not None:
218 logger.log(["E: Could not find %s in pool" % (filename)])
224 def add_known_changes(self, dirpath, in_queue=None, session=None, logger=None):
225 """add "missing" in fields which we will require for the known_changes table"""
228 changesfile = os.path.join(dirpath, self.changes_file)
229 filetime = datetime.datetime.fromtimestamp(os.path.getctime(changesfile))
231 self.mark_missing_fields()
234 for key in ("distribution", "architecture", "binary"):
235 if isinstance(self.changes[key], dict):
236 multivalues[key] = " ".join(self.changes[key].keys())
238 multivalues[key] = self.changes[key]
241 chg.changesname = self.changes_file
243 chg.in_queue_id = in_queue
244 chg.source = self.changes["source"]
245 chg.binaries = multivalues["binary"]
246 chg.architecture = multivalues["architecture"]
247 chg.version = self.changes["version"]
248 chg.distribution = multivalues["distribution"]
249 chg.urgency = self.changes["urgency"]
250 chg.maintainer = self.changes["maintainer"]
251 chg.fingerprint = self.changes["fingerprint"]
252 chg.changedby = self.changes["changed-by"]
253 chg.date = self.changes["date"]
258 for chg_fn, entry in self.files.items():
260 f = open(os.path.join(dirpath, chg_fn))
261 cpf = ChangePendingFile()
262 cpf.filename = chg_fn
263 cpf.size = entry['size']
264 cpf.md5sum = entry['md5sum']
266 if entry.has_key('sha1sum'):
267 cpf.sha1sum = entry['sha1sum']
270 cpf.sha1sum = apt_pkg.sha1sum(f)
272 if entry.has_key('sha256sum'):
273 cpf.sha256sum = entry['sha256sum']
276 cpf.sha256sum = apt_pkg.sha256sum(f)
283 # Can't find the file, try to look it up in the pool
284 poolfile = self.__get_file_from_pool(chg_fn, entry, session)
286 chg.poolfiles.append(poolfile)
290 # Add files referenced in .dsc, but not included in .changes
291 for name, entry in self.dsc_files.items():
292 if self.files.has_key(name):
295 entry['source'] = self.changes['source']
296 poolfile = self.__get_file_from_pool(name, entry, session, logger)
298 chg.poolfiles.append(poolfile)
301 chg = session.query(DBChange).filter_by(changesname = self.changes_file).one();
305 def unknown_files_fields(self, name):
306 return sorted(list( set(self.files[name].keys()) -
307 set(CHANGESFIELDS_FILES)))
309 def unknown_changes_fields(self):
310 return sorted(list( set(self.changes.keys()) -
311 set(CHANGESFIELDS_MANDATORY + CHANGESFIELDS_OPTIONAL)))
313 def unknown_dsc_fields(self):
314 return sorted(list( set(self.dsc.keys()) -
315 set(CHANGESFIELDS_DSC)))
317 def unknown_dsc_files_fields(self, name):
318 return sorted(list( set(self.dsc_files[name].keys()) -
319 set(CHANGESFIELDS_DSCFILES_MANDATORY + CHANGESFIELDS_DSCFILES_OPTIONAL)))
323 for name, entry in self.files.items():
324 r.append(" %s:" % (name))
325 for i in CHANGESFIELDS_FILES:
327 r.append(" %s: %s" % (i.capitalize(), entry[i]))
328 xfields = self.unknown_files_fields(name)
330 r.append("files[%s] still has following unrecognised keys: %s" % (name, ", ".join(xfields)))
334 def str_changes(self):
336 for i in CHANGESFIELDS_MANDATORY:
337 val = self.changes[i]
338 if isinstance(val, list):
340 elif isinstance(val, dict):
341 val = " ".join(val.keys())
342 r.append(' %s: %s' % (i.capitalize(), val))
344 for i in CHANGESFIELDS_OPTIONAL:
345 if self.changes.has_key(i):
346 r.append(' %s: %s' % (i.capitalize(), self.changes[i]))
348 xfields = self.unknown_changes_fields()
350 r.append("Warning: changes still has the following unrecognised fields: %s" % ", ".join(xfields))
356 for i in CHANGESFIELDS_DSC:
357 if self.dsc.has_key(i):
358 r.append(' %s: %s' % (i.capitalize(), self.dsc[i]))
360 xfields = self.unknown_dsc_fields()
362 r.append("Warning: dsc still has the following unrecognised fields: %s" % ", ".join(xfields))
366 def str_dsc_files(self):
368 for name, entry in self.dsc_files.items():
369 r.append(" %s:" % (name))
370 for i in CHANGESFIELDS_DSCFILES_MANDATORY:
371 r.append(" %s: %s" % (i.capitalize(), entry[i]))
372 for i in CHANGESFIELDS_DSCFILES_OPTIONAL:
374 r.append(" %s: %s" % (i.capitalize(), entry[i]))
375 xfields = self.unknown_dsc_files_fields(name)
377 r.append("dsc_files[%s] still has following unrecognised keys: %s" % (name, ", ".join(xfields)))
384 r.append(" Changes:")
385 r += self.str_changes()
395 r += self.str_files()
399 r.append(" Dsc Files:")
400 r += self.str_dsc_files()
404 __all__.append('Changes')