]> git.decadent.org.uk Git - dak.git/blob - daklib/queue_install.py
Merge branch 'master' into dbtests
[dak.git] / daklib / queue_install.py
1 #!/usr/bin/env python
2 # vim:set et sw=4:
3
4 """
5 Utility functions for process-upload
6
7 @contact: Debian FTP Master <ftpmaster@debian.org>
8 @copyright: 2000, 2001, 2002, 2003, 2004, 2005, 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 import os
29 from shutil import copyfile
30
31 from daklib import utils
32 from daklib.dbconn import *
33 from daklib.config import Config
34
35 ################################################################################
36
37 def package_to_suite(u, suite_name, session):
38     if not u.pkg.changes["distribution"].has_key(suite_name):
39         return False
40
41     ret = True
42
43     if not u.pkg.changes["architecture"].has_key("source"):
44         q = session.query(SrcAssociation.sa_id)
45         q = q.join(Suite).filter_by(suite_name=suite_name)
46         q = q.join(DBSource).filter_by(source=u.pkg.changes['source'])
47         q = q.filter_by(version=u.pkg.changes['version']).limit(1)
48
49         # NB: Careful, this logic isn't what you would think it is
50         # Source is already in the target suite so no need to go to policy
51         # Instead, we don't move to the policy area, we just do an ACCEPT
52         if q.count() > 0:
53             ret = False
54
55     return ret
56
57 def package_to_queue(u, summary, short_summary, queue, chg, session, announce=None):
58     cnf = Config()
59     dir = queue.path
60
61     print "Moving to %s policy queue" % queue.queue_name.upper()
62     u.logger.log(["Moving to %s" % queue.queue_name, u.pkg.changes_file])
63
64     u.move_to_queue(queue)
65     chg.in_queue_id = queue.policy_queue_id
66     session.add(chg)
67     session.commit()
68
69     # Check for override disparities
70     u.check_override()
71
72     # Send accept mail, announce to lists and close bugs
73     if announce:
74         template = os.path.join(cnf["Dir::Templates"], announce)
75         u.update_subst()
76         mail_message = utils.TemplateSubst(u.Subst, template)
77         utils.send_mail(mail_message)
78         u.announce(short_summary, True)
79
80 ################################################################################
81
82 def is_unembargo(u):
83    session = DBConn().session()
84    cnf = Config()
85
86    # If we dont have the disembargo queue we are not on security and so not interested
87    # in doing any security queue handling
88    if not get_policy_queue("disembargo"):
89        return False
90
91    # If we already are in newstage, then it means this just got passed through and accepted
92    # by a security team member. Don't try to accept it for disembargo again
93    dbc = get_dbchange(u.pkg.changes_file, session)
94    if dbc and dbc.in_queue.queue_name in [ 'newstage' ]:
95        return False
96
97    q = session.execute("SELECT package FROM disembargo WHERE package = :source AND version = :version",
98                        {'source': u.pkg.changes["source"],
99                         'version': u.pkg.changes["version"]})
100    if q.rowcount > 0:
101        session.close()
102        return True
103
104    oldcwd = os.getcwd()
105    os.chdir(cnf["Dir::Queue::Disembargo"])
106    disdir = os.getcwd()
107    os.chdir(oldcwd)
108
109    ret = False
110
111    if u.pkg.directory == disdir:
112        if u.pkg.changes["architecture"].has_key("source"):
113            session.execute("INSERT INTO disembargo (package, version) VALUES (:source, :version)",
114                            {'source': u.pkg.changes["source"],
115                             'version': u.pkg.changes["version"]})
116            session.commit()
117
118            ret = True
119
120    session.close()
121
122    return ret
123
124 def do_unembargo(u, summary, short_summary, chg, session=None):
125     polq=get_policy_queue('disembargo')
126     package_to_queue(u, summary, short_summary,
127                      polq, chg, session,
128                      announce=None)
129     for suite_name in u.pkg.changes["distribution"].keys():
130         suite = get_suite(suite_name, session)
131         for q in suite.copy_queues:
132             for f in u.pkg.files.keys():
133                 copyfile(os.path.join(polq.path, f), os.path.join(q.path, f))
134 #
135 #################################################################################
136 #
137 def is_embargo(u):
138    # if we are the security archive, we always have a embargo queue and its the
139    # last in line, so if that exists, return true
140    # Of course do not return true when we accept from out of newstage, as that means
141    # it just left embargo and we want it in the archive
142    if get_policy_queue('embargo'):
143        session = DBConn().session()
144        dbc = get_dbchange(u.pkg.changes_file, session)
145        if dbc and dbc.in_queue.queue_name in [ 'newstage' ]:
146            return False
147
148        return True
149
150 def do_embargo(u, summary, short_summary, chg, session=None):
151     polq=get_policy_queue('embargo')
152     package_to_queue(u, summary, short_summary,
153                      polq, chg, session,
154                      announce=None)
155     for suite_name in u.pkg.changes["distribution"].keys():
156         suite = get_suite(suite_name, session)
157         for q in suite.copy_queues:
158             for f in u.pkg.files.keys():
159                 copyfile(os.path.join(polq.path, f), os.path.join(q.path, f))
160
161 ################################################################################
162
163 def is_autobyhand(u):
164     cnf = Config()
165
166     all_auto = 1
167     any_auto = 0
168     for f in u.pkg.files.keys():
169         if u.pkg.files[f].has_key("byhand"):
170             any_auto = 1
171
172             # filename is of form "PKG_VER_ARCH.EXT" where PKG, VER and ARCH
173             # don't contain underscores, and ARCH doesn't contain dots.
174             # further VER matches the .changes Version:, and ARCH should be in
175             # the .changes Architecture: list.
176             if f.count("_") < 2:
177                 all_auto = 0
178                 continue
179
180             (pckg, ver, archext) = f.split("_", 2)
181             if archext.count(".") < 1 or u.pkg.changes["version"] != ver:
182                 all_auto = 0
183                 continue
184
185             ABH = cnf.SubTree("AutomaticByHandPackages")
186             if not ABH.has_key(pckg) or \
187               ABH["%s::Source" % (pckg)] != u.pkg.changes["source"]:
188                 print "not match %s %s" % (pckg, u.pkg.changes["source"])
189                 all_auto = 0
190                 continue
191
192             (arch, ext) = archext.split(".", 1)
193             if arch not in u.pkg.changes["architecture"]:
194                 all_auto = 0
195                 continue
196
197             u.pkg.files[f]["byhand-arch"] = arch
198             u.pkg.files[f]["byhand-script"] = ABH["%s::Script" % (pckg)]
199
200     return any_auto and all_auto
201
202 def do_autobyhand(u, summary, short_summary, chg, session):
203     print "Attempting AUTOBYHAND."
204     byhandleft = False
205     for f, entry in u.pkg.files.items():
206         byhandfile = f
207
208         if not entry.has_key("byhand"):
209             continue
210
211         if not entry.has_key("byhand-script"):
212             byhandleft = True
213             continue
214
215         os.system("ls -l %s" % byhandfile)
216
217         result = os.system("%s %s %s %s %s" % (
218                 entry["byhand-script"],
219                 byhandfile,
220                 u.pkg.changes["version"],
221                 entry["byhand-arch"],
222                 os.path.abspath(u.pkg.changes_file)))
223
224         if result == 0:
225             os.unlink(byhandfile)
226             del u.pkg.files[f]
227         else:
228             print "Error processing %s, left as byhand." % (f)
229             byhandleft = True
230
231     if byhandleft:
232         do_byhand(u, summary, short_summary, chg, session)
233     else:
234         u.accept(summary, short_summary, session)
235         u.check_override()
236
237 ################################################################################
238
239 def is_byhand(u):
240     for f in u.pkg.files.keys():
241         if u.pkg.files[f].has_key("byhand"):
242             return True
243     return False
244
245 def do_byhand(u, summary, short_summary, chg, session):
246     return package_to_queue(u, summary, short_summary,
247                             get_policy_queue('byhand'), chg, session,
248                             announce=None)
249
250 ################################################################################
251
252 def is_new(u):
253     for f in u.pkg.files.keys():
254         if u.pkg.files[f].has_key("new"):
255             return True
256     return False
257
258 def acknowledge_new(u, summary, short_summary, chg, session):
259     cnf = Config()
260
261     print "Moving to NEW queue."
262     u.logger.log(["Moving to new", u.pkg.changes_file])
263
264     q = get_policy_queue('new', session)
265
266     u.move_to_queue(q)
267     chg.in_queue_id = q.policy_queue_id
268     session.add(chg)
269     session.commit()
270
271     print "Sending new ack."
272     template = os.path.join(cnf["Dir::Templates"], 'process-unchecked.new')
273     u.update_subst()
274     u.Subst["__SUMMARY__"] = summary
275     new_ack_message = utils.TemplateSubst(u.Subst, template)
276     utils.send_mail(new_ack_message)
277
278 ################################################################################
279
280 # FIXME: queues should be able to get autobuild
281 #        the current logic doesnt allow this, as buildd stuff is AFTER accept...
282 #        embargo/disembargo use a workaround due to this
283 # q-unapproved hax0ring
284 QueueInfo = {
285     "new": { "is": is_new, "process": acknowledge_new },
286     "autobyhand" : { "is" : is_autobyhand, "process": do_autobyhand },
287     "byhand" : { "is": is_byhand, "process": do_byhand },
288     "embargoed" : { "is": is_embargo, "process": do_embargo },
289     "unembargoed" : { "is": is_unembargo, "process": do_unembargo },
290 }
291
292 def determine_target(u):
293     cnf = Config()
294
295     # Statically handled queues
296     target = None
297
298     for q in ["autobyhand", "byhand", "new", "unembargoed", "embargoed"]:
299         if QueueInfo[q]["is"](u):
300             target = q
301             break
302
303     return target
304
305 ###############################################################################
306