]> git.decadent.org.uk Git - dak.git/blob - daklib/changes.py
Merge commit 'mhy/sqlalchemy' 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 ###############################################################################
80
81 class Changes(object):
82     """ Convenience wrapper to carry around all the package information """
83
84     def __init__(self, **kwds):
85         self.reset()
86
87     def reset(self):
88         self.changes_file = ""
89
90         self.changes = {}
91         self.dsc = {}
92         self.files = {}
93         self.dsc_files = {}
94
95         self.orig_tar_id = None
96         self.orig_tar_location = ""
97         self.orig_tar_gz = None
98
99     def file_summary(self):
100         # changes["distribution"] may not exist in corner cases
101         # (e.g. unreadable changes files)
102         if not self.changes.has_key("distribution") or not \
103                isinstance(self.changes["distribution"], dict):
104             self.changes["distribution"] = {}
105
106         byhand = False
107         new = False
108         summary = ""
109         override_summary = ""
110
111         for name, entry in sorted(self.files.items()):
112             if entry.has_key("byhand"):
113                 byhand = True
114                 summary += name + " byhand\n"
115
116             elif entry.has_key("new"):
117                 new = True
118                 summary += "(new) %s %s %s\n" % (name, entry["priority"], entry["section"])
119
120                 if entry.has_key("othercomponents"):
121                     summary += "WARNING: Already present in %s distribution.\n" % (entry["othercomponents"])
122
123                 if entry["type"] == "deb":
124                     deb_fh = open_file(name)
125                     summary += ParseSection(debExtractControl(deb_fh))["Description"] + '\n'
126                     deb_fh.close()
127
128             else:
129                 entry["pool name"] = poolify(self.changes.get("source", ""), entry["component"])
130                 destination = entry["pool name"] + name
131                 summary += name + "\n  to " + destination + "\n"
132
133                 if not entry.has_key("type"):
134                     entry["type"] = "unknown"
135
136                 if entry["type"] in ["deb", "udeb", "dsc"]:
137                     # (queue/unchecked), there we have override entries already, use them
138                     # (process-new), there we dont have override entries, use the newly generated ones.
139                     override_prio = entry.get("override priority", entry["priority"])
140                     override_sect = entry.get("override section", entry["section"])
141                     override_summary += "%s - %s %s\n" % (name, override_prio, override_sect)
142
143         return (byhand, new, summary, override_summary)
144
145     def check_override(self):
146         """
147         Checks override entries for validity.
148
149         Returns an empty string if there are no problems
150         or the text of a warning if there are
151         """
152
153         summary = ""
154
155         # Abandon the check if it's a non-sourceful upload
156         if not self.changes["architecture"].has_key("source"):
157             return summary
158
159         for name, entry in sorted(self.files.items()):
160             if not entry.has_key("new") and entry["type"] == "deb":
161                 if entry["section"] != "-":
162                     if entry["section"].lower() != entry["override section"].lower():
163                         summary += "%s: package says section is %s, override says %s.\n" % (name,
164                                                                                             entry["section"],
165                                                                                             entry["override section"])
166
167                 if entry["priority"] != "-":
168                     if entry["priority"] != entry["override priority"]:
169                         summary += "%s: package says priority is %s, override says %s.\n" % (name,
170                                                                                              entry["priority"],
171                                                                                              entry["override priority"])
172
173         return summary
174
175
176     def load_dot_dak(self, changesfile):
177         """
178         Update ourself by reading a previously created cPickle .dak dumpfile.
179         """
180
181         self.changes_file = changesfile
182         dump_filename = self.changes_file[:-8]+".dak"
183         dump_file = open_file(dump_filename)
184
185         p = Unpickler(dump_file)
186
187         self.changes.update(p.load())
188         self.dsc.update(p.load())
189         self.files.update(p.load())
190         self.dsc_files.update(p.load())
191
192         self.orig_tar_id = p.load()
193         self.orig_tar_location = p.load()
194
195         dump_file.close()
196
197     def sanitised_files(self):
198         ret = {}
199         for name, entry in self.files.items():
200             ret[name] = {}
201             for i in CHANGESFIELDS_FILES:
202                 if entry.has_key(i):
203                     ret[name][i] = entry[i]
204
205         return ret
206
207     def sanitised_changes(self):
208         ret = {}
209         # Mandatory changes fields
210         for i in CHANGESFIELDS_MANDATORY:
211             ret[i] = self.changes[i]
212
213         # Optional changes fields
214         for i in CHANGESFIELDS_OPTIONAL:
215             if self.changes.has_key(i):
216                 ret[i] = self.changes[i]
217
218         return ret
219
220     def sanitised_dsc(self):
221         ret = {}
222         for i in CHANGESFIELDS_DSC:
223             if self.dsc.has_key(i):
224                 ret[i] = self.dsc[i]
225
226         return ret
227
228     def sanitised_dsc_files(self):
229         ret = {}
230         for name, entry in self.dsc_files.items():
231             ret[name] = {}
232             # Mandatory dsc_files fields
233             for i in CHANGESFIELDS_DSCFILES_MANDATORY:
234                 ret[name][i] = entry[i]
235
236             # Optional dsc_files fields
237             for i in CHANGESFIELDS_DSCFILES_OPTIONAL:
238                 if entry.has_key(i):
239                     ret[name][i] = entry[i]
240
241         return ret
242
243     def write_dot_dak(self, dest_dir):
244         """
245         Dump ourself into a cPickle file.
246
247         @type dest_dir: string
248         @param dest_dir: Path where the dumpfile should be stored
249
250         @note: This could just dump the dictionaries as is, but I'd like to avoid this so
251                there's some idea of what process-accepted & process-new use from
252                process-unchecked. (JT)
253
254         """
255
256         dump_filename = os.path.join(dest_dir, self.changes_file[:-8] + ".dak")
257         dump_file = open_file(dump_filename, 'w')
258
259         try:
260             os.chmod(dump_filename, 0664)
261         except OSError, e:
262             # chmod may fail when the dumpfile is not owned by the user
263             # invoking dak (like e.g. when NEW is processed by a member
264             # of ftpteam)
265             if e.errno == EPERM:
266                 perms = stat.S_IMODE(os.stat(dump_filename)[stat.ST_MODE])
267                 # security precaution, should never happen unless a weird
268                 # umask is set anywhere
269                 if perms & stat.S_IWOTH:
270                     fubar("%s is world writable and chmod failed." % \
271                         (dump_filename,))
272                 # ignore the failed chmod otherwise as the file should
273                 # already have the right privileges and is just, at worst,
274                 # unreadable for world
275             else:
276                 raise
277
278         p = Pickler(dump_file, 1)
279
280         p.dump(self.sanitised_changes())
281         p.dump(self.sanitised_dsc())
282         p.dump(self.sanitised_files())
283         p.dump(self.sanitised_dsc_files())
284         p.dump(self.orig_tar_id)
285         p.dump(self.orig_tar_location)
286
287         dump_file.close()
288
289     def unknown_files_fields(self, name):
290         return sorted(list( set(self.files[name].keys()) -
291                             set(CHANGESFIELDS_FILES)))
292
293     def unknown_changes_fields(self):
294         return sorted(list( set(self.changes.keys()) -
295                             set(CHANGESFIELDS_MANDATORY + CHANGESFIELDS_OPTIONAL)))
296
297     def unknown_dsc_fields(self):
298         return sorted(list( set(self.dsc.keys()) -
299                             set(CHANGESFIELDS_DSC)))
300
301     def unknown_dsc_files_fields(self, name):
302         return sorted(list( set(self.dsc_files[name].keys()) -
303                             set(CHANGESFIELDS_DSCFILES_MANDATORY + CHANGESFIELDS_DSCFILES_OPTIONAL)))
304
305     def str_files(self):
306         r = []
307         for name, entry in self.files.items():
308             r.append("  %s:" % (name))
309             for i in CHANGESFIELDS_FILES:
310                 if entry.has_key(i):
311                     r.append("   %s: %s" % (i.capitalize(), entry[i]))
312             xfields = self.unknown_files_fields(name)
313             if len(xfields) > 0:
314                 r.append("files[%s] still has following unrecognised keys: %s" % (name, ", ".join(xfields)))
315
316         return r
317
318     def str_changes(self):
319         r = []
320         for i in CHANGESFIELDS_MANDATORY:
321             val = self.changes[i]
322             if isinstance(val, list):
323                 val = " ".join(val)
324             elif isinstance(val, dict):
325                 val = " ".join(val.keys())
326             r.append('  %s: %s' % (i.capitalize(), val))
327
328         for i in CHANGESFIELDS_OPTIONAL:
329             if self.changes.has_key(i):
330                 r.append('  %s: %s' % (i.capitalize(), self.changes[i]))
331
332         xfields = self.unknown_changes_fields()
333         if len(xfields) > 0:
334             r.append("Warning: changes still has the following unrecognised fields: %s" % ", ".join(xfields))
335
336         return r
337
338     def str_dsc(self):
339         r = []
340         for i in CHANGESFIELDS_DSC:
341             if self.dsc.has_key(i):
342                 r.append('  %s: %s' % (i.capitalize(), self.dsc[i]))
343
344         xfields = self.unknown_dsc_fields()
345         if len(xfields) > 0:
346             r.append("Warning: dsc still has the following unrecognised fields: %s" % ", ".join(xfields))
347
348         return r
349
350     def str_dsc_files(self):
351         r = []
352         for name, entry in self.dsc_files.items():
353             r.append("  %s:" % (name))
354             for i in CHANGESFIELDS_DSCFILES_MANDATORY:
355                 r.append("   %s: %s" % (i.capitalize(), entry[i]))
356             for i in CHANGESFIELDS_DSCFILES_OPTIONAL:
357                 if entry.has_key(i):
358                     r.append("   %s: %s" % (i.capitalize(), entry[i]))
359             xfields = self.unknown_dsc_files_fields(name)
360             if len(xfields) > 0:
361                 r.append("dsc_files[%s] still has following unrecognised keys: %s" % (name, ", ".join(xfields)))
362
363         return r
364
365     def __str__(self):
366         r = []
367
368         r.append(" Changes:")
369         r += self.str_changes()
370
371         r.append("")
372
373         r.append(" Dsc:")
374         r += self.str_dsc()
375
376         r.append("")
377
378         r.append(" Files:")
379         r += self.str_files()
380
381         r.append("")
382
383         r.append(" Dsc Files:")
384         r += self.str_dsc_files()
385
386         return "\n".join(r)
387
388 __all__.append('Changes')