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