2 # Import contents files
4 # Copyright (C) 2008, 2009 Michael Casadevall <mcasadevall@debian.org>
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.
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.
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
20 ################################################################################
21 ################################################################################
23 ################################################################################
25 import sys, os, popen2, tempfile, stat, time, pg
26 import re, gzip, apt_pkg
27 from daklib import database, utils
28 from daklib.dak_exceptions import *
30 ################################################################################
37 ################################################################################
39 def usage (exit_code=0):
40 print """Usage: dak import-contents
43 -h, --help show this help and exit
44 -s, --suite=SUITE only write file lists for this suite
48 ################################################################################
50 def import_contents(suites):
54 projectB.query("BEGIN WORK")
56 # Needed to make sure postgreSQL doesn't freak out on some of the data
57 projectB.query("SET CLIENT_ENCODING TO 'LATIN1'")
59 # Get our suites, and the architectures
61 suite_id = database.get_suite_id(s)
63 q = projectB.query("SELECT s.architecture, a.arch_string FROM suite_architectures s JOIN architecture a ON (s.architecture=a.id) WHERE suite = '%d'" % suite_id)
66 for r in q.getresult():
67 if r[1] != "source" and r[1] != "all":
68 arch_list.append((r[0], r[1]))
70 arch_all_id = database.get_architecture_id("all")
72 for arch in arch_list:
73 print "Processing %s/%s" % (s, arch[1])
74 arch_id = database.get_architecture_id(arch[1])
75 f = gzip.open(Cnf["Dir::Root"] + "dists/%s/Contents-%s.gz" % (s, arch[1]), "r")
79 num_of_lines = len(lines)
81 # Ok, the file cursor is at the first entry, now comes the fun 'lets parse' bit
86 if found_header == False:
88 print "Unable to find end of Contents-%s.gz header!" % ( arch[1])
92 p = re.compile('^FILE')
97 # The format is simple enough, *filename*, *section/package1,section/package2,etc*
98 # Each file appears once per Contents file, so first, use some regex match
99 # to split the two bits
101 # Print out progress bar
102 print "\rProcessed %d lines of %d (%%%.2f)" % (lines_processed, num_of_lines, (float(lines_processed)/num_of_lines)),
104 # regex lifted from packages.d.o code
105 p = re.compile('^(.+?)\s+(\S+)$')
106 matchs = p.findall(line)
107 filename = matchs[0][0]
108 packages = matchs[0][1].split(',')
110 # Iterate through each file's packages
111 for package in packages:
112 p = re.compile('(\S+)/(\S+)$')
113 matchs = p.findall(package)
115 # Needed since the DB is unicode, and these files
117 section_name = matchs[0][0]
118 package_name = matchs[0][1]
120 section_id = database.get_section_id(section_name)
121 package_id = database.get_latest_binary_version_id(package_name, section_id, suite_id, arch_id)
123 if package_id == None:
124 # Likely got an arch all package
125 package_id = database.get_latest_binary_version_id(package_name, section_id, suite_id, arch_all_id)
127 database.insert_content_path(package_id, filename)
133 print "Committing to database ..."
134 projectB.query("COMMIT")
136 ################################################################################
139 global Cnf, projectB, out
142 Cnf = utils.get_conf()
144 Arguments = [('h',"help","Import-Contents::Options::Help"),
145 ('s',"suite","Import-Contents::Options::Suite","HasArg"),
148 for i in [ "help", "suite" ]:
149 if not Cnf.has_key("Import-Contents::Options::%s" % (i)):
150 Cnf["Import-Contents::Options::%s" % (i)] = ""
152 suites = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
153 Options = Cnf.SubTree("Import-Contents::Options")
159 suites = utils.split_args(Options["Suite"])
161 suites = Cnf.SubTree("Suite").List()
163 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
164 database.init(Cnf, projectB)
166 import_contents(suites)
168 #######################################################################################
170 if __name__ == '__main__':