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