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