]> git.decadent.org.uk Git - dak.git/blob - alicia
Add new top level directories
[dak.git] / alicia
1 #!/usr/bin/env python
2
3 # Microscopic modification and query tool for overrides in projectb
4 # Copyright (C) 2004  Daniel Silverstone <dsilvers@digital-scurf.org>
5 # $Id: alicia,v 1.6 2004-11-27 17:58:13 troup Exp $
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21
22 ################################################################################
23 ## So line up your soldiers and she'll shoot them all down
24 ## Coz Alisha Rules The World
25 ## You think you found a dream, then it shatters and it seems,
26 ## That Alisha Rules The World
27 ################################################################################
28
29 import pg, sys;
30 import utils, db_access;
31 import apt_pkg, logging;
32
33 ################################################################################
34
35 Cnf = None;
36 projectB = None;
37
38 ################################################################################
39
40 # Shamelessly stolen from melanie. Should probably end up in utils.py
41 def game_over():
42     answer = 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: alicia [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 = utils.get_conf()
63
64     Arguments = [('h',"help","Alicia::Options::Help"),
65                  ('d',"done","Alicia::Options::Done", "HasArg"),
66                  ('n',"no-action","Alicia::Options::No-Action"),
67                  ('s',"suite","Alicia::Options::Suite", "HasArg"),
68                  ];
69     for i in ["help", "no-action"]:
70         if not Cnf.has_key("Alicia::Options::%s" % (i)):
71             Cnf["Alicia::Options::%s" % (i)] = "";
72     if not Cnf.has_key("Alicia::Options::Suite"):
73         Cnf["Alicia::Options::Suite"] = "unstable";
74
75     arguments = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
76     Options = Cnf.SubTree("Alicia::Options")
77
78     if Options["Help"]:
79         usage();
80
81     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
82     db_access.init(Cnf, projectB);
83
84     if not arguments:
85         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         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             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         utils.fubar("Unable to find package %s" % (package));
121     if q.ntuples() > 1:
122         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         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         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         #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 = logging.Logger(Cnf, "alicia");
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["__ALICIA_ADDRESS__"] = Cnf["Alicia::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("Alicia::Bcc") != "":
218             bcc.append(Cnf["Alicia::Bcc"]);
219         if bcc:
220             Subst["__BCC__"] = "Bcc: " + ", ".join(bcc);
221         else:
222             Subst["__BCC__"] = "X-Filler: 42";
223         Subst["__CC__"] = "X-Katie: alicia $Revision: 1.6 $";
224         Subst["__ADMIN_ADDRESS__"] = Cnf["Dinstall::MyAdminAddress"];
225         Subst["__DISTRO__"] = Cnf["Dinstall::MyDistribution"];
226         Subst["__WHOAMI__"] = 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 utils.split_args(Options["Done"]):
237             Subst["__BUG_NUMBER__"] = bug;
238             mail_message = utils.TemplateSubst(
239                 Subst,Cnf["Dir::Templates"]+"/alicia.bug-close");
240             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()