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