3 Create all the contents files
5 @contact: Debian FTPMaster <ftpmaster@debian.org>
6 @copyright: 2008, 2009 Michael Casadevall <mcasadevall@debian.org>
7 @copyright: 2009 Mike O'Connor <stew@debian.org>
8 @license: GNU General Public License version 2 or later
11 ################################################################################
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 ################################################################################
29 # <Ganneff> there is the idea to slowly replace contents files
30 # <Ganneff> with a new generation of such files.
31 # <Ganneff> having more info.
33 # <Ganneff> of course that wont help for now where we need to generate them :)
35 ################################################################################
43 from daklib import utils
44 from daklib.binary import Binary
45 from daklib.config import Config
46 from daklib.dbconn import DBConn
47 ################################################################################
49 def usage (exit_code=0):
50 print """Usage: dak contents [options] command [arguments]
54 generate Contents-$arch.gz files
57 scan the debs in the existing pool and load contents in the the database
60 remove files/paths which are no longer referenced by a binary
64 show this help and exit
67 show verbose information messages
70 supress all output but errors
72 -s, --suite={stable,testing,unstable,...}
73 only operate on a single suite
75 -a, --arch={i386,amd64}
76 only operate on a single architecture
80 ################################################################################
82 # where in dak.conf all of our configuration will be stowed
84 options_prefix = "Contents"
85 options_prefix = "%s::Options" % options_prefix
87 log = logging.getLogger()
89 ################################################################################
91 # get all the arches delivered for a given suite
92 # this should probably exist somehere common
93 arches_q = """PREPARE arches_q(int) as
94 SELECT s.architecture, a.arch_string
95 FROM suite_architectures s
96 JOIN architecture a ON (s.architecture=a.id)
99 # find me the .deb for a given binary id
100 debs_q = """PREPARE debs_q(int, int) as
101 SELECT b.id, f.filename FROM bin_assoc_by_arch baa
102 JOIN binaries b ON baa.bin=b.id
103 JOIN files f ON b.file=f.id
107 # ask if we already have contents associated with this binary
108 olddeb_q = """PREPARE olddeb_q(int) as
109 SELECT 1 FROM content_associations
110 WHERE binary_pkg = $1
113 # find me all of the contents for a given .deb
114 contents_q = """PREPARE contents_q(int,int,int,int) as
115 SELECT (p.path||'/'||n.file) AS fn,
116 comma_separated_list(s.section||'/'||b.package)
117 FROM content_associations c
118 JOIN content_file_paths p ON (c.filepath=p.id)
119 JOIN content_file_names n ON (c.filename=n.id)
120 JOIN binaries b ON (b.id=c.binary_pkg)
121 JOIN bin_associations ba ON (b.id=ba.bin)
122 JOIN override o ON (o.package=b.package)
123 JOIN section s ON (s.id=o.section)
124 WHERE (b.architecture = $1 OR b.architecture = $2)
132 # find me all of the contents for a given .udeb
133 udeb_contents_q = """PREPARE udeb_contents_q(int,int,int) as
134 SELECT (p.path||'/'||n.file) as fn,
135 comma_separated_list(s.section||'/'||b.package)
136 FROM content_associations c
137 JOIN content_file_paths p ON (c.filepath=p.id)
138 JOIN content_file_names n ON (c.filename=n.id)
139 JOIN binaries b ON (b.id=c.binary_pkg)
140 JOIN bin_associations ba ON (b.id=ba.bin)
141 JOIN override o ON (o.package=b.package)
142 JOIN section s ON (s.id=o.section)
151 # clear out all of the temporarily stored content associations
152 # this should be run only after p-a has run. after a p-a
153 # run we should have either accepted or rejected every package
154 # so there should no longer be anything in the queue
155 remove_pending_contents_cruft_q = """DELETE FROM pending_content_associations"""
157 # delete any filenames we are storing which have no binary associated with them
158 remove_filename_cruft_q = """DELETE FROM content_file_names
159 WHERE id IN (SELECT cfn.id FROM content_file_names cfn
160 LEFT JOIN content_associations ca
161 ON ca.filename=cfn.id
162 WHERE ca.id IS NULL)"""
164 # delete any paths we are storing which have no binary associated with them
165 remove_filepath_cruft_q = """DELETE FROM content_file_paths
166 WHERE id IN (SELECT cfn.id FROM content_file_paths cfn
167 LEFT JOIN content_associations ca
168 ON ca.filepath=cfn.id
169 WHERE ca.id IS NULL)"""
170 class Contents(object):
172 Class capable of generating Contents-$arch.gz files
174 Usage GenerateContents().generateContents( ["main","contrib","non-free"] )
180 def reject(self, message):
181 log.error("E: %s" % message)
183 def _getHeader(self):
185 Internal method to return the header for Contents.gz files
187 This is boilerplate which explains the contents of the file and how
190 if self.header == None:
191 if Config().has_key("Contents::Header"):
193 h = open(os.path.join( Config()["Dir::Templates"],
194 Config()["Contents::Header"] ), "r")
195 self.header = h.read()
198 log.error( "error opening header file: %d\n%s" % (Config()["Contents::Header"],
199 traceback.format_exc() ))
206 # goal column for section column
209 def _write_content_file(self, cursor, filename):
211 Internal method for writing all the results to a given file.
212 The cursor should have a result set generated from a query already.
214 filepath = Config()["Contents::Root"] + filename
215 filedir = os.path.dirname(filepath)
216 if not os.path.isdir(filedir):
218 f = gzip.open(filepath, "w")
220 header = self._getHeader()
226 contents = cursor.fetchone()
231 int(math.ceil((self._goal_column - len(contents[0])-1) / 8)))
232 f.write(contents[0] + ( '\t' * num_tabs ) + contents[-1] + "\n")
239 remove files/paths from the DB which are no longer referenced
240 by binaries and clean the temporary table
242 cursor = DBConn().cursor();
243 cursor.execute( "BEGIN WORK" )
244 cursor.execute( remove_pending_contents_cruft_q )
245 cursor.execute( remove_filename_cruft_q )
246 cursor.execute( remove_filepath_cruft_q )
247 cursor.execute( "COMMIT" )
252 scan the existing debs in the pool to populate the contents database tables
254 pooldir = Config()[ 'Dir::Pool' ]
256 cursor = DBConn().cursor();
257 cursor.execute( debs_q )
258 cursor.execute( olddeb_q )
259 cursor.execute( arches_q )
261 suites = self._suites()
262 for suite in [i.lower() for i in suites]:
263 suite_id = DBConn().get_suite_id(suite)
265 arch_list = self._arches(cursor, suite_id)
266 arch_all_id = DBConn().get_architecture_id("all")
267 for arch_id in arch_list:
268 cursor.execute( "EXECUTE debs_q(%d, %d)" % ( suite_id, arch_id[0] ) )
272 deb = cursor.fetchone()
276 cursor1 = DBConn().cursor();
277 cursor1.execute( "EXECUTE olddeb_q(%d)" % (deb[0] ) )
278 old = cursor1.fetchone()
280 log.debug( "already imported: %s" % deb[1] )
282 debfile = os.path.join( pooldir, deb[1] )
283 if os.path.exists( debfile ):
284 Binary(debfile, self.reject).scan_package( deb[0] )
286 log.error( "missing .deb: %s" % deb[1] )
290 Generate Contents-$arch.gz files for every available arch in each given suite.
292 cursor = DBConn().cursor();
294 cursor.execute( arches_q )
295 cursor.execute( contents_q )
296 cursor.execute( udeb_contents_q )
298 suites = self._suites()
300 # Get our suites, and the architectures
301 for suite in [i.lower() for i in suites]:
302 suite_id = DBConn().get_suite_id(suite)
303 arch_list = self._arches(cursor, suite_id)
305 arch_all_id = DBConn().get_architecture_id("all")
307 for arch_id in arch_list:
308 cursor.execute( "EXECUTE contents_q(%d,%d,%d,%d)" % (arch_id[0], arch_all_id, suite_id, suite_id))
309 self._write_content_file(cursor, "dists/%s/Contents-%s.gz" % (suite, arch_id[1]))
311 # The MORE fun part. Ok, udebs need their own contents files, udeb, and udeb-nf (not-free)
312 # This is HORRIBLY debian specific :-/
313 for section_id, fn_pattern in [("debian-installer","dists/%s/Contents-udeb.gz"),
314 ("non-free/debian-installer", "dists/%s/Contents-udeb-nf.gz")]:
316 section_id = DBConn().get_section_id(section_id) # all udebs should be here)
318 cursor.execute("EXECUTE udeb_contents_q(%d,%d,%d)" % (section_id, suite_id, suite_id))
319 self._write_content_file(cursor, fn_pattern % suite)
322 ################################################################################
326 return a list of suites to operate on
328 if Config().has_key( "%s::%s" %(options_prefix,"Suite")):
329 suites = utils.split_args(Config()[ "%s::%s" %(options_prefix,"Suite")])
331 suites = Config().SubTree("Suite").List()
335 def _arches(self, cursor, suite):
337 return a list of archs to operate on
340 if Config().has_key( "%s::%s" %(options_prefix,"Arch")):
341 archs = utils.split_args(Config()[ "%s::%s" %(options_prefix,"Arch")])
342 for arch_name in archs:
343 arch_list.append((DBConn().get_architecture_id(arch_name), arch_name))
345 cursor.execute("EXECUTE arches_q(%d)" % (suite))
347 r = cursor.fetchone()
351 if r[1] != "source" and r[1] != "all":
352 arch_list.append((r[0], r[1]))
356 ################################################################################
361 arguments = [('h',"help", "%s::%s" % (options_prefix,"Help")),
362 ('s',"suite", "%s::%s" % (options_prefix,"Suite"),"HasArg"),
363 ('q',"quiet", "%s::%s" % (options_prefix,"Quiet")),
364 ('v',"verbose", "%s::%s" % (options_prefix,"Verbose")),
365 ('a',"arch", "%s::%s" % (options_prefix,"Arch"),"HasArg"),
368 commands = {'generate' : Contents.generate,
369 'bootstrap' : Contents.bootstrap,
370 'cruft' : Contents.cruft,
374 if cnf.has_key("%s::%s" % (options_prefix,"Quiet")):
377 elif cnf.has_key("%s::%s" % (options_prefix,"Verbose")):
381 logging.basicConfig( level=level,
382 format='%(asctime)s %(levelname)s %(message)s',
383 stream = sys.stderr )
385 args = apt_pkg.ParseCommandLine(cnf.Cnf, arguments,sys.argv)
387 if (len(args) < 1) or not commands.has_key(args[0]):
390 if cnf.has_key("%s::%s" % (options_prefix,"Help")):
393 commands[args[0]](Contents())
395 if __name__ == '__main__':