]> git.decadent.org.uk Git - dak.git/blob - dak/process_upload.py
dak/process_upload.py: improve error message for ArchiveExceptions and Rejects
[dak.git] / dak / process_upload.py
1 #!/usr/bin/env python
2
3 """
4 Checks Debian packages from Incoming
5 @contact: Debian FTP Master <ftpmaster@debian.org>
6 @copyright: 2000, 2001, 2002, 2003, 2004, 2005, 2006  James Troup <james@nocrew.org>
7 @copyright: 2009  Joerg Jaspert <joerg@debian.org>
8 @copyright: 2009  Mark Hymers <mhy@debian.org>
9 @copyright: 2009  Frank Lichtenheld <djpig@debian.org>
10 @license: GNU General Public License version 2 or later
11 """
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 # based on process-unchecked and process-accepted
28
29 ## pu|pa: locking (daily.lock)
30 ## pu|pa: parse arguments -> list of changes files
31 ## pa: initialize urgency log
32 ## pu|pa: sort changes list
33
34 ## foreach changes:
35 ###  pa: load dak file
36 ##   pu: copy CHG to tempdir
37 ##   pu: check CHG signature
38 ##   pu: parse changes file
39 ##   pu: checks:
40 ##     pu: check distribution (mappings, rejects)
41 ##     pu: copy FILES to tempdir
42 ##     pu: check whether CHG already exists in CopyChanges
43 ##     pu: check whether FILES already exist in one of the policy queues
44 ##     for deb in FILES:
45 ##       pu: extract control information
46 ##       pu: various checks on control information
47 ##       pu|pa: search for source (in CHG, projectb, policy queues)
48 ##       pu|pa: check whether "Version" fulfills target suite requirements/suite propagation
49 ##       pu|pa: check whether deb already exists in the pool
50 ##     for src in FILES:
51 ##       pu: various checks on filenames and CHG consistency
52 ##       pu: if isdsc: check signature
53 ##     for file in FILES:
54 ##       pu: various checks
55 ##       pu: NEW?
56 ##       //pu: check whether file already exists in the pool
57 ##       pu: store what "Component" the package is currently in
58 ##     pu: check whether we found everything we were looking for in CHG
59 ##     pu: check the DSC:
60 ##       pu: check whether we need and have ONE DSC
61 ##       pu: parse the DSC
62 ##       pu: various checks //maybe drop some of the in favor of lintian
63 ##       pu|pa: check whether "Version" fulfills target suite requirements/suite propagation
64 ##       pu: check whether DSC_FILES is consistent with "Format"
65 ##       for src in DSC_FILES:
66 ##         pu|pa: check whether file already exists in the pool (with special handling for .orig.tar.gz)
67 ##     pu: create new tempdir
68 ##     pu: create symlink mirror of source
69 ##     pu: unpack source
70 ##     pu: extract changelog information for BTS
71 ##     //pu: create missing .orig symlink
72 ##     pu: check with lintian
73 ##     for file in FILES:
74 ##       pu: check checksums and sizes
75 ##     for file in DSC_FILES:
76 ##       pu: check checksums and sizes
77 ##     pu: CHG: check urgency
78 ##     for deb in FILES:
79 ##       pu: extract contents list and check for dubious timestamps
80 ##     pu: check that the uploader is actually allowed to upload the package
81 ###  pa: install:
82 ###    if stable_install:
83 ###      pa: remove from p-u
84 ###      pa: add to stable
85 ###      pa: move CHG to morgue
86 ###      pa: append data to ChangeLog
87 ###      pa: send mail
88 ###      pa: remove .dak file
89 ###    else:
90 ###      pa: add dsc to db:
91 ###        for file in DSC_FILES:
92 ###          pa: add file to file
93 ###          pa: add file to dsc_files
94 ###        pa: create source entry
95 ###        pa: update source associations
96 ###        pa: update src_uploaders
97 ###      for deb in FILES:
98 ###        pa: add deb to db:
99 ###          pa: add file to file
100 ###          pa: find source entry
101 ###          pa: create binaries entry
102 ###          pa: update binary associations
103 ###      pa: .orig component move
104 ###      pa: move files to pool
105 ###      pa: save CHG
106 ###      pa: move CHG to done/
107 ###      pa: change entry in queue_build
108 ##   pu: use dispatch table to choose target queue:
109 ##     if NEW:
110 ##       pu: write .dak file
111 ##       pu: move to NEW
112 ##       pu: send mail
113 ##     elsif AUTOBYHAND:
114 ##       pu: run autobyhand script
115 ##       pu: if stuff left, do byhand or accept
116 ##     elsif targetqueue in (oldstable, stable, embargo, unembargo):
117 ##       pu: write .dak file
118 ##       pu: check overrides
119 ##       pu: move to queue
120 ##       pu: send mail
121 ##     else:
122 ##       pu: write .dak file
123 ##       pu: move to ACCEPTED
124 ##       pu: send mails
125 ##       pu: create files for BTS
126 ##       pu: create entry in queue_build
127 ##       pu: check overrides
128
129 # Integrity checks
130 ## GPG
131 ## Parsing changes (check for duplicates)
132 ## Parse dsc
133 ## file list checks
134
135 # New check layout (TODO: Implement)
136 ## Permission checks
137 ### suite mappings
138 ### ACLs
139 ### version checks (suite)
140 ### override checks
141
142 ## Source checks
143 ### copy orig
144 ### unpack
145 ### BTS changelog
146 ### src contents
147 ### lintian
148 ### urgency log
149
150 ## Binary checks
151 ### timestamps
152 ### control checks
153 ### src relation check
154 ### contents
155
156 ## Database insertion (? copy from stuff)
157 ### BYHAND / NEW / Policy queues
158 ### Pool
159
160 ## Queue builds
161
162 import datetime
163 import errno
164 from errno import EACCES, EAGAIN
165 import fcntl
166 import os
167 import sys
168 import traceback
169 import apt_pkg
170 import time
171 from sqlalchemy.orm.exc import NoResultFound
172
173 from daklib import daklog
174 from daklib.dbconn import *
175 from daklib.urgencylog import UrgencyLog
176 from daklib.summarystats import SummaryStats
177 from daklib.config import Config
178 import daklib.utils as utils
179 from daklib.textutils import fix_maintainer
180 from daklib.regexes import *
181
182 import daklib.archive
183 import daklib.checks
184 import daklib.upload
185
186 ###############################################################################
187
188 Options = None
189 Logger = None
190
191 ###############################################################################
192
193 def usage (exit_code=0):
194     print """Usage: dak process-upload [OPTION]... [CHANGES]...
195   -a, --automatic           automatic run
196   -d, --directory <DIR>     process uploads in <DIR>
197   -h, --help                show this help and exit.
198   -n, --no-action           don't do anything
199   -p, --no-lock             don't check lockfile !! for cron.daily only !!
200   -s, --no-mail             don't send any mail
201   -V, --version             display the version number and exit"""
202     sys.exit(exit_code)
203
204 ###############################################################################
205
206 def try_or_reject(function):
207     """Try to call function or reject the upload if that fails
208     """
209     def wrapper(directory, upload, *args, **kwargs):
210         reason = 'No exception caught. This should not happen.'
211
212         try:
213             return function(directory, upload, *args, **kwargs)
214         except (daklib.archive.ArchiveException, daklib.checks.Reject) as e:
215             reason = e
216         except Exception as e:
217             reason = "There was an uncaught exception when processing your upload:\n{0}\nAny original reject reason follows below.".format(traceback.format_exc())
218
219         try:
220             upload.rollback()
221             return real_reject(directory, upload, reason=reason)
222         except Exception as e:
223             reason = "In addition there was an exception when rejecting the package:\n{0}\nPrevious reasons:\n{1}".format(traceback.format_exc(), reason)
224             upload.rollback()
225             return real_reject(directory, upload, reason=reason, notify=False)
226
227         raise Exception('Rejecting upload failed after multiple tries. Giving up. Last reason:\n{0}'.format(reason))
228
229     return wrapper
230
231 def subst_for_upload(upload):
232     cnf = Config()
233
234     changes = upload.changes
235     control = upload.changes.changes
236
237     if upload.final_suites is None or len(upload.final_suites) == 0:
238         suite_name = '(unknown)'
239     else:
240         suite_names = []
241         for suite in upload.final_suites:
242             if suite.policy_queue:
243                 suite_names.append("{0}->{1}".format(suite.suite_name, suite.policy_queue.queue_name))
244             else:
245                 suite_names.append(suite.suite_name)
246         suite_name = ','.join(suite_names)
247
248     maintainer_field = control.get('Maintainer', cnf['Dinstall::MyEmailAddress'])
249     changed_by_field = control.get('Changed-By', maintainer_field)
250     maintainer = fix_maintainer(changed_by_field)
251     if upload.changes.source is not None:
252         addresses = utils.mail_addresses_for_upload(maintainer_field, changed_by_field, changes.primary_fingerprint)
253     else:
254         addresses = utils.mail_addresses_for_upload(maintainer_field, maintainer_field, changes.primary_fingerprint)
255
256     # debian-{devel-,}-changes@lists.debian.org toggles writes access based on this header
257     bcc = 'X-DAK: dak process-upload'
258     if 'Dinstall::Bcc' in cnf:
259         bcc = '{0}\nBcc: {1}'.format(bcc, cnf['Dinstall::Bcc'])
260
261     subst = {
262         '__DISTRO__': cnf['Dinstall::MyDistribution'],
263         '__ADMIN_ADDRESS__': cnf['Dinstall::MyAdminAddress'],
264
265         '__CHANGES_FILENAME__': upload.changes.filename,
266
267         '__SOURCE__': control.get('Source', '(unknown)'),
268         '__ARCHITECTURE__': control.get('Architecture', '(unknown)'),
269         '__VERSION__': control.get('Version', '(unknown)'),
270
271         '__SUITE__': suite_name,
272
273         '__DAK_ADDRESS__': cnf['Dinstall::MyEmailAddress'],
274         '__MAINTAINER_FROM__': maintainer[1],
275         '__MAINTAINER_TO__': ", ".join(addresses),
276         '__MAINTAINER__': changed_by_field,
277         '__BCC__': bcc,
278
279         '__BUG_SERVER__': cnf.get('Dinstall::BugServer'),
280
281         '__FILE_CONTENTS__': open(upload.changes.path, 'r').read(),
282         }
283
284     override_maintainer = cnf.get('Dinstall::OverrideMaintainer')
285     if override_maintainer:
286         subst['__MAINTAINER_TO__'] = subst['__MAINTAINER_FROM__'] = override_maintainer
287
288     return subst
289
290 @try_or_reject
291 def accept(directory, upload):
292     cnf = Config()
293
294     Logger.log(['ACCEPT', upload.changes.filename])
295
296     upload.install()
297
298     accepted_to_real_suite = False
299     for suite in upload.final_suites:
300         accepted_to_real_suite = accepted_to_real_suite or suite.policy_queue is None
301
302     sourceful_upload = 'source' in upload.changes.architectures
303
304     control = upload.changes.changes
305     if sourceful_upload and not Options['No-Action']:
306         urgency = control.get('Urgency')
307         if urgency not in cnf.value_list('Urgency::Valid'):
308             urgency = cnf['Urgency::Default']
309         UrgencyLog().log(control['Source'], control['Version'], urgency)
310
311     # send mail to maintainer
312     subst = subst_for_upload(upload)
313     message = utils.TemplateSubst(subst, os.path.join(cnf['Dir::Templates'], 'process-unchecked.accepted'))
314     utils.send_mail(message)
315
316     # send mail to announce lists and tracking server
317     if accepted_to_real_suite and sourceful_upload:
318         subst = subst_for_upload(upload)
319         announce = set()
320         for suite in upload.final_suites:
321             if suite.policy_queue is not None:
322                 continue
323             announce.update(suite.announce or [])
324         announce_address = ", ".join(announce)
325
326         tracking = cnf.get('Dinstall::TrackingServer')
327         if tracking and 'source' in upload.changes.architectures:
328             announce_address = '{0}\nBcc: {1}@{2}'.format(announce_address, control['Source'], tracking)
329
330         subst['__ANNOUNCE_LIST_ADDRESS__'] = announce_address
331
332         message = utils.TemplateSubst(subst, os.path.join(cnf['Dir::Templates'], 'process-unchecked.announce'))
333         utils.send_mail(message)
334
335     # Only close bugs for uploads that were not redirected to a policy queue.
336     # process-policy will close bugs for those once they are accepted.
337     subst = subst_for_upload(upload)
338     if accepted_to_real_suite and cnf.find_b('Dinstall::CloseBugs') and sourceful_upload:
339         for bugnum in upload.changes.closed_bugs:
340             subst['__BUG_NUMBER__'] = str(bugnum)
341
342             message = utils.TemplateSubst(subst, os.path.join(cnf['Dir::Templates'], 'process-unchecked.bug-close'))
343             utils.send_mail(message)
344
345             del subst['__BUG_NUMBER__']
346
347     # Move .changes to done, but only for uploads that were accepted to a
348     # real suite.  process-policy will handle this for uploads to queues.
349     if accepted_to_real_suite:
350         src = os.path.join(upload.directory, upload.changes.filename)
351
352         now = datetime.datetime.now()
353         donedir = os.path.join(cnf['Dir::Done'], now.strftime('%Y/%m/%d'))
354         dst = os.path.join(donedir, upload.changes.filename)
355         dst = utils.find_next_free(dst)
356
357         upload.transaction.fs.copy(src, dst, mode=0o644)
358
359     SummaryStats().accept_count += 1
360     SummaryStats().accept_bytes += upload.changes.bytes
361
362 @try_or_reject
363 def accept_to_new(directory, upload):
364     cnf = Config()
365
366     Logger.log(['ACCEPT-TO-NEW', upload.changes.filename])
367
368     upload.install_to_new()
369     # TODO: tag bugs pending
370
371     subst = subst_for_upload(upload)
372     message = utils.TemplateSubst(subst, os.path.join(cnf['Dir::Templates'], 'process-unchecked.new'))
373     utils.send_mail(message)
374
375     SummaryStats().accept_count += 1
376     SummaryStats().accept_bytes += upload.changes.bytes
377
378 @try_or_reject
379 def reject(directory, upload, reason=None, notify=True):
380     real_reject(directory, upload, reason, notify)
381
382 def real_reject(directory, upload, reason=None, notify=True):
383     # XXX: rejection itself should go to daklib.archive.ArchiveUpload
384     cnf = Config()
385
386     Logger.log(['REJECT', upload.changes.filename])
387
388     fs = upload.transaction.fs
389     rejectdir = cnf['Dir::Reject']
390
391     files = [ f.filename for f in upload.changes.files.itervalues() ]
392     files.append(upload.changes.filename)
393
394     for fn in files:
395         src = os.path.join(upload.directory, fn)
396         dst = utils.find_next_free(os.path.join(rejectdir, fn))
397         if not os.path.exists(src):
398             continue
399         fs.copy(src, dst)
400
401     if upload.reject_reasons is not None:
402         if reason is None:
403             reason = ''
404         reason = reason + '\n' + '\n'.join(upload.reject_reasons)
405
406     if reason is None:
407         reason = '(Unknown reason. Please check logs.)'
408
409     dst = utils.find_next_free(os.path.join(rejectdir, '{0}.reason'.format(upload.changes.filename)))
410     fh = fs.create(dst)
411     fh.write(reason)
412     fh.close()
413
414     if notify:
415         subst = subst_for_upload(upload)
416         subst['__REJECTOR_ADDRESS__'] = cnf['Dinstall::MyEmailAddress']
417         subst['__MANUAL_REJECT_MESSAGE__'] = ''
418         subst['__REJECT_MESSAGE__'] = reason
419         subst['__CC__'] = 'X-DAK-Rejection: automatic (moo)'
420
421         message = utils.TemplateSubst(subst, os.path.join(cnf['Dir::Templates'], 'queue.rejected'))
422         utils.send_mail(message)
423
424     SummaryStats().reject_count += 1
425
426 ###############################################################################
427
428 def action(directory, upload):
429     changes = upload.changes
430     processed = True
431
432     global Logger
433
434     cnf = Config()
435
436     okay = upload.check()
437
438     summary = changes.changes.get('Changes', '')
439
440     package_info = []
441     if okay:
442         if changes.source is not None:
443             package_info.append("source:{0}".format(changes.source.dsc['Source']))
444         for binary in changes.binaries:
445             package_info.append("binary:{0}".format(binary.control['Package']))
446
447     (prompt, answer) = ("", "XXX")
448     if Options["No-Action"] or Options["Automatic"]:
449         answer = 'S'
450
451     queuekey = ''
452
453     print summary
454     print
455     print "\n".join(package_info)
456     print
457
458     if len(upload.reject_reasons) > 0:
459         print "Reason:"
460         print "\n".join(upload.reject_reasons)
461         print
462
463         path = os.path.join(directory, changes.filename)
464         created = os.stat(path).st_mtime
465         now = time.time()
466         too_new = (now - created < int(cnf['Dinstall::SkipTime']))
467
468         if too_new:
469             print "SKIP (too new)"
470             prompt = "[S]kip, Quit ?"
471         else:
472             prompt = "[R]eject, Skip, Quit ?"
473             if Options["Automatic"]:
474                 answer = 'R'
475     elif upload.new:
476         prompt = "[N]ew, Skip, Quit ?"
477         if Options['Automatic']:
478             answer = 'N'
479     else:
480         prompt = "[A]ccept, Skip, Quit ?"
481         if Options['Automatic']:
482             answer = 'A'
483
484     while prompt.find(answer) == -1:
485         answer = utils.our_raw_input(prompt)
486         m = re_default_answer.match(prompt)
487         if answer == "":
488             answer = m.group(1)
489         answer = answer[:1].upper()
490
491     if answer == 'R':
492         reject(directory, upload)
493     elif answer == 'A':
494         # upload.try_autobyhand must not be run with No-Action.
495         if Options['No-Action']:
496             accept(directory, upload)
497         elif upload.try_autobyhand():
498             accept(directory, upload)
499         else:
500             print "W: redirecting to BYHAND as automatic processing failed."
501             accept_to_new(directory, upload)
502     elif answer == 'N':
503         accept_to_new(directory, upload)
504     elif answer == 'Q':
505         sys.exit(0)
506     elif answer == 'S':
507         processed = False
508
509     if not Options['No-Action']:
510         upload.commit()
511
512     return processed
513
514 ###############################################################################
515
516 def unlink_if_exists(path):
517     try:
518         os.unlink(path)
519     except OSError as e:
520         if e.errno != errno.ENOENT:
521             raise
522
523 def process_it(directory, changes, keyrings, session):
524     global Logger
525
526     print "\n{0}\n".format(changes.filename)
527     Logger.log(["Processing changes file", changes.filename])
528
529     with daklib.archive.ArchiveUpload(directory, changes, keyrings) as upload:
530         processed = action(directory, upload)
531         if processed and not Options['No-Action']:
532             unlink_if_exists(os.path.join(directory, changes.filename))
533             for fn in changes.files:
534                 unlink_if_exists(os.path.join(directory, fn))
535
536 ###############################################################################
537
538 def process_changes(changes_filenames):
539     session = DBConn().session()
540     keyrings = session.query(Keyring).filter_by(active=True).order_by(Keyring.priority)
541     keyring_files = [ k.keyring_name for k in keyrings ]
542
543     changes = []
544     for fn in changes_filenames:
545         try:
546             directory, filename = os.path.split(fn)
547             c = daklib.upload.Changes(directory, filename, keyring_files)
548             changes.append([directory, c])
549         except Exception as e:
550             Logger.log([filename, "Error while loading changes: {0}".format(e)])
551
552     changes.sort(key=lambda x: x[1])
553
554     for directory, c in changes:
555         process_it(directory, c, keyring_files, session)
556
557     session.rollback()
558
559 ###############################################################################
560
561 def main():
562     global Options, Logger
563
564     cnf = Config()
565     summarystats = SummaryStats()
566
567     Arguments = [('a',"automatic","Dinstall::Options::Automatic"),
568                  ('h',"help","Dinstall::Options::Help"),
569                  ('n',"no-action","Dinstall::Options::No-Action"),
570                  ('p',"no-lock", "Dinstall::Options::No-Lock"),
571                  ('s',"no-mail", "Dinstall::Options::No-Mail"),
572                  ('d',"directory", "Dinstall::Options::Directory", "HasArg")]
573
574     for i in ["automatic", "help", "no-action", "no-lock", "no-mail",
575               "version", "directory"]:
576         if not cnf.has_key("Dinstall::Options::%s" % (i)):
577             cnf["Dinstall::Options::%s" % (i)] = ""
578
579     changes_files = apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv)
580     Options = cnf.subtree("Dinstall::Options")
581
582     if Options["Help"]:
583         usage()
584
585     # -n/--dry-run invalidates some other options which would involve things happening
586     if Options["No-Action"]:
587         Options["Automatic"] = ""
588
589     # Check that we aren't going to clash with the daily cron job
590     if not Options["No-Action"] and os.path.exists("%s/daily.lock" % (cnf["Dir::Lock"])) and not Options["No-Lock"]:
591         utils.fubar("Archive maintenance in progress.  Try again later.")
592
593     # Obtain lock if not in no-action mode and initialize the log
594     if not Options["No-Action"]:
595         lock_fd = os.open(os.path.join(cnf["Dir::Lock"], 'dinstall.lock'), os.O_RDWR | os.O_CREAT)
596         try:
597             fcntl.lockf(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
598         except IOError as e:
599             if errno.errorcode[e.errno] == 'EACCES' or errno.errorcode[e.errno] == 'EAGAIN':
600                 utils.fubar("Couldn't obtain lock; assuming another 'dak process-upload' is already running.")
601             else:
602                 raise
603
604         # Initialise UrgencyLog() - it will deal with the case where we don't
605         # want to log urgencies
606         urgencylog = UrgencyLog()
607
608     Logger = daklog.Logger("process-upload", Options["No-Action"])
609
610     # If we have a directory flag, use it to find our files
611     if cnf["Dinstall::Options::Directory"] != "":
612         # Note that we clobber the list of files we were given in this case
613         # so warn if the user has done both
614         if len(changes_files) > 0:
615             utils.warn("Directory provided so ignoring files given on command line")
616
617         changes_files = utils.get_changes_files(cnf["Dinstall::Options::Directory"])
618         Logger.log(["Using changes files from directory", cnf["Dinstall::Options::Directory"], len(changes_files)])
619     elif not len(changes_files) > 0:
620         utils.fubar("No changes files given and no directory specified")
621     else:
622         Logger.log(["Using changes files from command-line", len(changes_files)])
623
624     process_changes(changes_files)
625
626     if summarystats.accept_count:
627         sets = "set"
628         if summarystats.accept_count > 1:
629             sets = "sets"
630         print "Installed %d package %s, %s." % (summarystats.accept_count, sets,
631                                                 utils.size_type(int(summarystats.accept_bytes)))
632         Logger.log(["total", summarystats.accept_count, summarystats.accept_bytes])
633
634     if summarystats.reject_count:
635         sets = "set"
636         if summarystats.reject_count > 1:
637             sets = "sets"
638         print "Rejected %d package %s." % (summarystats.reject_count, sets)
639         Logger.log(["rejected", summarystats.reject_count])
640
641     if not Options["No-Action"]:
642         urgencylog.close()
643
644     Logger.close()
645
646 ###############################################################################
647
648 if __name__ == '__main__':
649     main()