]> git.decadent.org.uk Git - dak.git/blob - catherine
Fix database name to be a config option. Fix debian/rules typo. [Ryan Murray]
[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.7 2001-03-20 00:28:11 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 ################################################################################
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                  ('v',"verbose","Catherine::Options::Verbose")];
52
53     apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
54
55     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
56     db_access.init(Cnf, projectB);
57
58     poolized_size = 0;
59     poolized_count = 0;
60
61     # Parse -l/--limit argument
62     if not Cnf["Catherine::Options::Limit"]:
63         limit = -1;
64     else:
65         limit = int(Cnf["Catherine::Options::Limit"]) * 1024;
66
67     # -n/--no-action implies -v/--verbose
68     if Cnf["Catherine::Options::No-Action"]:
69         Cnf["Catherine::Options::Verbose"] = "true";
70
71     # Sanity check the limit argument
72     if limit > 0 and limit < 1024:
73         sys.stderr.write("-l/--limit takes an argument with a value in kilobytes.\n");
74         sys.exit(1);
75
76     # Grab a list of all files not already in the pool
77     q = projectB.query("""
78 SELECT l.path, f.filename, f.id as files_id, c.name as component 
79    FROM files f, location l, component c WHERE 
80     NOT EXISTS (SELECT * FROM location l WHERE l.type = 'pool' AND f.location = l.id) 
81     AND NOT (f.filename ~ '^potato') AND f.location = l.id AND l.component = c.id 
82 UNION SELECT l.path, f.filename, f.id as files_id, null as component
83    FROM files f, location l WHERE 
84     NOT EXISTS (SELECT * FROM location l WHERE l.type = 'pool' AND f.location = l.id) 
85     AND NOT (f.filename ~ '^potato') AND f.location = l.id AND NOT EXISTS 
86      (SELECT l.path FROM location l WHERE l.component IS NOT NULL AND f.location = l.id);""");
87     qd = q.dictresult();
88     for qid in qd:
89         legacy_filename = qid["path"]+qid["filename"];
90         size = os.stat(legacy_filename)[stat.ST_SIZE];
91         if (poolized_size + size) > limit and limit >= 0:
92             sys.stderr.write("Hit %s limit.\n" % (utils.size_type(limit)));
93             break;
94         poolized_size = poolized_size + size;
95         poolized_count = poolized_count + 1;
96         base_filename = os.path.basename(legacy_filename);
97         destination_filename = base_filename;
98         # Work out the source package name
99         if utils.re_isadeb.match(base_filename) != None:
100             control = apt_pkg.ParseSection(apt_inst.debExtractControl(utils.open_file(legacy_filename,"r")))
101             package = control.Find("Package", "");
102             source = control.Find("Source", package);
103             if string.find(source, "(") != -1:
104                 m = utils.re_extract_src_version.match(source)
105                 source = m.group(1)
106             # If it's a binary, we need to also rename the file to include the architecture
107             version = control.Find("Version", "");
108             architecture = control.Find("Architecture", "");
109             if package == "" or version == "" or architecture == "":
110                 sys.stderr.write("%s: couldn't determine required information to rename .deb file.\n" % (legacy_filename));
111                 sys.exit(1);
112             version = utils.re_no_epoch.sub('', version);
113             destination_filename = "%s_%s_%s.deb" % (package, version, architecture);
114         else:
115             m = utils.re_issource.match(base_filename)
116             if m != None:
117                 source = m.group(1);
118             else:
119                 sys.stderr.write("%s: say what?\n" % (legacy_filename));
120                 sys.exit(1);
121         # Work out the component name
122         component = qid["component"];
123         if component == "":
124             q = projectB.query("SELECT DISTINCT(c.name) FROM override o, component c WHERE o.package = '%s' AND o.component = c.id;" % (source));
125             ql = q.getresult();
126             if ql == []:
127                 sys.stderr.write("%s: No override match so I can't work out the component.\n" % (source));
128                 sys.exit(1);
129             if len(ql) > 1:
130                 sys.stderr.write("%s: multiple override matches for %s so I can't work out the component.\n" % (source));
131                 sys.exit(1);
132             component = ql[0][0];
133         # Work out the new location
134         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));
135         ql = q.getresult();
136         if len(ql) != 1:
137             sys.stderr.write("%s: couldn't determine location ID, query returned %d matches and not 1 as expected.\n" % (source, len(ql)));
138             sys.exit(1);
139         location_id = ql[0][0];
140         # First move the files to the new location
141         pool_location = utils.poolify (source, component);
142         pool_filename = pool_location + destination_filename;
143         destination = Cnf["Dir::PoolDir"] + pool_location + destination_filename;
144         if os.path.exists(destination):
145             sys.stderr.write("%s: already exists in the pool; serious FUBARity.\n" % (legacy_filename));
146             sys.exit(1);
147         if Cnf["Catherine::Options::Verbose"]:
148             print "Moving: %s -> %s" % (legacy_filename, destination);
149         if not Cnf["Catherine::Options::No-Action"]:
150             utils.move(legacy_filename, destination);
151         # Then Update the DB's files table
152         if Cnf["Catherine::Options::Verbose"]:
153             print "SQL: UPDATE files SET filename = '%s', location = '%s' WHERE id = '%s'" % (pool_filename, location_id, qid["files_id"]);
154         if not Cnf["Catherine::Options::No-Action"]:
155             q = projectB.query("UPDATE files SET filename = '%s', location = '%s' WHERE id = '%s'" % (pool_filename, location_id, qid["files_id"]));
156
157     sys.stderr.write("Poolized %s in %s files.\n" % (utils.size_type(poolized_size), poolized_count));
158
159 #######################################################################################
160
161 if __name__ == '__main__':
162     main()
163