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.6 2001-03-02 02:24:33 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 ################################################################################
43 Cnf = apt_pkg.newConfiguration();
44 apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
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")];
53 apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
55 projectB = pg.connect('projectb', 'localhost');
56 db_access.init(Cnf, projectB);
61 # Parse -l/--limit argument
62 if not Cnf["Catherine::Options::Limit"]:
65 limit = int(Cnf["Catherine::Options::Limit"]) * 1024;
67 # -n/--no-action implies -v/--verbose
68 if Cnf["Catherine::Options::No-Action"]:
69 Cnf["Catherine::Options::Verbose"] = "true";
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");
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);""");
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)));
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)
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));
112 version = utils.re_no_epoch.sub('', version);
113 destination_filename = "%s_%s_%s.deb" % (package, version, architecture);
115 m = utils.re_issource.match(base_filename)
119 sys.stderr.write("%s: say what?\n" % (legacy_filename));
121 # Work out the component name
122 component = qid["component"];
124 q = projectB.query("SELECT DISTINCT(c.name) FROM override o, component c WHERE o.package = '%s' AND o.component = c.id;" % (source));
127 sys.stderr.write("%s: No override match so I can't work out the component.\n" % (source));
130 sys.stderr.write("%s: multiple override matches for %s so I can't work out the component.\n" % (source));
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));
137 sys.stderr.write("%s: couldn't determine location ID, query returned %d matches and not 1 as expected.\n" % (source, len(ql)));
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));
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"]));
157 sys.stderr.write("Poolized %s in %s files.\n" % (utils.size_type(poolized_size), poolized_count));
159 #######################################################################################
161 if __name__ == '__main__':