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