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