]> git.decadent.org.uk Git - dak.git/blob - catherine
Dir rationalization
[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, 2002  James Troup <james@nocrew.org>
5 # $Id: catherine,v 1.16 2002-05-08 11:13:02 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 usage (exit_code=0):
39     print """Usage: catherine [OPTIONS]
40 Migrate packages from legacy locations into the pool.
41
42   -l, --limit=AMOUNT         only migrate AMOUNT Kb of packages
43   -n, --no-action            don't do anything
44   -v, --verbose              explain what is being done
45   -h, --help                 show this help and exit"""
46
47     sys.exit(exit_code)
48
49 ################################################################################
50
51 # Q is a python-postgresql query result set and must have the
52 # following four columns:
53 #  o files.id (as 'files_id')
54 #  o files.filename
55 #  o location.path
56 #  o component.name (as 'component')
57 #
58 # limit is a value in bytes or -1 for no limit (use with care!)
59 # verbose and no_action are booleans
60
61 def poolize (q, limit, verbose, no_action):
62     poolized_size = 0L;
63     poolized_count = 0;
64
65     # Parse -l/--limit argument
66     qd = q.dictresult();
67     for qid in qd:
68         legacy_filename = qid["path"]+qid["filename"];
69         size = os.stat(legacy_filename)[stat.ST_SIZE];
70         if (poolized_size + size) > limit and limit >= 0:
71             utils.warn("Hit %s limit." % (utils.size_type(limit)));
72             break;
73         poolized_size = poolized_size + size;
74         poolized_count = poolized_count + 1;
75         base_filename = os.path.basename(legacy_filename);
76         destination_filename = base_filename;
77         # Work out the source package name
78         if utils.re_isadeb.match(base_filename) != None:
79             control = apt_pkg.ParseSection(apt_inst.debExtractControl(utils.open_file(legacy_filename)))
80             package = control.Find("Package", "");
81             source = control.Find("Source", package);
82             if string.find(source, "(") != -1:
83                 m = utils.re_extract_src_version.match(source)
84                 source = m.group(1)
85             # If it's a binary, we need to also rename the file to include the architecture
86             version = control.Find("Version", "");
87             architecture = control.Find("Architecture", "");
88             if package == "" or version == "" or architecture == "":
89                 utils.fubar("%s: couldn't determine required information to rename .deb file." % (legacy_filename));
90             version = utils.re_no_epoch.sub('', version);
91             destination_filename = "%s_%s_%s.deb" % (package, version, architecture);
92         else:
93             m = utils.re_issource.match(base_filename)
94             if m != None:
95                 source = m.group(1);
96             else:
97                 utils.fubar("expansion of source filename '%s' failed." % (legacy_filename));
98         # Work out the component name
99         component = qid["component"];
100         if component == "":
101             q = projectB.query("SELECT DISTINCT(c.name) FROM override o, component c WHERE o.package = '%s' AND o.component = c.id;" % (source));
102             ql = q.getresult();
103             if not ql:
104                 utils.fubar("No override match for '%s' so I can't work out the component." % (source));
105             if len(ql) > 1:
106                 utils.fubar("Multiple override matches for '%s' so I can't work out the component." % (source));
107             component = ql[0][0];
108         # Work out the new location
109         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));
110         ql = q.getresult();
111         if len(ql) != 1:
112             utils.fubar("couldn't determine location ID for '%s'. [query returned %d matches, not 1 as expected]" % (source, len(ql)));
113         location_id = ql[0][0];
114         # First move the files to the new location
115         pool_location = utils.poolify (source, component);
116         pool_filename = pool_location + destination_filename;
117         destination = Cnf["Dir::Pool"] + pool_location + destination_filename;
118         if os.path.exists(destination):
119             utils.fubar("'%s' already exists in the pool; serious FUBARity." % (legacy_filename));
120         if verbose:
121             print "Moving: %s -> %s" % (legacy_filename, destination);
122         if not no_action:
123             utils.move(legacy_filename, destination);
124         # Then Update the DB's files table
125         if verbose:
126             print "SQL: UPDATE files SET filename = '%s', location = '%s' WHERE id = '%s'" % (pool_filename, location_id, qid["files_id"]);
127         if not no_action:
128             q = projectB.query("UPDATE files SET filename = '%s', location = '%s' WHERE id = '%s'" % (pool_filename, location_id, qid["files_id"]));
129
130     sys.stderr.write("Poolized %s in %s files.\n" % (utils.size_type(poolized_size), poolized_count));
131
132 ################################################################################
133
134 def main ():
135     global Cnf, projectB;
136
137     Cnf = utils.get_conf()
138
139     for i in ["help", "limit", "no-action", "verbose" ]:
140         if not Cnf.has_key("Catherine::Options::%s" % (i)):
141             Cnf["Catherine::Options::%s" % (i)] = "";
142
143
144     Arguments = [('h',"help","Catherine::Options::Help"),
145                  ('l',"limit", "Catherine::Options::Limit", "HasArg"),
146                  ('n',"no-action","Catherine::Options::No-Action"),
147                  ('v',"verbose","Catherine::Options::Verbose")];
148
149     apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
150     Options = Cnf.SubTree("Catherine::Options")
151
152     if Options["Help"]:
153         usage();
154
155     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
156     db_access.init(Cnf, projectB);
157
158     if not Options["Limit"]:
159         limit = -1;
160     else:
161         limit = int(Options["Limit"]) * 1024;
162
163     # -n/--no-action implies -v/--verbose
164     if Options["No-Action"]:
165         Options["Verbose"] = "true";
166
167     # Sanity check the limit argument
168     if limit > 0 and limit < 1024:
169         utils.fubar("-l/--limit takes an argument with a value in kilobytes.");
170
171     # Grab a list of all files not already in the pool
172     q = projectB.query("""
173 SELECT l.path, f.filename, f.id as files_id, c.name as component
174    FROM files f, location l, component c WHERE
175     NOT EXISTS (SELECT * FROM location l WHERE l.type = 'pool' AND f.location = l.id)
176     AND NOT (f.filename ~ '^potato') AND f.location = l.id AND l.component = c.id
177 UNION SELECT l.path, f.filename, f.id as files_id, null as component
178    FROM files f, location l WHERE
179     NOT EXISTS (SELECT * FROM location l WHERE l.type = 'pool' AND f.location = l.id)
180     AND NOT (f.filename ~ '^potato') AND f.location = l.id AND NOT EXISTS
181      (SELECT l.path FROM location l WHERE l.component IS NOT NULL AND f.location = l.id);""");
182
183     poolize(q, limit, Options["Verbose"], Options["No-Action"]);
184
185 #######################################################################################
186
187 if __name__ == '__main__':
188     main()
189