]> git.decadent.org.uk Git - dak.git/blob - dak/process_policy.py
Debug suites might also miss the source package
[dak.git] / dak / process_policy.py
1 #!/usr/bin/env python
2 # vim:set et ts=4 sw=4:
3
4 """ Handles packages from policy queues
5
6 @contact: Debian FTP Master <ftpmaster@debian.org>
7 @copyright: 2001, 2002, 2003, 2004, 2005, 2006  James Troup <james@nocrew.org>
8 @copyright: 2009 Joerg Jaspert <joerg@debian.org>
9 @copyright: 2009 Frank Lichtenheld <djpig@debian.org>
10 @copyright: 2009 Mark Hymers <mhy@debian.org>
11 @license: GNU General Public License version 2 or later
12 """
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
27 ################################################################################
28
29 # <mhy> So how do we handle that at the moment?
30 # <stew> Probably incorrectly.
31
32 ################################################################################
33
34 import os
35 import datetime
36 import re
37 import sys
38 import traceback
39 import apt_pkg
40 from sqlalchemy.orm.exc import NoResultFound
41
42 from daklib.dbconn import *
43 from daklib import daklog
44 from daklib import utils
45 from daklib.dak_exceptions import CantOpenError, AlreadyLockedError, CantGetLockError
46 from daklib.config import Config
47 from daklib.archive import ArchiveTransaction, source_component_from_package_list
48 from daklib.urgencylog import UrgencyLog
49 from daklib.packagelist import PackageList
50
51 import daklib.announce
52 import daklib.utils
53
54 # Globals
55 Options = None
56 Logger = None
57
58 ################################################################################
59
60 def do_comments(dir, srcqueue, opref, npref, line, fn, transaction):
61     session = transaction.session
62     actions = []
63     for comm in [ x for x in os.listdir(dir) if x.startswith(opref) ]:
64         lines = open(os.path.join(dir, comm)).readlines()
65         if len(lines) == 0 or lines[0] != line + "\n": continue
66
67         # If the ACCEPT includes a _<arch> we only accept that .changes.
68         # Otherwise we accept all .changes that start with the given prefix
69         changes_prefix = comm[len(opref):]
70         if changes_prefix.count('_') < 2:
71             changes_prefix = changes_prefix + '_'
72         else:
73             changes_prefix = changes_prefix + '.changes'
74
75         # We need to escape "_" as we use it with the LIKE operator (via the
76         # SQLA startwith) later.
77         changes_prefix = changes_prefix.replace("_", r"\_")
78
79         uploads = session.query(PolicyQueueUpload).filter_by(policy_queue=srcqueue) \
80             .join(PolicyQueueUpload.changes).filter(DBChange.changesname.startswith(changes_prefix)) \
81             .order_by(PolicyQueueUpload.source_id)
82         reason = "".join(lines[1:])
83         actions.extend((u, reason) for u in uploads)
84
85         if opref != npref:
86             newcomm = npref + comm[len(opref):]
87             newcomm = utils.find_next_free(os.path.join(dir, newcomm))
88             transaction.fs.move(os.path.join(dir, comm), newcomm)
89
90     actions.sort()
91
92     for u, reason in actions:
93         print("Processing changes file: {0}".format(u.changes.changesname))
94         fn(u, srcqueue, reason, transaction)
95
96 ################################################################################
97
98 def try_or_reject(function):
99     def wrapper(upload, srcqueue, comments, transaction):
100         try:
101             function(upload, srcqueue, comments, transaction)
102         except Exception as e:
103             comments = 'An exception was raised while processing the package:\n{0}\nOriginal comments:\n{1}'.format(traceback.format_exc(), comments)
104             try:
105                 transaction.rollback()
106                 real_comment_reject(upload, srcqueue, comments, transaction)
107             except Exception as e:
108                 comments = 'In addition an exception was raised while trying to reject the upload:\n{0}\nOriginal rejection:\n{1}'.format(traceback.format_exc(), comments)
109                 transaction.rollback()
110                 real_comment_reject(upload, srcqueue, comments, transaction, notify=False)
111         if not Options['No-Action']:
112             transaction.commit()
113         else:
114             transaction.rollback()
115     return wrapper
116
117 ################################################################################
118
119 @try_or_reject
120 def comment_accept(upload, srcqueue, comments, transaction):
121     for byhand in upload.byhand:
122         path = os.path.join(srcqueue.path, byhand.filename)
123         if os.path.exists(path):
124             raise Exception('E: cannot ACCEPT upload with unprocessed byhand file {0}'.format(byhand.filename))
125
126     cnf = Config()
127
128     fs = transaction.fs
129     session = transaction.session
130     changesname = upload.changes.changesname
131     allow_tainted = srcqueue.suite.archive.tainted
132
133     # We need overrides to get the target component
134     overridesuite = upload.target_suite
135     if overridesuite.overridesuite is not None:
136         overridesuite = session.query(Suite).filter_by(suite_name=overridesuite.overridesuite).one()
137
138     def binary_component_func(db_binary):
139         section = db_binary.proxy['Section']
140         component_name = 'main'
141         if section.find('/') != -1:
142             component_name = section.split('/', 1)[0]
143         return get_mapped_component(component_name, session=session)
144
145     def is_debug_binary(db_binary):
146         return daklib.utils.is_in_debug_section(db_binary.proxy)
147
148     def has_debug_binaries(upload):
149         return any((is_debug_binary(x) for x in upload.binaries))
150
151     def source_component_func(db_source):
152         package_list = PackageList(db_source.proxy)
153         component = source_component_from_package_list(package_list, upload.target_suite)
154         if component is not None:
155             return get_mapped_component(component.component_name, session=session)
156
157         # Fallback for packages without Package-List field
158         query = session.query(Override).filter_by(suite=overridesuite, package=db_source.source) \
159             .join(OverrideType).filter(OverrideType.overridetype == 'dsc') \
160             .join(Component)
161         return query.one().component
162
163     all_target_suites = [upload.target_suite]
164     all_target_suites.extend([q.suite for q in upload.target_suite.copy_queues])
165
166     for suite in all_target_suites:
167         debug_suite = suite.debug_suite
168
169         if upload.source is not None:
170             # If we have Source in this upload, let's include it into
171             # upload suite.
172             transaction.copy_source(
173                 upload.source,
174                 suite,
175                 source_component_func(upload.source),
176                 allow_tainted=allow_tainted,
177             )
178
179             if debug_suite is not None and has_debug_binaries(upload):
180                 # If we're handing a debug package, we also need to include the
181                 # source in the debug suite as well.
182                 transaction.copy_source(
183                     upload.source,
184                     debug_suite,
185                     source_component_func(upload.source),
186                     allow_tainted=allow_tainted,
187                 )
188
189         for db_binary in upload.binaries:
190             # Now, let's work out where to copy this guy to -- if it's
191             # a debug binary, and the suite has a debug suite, let's go
192             # ahead and target the debug suite rather then the stock
193             # suite.
194             copy_to_suite = suite
195             if debug_suite is not None and is_debug_binary(db_binary):
196                 copy_to_suite = debug_suite
197
198             # build queues and debug suites may miss the source package
199             # if this is a binary-only upload.
200             if copy_to_suite != upload.target_suite:
201                 transaction.copy_source(
202                     db_binary.source,
203                     copy_to_suite,
204                     source_component_func(db_binary.source),
205                     allow_tainted=allow_tainted,
206                 )
207
208             transaction.copy_binary(
209                 db_binary,
210                 copy_to_suite,
211                 binary_component_func(db_binary),
212                 allow_tainted=allow_tainted,
213                 extra_archives=[upload.target_suite.archive],
214             )
215
216     # Copy .changes if needed
217     if upload.target_suite.copychanges:
218         src = os.path.join(upload.policy_queue.path, upload.changes.changesname)
219         dst = os.path.join(upload.target_suite.path, upload.changes.changesname)
220         fs.copy(src, dst, mode=upload.target_suite.archive.mode)
221
222     # Copy upload to Process-Policy::CopyDir
223     # Used on security.d.o to sync accepted packages to ftp-master, but this
224     # should eventually be replaced by something else.
225     copydir = cnf.get('Process-Policy::CopyDir') or None
226     if copydir is not None:
227         mode = upload.target_suite.archive.mode
228         if upload.source is not None:
229             for f in [ df.poolfile for df in upload.source.srcfiles ]:
230                 dst = os.path.join(copydir, f.basename)
231                 if not os.path.exists(dst):
232                     fs.copy(f.fullpath, dst, mode=mode)
233
234         for db_binary in upload.binaries:
235             f = db_binary.poolfile
236             dst = os.path.join(copydir, f.basename)
237             if not os.path.exists(dst):
238                 fs.copy(f.fullpath, dst, mode=mode)
239
240         src = os.path.join(upload.policy_queue.path, upload.changes.changesname)
241         dst = os.path.join(copydir, upload.changes.changesname)
242         if not os.path.exists(dst):
243             fs.copy(src, dst, mode=mode)
244
245     if upload.source is not None and not Options['No-Action']:
246         urgency = upload.changes.urgency
247         if urgency not in cnf.value_list('Urgency::Valid'):
248             urgency = cnf['Urgency::Default']
249         UrgencyLog().log(upload.source.source, upload.source.version, urgency)
250
251     print "  ACCEPT"
252     if not Options['No-Action']:
253         Logger.log(["Policy Queue ACCEPT", srcqueue.queue_name, changesname])
254
255     pu = get_processed_upload(upload)
256     daklib.announce.announce_accept(pu)
257
258     # TODO: code duplication. Similar code is in process-upload.
259     # Move .changes to done
260     src = os.path.join(upload.policy_queue.path, upload.changes.changesname)
261     now = datetime.datetime.now()
262     donedir = os.path.join(cnf['Dir::Done'], now.strftime('%Y/%m/%d'))
263     dst = os.path.join(donedir, upload.changes.changesname)
264     dst = utils.find_next_free(dst)
265     fs.copy(src, dst, mode=0o644)
266
267     remove_upload(upload, transaction)
268
269 ################################################################################
270
271 @try_or_reject
272 def comment_reject(*args):
273     real_comment_reject(*args, manual=True)
274
275 def real_comment_reject(upload, srcqueue, comments, transaction, notify=True, manual=False):
276     cnf = Config()
277
278     fs = transaction.fs
279     session = transaction.session
280     changesname = upload.changes.changesname
281     queuedir = upload.policy_queue.path
282     rejectdir = cnf['Dir::Reject']
283
284     ### Copy files to reject/
285
286     poolfiles = [b.poolfile for b in upload.binaries]
287     if upload.source is not None:
288         poolfiles.extend([df.poolfile for df in upload.source.srcfiles])
289     # Not beautiful...
290     files = [ af.path for af in session.query(ArchiveFile) \
291                   .filter_by(archive=upload.policy_queue.suite.archive) \
292                   .join(ArchiveFile.file) \
293                   .filter(PoolFile.file_id.in_([ f.file_id for f in poolfiles ])) ]
294     for byhand in upload.byhand:
295         path = os.path.join(queuedir, byhand.filename)
296         if os.path.exists(path):
297             files.append(path)
298     files.append(os.path.join(queuedir, changesname))
299
300     for fn in files:
301         dst = utils.find_next_free(os.path.join(rejectdir, os.path.basename(fn)))
302         fs.copy(fn, dst, link=True)
303
304     ### Write reason
305
306     dst = utils.find_next_free(os.path.join(rejectdir, '{0}.reason'.format(changesname)))
307     fh = fs.create(dst)
308     fh.write(comments)
309     fh.close()
310
311     ### Send mail notification
312
313     if notify:
314         rejected_by = None
315         reason = comments
316
317         # Try to use From: from comment file if there is one.
318         # This is not very elegant...
319         match = re.match(r"\AFrom: ([^\n]+)\n\n", comments)
320         if match:
321             rejected_by = match.group(1)
322             reason = '\n'.join(comments.splitlines()[2:])
323
324         pu = get_processed_upload(upload)
325         daklib.announce.announce_reject(pu, reason, rejected_by)
326
327     print "  REJECT"
328     if not Options["No-Action"]:
329         Logger.log(["Policy Queue REJECT", srcqueue.queue_name, upload.changes.changesname])
330
331     changes = upload.changes
332     remove_upload(upload, transaction)
333     session.delete(changes)
334
335 ################################################################################
336
337 def remove_upload(upload, transaction):
338     fs = transaction.fs
339     session = transaction.session
340     changes = upload.changes
341
342     # Remove byhand and changes files. Binary and source packages will be
343     # removed from {bin,src}_associations and eventually removed by clean-suites automatically.
344     queuedir = upload.policy_queue.path
345     for byhand in upload.byhand:
346         path = os.path.join(queuedir, byhand.filename)
347         if os.path.exists(path):
348             fs.unlink(path)
349         session.delete(byhand)
350     fs.unlink(os.path.join(queuedir, upload.changes.changesname))
351
352     session.delete(upload)
353     session.flush()
354
355 ################################################################################
356
357 def get_processed_upload(upload):
358     pu = daklib.announce.ProcessedUpload()
359
360     pu.maintainer = upload.changes.maintainer
361     pu.changed_by = upload.changes.changedby
362     pu.fingerprint = upload.changes.fingerprint
363
364     pu.suites = [ upload.target_suite ]
365     pu.from_policy_suites = [ upload.target_suite ]
366
367     changes_path = os.path.join(upload.policy_queue.path, upload.changes.changesname)
368     pu.changes = open(changes_path, 'r').read()
369     pu.changes_filename = upload.changes.changesname
370     pu.sourceful = upload.source is not None
371     pu.source = upload.changes.source
372     pu.version = upload.changes.version
373     pu.architecture = upload.changes.architecture
374     pu.bugs = upload.changes.closes
375
376     pu.program = "process-policy"
377
378     return pu
379
380 ################################################################################
381
382 def remove_unreferenced_binaries(policy_queue, transaction):
383     """Remove binaries that are no longer referenced by an upload
384
385     @type  policy_queue: L{daklib.dbconn.PolicyQueue}
386
387     @type  transaction: L{daklib.archive.ArchiveTransaction}
388     """
389     session = transaction.session
390     suite = policy_queue.suite
391
392     query = """
393        SELECT b.*
394          FROM binaries b
395          JOIN bin_associations ba ON b.id = ba.bin
396         WHERE ba.suite = :suite_id
397           AND NOT EXISTS (SELECT 1 FROM policy_queue_upload_binaries_map pqubm
398                                    JOIN policy_queue_upload pqu ON pqubm.policy_queue_upload_id = pqu.id
399                                   WHERE pqu.policy_queue_id = :policy_queue_id
400                                     AND pqubm.binary_id = b.id)"""
401     binaries = session.query(DBBinary).from_statement(query) \
402         .params({'suite_id': policy_queue.suite_id, 'policy_queue_id': policy_queue.policy_queue_id})
403
404     for binary in binaries:
405         Logger.log(["removed binary from policy queue", policy_queue.queue_name, binary.package, binary.version])
406         transaction.remove_binary(binary, suite)
407
408 def remove_unreferenced_sources(policy_queue, transaction):
409     """Remove sources that are no longer referenced by an upload or a binary
410
411     @type  policy_queue: L{daklib.dbconn.PolicyQueue}
412
413     @type  transaction: L{daklib.archive.ArchiveTransaction}
414     """
415     session = transaction.session
416     suite = policy_queue.suite
417
418     query = """
419        SELECT s.*
420          FROM source s
421          JOIN src_associations sa ON s.id = sa.source
422         WHERE sa.suite = :suite_id
423           AND NOT EXISTS (SELECT 1 FROM policy_queue_upload pqu
424                                   WHERE pqu.policy_queue_id = :policy_queue_id
425                                     AND pqu.source_id = s.id)
426           AND NOT EXISTS (SELECT 1 FROM binaries b
427                                    JOIN bin_associations ba ON b.id = ba.bin
428                                   WHERE b.source = s.id
429                                     AND ba.suite = :suite_id)"""
430     sources = session.query(DBSource).from_statement(query) \
431         .params({'suite_id': policy_queue.suite_id, 'policy_queue_id': policy_queue.policy_queue_id})
432
433     for source in sources:
434         Logger.log(["removed source from policy queue", policy_queue.queue_name, source.source, source.version])
435         transaction.remove_source(source, suite)
436
437 ################################################################################
438
439 def main():
440     global Options, Logger
441
442     cnf = Config()
443     session = DBConn().session()
444
445     Arguments = [('h',"help","Process-Policy::Options::Help"),
446                  ('n',"no-action","Process-Policy::Options::No-Action")]
447
448     for i in ["help", "no-action"]:
449         if not cnf.has_key("Process-Policy::Options::%s" % (i)):
450             cnf["Process-Policy::Options::%s" % (i)] = ""
451
452     queue_name = apt_pkg.parse_commandline(cnf.Cnf,Arguments,sys.argv)
453
454     if len(queue_name) != 1:
455         print "E: Specify exactly one policy queue"
456         sys.exit(1)
457
458     queue_name = queue_name[0]
459
460     Options = cnf.subtree("Process-Policy::Options")
461
462     if Options["Help"]:
463         usage()
464
465     Logger = daklog.Logger("process-policy")
466     if not Options["No-Action"]:
467         urgencylog = UrgencyLog()
468
469     with ArchiveTransaction() as transaction:
470         session = transaction.session
471         try:
472             pq = session.query(PolicyQueue).filter_by(queue_name=queue_name).one()
473         except NoResultFound:
474             print "E: Cannot find policy queue %s" % queue_name
475             sys.exit(1)
476
477         commentsdir = os.path.join(pq.path, 'COMMENTS')
478         # The comments stuff relies on being in the right directory
479         os.chdir(pq.path)
480
481         do_comments(commentsdir, pq, "REJECT.", "REJECTED.", "NOTOK", comment_reject, transaction)
482         do_comments(commentsdir, pq, "ACCEPT.", "ACCEPTED.", "OK", comment_accept, transaction)
483         do_comments(commentsdir, pq, "ACCEPTED.", "ACCEPTED.", "OK", comment_accept, transaction)
484
485         remove_unreferenced_binaries(pq, transaction)
486         remove_unreferenced_sources(pq, transaction)
487
488     if not Options['No-Action']:
489         urgencylog.close()
490
491 ################################################################################
492
493 if __name__ == '__main__':
494     main()