]> git.decadent.org.uk Git - dak.git/blob - catherine
Partially functionize.
[dak.git] / catherine
1 #!/usr/bin/env python
2
3 # Poolify (move packages from "legacy" type locations to pool locations)
4 # Copyright (C) 2000, 2001  James Troup <james@nocrew.org>
5 # $Id: catherine,v 1.8 2001-03-24 03:29: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 # "Welcome to where time stands still,
22 #  No one leaves and no one will."
23 #   - Sanitarium - Metallica / Master of the puppets
24
25 ################################################################################
26
27 import os, pg, stat, string, sys
28 import utils, db_access
29 import apt_pkg, apt_inst;
30
31 ################################################################################
32
33 Cnf = None;
34 projectB = None;
35
36 # Q is a python-postgresql query result set and must have the
37 # following four columns:
38 #  o files.id (as 'files_id')
39 #  o files.filename
40 #  o location.path
41 #  o component.name (as 'component')
42 #
43 # limit is a value in bytes or -1 for no limit (use with care!)
44 # verbose and no_action are booleans
45
46 def poolize (q, limit, verbose, no_action):
47     poolized_size = 0;
48     poolized_count = 0;
49
50     # Parse -l/--limit argument
51     qd = q.dictresult();
52     for qid in qd:
53         legacy_filename = qid["path"]+qid["filename"];
54         size = os.stat(legacy_filename)[stat.ST_SIZE];
55         if (poolized_size + size) > limit and limit >= 0:
56             sys.stderr.write("Hit %s limit.\n" % (utils.size_type(limit)));
57             break;
58         poolized_size = poolized_size + size;
59         poolized_count = poolized_count + 1;
60         base_filename = os.path.basename(legacy_filename);
61         destination_filename = base_filename;
62         # Work out the source package name
63         if utils.re_isadeb.match(base_filename) != None:
64             control = apt_pkg.ParseSection(apt_inst.debExtractControl(utils.open_file(legacy_filename,"r")))
65             package = control.Find("Package", "");
66             source = control.Find("Source", package);
67             if string.find(source, "(") != -1:
68                 m = utils.re_extract_src_version.match(source)
69                 source = m.group(1)
70             # If it's a binary, we need to also rename the file to include the architecture
71             version = control.Find("Version", "");
72             architecture = control.Find("Architecture", "");
73             if package == "" or version == "" or architecture == "":
74                 sys.stderr.write("%s: couldn't determine required information to rename .deb file.\n" % (legacy_filename));
75                 sys.exit(1);
76             version = utils.re_no_epoch.sub('', version);
77             destination_filename = "%s_%s_%s.deb" % (package, version, architecture);
78         else:
79             m = utils.re_issource.match(base_filename)
80             if m != None:
81                 source = m.group(1);
82             else:
83                 sys.stderr.write("%s: say what?\n" % (legacy_filename));
84                 sys.exit(1);
85         # Work out the component name
86         component = qid["component"];
87         if component == "":
88             q = projectB.query("SELECT DISTINCT(c.name) FROM override o, component c WHERE o.package = '%s' AND o.component = c.id;" % (source));
89             ql = q.getresult();
90             if ql == []:
91                 sys.stderr.write("%s: No override match so I can't work out the component.\n" % (source));
92                 sys.exit(1);
93             if len(ql) > 1:
94                 sys.stderr.write("%s: multiple override matches for %s so I can't work out the component.\n" % (source));
95                 sys.exit(1);
96             component = ql[0][0];
97         # Work out the new location
98         q = projectB.query("SELECT l.id FROM location l, component c WHERE c.name = '%s' AND c.id = l.component AND l.type = 'pool';" % (component));
99         ql = q.getresult();
100         if len(ql) != 1:
101             sys.stderr.write("%s: couldn't determine location ID, query returned %d matches and not 1 as expected.\n" % (source, len(ql)));
102             sys.exit(1);
103         location_id = ql[0][0];
104         # First move the files to the new location
105         pool_location = utils.poolify (source, component);
106         pool_filename = pool_location + destination_filename;
107         destination = Cnf["Dir::PoolDir"] + pool_location + destination_filename;
108         if os.path.exists(destination):
109             sys.stderr.write("%s: already exists in the pool; serious FUBARity.\n" % (legacy_filename));
110             sys.exit(1);
111         if verbose:
112             print "Moving: %s -> %s" % (legacy_filename, destination);
113         if not no_action:
114             utils.move(legacy_filename, destination);
115         # Then Update the DB's files table
116         if verbose:
117             print "SQL: UPDATE files SET filename = '%s', location = '%s' WHERE id = '%s'" % (pool_filename, location_id, qid["files_id"]);
118         if not no_action:
119             q = projectB.query("UPDATE files SET filename = '%s', location = '%s' WHERE id = '%s'" % (pool_filename, location_id, qid["files_id"]));
120
121     sys.stderr.write("Poolized %s in %s files.\n" % (utils.size_type(poolized_size), poolized_count));
122     
123 ################################################################################
124
125 def main ():
126     global Cnf, projectB;
127
128     apt_pkg.init();
129     
130     Cnf = apt_pkg.newConfiguration();
131     apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
132
133     Arguments = [('D',"debug","Catherine::Options::Debug", "IntVal"),
134                  ('h',"help","Catherine::Options::Help"),
135                  ('V',"version","Catherine::Options::Version"),
136                  ('l',"limit", "Catherine::Options::Limit", "HasArg"),
137                  ('n',"no-action","Catherine::Options::No-Action"),
138                  ('v',"verbose","Catherine::Options::Verbose")];
139
140     apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
141     Options = Cnf.SubTree("Catherine::Options")
142
143     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
144     db_access.init(Cnf, projectB);
145
146     if not Options["Limit"]:
147         limit = -1;
148     else:
149         limit = int(Options["Limit"]) * 1024;
150
151     # -n/--no-action implies -v/--verbose
152     if Options["No-Action"]:
153         Options["Verbose"] = "true";
154
155     # Sanity check the limit argument
156     if limit > 0 and limit < 1024:
157         sys.stderr.write("-l/--limit takes an argument with a value in kilobytes.\n");
158         sys.exit(1);
159
160     # Grab a list of all files not already in the pool
161     q = projectB.query("""
162 SELECT l.path, f.filename, f.id as files_id, c.name as component 
163    FROM files f, location l, component c WHERE 
164     NOT EXISTS (SELECT * FROM location l WHERE l.type = 'pool' AND f.location = l.id) 
165     AND NOT (f.filename ~ '^potato') AND f.location = l.id AND l.component = c.id 
166 UNION SELECT l.path, f.filename, f.id as files_id, null as component
167    FROM files f, location l WHERE 
168     NOT EXISTS (SELECT * FROM location l WHERE l.type = 'pool' AND f.location = l.id) 
169     AND NOT (f.filename ~ '^potato') AND f.location = l.id AND NOT EXISTS 
170      (SELECT l.path FROM location l WHERE l.component IS NOT NULL AND f.location = l.id);""");
171
172     poolize(q, limit, Options["Verbose"], Options["No-Action"]);
173
174 #######################################################################################
175
176 if __name__ == '__main__':
177     main()
178