]> git.decadent.org.uk Git - dak.git/blob - dak/process_upload.py
process-upload: import process_it() from process-unchecked
[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 import errno
129 import fcntl
130 import os
131 import sys
132 #from datetime import datetime
133 import apt_pkg
134
135 from daklib import daklog
136 from daklib.queue import *
137 from daklib import utils
138 from daklib.dbconn import *
139 #from daklib.dak_exceptions import *
140 #from daklib.regexes import re_default_answer, re_issource, re_fdnic
141 from daklib.urgencylog import UrgencyLog
142 from daklib.summarystats import SummaryStats
143 from daklib.holding import Holding
144 from daklib.config import Config
145
146 ###############################################################################
147
148 Options = None
149 Logger = None
150
151 ###############################################################################
152
153 def usage (exit_code=0):
154     print """Usage: dak process-upload [OPTION]... [CHANGES]...
155   -a, --automatic           automatic run
156   -h, --help                show this help and exit.
157   -n, --no-action           don't do anything
158   -p, --no-lock             don't check lockfile !! for cron.daily only !!
159   -s, --no-mail             don't send any mail
160   -V, --version             display the version number and exit"""
161     sys.exit(exit_code)
162
163 ###############################################################################
164
165 def process_it(changes_file):
166     global Logger
167
168     cnf = Config()
169
170     holding = Holding()
171
172     u = Upload()
173     u.pkg.changes_file = changes_file
174     u.pkg.directory = os.getcwd()
175     u.logger = Logger
176     origchanges = os.path.join(u.pkg.directory, u.pkg.changes_file)
177
178     # Some defaults in case we can't fully process the .changes file
179     u.pkg.changes["maintainer2047"] = cnf["Dinstall::MyEmailAddress"]
180     u.pkg.changes["changedby2047"] = cnf["Dinstall::MyEmailAddress"]
181
182     # debian-{devel-,}-changes@lists.debian.org toggles writes access based on this header
183     bcc = "X-DAK: dak process-unchecked"
184     if cnf.has_key("Dinstall::Bcc"):
185         u.Subst["__BCC__"] = bcc + "\nBcc: %s" % (cnf["Dinstall::Bcc"])
186     else:
187         u.Subst["__BCC__"] = bcc
188
189     # Remember where we are so we can come back after cd-ing into the
190     # holding directory.  TODO: Fix this stupid hack
191     u.prevdir = os.getcwd()
192
193     # TODO: Figure out something better for this (or whether it's even
194     #       necessary - it seems to have been for use when we were
195     #       still doing the is_unchecked check; reprocess = 2)
196     u.reprocess = 1
197
198     try:
199         # If this is the Real Thing(tm), copy things into a private
200         # holding directory first to avoid replacable file races.
201         if not Options["No-Action"]:
202             os.chdir(cnf["Dir::Queue::Holding"])
203
204             # Absolutize the filename to avoid the requirement of being in the
205             # same directory as the .changes file.
206             holding.copy_to_holding(origchanges)
207
208             # Relativize the filename so we use the copy in holding
209             # rather than the original...
210             changespath = os.path.basename(u.pkg.changes_file)
211
212         (u.pkg.changes["fingerprint"], rejects) = utils.check_signature(changespath)
213
214         if u.pkg.changes["fingerprint"]:
215             valid_changes_p = u.load_changes(changespath)
216         else:
217             valid_changes_p = False
218             u.rejects.extend(rejects)
219
220         if valid_changes_p:
221             while u.reprocess:
222                 u.check_distributions()
223                 u.check_files(not Options["No-Action"])
224                 valid_dsc_p = u.check_dsc(not Options["No-Action"])
225                 if valid_dsc_p and not Options["No-Action"]:
226                     u.check_source()
227                     u.check_lintian()
228                 u.check_hashes()
229                 u.check_urgency()
230                 u.check_timestamps()
231                 u.check_signed_by_key()
232
233         action(u)
234
235     except (SystemExit, KeyboardInterrupt):
236         raise
237
238     except:
239         print "ERROR"
240         traceback.print_exc(file=sys.stderr)
241
242     # Restore previous WD
243     os.chdir(u.prevdir)
244
245 ###############################################################################
246
247 def main():
248     global Options, Logger
249
250     cnf = Config()
251     summarystats = SummaryStats()
252     log_urgency = False
253
254     DBConn()
255
256     Arguments = [('a',"automatic","Dinstall::Options::Automatic"),
257                  ('h',"help","Dinstall::Options::Help"),
258                  ('n',"no-action","Dinstall::Options::No-Action"),
259                  ('p',"no-lock", "Dinstall::Options::No-Lock"),
260                  ('s',"no-mail", "Dinstall::Options::No-Mail"),
261                  ('d',"directory", "Dinstall::Options::Directory", "HasArg")]
262
263     for i in ["automatic", "help", "no-action", "no-lock", "no-mail",
264               "version", "directory"]:
265         if not cnf.has_key("Dinstall::Options::%s" % (i)):
266             cnf["Dinstall::Options::%s" % (i)] = ""
267
268     changes_files = apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv)
269     Options = cnf.SubTree("Dinstall::Options")
270
271     if Options["Help"]:
272         usage()
273
274     # -n/--dry-run invalidates some other options which would involve things happening
275     if Options["No-Action"]:
276         Options["Automatic"] = ""
277
278     # Check that we aren't going to clash with the daily cron job
279     if not Options["No-Action"] and os.path.exists("%s/daily.lock" % (cnf["Dir::Lock"])) and not Options["No-Lock"]:
280         utils.fubar("Archive maintenance in progress.  Try again later.")
281
282     # Obtain lock if not in no-action mode and initialize the log
283     if not Options["No-Action"]:
284         lock_fd = os.open(cnf["Dinstall::LockFile"], os.O_RDWR | os.O_CREAT)
285         try:
286             fcntl.lockf(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
287         except IOError, e:
288             if errno.errorcode[e.errno] == 'EACCES' or errno.errorcode[e.errno] == 'EAGAIN':
289                 utils.fubar("Couldn't obtain lock; assuming another 'dak process-upload' is already running.")
290             else:
291                 raise
292         if cnf.get("Dir::UrgencyLog"):
293             # Initialise UrgencyLog()
294             log_urgency = True
295             UrgencyLog()
296
297     Logger = daklog.Logger(cnf, "process-upload", Options["No-Action"])
298
299     # If we have a directory flag, use it to find our files
300     if cnf["Dinstall::Options::Directory"] != "":
301         # Note that we clobber the list of files we were given in this case
302         # so warn if the user has done both
303         if len(changes_files) > 0:
304             utils.warn("Directory provided so ignoring files given on command line")
305
306         changes_files = utils.get_changes_files(cnf["Dinstall::Options::Directory"])
307         Logger.log(["Using changes files from directory", cnf["Dinstall::Options::Directory"], len(changes_files)])
308     elif not len(changes_files) > 0:
309         utils.fubar("No changes files given and no directory specified")
310     else:
311         Logger.log(["Using changes files from command-line", len(changes_files)])
312
313     # Sort the .changes files so that we process sourceful ones first
314     changes_files.sort(utils.changes_compare)
315
316     # Process the changes files
317     for changes_file in changes_files:
318         print "\n" + changes_file
319         session = DBConn().session()
320         process_it(changes_file)
321         session.close()
322
323     if summarystats.accept_count:
324         sets = "set"
325         if summarystats.accept_count > 1:
326             sets = "sets"
327         sys.stderr.write("Installed %d package %s, %s.\n" % (summarystats.accept_count, sets,
328                                                              utils.size_type(int(summarystats.accept_bytes))))
329         Logger.log(["total", summarystats.accept_count, summarystats.accept_bytes])
330
331     if not Options["No-Action"]:
332         if log_urgency:
333             UrgencyLog().close()
334     Logger.close()
335
336 ###############################################################################
337
338 if __name__ == '__main__':
339     main()