]> git.decadent.org.uk Git - dak.git/blob - alicia
* alicia: Added * docs/alicia.1.sgml: Added * docs/Makefile: Added alicia to the...
[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.1 2004-01-29 01:00:08 dsilvers 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, pwd, 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 a microscopic override change/query
51
52   -h, --help                 show this help and exit
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 = utils.get_conf()
62
63     Arguments = [('h',"help","Alicia::Options::Help"),
64                  ('n',"no-action","Alicia::Options::No-Action"),
65                  ('s',"suite","Alicia::Options::Suite", "HasArg"),
66                  ];
67     for i in ["help", "no-action"]:
68         if not Cnf.has_key("Alicia::Options::%s" % (i)):
69             Cnf["Alicia::Options::%s" % (i)] = "";
70     if not Cnf.has_key("Alicia::Options::Suite"):
71         Cnf["Alicia::Options::Suite"] = "unstable";
72
73     arguments = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
74     Options = Cnf.SubTree("Alicia::Options")
75
76     if Options["Help"]:
77         usage();
78
79     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
80     db_access.init(Cnf, projectB);
81
82     if not arguments:
83         utils.fubar("package name is a required argument.");
84
85     package = arguments.pop(0);
86
87     if arguments and len(arguments) > 2:
88         utils.fubar("Too many arguments");
89
90     if arguments and len(arguments) == 1:
91         # Determine if the argument is a priority or a section...
92         arg = arguments.pop();
93         q = projectB.query("""
94         SELECT ( SELECT COUNT(*) FROM section WHERE section=%s ) AS secs,
95                ( SELECT COUNT(*) FROM priority WHERE priority=%s ) AS prios
96                """ % ( pg._quote(arg,"str"), pg._quote(arg,"str")));
97         r = q.getresult();
98         if r[0][0] == 1:
99             arguments = (arg,".");
100         elif r[0][1] == 1:
101             arguments = (".",arg);
102         else:
103             utils.fubar("%s is not a valid section or priority");
104         
105
106     # Retrieve current section/priority...
107     q = projectB.query("""
108     SELECT priority.priority AS prio, section.section AS sect
109       FROM override, priority, section, suite
110      WHERE override.priority = priority.id
111        AND override.section = section.id
112        AND override.package = %s
113        AND override.suite = suite.id
114        AND suite.suite_name = %s
115     """ % (pg._quote(package,"str"), pg._quote(Options["Suite"],"str")));
116
117     if q.ntuples() == 0:
118         utils.fubar("Unable to find package %s" % (package));
119     if q.ntuples() > 1:
120         utils.fubar("%s is ambiguous. Matches %d packages" % (package,q.ntuples()));
121
122     r = q.getresult();
123     oldsection = r[0][1];
124     oldpriority = r[0][0];
125
126     if not arguments:
127         print "%s is in section '%s' at priority '%s'" % (
128             package,oldsection,oldpriority);
129         sys.exit(0);
130
131     # At this point, we have a new section and priority... check they're valid...
132     newsection, newpriority = arguments;
133
134     if newsection == ".":
135         newsection = oldsection;
136     if newpriority == ".":
137         newpriority = oldpriority;
138     
139     q = projectB.query("SELECT id FROM section WHERE section=%s" % (
140         pg._quote(newsection,"str")));
141
142     if q.ntuples() == 0:
143         utils.fubar("Supplied section %s is invalid" % (newsection));
144     newsecid = q.getresult()[0][0];
145     
146     q = projectB.query("SELECT id FROM priority WHERE priority=%s" % (
147         pg._quote(newpriority,"str")));
148
149     if q.ntuples() == 0:
150         utils.fubar("Supplied priority %s is invalid" % (newpriority));
151     newprioid = q.getresult()[0][0];
152
153     if newpriority == oldpriority and newsection == oldsection:
154         print "I: Doing nothing"
155         sys.exit(0);
156     
157     # If we're in no-action mode
158     if Options["No-Action"]:
159         if newpriority != oldpriority:
160             print "I: Would change priority from %s to %s" % (oldpriority,newpriority);
161         if newsection != oldsection:
162             print "I: Would change section from %s to %s" % (oldsection,newsection);
163         sys.exit(0);
164
165     if newpriority != oldpriority:
166         print "I: Will change priority from %s to %s" % (oldpriority,newpriority);
167     if newsection != oldsection:
168         print "I: Will change section from %s to %s" % (oldsection,newsection);
169
170     game_over();
171
172     Logger = logging.Logger(Cnf, "alicia");
173
174     projectB.query("BEGIN WORK");
175     # We're in "do it" mode, we have something to do... do it
176     if newpriority != oldpriority:
177         q = projectB.query("""
178         UPDATE override
179            SET priority=%d
180          WHERE package=%s
181            AND suite = (SELECT id FROM suite WHERE suite_name=%s)""" % (
182             newprioid,
183             pg._quote(package,"str"),
184             pg._quote(Options["Suite"],"str") ));
185         Logger.log(["changed priority",package,oldpriority,newpriority]);
186
187     if newsection != oldsection:
188         q = projectB.query("""
189         UPDATE override
190            SET section=%d
191          WHERE package=%s
192            AND suite = (SELECT id FROM suite WHERE suite_name=%s)""" % (
193             newsecid,
194             pg._quote(package,"str"),
195             pg._quote(Options["Suite"],"str") ));
196         Logger.log(["changed priority",package,oldsection,newsection]);
197     projectB.query("COMMIT WORK");
198
199     Logger.close();
200
201     print "Done";
202     
203 #################################################################################
204
205 if __name__ == '__main__':
206     main()