]> git.decadent.org.uk Git - dak.git/blob - dak/reject_proposed_updates.py
convert cruft_report to new DB API
[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, sys
23 import apt_pkg
24
25 from daklib.dbconn import *
26 from daklib import daklog
27 from daklib.queue import Upload
28 from daklib import utils
29 from daklib.regexes import re_default_answer
30
31 ################################################################################
32
33 # Globals
34 Cnf = None
35 Options = 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
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     # Initialise database
71     dbconn = DBConn()
72
73     Logger = daklog.Logger(Cnf, "reject-proposed-updates")
74
75     bcc = "X-DAK: dak rejected-proposed-updates\nX-Katie: lauren $Revision: 1.4 $"
76
77     for arg in arguments:
78         arg = utils.validate_changes_file_arg(arg)
79         cwd = os.getcwd()
80         os.chdir(Cnf["Suite::Proposed-Updates::CopyDotDak"])
81
82         u = Upload()
83         u = load_dot_dak(arg)
84
85         if Cnf.has_key("Dinstall::Bcc"):
86             u.Subst["__BCC__"] = bcc + "\nBcc: %s" % (Cnf["Dinstall::Bcc"])
87         else:
88             u.Subst["__BCC__"] = bcc
89         u.update_subst()
90
91         os.chdir(cwd)
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(u, 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 (u, reject_message = ""):
120     files = u.pkg.files
121     dsc = u.pkg.dsc
122     changes_file = u.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     u.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     u.Subst["__REJECTOR_ADDRESS__"] = user_email_address
170     u.Subst["__MANUAL_REJECT_MESSAGE__"] = reject_message
171     u.Subst["__STABLE_REJECTOR__"] = Cnf["Reject-Proposed-Updates::StableRejector"]
172     u.Subst["__STABLE_MAIL__"] = Cnf["Reject-Proposed-Updates::StableMail"]
173     u.Subst["__MORE_INFO_URL__"] = Cnf["Reject-Proposed-Updates::MoreInfoURL"]
174     u.Subst["__CC__"] = "Cc: " + Cnf["Dinstall::MyEmailAddress"]
175     reject_mail_message = utils.TemplateSubst(u.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     session = DBConn().session()
182
183     # Remove the packages from proposed-updates
184     suite_name = 'proposed-updates'
185
186     # Remove files from proposed-updates suite
187     for f in files.keys():
188         if files[f]["type"] == "dsc":
189             package = dsc["source"]
190             version = dsc["version"];  # NB: not files[f]["version"], that has no epoch
191
192             source = get_sources_from_name(package, version, session=session)
193             if len(source) != 1:
194                 utils.fubar("reject: Couldn't find %s_%s in source table." % (package, version))
195
196             source = source[0]
197
198             for sa in source.srcassociations:
199                 if sa.suite.suite_name == suite_name:
200                     session.delete(sa)
201
202         elif files[f]["type"] == "deb":
203             package = files[f]["package"]
204             version = files[f]["version"]
205             architecture = files[f]["architecture"]
206
207             binary = get_binaries_from_name(package, version [architecture, 'all'])
208
209             # Horrible hack to work around partial replacement of
210             # packages with newer versions (from different source
211             # packages).  This, obviously, should instead check for a
212             # newer version of the package and only do the
213             # warn&continue thing if it finds one.
214             if len(binary) != 1:
215                 utils.warn("reject: Couldn't find %s_%s_%s in binaries table." % (package, version, architecture))
216             else:
217                 for ba in binary[0].binassociations:
218                     if ba.suite.suite_name == suite_name:
219                         session.delete(ba)
220
221     session.commit()
222
223     # Send the rejection mail if appropriate
224     if not Options["No-Mail"]:
225         utils.send_mail(reject_mail_message)
226
227     # Finally remove the .dak file
228     dot_dak_file = os.path.join(Cnf["Suite::Proposed-Updates::CopyDotDak"], os.path.basename(changes_file[:-8]+".dak"))
229     os.unlink(dot_dak_file)
230
231     Logger.log(["rejected", changes_file])
232     return 0
233
234 ################################################################################
235
236 if __name__ == '__main__':
237     main()