]> git.decadent.org.uk Git - dak.git/blob - heidi
set TZ to UTC while running ls -lR. closes: #99058
[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.8 2001-09-14 17:16:18 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, logging;
48
49 #######################################################################################
50
51 Cnf = None;
52 projectB = None;
53 Logger = None;
54
55 #######################################################################################
56
57 def get_id (package, version, architecture):
58     if architecture == "source":
59         q = projectB.query("SELECT id FROM source WHERE source = '%s' AND version = '%s'" % (package, version))
60     else:
61         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))
62
63     ql = q.getresult();
64     if ql == []:
65         utils.warn("Couldn't find '%s~%s~%s'." % (package, version, architecture));
66         return None;
67     if len(ql) > 1:
68         utils.warn("Found more than one match for '%s~%s~%s'." % (package, version, architecture));
69         return None;
70     id = ql[0][0];
71     return id;
72
73 #######################################################################################
74
75 def set_suite (file, suite_id):
76     lines = file.readlines();
77
78     projectB.query("BEGIN WORK");
79
80     # Build up a dictionary of what is currently in the suite
81     current = {};
82     q = projectB.query("SELECT b.package, b.version, a.arch_string, ba.id FROM binaries b, bin_associations ba, architecture a WHERE ba.suite = %s AND ba.bin = b.id AND b.architecture = a.id" % (suite_id));
83     ql = q.getresult();
84     for i in ql:
85         key = string.join(i[:3]);
86         current[key] = i[3];
87     q = projectB.query("SELECT s.source, s.version, sa.id FROM source s, src_associations sa WHERE sa.suite = %s AND sa.source = s.id" % (suite_id));
88     ql = q.getresult();
89     for i in ql:
90         key = string.join(i[:2]) + " source";
91         current[key] = i[2];
92
93     # Build up a dictionary of what should be in the suite
94     desired = {};
95     for line in lines:
96         split_line = string.split(string.strip(line[:-1]));
97         if len(split_line) != 3:
98             utils.warn("'%s' does not break into 'package version architecture'." % (line[:-1]));
99             continue;
100         key = string.join(split_line);
101         desired[key] = "";
102
103     # Check to see which packages need removed and remove them
104     for key in current.keys():
105         if not desired.has_key(key):
106             (package, version, architecture) = string.split(key);
107             id = current[key];
108             if architecture == "source":
109                 q = projectB.query("DELETE FROM src_associations WHERE id = %s" % (id));
110             else:
111                 q = projectB.query("DELETE FROM bin_associations WHERE id = %s" % (id));
112             Logger.log(["removed",key,id]);
113
114     # Check to see which packages need added and add them
115     for key in desired.keys():
116         if not current.has_key(key):
117             (package, version, architecture) = string.split(key);
118             id = get_id (package, version, architecture);
119             if not id:
120                 continue;
121             if architecture == "source":
122                 q = projectB.query("INSERT INTO src_associations (suite, source) VALUES (%s, %s)" % (suite_id, id));
123             else:
124                 q = projectB.query("INSERT INTO bin_associations (suite, bin) VALUES (%s, %s)" % (suite_id, id));
125             Logger.log(["added",key,id]);
126
127     projectB.query("COMMIT WORK");
128
129 #######################################################################################
130
131 def process_file (file, suite_id, action):
132
133     if action == "set":
134         set_suite (file, suite_id);
135         return;
136
137     lines = file.readlines();
138
139     projectB.query("BEGIN WORK");
140
141     for line in lines:
142         split_line = string.split(string.strip(line[:-1]));
143         if len(split_line) != 3:
144             utils.warn("'%s' does not break into 'package version architecture'." % (line[:-1]));
145             continue;
146
147         (package, version, architecture) = split_line;
148
149         id = get_id(package, version, architecture);
150         if not id:
151             continue;
152
153         if architecture == "source":
154             # Find the existing assoications ID, if any
155             q = projectB.query("SELECT id FROM src_associations WHERE suite = %s and source = %s" % (suite_id, id));
156             ql = q.getresult();
157             if ql == []:
158                 assoication_id = None;
159             else:
160                 assoication_id = ql[0][0];
161             # Take action
162             if action == "add":
163                 if assoication_id != None:
164                     utils.warn("'%s~%s~%s' already exists in suite %s." % (package, version, architecture, suite_id));
165                     continue;
166                 else:
167                     q = projectB.query("INSERT INTO src_associations (suite, source) VALUES (%s, %s)" % (suite_id, id));
168             elif action == "remove":
169                 if assoication_id == None:
170                     utils.warn("'%s~%s~%s' doesn't exist in suite %s." % (package, version, architecture, suite_id));
171                     continue;
172                 else:
173                     q = projectB.query("DELETE FROM src_associations WHERE id = %s" % (assoication_id));
174         else:
175             # Find the existing assoications ID, if any
176             q = projectB.query("SELECT id FROM bin_associations WHERE suite = %s and bin = %s" % (suite_id, id));
177             ql = q.getresult();
178             if ql == []:
179                 assoication_id = None;
180             else:
181                 assoication_id = ql[0][0];
182             # Take action
183             if action == "add":
184                 if assoication_id != None:
185                     utils.warn("'%s~%s~%s' already exists in suite %s." % (package, version, architecture, suite_id));
186                     continue;
187                 else:
188                     q = projectB.query("INSERT INTO bin_associations (suite, bin) VALUES (%s, %s)" % (suite_id, id));
189             elif action == "remove":
190                 if assoication_id == None:
191                     utils.warn("'%s~%s~%s' doesn't exist in suite %s." % (package, version, architecture, suite_id));
192                     continue;
193                 else:
194                     q = projectB.query("DELETE FROM bin_associations WHERE id = %s" % (assoication_id));
195
196     projectB.query("COMMIT WORK");
197
198 #######################################################################################
199
200 def get_list (suite_id):
201     # List binaries
202     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));
203     ql = q.getresult();
204     for i in ql:
205         print string.join(i);
206
207     # List source
208     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));
209     ql = q.getresult();
210     for i in ql:
211         print string.join(i) + " source";
212
213 #######################################################################################
214
215 def main ():
216     global Cnf, projectB, Logger;
217
218     apt_pkg.init();
219
220     Cnf = apt_pkg.newConfiguration();
221     apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
222
223     Arguments = [('a',"add","Heidi::Options::Add", "HasArg"),
224                  ('D',"debug","Heidi::Options::Debug", "IntVal"),
225                  ('h',"help","Heidi::Options::Help"),
226                  ('l',"list","Heidi::Options::List","HasArg"),
227                  ('r',"remove", "Heidi::Options::Remove", "HasArg"),
228                  ('s',"set", "Heidi::Options::Set", "HasArg"),
229                  ('V',"version","Heidi::Options::Version")];
230
231     file_list = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
232
233     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"],int(Cnf["DB::Port"]));
234
235     db_access.init(Cnf, projectB);
236
237     action = None;
238
239     for i in ("add", "list", "remove", "set"):
240         suite = Cnf["Heidi::Options::%s" % (i)];
241         if suite !="":
242             if not Cnf.has_key("Suite::%s" % (suite)):
243                 utils.fubar("Unknown suite '%s'." %(suite));
244             else:
245                 suite_id = db_access.get_suite_id(suite);
246                 if action != None:
247                     utils.fubar("Can only perform one action at a time.");
248                 action = i;
249
250     # Need an action...
251     if action == None:
252         utils.fubar("No action specified.");
253
254     # Safety/Sanity check
255     if action == "set" and suite != "testing":
256         utils.fubar("Will not reset a suite other than testing.");
257
258     if action == "list":
259         get_list(suite_id);
260     else:
261         Logger = logging.Logger(Cnf, "heidi");
262         if file_list != []:
263             for file in file_list:
264                 process_file(utils.open_file(file,'r'), suite_id, action);
265         else:
266             process_file(sys.stdin, suite_id, action);
267         Logger.close();
268
269 #######################################################################################
270
271 if __name__ == '__main__':
272     main()
273