]> git.decadent.org.uk Git - dak.git/blob - daklib/changes.py
Merge commit 'lamby/master' into merge
[dak.git] / daklib / changes.py
1 #!/usr/bin/env python
2 # vim:set et sw=4:
3
4 """
5 Changes class for dak
6
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
12 """
13
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.
18
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.
23
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
27
28 ###############################################################################
29
30 import os
31 import stat
32 from cPickle import Unpickler, Pickler
33 from errno import EPERM
34
35 from apt_inst import debExtractControl
36 from apt_pkg import ParseSection
37
38 from utils import open_file, fubar, poolify
39
40 ###############################################################################
41
42 __all__ = []
43
44 ###############################################################################
45
46 CHANGESFIELDS_MANDATORY = [ "distribution", "source", "architecture",
47         "version", "maintainer", "urgency", "fingerprint", "changedby822",
48         "changedby2047", "changedbyname", "maintainer822", "maintainer2047",
49         "maintainername", "maintaineremail", "closes", "changes" ]
50
51 __all__.append('CHANGESFIELDS_MANDATORY')
52
53 CHANGESFIELDS_OPTIONAL = [ "changed-by", "filecontents", "format",
54         "process-new note", "adv id", "distribution-version", "sponsoremail" ]
55
56 __all__.append('CHANGESFIELDS_OPTIONAL')
57
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" ]
63
64 __all__.append('CHANGESFIELDS_FILES')
65
66 CHANGESFIELDS_DSC = [ "source", "version", "maintainer", "fingerprint",
67         "uploaders", "bts changelog", "dm-upload-allowed" ]
68
69 __all__.append('CHANGESFIELDS_DSC')
70
71 CHANGESFIELDS_DSCFILES_MANDATORY = [ "size", "md5sum" ]
72
73 __all__.append('CHANGESFIELDS_DSCFILES_MANDATORY')
74
75 CHANGESFIELDS_DSCFILES_OPTIONAL = [ "files id" ]
76
77 __all__.append('CHANGESFIELDS_DSCFILES_OPTIONAL')
78
79 CHANGESFIELDS_ORIGFILES = [ "id", "location" ]
80
81 __all__.append('CHANGESFIELDS_ORIGFILES')
82
83 ###############################################################################
84
85 class Changes(object):
86     """ Convenience wrapper to carry around all the package information """
87
88     def __init__(self, **kwds):
89         self.reset()
90
91     def reset(self):
92         self.changes_file = ""
93
94         self.changes = {}
95         self.dsc = {}
96         self.files = {}
97         self.dsc_files = {}
98         self.orig_files = {}
99
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"] = {}
106
107         byhand = False
108         new = False
109         summary = ""
110         override_summary = ""
111
112         for name, entry in sorted(self.files.items()):
113             if entry.has_key("byhand"):
114                 byhand = True
115                 summary += name + " byhand\n"
116
117             elif entry.has_key("new"):
118                 new = True
119                 summary += "(new) %s %s %s\n" % (name, entry["priority"], entry["section"])
120
121                 if entry.has_key("othercomponents"):
122                     summary += "WARNING: Already present in %s distribution.\n" % (entry["othercomponents"])
123
124                 if entry["type"] == "deb":
125                     deb_fh = open_file(name)
126                     summary += ParseSection(debExtractControl(deb_fh))["Description"] + '\n'
127                     deb_fh.close()
128
129             else:
130                 entry["pool name"] = poolify(self.changes.get("source", ""), entry["component"])
131                 destination = entry["pool name"] + name
132                 summary += name + "\n  to " + destination + "\n"
133
134                 if not entry.has_key("type"):
135                     entry["type"] = "unknown"
136
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)
143
144         return (byhand, new, summary, override_summary)
145
146     def check_override(self):
147         """
148         Checks override entries for validity.
149
150         Returns an empty string if there are no problems
151         or the text of a warning if there are
152         """
153
154         summary = ""
155
156         # Abandon the check if it's a non-sourceful upload
157         if not self.changes["architecture"].has_key("source"):
158             return summary
159
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,
165                                                                                             entry["section"],
166                                                                                             entry["override section"])
167
168                 if entry["priority"] != "-":
169                     if entry["priority"] != entry["override priority"]:
170                         summary += "%s: package says priority is %s, override says %s.\n" % (name,
171                                                                                              entry["priority"],
172                                                                                              entry["override priority"])
173
174         return summary
175
176
177     def load_dot_dak(self, changesfile):
178         """
179         Update ourself by reading a previously created cPickle .dak dumpfile.
180         """
181
182         self.changes_file = changesfile
183         dump_filename = self.changes_file[:-8]+".dak"
184         dump_file = open_file(dump_filename)
185
186         p = Unpickler(dump_file)
187
188         self.changes.update(p.load())
189         self.dsc.update(p.load())
190         self.files.update(p.load())
191         self.dsc_files.update(p.load())
192
193         next_obj = p.load()
194         if isinstance(next_obj, dict):
195             self.orig_files.update(next_obj)
196         else:
197             # Auto-convert old dak files to new format supporting
198             # multiple tarballs
199             orig_tar_gz = None
200             for dsc_file in self.dsc_files.keys():
201                 if dsc_file.endswith(".orig.tar.gz"):
202                     orig_tar_gz = dsc_file
203             self.orig_files[orig_tar_gz] = {}
204             if next_obj != None:
205                 self.orig_files[orig_tar_gz]["id"] = next_obj
206             next_obj = p.load()
207             if next_obj != None and next_obj != "":
208                 self.orig_files[orig_tar_gz]["location"] = next_obj
209             if len(self.orig_files[orig_tar_gz]) == 0:
210                 del self.orig_files[orig_tar_gz]
211
212         dump_file.close()
213
214     def sanitised_files(self):
215         ret = {}
216         for name, entry in self.files.items():
217             ret[name] = {}
218             for i in CHANGESFIELDS_FILES:
219                 if entry.has_key(i):
220                     ret[name][i] = entry[i]
221
222         return ret
223
224     def sanitised_changes(self):
225         ret = {}
226         # Mandatory changes fields
227         for i in CHANGESFIELDS_MANDATORY:
228             ret[i] = self.changes[i]
229
230         # Optional changes fields
231         for i in CHANGESFIELDS_OPTIONAL:
232             if self.changes.has_key(i):
233                 ret[i] = self.changes[i]
234
235         return ret
236
237     def sanitised_dsc(self):
238         ret = {}
239         for i in CHANGESFIELDS_DSC:
240             if self.dsc.has_key(i):
241                 ret[i] = self.dsc[i]
242
243         return ret
244
245     def sanitised_dsc_files(self):
246         ret = {}
247         for name, entry in self.dsc_files.items():
248             ret[name] = {}
249             # Mandatory dsc_files fields
250             for i in CHANGESFIELDS_DSCFILES_MANDATORY:
251                 ret[name][i] = entry[i]
252
253             # Optional dsc_files fields
254             for i in CHANGESFIELDS_DSCFILES_OPTIONAL:
255                 if entry.has_key(i):
256                     ret[name][i] = entry[i]
257
258         return ret
259
260     def sanitised_orig_files(self):
261         ret = {}
262         for name, entry in self.orig_files.items():
263             ret[name] = {}
264             # Optional orig_files fields
265             for i in CHANGESFIELDS_ORIGFILES:
266                 if entry.has_key(i):
267                     ret[name][i] = entry[i]
268
269         return ret
270
271     def write_dot_dak(self, dest_dir):
272         """
273         Dump ourself into a cPickle file.
274
275         @type dest_dir: string
276         @param dest_dir: Path where the dumpfile should be stored
277
278         @note: This could just dump the dictionaries as is, but I'd like to avoid this so
279                there's some idea of what process-accepted & process-new use from
280                process-unchecked. (JT)
281
282         """
283
284         dump_filename = os.path.join(dest_dir, self.changes_file[:-8] + ".dak")
285         dump_file = open_file(dump_filename, 'w')
286
287         try:
288             os.chmod(dump_filename, 0664)
289         except OSError, e:
290             # chmod may fail when the dumpfile is not owned by the user
291             # invoking dak (like e.g. when NEW is processed by a member
292             # of ftpteam)
293             if e.errno == EPERM:
294                 perms = stat.S_IMODE(os.stat(dump_filename)[stat.ST_MODE])
295                 # security precaution, should never happen unless a weird
296                 # umask is set anywhere
297                 if perms & stat.S_IWOTH:
298                     fubar("%s is world writable and chmod failed." % \
299                         (dump_filename,))
300                 # ignore the failed chmod otherwise as the file should
301                 # already have the right privileges and is just, at worst,
302                 # unreadable for world
303             else:
304                 raise
305
306         p = Pickler(dump_file, 1)
307
308         p.dump(self.sanitised_changes())
309         p.dump(self.sanitised_dsc())
310         p.dump(self.sanitised_files())
311         p.dump(self.sanitised_dsc_files())
312         p.dump(self.sanitised_orig_files())
313
314         dump_file.close()
315
316     def unknown_files_fields(self, name):
317         return sorted(list( set(self.files[name].keys()) -
318                             set(CHANGESFIELDS_FILES)))
319
320     def unknown_changes_fields(self):
321         return sorted(list( set(self.changes.keys()) -
322                             set(CHANGESFIELDS_MANDATORY + CHANGESFIELDS_OPTIONAL)))
323
324     def unknown_dsc_fields(self):
325         return sorted(list( set(self.dsc.keys()) -
326                             set(CHANGESFIELDS_DSC)))
327
328     def unknown_dsc_files_fields(self, name):
329         return sorted(list( set(self.dsc_files[name].keys()) -
330                             set(CHANGESFIELDS_DSCFILES_MANDATORY + CHANGESFIELDS_DSCFILES_OPTIONAL)))
331
332     def str_files(self):
333         r = []
334         for name, entry in self.files.items():
335             r.append("  %s:" % (name))
336             for i in CHANGESFIELDS_FILES:
337                 if entry.has_key(i):
338                     r.append("   %s: %s" % (i.capitalize(), entry[i]))
339             xfields = self.unknown_files_fields(name)
340             if len(xfields) > 0:
341                 r.append("files[%s] still has following unrecognised keys: %s" % (name, ", ".join(xfields)))
342
343         return r
344
345     def str_changes(self):
346         r = []
347         for i in CHANGESFIELDS_MANDATORY:
348             val = self.changes[i]
349             if isinstance(val, list):
350                 val = " ".join(val)
351             elif isinstance(val, dict):
352                 val = " ".join(val.keys())
353             r.append('  %s: %s' % (i.capitalize(), val))
354
355         for i in CHANGESFIELDS_OPTIONAL:
356             if self.changes.has_key(i):
357                 r.append('  %s: %s' % (i.capitalize(), self.changes[i]))
358
359         xfields = self.unknown_changes_fields()
360         if len(xfields) > 0:
361             r.append("Warning: changes still has the following unrecognised fields: %s" % ", ".join(xfields))
362
363         return r
364
365     def str_dsc(self):
366         r = []
367         for i in CHANGESFIELDS_DSC:
368             if self.dsc.has_key(i):
369                 r.append('  %s: %s' % (i.capitalize(), self.dsc[i]))
370
371         xfields = self.unknown_dsc_fields()
372         if len(xfields) > 0:
373             r.append("Warning: dsc still has the following unrecognised fields: %s" % ", ".join(xfields))
374
375         return r
376
377     def str_dsc_files(self):
378         r = []
379         for name, entry in self.dsc_files.items():
380             r.append("  %s:" % (name))
381             for i in CHANGESFIELDS_DSCFILES_MANDATORY:
382                 r.append("   %s: %s" % (i.capitalize(), entry[i]))
383             for i in CHANGESFIELDS_DSCFILES_OPTIONAL:
384                 if entry.has_key(i):
385                     r.append("   %s: %s" % (i.capitalize(), entry[i]))
386             xfields = self.unknown_dsc_files_fields(name)
387             if len(xfields) > 0:
388                 r.append("dsc_files[%s] still has following unrecognised keys: %s" % (name, ", ".join(xfields)))
389
390         return r
391
392     def __str__(self):
393         r = []
394
395         r.append(" Changes:")
396         r += self.str_changes()
397
398         r.append("")
399
400         r.append(" Dsc:")
401         r += self.str_dsc()
402
403         r.append("")
404
405         r.append(" Files:")
406         r += self.str_files()
407
408         r.append("")
409
410         r.append(" Dsc Files:")
411         r += self.str_dsc_files()
412
413         return "\n".join(r)
414
415 __all__.append('Changes')