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