]> git.decadent.org.uk Git - dak.git/blob - heidi
read all input before doing anything and use transactions.
[dak.git] / heidi
1 #!/usr/bin/env python
2
3 # Manipulate suite tags
4 # Copyright (C) 2000, 2001  James Troup <james@nocrew.org>
5 # $Id: heidi,v 1.6 2001-06-20 18:47:37 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 # 8to6Guy: "Wow, Bob, You look rough!"
24 # BTAF: "Mbblpmn..."
25 # BTAF <.oO>: "You moron! This is what you get for staying up all night drinking vodka and salad dressing!"
26 # BTAF <.oO>: "This coffee I.V. drip is barely even keeping me awake! I need something with more kick! But what?"
27 # BTAF: "OMIGOD! I OVERDOSED ON HEROIN"
28 # CoWorker#n: "Give him air!!"
29 # CoWorker#n+1: "We need a syringe full of adrenaline!"
30 # CoWorker#n+2: "Stab him in the heart!"
31 # BTAF: "*YES!*"
32 # CoWorker#n+3: "Bob's been overdosing quite a bit lately..."
33 # CoWorker#n+4: "Third time this week."
34
35 # -- http://www.angryflower.com/8to6.gif
36
37 #######################################################################################
38
39 # Adds or removes packages from a suite.  Takes the list of files
40 # either from stdin or as a command line argument.  Special action
41 # "set", will reset the suite (!) and add all packages from scratch. 
42
43 #######################################################################################
44
45 import os, pg, string, sys;
46 import apt_pkg;
47 import utils, db_access;
48
49 #######################################################################################
50
51 Cnf = None;
52 projectB = None;
53
54 #######################################################################################
55
56 def process_file (file, suite_id, action):
57
58     lines = file.readlines();
59
60     projectB.query("BEGIN WORK");
61
62     if action == "set":
63         projectB.query("DELETE FROM bin_associations WHERE suite = %s" % (suite_id));
64         projectB.query("DELETE FROM src_associations WHERE suite = %s" % (suite_id));
65         action = "add";
66         
67     for line in lines:
68         split_line = string.split(string.strip(line[:-1]));
69         if len(split_line) != 3:
70             sys.stderr.write("W: '%s' does not break into 'package version architecture'.\n" % (line[:-1]));
71             continue;
72         
73         (package, version, architecture) = split_line;
74
75         if architecture == "source":
76             q = projectB.query("SELECT id FROM source WHERE source = '%s' AND version = '%s'" % (package, version))
77         else:
78             q = projectB.query("SELECT b.id FROM binaries b, architecture a WHERE b.package = '%s' AND b.version = '%s' AND (a.arch_string = '%s' OR a.arch_string = 'all') AND b.architecture = a.id" % (package, version, architecture))
79
80         ql = q.getresult();
81         if ql == []:
82             sys.stderr.write("W: Couldn't find '%s~%s~%s'.\n" % (package, version, architecture));
83             continue;
84         if len(ql) > 1:
85             sys.stderr.write("E: Found more than one match for '%s~%s~%s'.\n" % (package, version, architecture));
86             continue;
87         id = ql[0][0];
88
89         if architecture == "source": 
90             # Find the existing assoications ID, if any
91             q = projectB.query("SELECT id FROM src_associations WHERE suite = %s and source = %s" % (suite_id, id));
92             ql = q.getresult();
93             if ql == []:
94                 assoication_id = None;
95             else:
96                 assoication_id = ql[0][0];
97             # Take action
98             if action == "add":
99                 if assoication_id != None:
100                     sys.stderr.write("W: '%s~%s~%s' already exists in suite %s.\n" % (package, version, architecture, suite_id));
101                     continue;
102                 else:
103                     q = projectB.query("INSERT INTO src_associations (suite, source) VALUES (%s, %s)" % (suite_id, id));
104             elif action == "remove":
105                 if assoication_id == None:
106                     sys.stderr.write("W: '%s~%s~%s' doesn't exist in suite %s.\n" % (package, version, architecture, suite_id));
107                     continue;
108                 else:
109                     q = projectB.query("DELETE FROM src_associations WHERE id = %s" % (assoication_id));
110         else:
111             # Find the existing assoications ID, if any
112             q = projectB.query("SELECT id FROM bin_associations WHERE suite = %s and bin = %s" % (suite_id, id));
113             ql = q.getresult();
114             if ql == []:
115                 assoication_id = None;
116             else:
117                 assoication_id = ql[0][0];
118             # Take action
119             if action == "add":
120                 if assoication_id != None:
121                     sys.stderr.write("W: '%s~%s~%s' already exists in suite %s.\n" % (package, version, architecture, suite_id));
122                     continue;
123                 else:
124                     q = projectB.query("INSERT INTO bin_associations (suite, bin) VALUES (%s, %s)" % (suite_id, id));
125             elif action == "remove":
126                 if assoication_id == None:
127                     sys.stderr.write("W: '%s~%s~%s' doesn't exist in suite %s.\n" % (package, version, architecture, suite_id));
128                     continue;
129                 else:
130                     q = projectB.query("DELETE FROM bin_associations WHERE id = %s" % (assoication_id));
131
132     projectB.query("COMMIT WORK");
133
134 #######################################################################################
135
136 def get_list (suite_id):
137     # List binaries
138     q = projectB.query("SELECT b.package, b.version, a.arch_string FROM binaries b, bin_associations ba, architecture a WHERE ba.suite = %s AND ba.bin = b.id AND b.architecture = a.id" % (suite_id));
139     ql = q.getresult();
140     for i in ql:
141         print string.join(i);
142
143     # List source
144     q = projectB.query("SELECT s.source, s.version FROM source s, src_associations sa WHERE sa.suite = %s AND sa.source = s.id" % (suite_id));
145     ql = q.getresult();
146     for i in ql:
147         print string.join(i) + " source";
148
149 #######################################################################################
150
151 def main ():
152     global Cnf, projectB;
153
154     apt_pkg.init();
155     
156     Cnf = apt_pkg.newConfiguration();
157     apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
158
159     Arguments = [('a',"add","Heidi::Options::Add", "HasArg"),
160                  ('d',"debug","Heidi::Options::Debug", "IntVal"),
161                  ('h',"help","Heidi::Options::Help"),
162                  ('l',"list","Heidi::Options::List","HasArg"),
163                  ('r',"remove", "Heidi::Options::Remove", "HasArg"),
164                  ('s',"set", "Heidi::Options::Set", "HasArg"),
165                  ('v',"version","Heidi::Options::Version")];
166
167     file_list = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
168
169     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"],int(Cnf["DB::Port"]));
170
171     db_access.init(Cnf, projectB);
172
173     action = None;
174
175     for i in ("add", "list", "remove", "set"):
176         suite = Cnf["Heidi::Options::%s" % (i)];
177         if suite !="":
178             if not Cnf.has_key("Suite::%s" % (suite)):
179                 sys.stderr.write("Unknown suite %s.\n" %(suite));
180                 sys.exit(2);
181             else:
182                 suite_id = db_access.get_suite_id(suite);
183                 if action != None:
184                     sys.stderr.write("Can only do one action at a time.\n");
185                     sys.exit(2);
186                 action = i;
187
188     # Need an action...
189     if action == None:
190         sys.stderr.write("No action specified.\n");
191         sys.exit(2);
192
193     # Safety/Sanity check
194     if action == "set" and suite != "testing":
195         sys.stderr.write("Will not reset a suite other than testing...\n");
196         sys.exit(2);
197
198     if action == "list":
199         get_list(suite_id);
200     else:
201         if file_list != []:
202             for file in file_list:
203                 process_file(utils.open_file(file,'r'), suite_id, action);
204         else:
205             process_file(sys.stdin, suite_id, action);
206
207 #######################################################################################
208
209 if __name__ == '__main__':
210     main()
211