]> git.decadent.org.uk Git - dak.git/blob - catherine
sync
[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.10 2001-06-22 22:53:14 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 = 0L;
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             utils.warn("Hit %s limit." % (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                 utils.fubar("%s: couldn't determine required information to rename .deb file." % (legacy_filename));
75             version = utils.re_no_epoch.sub('', version);
76             destination_filename = "%s_%s_%s.deb" % (package, version, architecture);
77         else:
78             m = utils.re_issource.match(base_filename)
79             if m != None:
80                 source = m.group(1);
81             else:
82                 utils.fubar("expandsion of source filename '%s' failed." % (legacy_filename));
83         # Work out the component name
84         component = qid["component"];
85         if component == "":
86             q = projectB.query("SELECT DISTINCT(c.name) FROM override o, component c WHERE o.package = '%s' AND o.component = c.id;" % (source));
87             ql = q.getresult();
88             if ql == []:
89                 utils.fubar("No override match for '%s' so I can't work out the component." % (source));
90             if len(ql) > 1:
91                 utils.fubar("Multiple override matches for '%s' so I can't work out the component." % (source));
92             component = ql[0][0];
93         # Work out the new location
94         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));
95         ql = q.getresult();
96         if len(ql) != 1:
97             utils.fubar("couldn't determine location ID for '%s'. [query returned %d matches, not 1 as expected]" % (source, len(ql)));
98         location_id = ql[0][0];
99         # First move the files to the new location
100         pool_location = utils.poolify (source, component);
101         pool_filename = pool_location + destination_filename;
102         destination = Cnf["Dir::PoolDir"] + pool_location + destination_filename;
103         if os.path.exists(destination):
104             utils.fubar("'%s' already exists in the pool; serious FUBARity." % (legacy_filename));
105         if verbose:
106             print "Moving: %s -> %s" % (legacy_filename, destination);
107         if not no_action:
108             utils.move(legacy_filename, destination);
109         # Then Update the DB's files table
110         if verbose:
111             print "SQL: UPDATE files SET filename = '%s', location = '%s' WHERE id = '%s'" % (pool_filename, location_id, qid["files_id"]);
112         if not no_action:
113             q = projectB.query("UPDATE files SET filename = '%s', location = '%s' WHERE id = '%s'" % (pool_filename, location_id, qid["files_id"]));
114
115     sys.stderr.write("Poolized %s in %s files.\n" % (utils.size_type(poolized_size), poolized_count));
116     
117 ################################################################################
118
119 def main ():
120     global Cnf, projectB;
121
122     apt_pkg.init();
123     
124     Cnf = apt_pkg.newConfiguration();
125     apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
126
127     Arguments = [('D',"debug","Catherine::Options::Debug", "IntVal"),
128                  ('h',"help","Catherine::Options::Help"),
129                  ('V',"version","Catherine::Options::Version"),
130                  ('l',"limit", "Catherine::Options::Limit", "HasArg"),
131                  ('n',"no-action","Catherine::Options::No-Action"),
132                  ('v',"verbose","Catherine::Options::Verbose")];
133
134     apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
135     Options = Cnf.SubTree("Catherine::Options")
136
137     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
138     db_access.init(Cnf, projectB);
139
140     if not Options["Limit"]:
141         limit = -1;
142     else:
143         limit = int(Options["Limit"]) * 1024;
144
145     # -n/--no-action implies -v/--verbose
146     if Options["No-Action"]:
147         Options["Verbose"] = "true";
148
149     # Sanity check the limit argument
150     if limit > 0 and limit < 1024:
151         utils.fubar("-l/--limit takes an argument with a value in kilobytes.");
152
153     # Grab a list of all files not already in the pool
154     q = projectB.query("""
155 SELECT l.path, f.filename, f.id as files_id, c.name as component 
156    FROM files f, location l, component c WHERE 
157     NOT EXISTS (SELECT * FROM location l WHERE l.type = 'pool' AND f.location = l.id) 
158     AND NOT (f.filename ~ '^potato') AND f.location = l.id AND l.component = c.id 
159 UNION SELECT l.path, f.filename, f.id as files_id, null as component
160    FROM files f, location l WHERE 
161     NOT EXISTS (SELECT * FROM location l WHERE l.type = 'pool' AND f.location = l.id) 
162     AND NOT (f.filename ~ '^potato') AND f.location = l.id AND NOT EXISTS 
163      (SELECT l.path FROM location l WHERE l.component IS NOT NULL AND f.location = l.id);""");
164
165     poolize(q, limit, Options["Verbose"], Options["No-Action"]);
166
167 #######################################################################################
168
169 if __name__ == '__main__':
170     main()
171