]> git.decadent.org.uk Git - dak.git/blob - daklib/queue_install.py
c517f53cf1fdc14e7d0822cfc9f466df490e88b8
[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
30 from daklib import utils
31 from daklib.dbconn import *
32 from daklib.config import Config
33
34 ###############################################################################
35
36 # q-unapproved hax0ring
37 QueueInfo = {
38     "New": { "is": is_new, "process": acknowledge_new },
39     "Autobyhand" : { "is" : is_autobyhand, "process": do_autobyhand },
40     "Byhand" : { "is": is_byhand, "process": do_byhand },
41     "OldStableUpdate" : { "is": is_oldstableupdate,
42                           "process": do_oldstableupdate },
43     "StableUpdate" : { "is": is_stableupdate, "process": do_stableupdate },
44     "Unembargo" : { "is": is_unembargo, "process": queue_unembargo },
45     "Embargo" : { "is": is_embargo, "process": queue_embargo },
46 }
47
48 def determine_target(u):
49     cnf = Config()
50     
51     queues = [ "New", "Autobyhand", "Byhand" ]
52     if cnf.FindB("Dinstall::SecurityQueueHandling"):
53         queues += [ "Unembargo", "Embargo" ]
54     else:
55         queues += [ "OldStableUpdate", "StableUpdate" ]
56
57     target = None
58     for q in queues:
59         if QueueInfo[q]["is"](u):
60             target = q
61             break
62
63     return target
64
65 ################################################################################
66
67 def package_to_suite(u, suite):
68     if not u.pkg.changes["distribution"].has_key(suite):
69         return False
70
71     ret = True
72
73     if not u.pkg.changes["architecture"].has_key("source"):
74         s = DBConn().session()
75         q = s.query(SrcAssociation.sa_id)
76         q = q.join(Suite).filter_by(suite_name=suite)
77         q = q.join(DBSource).filter_by(source=u.pkg.changes['source'])
78         q = q.filter_by(version=u.pkg.changes['version']).limit(1)
79
80         # NB: Careful, this logic isn't what you would think it is
81         # Source is already in {old-,}proposed-updates so no need to hold
82         # Instead, we don't move to the holding area, we just do an ACCEPT
83         if q.count() > 0:
84             ret = False
85
86         s.close()
87
88     return ret
89
90 def package_to_queue(u, summary, short_summary, queue, perms=0660, build=True, announce=None):
91     cnf = Config()
92     dir = cnf["Dir::Queue::%s" % queue]
93
94     print "Moving to %s holding area" % queue.upper()
95     u.logger.log(["Moving to %s" % queue, u.pkg.changes_file])
96
97     u.move_to_dir(dir, perms=perms)
98     if build:
99         get_or_set_queue(queue.lower()).autobuild_upload(u.pkg, dir)
100
101     # Check for override disparities
102     u.check_override()
103
104     # Send accept mail, announce to lists and close bugs
105     if announce and not cnf["Dinstall::Options::No-Mail"]:
106         template = os.path.join(cnf["Dir::Templates"], announce)
107         u.update_subst()
108         u.Subst["__SUITE__"] = ""
109         mail_message = utils.TemplateSubst(u.Subst, template)
110         utils.send_mail(mail_message)
111         u.announce(short_summary, True)
112
113 ################################################################################
114
115 def is_unembargo(u):
116     session = DBConn().session()
117     cnf = Config()
118
119     q = session.execute("SELECT package FROM disembargo WHERE package = :source AND version = :version", u.pkg.changes)
120     if q.rowcount > 0:
121         session.close()
122         return True
123
124     oldcwd = os.getcwd()
125     os.chdir(cnf["Dir::Queue::Disembargo"])
126     disdir = os.getcwd()
127     os.chdir(oldcwd)
128
129     ret = False
130
131     if u.pkg.directory == disdir:
132         if u.pkg.changes["architecture"].has_key("source"):
133             session.execute("INSERT INTO disembargo (package, version) VALUES (:package, :version)", u.pkg.changes)
134             session.commit()
135
136             ret = True
137
138     session.close()
139
140     return ret
141
142 def queue_unembargo(u, summary, short_summary):
143     return package_to_queue(u, summary, short_summary, "Unembargoed",
144                             perms=0660, build=True, announce='process-unchecked.accepted')
145
146 ################################################################################
147
148 def is_embargo(u):
149     # if embargoed queues are enabled always embargo
150     return True
151
152 def queue_embargo(u, summary, short_summary):
153     return package_to_queue(u, summary, short_summary, "Unembargoed",
154                             perms=0660, build=True, announce='process-unchecked.accepted')
155
156 ################################################################################
157
158 def is_stableupdate(u):
159     return package_to_suite(u, 'proposed-updates')
160
161 def do_stableupdate(u, summary, short_summary):
162     return package_to_queue(u, summary, short_summary, "ProposedUpdates",
163                             perms=0664, build=False, announce=None)
164
165 ################################################################################
166
167 def is_oldstableupdate(u):
168     return package_to_suite(u, 'oldstable-proposed-updates')
169
170 def do_oldstableupdate(u, summary, short_summary):
171     return package_to_queue(u, summary, short_summary, "OldProposedUpdates",
172                             perms=0664, build=False, announce=None)
173
174 ################################################################################
175
176 def is_autobyhand(u):
177     cnf = Config()
178
179     all_auto = 1
180     any_auto = 0
181     for f in u.pkg.files.keys():
182         if u.pkg.files[f].has_key("byhand"):
183             any_auto = 1
184
185             # filename is of form "PKG_VER_ARCH.EXT" where PKG, VER and ARCH
186             # don't contain underscores, and ARCH doesn't contain dots.
187             # further VER matches the .changes Version:, and ARCH should be in
188             # the .changes Architecture: list.
189             if f.count("_") < 2:
190                 all_auto = 0
191                 continue
192
193             (pckg, ver, archext) = f.split("_", 2)
194             if archext.count(".") < 1 or u.pkg.changes["version"] != ver:
195                 all_auto = 0
196                 continue
197
198             ABH = cnf.SubTree("AutomaticByHandPackages")
199             if not ABH.has_key(pckg) or \
200               ABH["%s::Source" % (pckg)] != u.pkg.changes["source"]:
201                 print "not match %s %s" % (pckg, u.pkg.changes["source"])
202                 all_auto = 0
203                 continue
204
205             (arch, ext) = archext.split(".", 1)
206             if arch not in u.pkg.changes["architecture"]:
207                 all_auto = 0
208                 continue
209
210             u.pkg.files[f]["byhand-arch"] = arch
211             u.pkg.files[f]["byhand-script"] = ABH["%s::Script" % (pckg)]
212
213     return any_auto and all_auto
214
215 def do_autobyhand(u, summary, short_summary):
216     print "Attempting AUTOBYHAND."
217     byhandleft = True
218     for f, entry in u.pkg.files.items():
219         byhandfile = f
220
221         if not entry.has_key("byhand"):
222             continue
223
224         if not entry.has_key("byhand-script"):
225             byhandleft = True
226             continue
227
228         os.system("ls -l %s" % byhandfile)
229
230         result = os.system("%s %s %s %s %s" % (
231                 entry["byhand-script"],
232                 byhandfile,
233                 u.pkg.changes["version"],
234                 entry["byhand-arch"],
235                 os.path.abspath(u.pkg.changes_file)))
236
237         if result == 0:
238             os.unlink(byhandfile)
239             del entry
240         else:
241             print "Error processing %s, left as byhand." % (f)
242             byhandleft = True
243
244     if byhandleft:
245         do_byhand(u, summary, short_summary)
246     else:
247         u.accept(summary, short_summary)
248         u.check_override()
249         # XXX: We seem to be missing a u.remove() here
250         #      This might explain why we get byhand leftovers in unchecked - mhy
251
252 ################################################################################
253
254 def is_byhand(u):
255     for f in u.pkg.files.keys():
256         if u.pkg.files[f].has_key("byhand"):
257             return True
258     return False
259
260 def do_byhand(u, summary, short_summary):
261     return package_to_queue(u, summary, short_summary, "Byhand",
262                             perms=0660, build=False, announce=None)
263
264 ################################################################################
265
266 def is_new(u):
267     for f in u.pkg.files.keys():
268         if u.pkg.files[f].has_key("new"):
269             return True
270     return False
271
272 def acknowledge_new(u, summary, short_summary):
273     cnf = Config()
274
275     print "Moving to NEW holding area."
276     u.logger.log(["Moving to new", u.pkg.changes_file])
277
278     u.move_to_dir(cnf["Dir::Queue::New"], perms=0640, changesperms=0644)
279
280     if not Options["No-Mail"]:
281         print "Sending new ack."
282         template = os.path.join(cnf["Dir::Templates"], 'process-unchecked.new')
283         u.update_subst()
284         u.Subst["__SUMMARY__"] = summary
285         new_ack_message = utils.TemplateSubst(u.Subst, template)
286         utils.send_mail(new_ack_message)