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