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 $
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.
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.
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
21 # "Welcome to where time stands still,
22 # No one leaves and no one will."
23 # - Sanitarium - Metallica / Master of the puppets
25 ################################################################################
27 import os, pg, stat, string, sys
28 import utils, db_access
29 import apt_pkg, apt_inst;
31 ################################################################################
36 # Q is a python-postgresql query result set and must have the
37 # following four columns:
38 # o files.id (as 'files_id')
41 # o component.name (as 'component')
43 # limit is a value in bytes or -1 for no limit (use with care!)
44 # verbose and no_action are booleans
46 def poolize (q, limit, verbose, no_action):
50 # Parse -l/--limit argument
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)));
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)
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));
76 version = utils.re_no_epoch.sub('', version);
77 destination_filename = "%s_%s_%s.deb" % (package, version, architecture);
79 m = utils.re_issource.match(base_filename)
83 sys.stderr.write("%s: say what?\n" % (legacy_filename));
85 # Work out the component name
86 component = qid["component"];
88 q = projectB.query("SELECT DISTINCT(c.name) FROM override o, component c WHERE o.package = '%s' AND o.component = c.id;" % (source));
91 sys.stderr.write("%s: No override match so I can't work out the component.\n" % (source));
94 sys.stderr.write("%s: multiple override matches for %s so I can't work out the component.\n" % (source));
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));
101 sys.stderr.write("%s: couldn't determine location ID, query returned %d matches and not 1 as expected.\n" % (source, len(ql)));
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));
112 print "Moving: %s -> %s" % (legacy_filename, destination);
114 utils.move(legacy_filename, destination);
115 # Then Update the DB's files table
117 print "SQL: UPDATE files SET filename = '%s', location = '%s' WHERE id = '%s'" % (pool_filename, location_id, qid["files_id"]);
119 q = projectB.query("UPDATE files SET filename = '%s', location = '%s' WHERE id = '%s'" % (pool_filename, location_id, qid["files_id"]));
121 sys.stderr.write("Poolized %s in %s files.\n" % (utils.size_type(poolized_size), poolized_count));
123 ################################################################################
126 global Cnf, projectB;
130 Cnf = apt_pkg.newConfiguration();
131 apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
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")];
140 apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
141 Options = Cnf.SubTree("Catherine::Options")
143 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
144 db_access.init(Cnf, projectB);
146 if not Options["Limit"]:
149 limit = int(Options["Limit"]) * 1024;
151 # -n/--no-action implies -v/--verbose
152 if Options["No-Action"]:
153 Options["Verbose"] = "true";
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");
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);""");
172 poolize(q, limit, Options["Verbose"], Options["No-Action"]);
174 #######################################################################################
176 if __name__ == '__main__':