]> git.decadent.org.uk Git - dak.git/blob - dak/reject_proposed_updates.py
stop setting the db connection to latin1
[dak.git] / dak / reject_proposed_updates.py
1 #!/usr/bin/env python
2
3 """ Manually reject packages for proprosed-updates """
4 # Copyright (C) 2001, 2002, 2003, 2004, 2006  James Troup <james@nocrew.org>
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 ################################################################################
21
22 import os, pg, sys
23 import apt_pkg
24 from daklib import database
25 from daklib import logging
26 from daklib import queue
27 from daklib import utils
28 from daklib.regexes import re_default_answer
29
30 ################################################################################
31
32 # Globals
33 Cnf = None
34 Options = None
35 projectB = None
36 Upload = None
37 Logger = None
38
39 ################################################################################
40
41 def usage(exit_code=0):
42     print """Usage: dak reject-proposed-updates .CHANGES[...]
43 Manually reject the .CHANGES file(s).
44
45   -h, --help                show this help and exit.
46   -m, --message=MSG         use this message for the rejection.
47   -s, --no-mail             don't send any mail."""
48     sys.exit(exit_code)
49
50 ################################################################################
51
52 def main():
53     global Cnf, Logger, Options, projectB, Upload
54
55     Cnf = utils.get_conf()
56     Arguments = [('h',"help","Reject-Proposed-Updates::Options::Help"),
57                  ('m',"manual-reject","Reject-Proposed-Updates::Options::Manual-Reject", "HasArg"),
58                  ('s',"no-mail", "Reject-Proposed-Updates::Options::No-Mail")]
59     for i in [ "help", "manual-reject", "no-mail" ]:
60         if not Cnf.has_key("Reject-Proposed-Updates::Options::%s" % (i)):
61             Cnf["Reject-Proposed-Updates::Options::%s" % (i)] = ""
62
63     arguments = apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv)
64
65     Options = Cnf.SubTree("Reject-Proposed-Updates::Options")
66     if Options["Help"]:
67         usage()
68     if not arguments:
69         utils.fubar("need at least one .changes filename as an argument.")
70
71     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
72     database.init(Cnf, projectB)
73
74     Upload = queue.Upload(Cnf)
75     Logger = Upload.Logger = logging.Logger(Cnf, "reject-proposed-updates")
76
77     bcc = "X-DAK: dak rejected-proposed-updates\nX-Katie: lauren $Revision: 1.4 $"
78     if Cnf.has_key("Dinstall::Bcc"):
79         Upload.Subst["__BCC__"] = bcc + "\nBcc: %s" % (Cnf["Dinstall::Bcc"])
80     else:
81         Upload.Subst["__BCC__"] = bcc
82
83     for arg in arguments:
84         arg = utils.validate_changes_file_arg(arg)
85         Upload.pkg.changes_file = arg
86         Upload.init_vars()
87         cwd = os.getcwd()
88         os.chdir(Cnf["Suite::Proposed-Updates::CopyDotDak"])
89         Upload.update_vars()
90         os.chdir(cwd)
91         Upload.update_subst()
92
93         print arg
94         done = 0
95         prompt = "Manual reject, [S]kip, Quit ?"
96         while not done:
97             answer = "XXX"
98
99             while prompt.find(answer) == -1:
100                 answer = utils.our_raw_input(prompt)
101                 m = re_default_answer.search(prompt)
102                 if answer == "":
103                     answer = m.group(1)
104                 answer = answer[:1].upper()
105
106             if answer == 'M':
107                 aborted = reject(Options["Manual-Reject"])
108                 if not aborted:
109                     done = 1
110             elif answer == 'S':
111                 done = 1
112             elif answer == 'Q':
113                 sys.exit(0)
114
115     Logger.close()
116
117 ################################################################################
118
119 def reject (reject_message = ""):
120     files = Upload.pkg.files
121     dsc = Upload.pkg.dsc
122     changes_file = Upload.pkg.changes_file
123
124     # If we weren't given a manual rejection message, spawn an editor
125     # so the user can add one in...
126     if not reject_message:
127         (fd, temp_filename) = utils.temp_filename()
128         editor = os.environ.get("EDITOR","vi")
129         answer = 'E'
130         while answer == 'E':
131             os.system("%s %s" % (editor, temp_filename))
132             f = os.fdopen(fd)
133             reject_message = "".join(f.readlines())
134             f.close()
135             print "Reject message:"
136             print utils.prefix_multi_line_string(reject_message,"  ", include_blank_lines=1)
137             prompt = "[R]eject, Edit, Abandon, Quit ?"
138             answer = "XXX"
139             while prompt.find(answer) == -1:
140                 answer = utils.our_raw_input(prompt)
141                 m = re_default_answer.search(prompt)
142                 if answer == "":
143                     answer = m.group(1)
144                 answer = answer[:1].upper()
145         os.unlink(temp_filename)
146         if answer == 'A':
147             return 1
148         elif answer == 'Q':
149             sys.exit(0)
150
151     print "Rejecting.\n"
152
153     # Reject the .changes file
154     Upload.force_reject([changes_file])
155
156     # Setup the .reason file
157     reason_filename = changes_file[:-8] + ".reason"
158     reject_filename = Cnf["Dir::Queue::Reject"] + '/' + reason_filename
159
160     # If we fail here someone is probably trying to exploit the race
161     # so let's just raise an exception ...
162     if os.path.exists(reject_filename):
163         os.unlink(reject_filename)
164     reject_fd = os.open(reject_filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0644)
165
166     # Build up the rejection email
167     user_email_address = utils.whoami() + " <%s>" % (Cnf["Dinstall::MyAdminAddress"])
168
169     Upload.Subst["__REJECTOR_ADDRESS__"] = user_email_address
170     Upload.Subst["__MANUAL_REJECT_MESSAGE__"] = reject_message
171     Upload.Subst["__STABLE_REJECTOR__"] = Cnf["Reject-Proposed-Updates::StableRejector"]
172     Upload.Subst["__STABLE_MAIL__"] = Cnf["Reject-Proposed-Updates::StableMail"]
173     Upload.Subst["__MORE_INFO_URL__"] = Cnf["Reject-Proposed-Updates::MoreInfoURL"]
174     Upload.Subst["__CC__"] = "Cc: " + Cnf["Dinstall::MyEmailAddress"]
175     reject_mail_message = utils.TemplateSubst(Upload.Subst,Cnf["Dir::Templates"]+"/reject-proposed-updates.rejected")
176
177     # Write the rejection email out as the <foo>.reason file
178     os.write(reject_fd, reject_mail_message)
179     os.close(reject_fd)
180
181     # Remove the packages from proposed-updates
182     suite_id = database.get_suite_id('proposed-updates')
183
184     projectB.query("BEGIN WORK")
185     # Remove files from proposed-updates suite
186     for f in files.keys():
187         if files[f]["type"] == "dsc":
188             package = dsc["source"]
189             version = dsc["version"];  # NB: not files[f]["version"], that has no epoch
190             q = projectB.query("SELECT id FROM source WHERE source = '%s' AND version = '%s'" % (package, version))
191             ql = q.getresult()
192             if not ql:
193                 utils.fubar("reject: Couldn't find %s_%s in source table." % (package, version))
194             source_id = ql[0][0]
195             projectB.query("DELETE FROM src_associations WHERE suite = '%s' AND source = '%s'" % (suite_id, source_id))
196         elif files[f]["type"] == "deb":
197             package = files[f]["package"]
198             version = files[f]["version"]
199             architecture = files[f]["architecture"]
200             q = projectB.query("SELECT b.id FROM binaries b, architecture a WHERE b.package = '%s' AND b.version = '%s' AND (a.arch_string = '%s' OR a.arch_string = 'all') AND b.architecture = a.id" % (package, version, architecture))
201             ql = q.getresult()
202
203             # Horrible hack to work around partial replacement of
204             # packages with newer versions (from different source
205             # packages).  This, obviously, should instead check for a
206             # newer version of the package and only do the
207             # warn&continue thing if it finds one.
208             if not ql:
209                 utils.warn("reject: Couldn't find %s_%s_%s in binaries table." % (package, version, architecture))
210             else:
211                 binary_id = ql[0][0]
212                 projectB.query("DELETE FROM bin_associations WHERE suite = '%s' AND bin = '%s'" % (suite_id, binary_id))
213     projectB.query("COMMIT WORK")
214
215     # Send the rejection mail if appropriate
216     if not Options["No-Mail"]:
217         utils.send_mail(reject_mail_message)
218
219     # Finally remove the .dak file
220     dot_dak_file = os.path.join(Cnf["Suite::Proposed-Updates::CopyDotDak"], os.path.basename(changes_file[:-8]+".dak"))
221     os.unlink(dot_dak_file)
222
223     Logger.log(["rejected", changes_file])
224     return 0
225
226 ################################################################################
227
228 if __name__ == '__main__':
229     main()