]> git.decadent.org.uk Git - dak.git/blob - dak/override.py
Enmasse adaptation for removal of silly names.
[dak.git] / dak / override.py
1 #!/usr/bin/env python
2
3 # Microscopic modification and query tool for overrides in projectb
4 # Copyright (C) 2004, 2006  Daniel Silverstone <dsilvers@digital-scurf.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 ## So line up your soldiers and she'll shoot them all down
23 ## Coz Alisha Rules The World
24 ## You think you found a dream, then it shatters and it seems,
25 ## That Alisha Rules The World
26 ################################################################################
27
28 import pg, sys
29 import dak.lib.utils, dak.lib.database
30 import apt_pkg, dak.lib.logging
31
32 ################################################################################
33
34 Cnf = None
35 projectB = None
36
37 ################################################################################
38
39 # Shamelessly stolen from 'dak rm'. Should probably end up in dak.lib.utils.py
40 def game_over():
41     answer = dak.lib.utils.our_raw_input("Continue (y/N)? ").lower()
42     if answer != "y":
43         print "Aborted."
44         sys.exit(1)
45
46
47 def usage (exit_code=0):
48     print """Usage: dak override [OPTIONS] package [section] [priority]
49 Make microchanges or microqueries of the overrides
50
51   -h, --help                 show this help and exit
52   -d, --done=BUG#            send priority/section change as closure to bug#
53   -n, --no-action            don't do anything
54   -s, --suite                specify the suite to use
55 """
56     sys.exit(exit_code)
57
58 def main ():
59     global Cnf, projectB
60
61     Cnf = dak.lib.utils.get_conf()
62
63     Arguments = [('h',"help","Override::Options::Help"),
64                  ('d',"done","Override::Options::Done", "HasArg"),
65                  ('n',"no-action","Override::Options::No-Action"),
66                  ('s',"suite","Override::Options::Suite", "HasArg"),
67                  ]
68     for i in ["help", "no-action"]:
69         if not Cnf.has_key("Override::Options::%s" % (i)):
70             Cnf["Override::Options::%s" % (i)] = ""
71     if not Cnf.has_key("Override::Options::Suite"):
72         Cnf["Override::Options::Suite"] = "unstable"
73
74     arguments = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
75     Options = Cnf.SubTree("Override::Options")
76
77     if Options["Help"]:
78         usage()
79
80     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
81     dak.lib.database.init(Cnf, projectB)
82
83     if not arguments:
84         dak.lib.utils.fubar("package name is a required argument.")
85
86     package = arguments.pop(0)
87     suite = Options["Suite"]
88     if arguments and len(arguments) > 2:
89         dak.lib.utils.fubar("Too many arguments")
90
91     if arguments and len(arguments) == 1:
92         # Determine if the argument is a priority or a section...
93         arg = arguments.pop()
94         q = projectB.query("""
95         SELECT ( SELECT COUNT(*) FROM section WHERE section=%s ) AS secs,
96                ( SELECT COUNT(*) FROM priority WHERE priority=%s ) AS prios
97                """ % ( pg._quote(arg,"str"), pg._quote(arg,"str")))
98         r = q.getresult()
99         if r[0][0] == 1:
100             arguments = (arg,".")
101         elif r[0][1] == 1:
102             arguments = (".",arg)
103         else:
104             dak.lib.utils.fubar("%s is not a valid section or priority" % (arg))
105
106
107     # Retrieve current section/priority...
108     q = projectB.query("""
109     SELECT priority.priority AS prio, section.section AS sect
110       FROM override, priority, section, suite
111      WHERE override.priority = priority.id
112        AND override.section = section.id
113        AND override.package = %s
114        AND override.suite = suite.id
115        AND suite.suite_name = %s
116     """ % (pg._quote(package,"str"), pg._quote(suite,"str")))
117
118     if q.ntuples() == 0:
119         dak.lib.utils.fubar("Unable to find package %s" % (package))
120     if q.ntuples() > 1:
121         dak.lib.utils.fubar("%s is ambiguous. Matches %d packages" % (package,q.ntuples()))
122
123     r = q.getresult()
124     oldsection = r[0][1]
125     oldpriority = r[0][0]
126
127     if not arguments:
128         print "%s is in section '%s' at priority '%s'" % (
129             package,oldsection,oldpriority)
130         sys.exit(0)
131
132     # At this point, we have a new section and priority... check they're valid...
133     newsection, newpriority = arguments
134
135     if newsection == ".":
136         newsection = oldsection
137     if newpriority == ".":
138         newpriority = oldpriority
139
140     q = projectB.query("SELECT id FROM section WHERE section=%s" % (
141         pg._quote(newsection,"str")))
142
143     if q.ntuples() == 0:
144         dak.lib.utils.fubar("Supplied section %s is invalid" % (newsection))
145     newsecid = q.getresult()[0][0]
146
147     q = projectB.query("SELECT id FROM priority WHERE priority=%s" % (
148         pg._quote(newpriority,"str")))
149
150     if q.ntuples() == 0:
151         dak.lib.utils.fubar("Supplied priority %s is invalid" % (newpriority))
152     newprioid = q.getresult()[0][0]
153
154     if newpriority == oldpriority and newsection == oldsection:
155         print "I: Doing nothing"
156         sys.exit(0)
157
158     # If we're in no-action mode
159     if Options["No-Action"]:
160         if newpriority != oldpriority:
161             print "I: Would change priority from %s to %s" % (oldpriority,newpriority)
162         if newsection != oldsection:
163             print "I: Would change section from %s to %s" % (oldsection,newsection)
164         if Options.has_key("Done"):
165             print "I: Would also close bug(s): %s" % (Options["Done"])
166
167         sys.exit(0)
168
169     if newpriority != oldpriority:
170         print "I: Will change priority from %s to %s" % (oldpriority,newpriority)
171     if newsection != oldsection:
172         print "I: Will change section from %s to %s" % (oldsection,newsection)
173
174     if not Options.has_key("Done"):
175         pass
176         #dak.lib.utils.warn("No bugs to close have been specified. Noone will know you have done this.")
177     else:
178         print "I: Will close bug(s): %s" % (Options["Done"])
179
180     game_over()
181
182     Logger = dak.lib.logging.Logger(Cnf, "override")
183
184     projectB.query("BEGIN WORK")
185     # We're in "do it" mode, we have something to do... do it
186     if newpriority != oldpriority:
187         q = projectB.query("""
188         UPDATE override
189            SET priority=%d
190          WHERE package=%s
191            AND suite = (SELECT id FROM suite WHERE suite_name=%s)""" % (
192             newprioid,
193             pg._quote(package,"str"),
194             pg._quote(suite,"str") ))
195         Logger.log(["changed priority",package,oldpriority,newpriority])
196
197     if newsection != oldsection:
198         q = projectB.query("""
199         UPDATE override
200            SET section=%d
201          WHERE package=%s
202            AND suite = (SELECT id FROM suite WHERE suite_name=%s)""" % (
203             newsecid,
204             pg._quote(package,"str"),
205             pg._quote(suite,"str") ))
206         Logger.log(["changed priority",package,oldsection,newsection])
207     projectB.query("COMMIT WORK")
208
209     if Options.has_key("Done"):
210         Subst = {}
211         Subst["__OVERRIDE_ADDRESS__"] = Cnf["Override::MyEmailAddress"]
212         Subst["__BUG_SERVER__"] = Cnf["Dinstall::BugServer"]
213         bcc = []
214         if Cnf.Find("Dinstall::Bcc") != "":
215             bcc.append(Cnf["Dinstall::Bcc"])
216         if Cnf.Find("Override::Bcc") != "":
217             bcc.append(Cnf["Override::Bcc"])
218         if bcc:
219             Subst["__BCC__"] = "Bcc: " + ", ".join(bcc)
220         else:
221             Subst["__BCC__"] = "X-Filler: 42"
222         Subst["__CC__"] = "X-DAK: dak override"
223         Subst["__ADMIN_ADDRESS__"] = Cnf["Dinstall::MyAdminAddress"]
224         Subst["__DISTRO__"] = Cnf["Dinstall::MyDistribution"]
225         Subst["__WHOAMI__"] = dak.lib.utils.whoami()
226
227         summary = "Concerning package %s...\n" % (package)
228         summary += "Operating on the %s suite\n" % (suite)
229         if newpriority != oldpriority:
230             summary += "Changed priority from %s to %s\n" % (oldpriority,newpriority)
231         if newsection != oldsection:
232             summary += "Changed section from %s to %s\n" % (oldsection,newsection)
233         Subst["__SUMMARY__"] = summary
234
235         for bug in dak.lib.utils.split_args(Options["Done"]):
236             Subst["__BUG_NUMBER__"] = bug
237             mail_message = dak.lib.utils.TemplateSubst(
238                 Subst,Cnf["Dir::Templates"]+"/override.bug-close")
239             dak.lib.utils.send_mail(mail_message)
240             Logger.log(["closed bug",bug])
241
242     Logger.close()
243
244     print "Done"
245
246 #################################################################################
247
248 if __name__ == '__main__':
249     main()