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