]> git.decadent.org.uk Git - dak.git/blob - dak/generate_packages_sources2.py
Merge remote branch 'mhy/per-suite-signing-keys'
[dak.git] / dak / generate_packages_sources2.py
1 #!/usr/bin/python
2
3 """
4 Generate Packages/Sources files
5
6 @contact: Debian FTP Master <ftpmaster@debian.org>
7 @copyright: 2011  Ansgar Burchardt <ansgar@debian.org>
8 @copyright: Based on daklib/lists.py and dak/generate_filelist.py:
9             2009-2011  Torsten Werner <twerner@debian.org>
10 @copyright: Based on dak/generate_packages_sources.py:
11             2000, 2001, 2002, 2006  James Troup <james@nocrew.org>
12             2009  Mark Hymers <mhy@debian.org>
13             2010  Joerg Jaspert <joerg@debian.org>
14 @license: GNU General Public License version 2 or later
15 """
16
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26
27 # You should have received a copy of the GNU General Public License
28 # along with this program; if not, write to the Free Software
29 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30
31 from daklib.dbconn import *
32 from daklib.config import Config
33 from daklib import utils, daklog
34 from multiprocessing import Pool
35 import apt_pkg, os, stat, sys
36
37 def usage():
38     print """Usage: dak generate-packages-sources2 [OPTIONS]
39 Generate the Packages/Sources files
40
41   -s, --suite=SUITE            process this suite
42                                Default: All suites not marked 'untouchable'
43   -f, --force                  Allow processing of untouchable suites
44                                CAREFUL: Only to be used at point release time!
45   -h, --help                   show this help and exit
46
47 SUITE can be a space seperated list, e.g.
48    --suite=unstable testing
49 """
50     sys.exit()
51
52 #############################################################################
53
54 # Here be dragons.
55 _sources_query = R"""
56 SELECT
57
58   (SELECT
59      STRING_AGG(
60        CASE
61          WHEN key = 'Source' THEN 'Package\: '
62          WHEN key = 'Files' THEN E'Files\:\n ' || f.md5sum || ' ' || f.size || ' ' || SUBSTRING(f.filename FROM E'/([^/]*)\\Z')
63          WHEN key = 'Checksums-Sha1' THEN E'Checksums-Sha1\:\n ' || f.sha1sum || ' ' || f.size || ' ' || SUBSTRING(f.filename FROM E'/([^/]*)\\Z')
64          WHEN key = 'Checksums-Sha256' THEN E'Checksums-Sha256\:\n ' || f.sha256sum || ' ' || f.size || ' ' || SUBSTRING(f.filename FROM E'/([^/]*)\\Z')
65          ELSE key || '\: '
66        END || value, E'\n' ORDER BY mk.ordering, mk.key)
67    FROM
68      source_metadata sm
69      JOIN metadata_keys mk ON mk.key_id = sm.key_id
70    WHERE s.id=sm.src_id
71   )
72   ||
73   E'\nDirectory\: pool/' || SUBSTRING(f.filename FROM E'\\A(.*)/[^/]*\\Z')
74   ||
75   E'\nPriority\: ' || pri.priority
76   ||
77   E'\nSection\: ' || sec.section
78
79 FROM
80
81 source s
82 JOIN src_associations sa ON s.id = sa.source
83 JOIN files f ON s.file=f.id
84 JOIN override o ON o.package = s.source
85 JOIN section sec ON o.section = sec.id
86 JOIN priority pri ON o.priority = pri.id
87
88 WHERE
89   sa.suite = :suite
90   AND o.suite = :suite AND o.component = :component AND o.type = :dsc_type
91
92 ORDER BY
93 s.source, s.version
94 """
95
96 def open_sources(suite, component):
97     cnf = Config()
98     dest = os.path.join(cnf['Dir::Root'], 'dists', suite.suite_name, component.component_name, 'source', 'Sources')
99
100     # create queue if it does not exist yet
101     if os.path.exists(dest) and os.path.isdir(dest):
102         dest_dir = dest
103     else:
104         dest_dir = os.path.dirname(dest)
105     if not os.path.exists(dest_dir):
106         umask = os.umask(00000)
107         os.makedirs(dest_dir, 02775)
108         os.umask(umask)
109
110     f = open(dest, 'w')
111     return f
112
113 def generate_sources(suite_id, component_id):
114     global _sources_query
115
116     session = DBConn().session()
117     dsc_type = session.query(OverrideType).filter_by(overridetype='dsc').one().overridetype_id
118
119     suite = session.query(Suite).get(suite_id)
120     component = session.query(Component).get(component_id)
121
122     output = open_sources(suite, component)
123
124     # run query and write Sources
125     r = session.execute(_sources_query, {"suite": suite_id, "component": component_id, "dsc_type": dsc_type})
126     for (stanza,) in r:
127         print >>output, stanza
128         print >>output, ""
129
130     return ["generate sources", suite.suite_name, component.component_name]
131
132 #############################################################################
133
134 # Here be large dragons.
135 _packages_query = R"""
136 WITH
137
138   tmp AS (
139     SELECT
140       b.id AS binary_id,
141       b.package AS package,
142       b.version AS version,
143       b.architecture AS architecture,
144       b.source AS source_id,
145       s.source AS source,
146       f.filename AS filename,
147       f.size AS size,
148       f.md5sum AS md5sum,
149       f.sha1sum AS sha1sum,
150       f.sha256sum AS sha256sum
151     FROM
152       binaries b
153       JOIN bin_associations ba ON b.id = ba.bin
154       JOIN files f ON f.id = b.file
155       JOIN location l ON l.id = f.location
156       JOIN source s ON b.source = s.id
157     WHERE
158       (b.architecture = :arch_all OR b.architecture = :arch) AND b.type = :type_name
159       AND ba.suite = :suite
160       AND l.component = :component
161   )
162
163 SELECT
164   (SELECT
165      STRING_AGG(key || '\: ' || value, E'\n' ORDER BY mk.ordering, mk.key)
166    FROM
167      binaries_metadata bm
168      JOIN metadata_keys mk ON mk.key_id = bm.key_id
169    WHERE
170      bm.bin_id = tmp.binary_id
171      AND key != 'Section' AND key != 'Priority'
172   )
173   || E'\nSection\: ' || sec.section
174   || E'\nPriority\: ' || pri.priority
175   || E'\nFilename\: pool/' || tmp.filename
176   || E'\nSize\: ' || tmp.size
177   || E'\nMD5sum\: ' || tmp.md5sum
178   || E'\nSHA1\: ' || tmp.sha1sum
179   || E'\nSHA256\: ' || tmp.sha256sum
180
181 FROM
182   tmp
183   JOIN override o ON o.package = tmp.package
184   JOIN section sec ON sec.id = o.section
185   JOIN priority pri ON pri.id = o.priority
186
187 WHERE
188   (
189       architecture <> :arch_all
190     OR
191       (architecture = :arch_all AND source_id IN (SELECT source_id FROM tmp WHERE architecture <> :arch_all))
192     OR
193       (architecture = :arch_all AND source NOT IN (SELECT DISTINCT source FROM tmp WHERE architecture <> :arch_all))
194   )
195   AND
196     o.type = :type_id AND o.suite = :suite AND o.component = :component
197
198 ORDER BY tmp.package, tmp.version
199 """
200
201 def open_packages(suite, component, architecture, type_name):
202     cnf = Config()
203     if type_name == 'udeb':
204         dest = os.path.join(cnf['Dir::Root'], 'dists', suite.suite_name, component.component_name, 'debian-installer', 'binary-%s' % architecture.arch_string, 'Packages')
205     else:
206         dest = os.path.join(cnf['Dir::Root'], 'dists', suite.suite_name, component.component_name, 'binary-%s' % architecture.arch_string, 'Packages')
207
208     # create queue if it does not exist yet
209     if os.path.exists(dest) and os.path.isdir(dest):
210         dest_dir = dest
211     else:
212         dest_dir = os.path.dirname(dest)
213     if not os.path.exists(dest_dir):
214         umask = os.umask(00000)
215         os.makedirs(dest_dir, 02775)
216         os.umask(umask)
217
218     f = open(dest, 'w')
219     return f
220
221 def generate_packages(suite_id, component_id, architecture_id, type_name):
222     global _packages_query
223
224     session = DBConn().session()
225     arch_all_id = session.query(Architecture).filter_by(arch_string='all').one().arch_id
226     type_id = session.query(OverrideType).filter_by(overridetype=type_name).one().overridetype_id
227
228     suite = session.query(Suite).get(suite_id)
229     component = session.query(Component).get(component_id)
230     architecture = session.query(Architecture).get(architecture_id)
231
232     output = open_packages(suite, component, architecture, type_name)
233
234     r = session.execute(_packages_query, {"suite": suite_id, "component": component_id,
235         "arch": architecture_id, "type_id": type_id, "type_name": type_name, "arch_all": arch_all_id})
236     for (stanza,) in r:
237         print >>output, stanza
238         print >>output, ""
239
240     session.close()
241
242     return ["generate-packages", suite.suite_name, component.component_name, architecture.arch_string]
243
244 #############################################################################
245
246 def main():
247     cnf = Config()
248
249     Arguments = [('h',"help","Generate-Packages-Sources::Options::Help"),
250                  ('s',"suite","Generate-Packages-Sources::Options::Suite"),
251                  ('f',"force","Generate-Packages-Sources::Options::Force")]
252
253     suite_names = apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv)
254     try:
255         Options = cnf.SubTree("Generate-Packages-Sources::Options")
256     except KeyError:
257         Options = {}
258
259     if Options.has_key("Help"):
260         usage()
261
262     logger = daklog.Logger(cnf, 'generate-packages-sources2')
263
264     session = DBConn().session()
265
266     if Options.has_key("Suite"):
267         suites = []
268         for s in suite_names:
269             suite = get_suite(s.lower(), session)
270             if suite:
271                 suites.append(suite)
272             else:
273                 print "I: Cannot find suite %s" % s
274                 logger.log(['Cannot find suite %s' % s])
275     else:
276         suites = session.query(Suite).filter(Suite.untouchable == False).all()
277
278     force = Options.has_key("Force") and Options["Force"]
279
280     component_ids = [ c.component_id for c in session.query(Component).all() ]
281
282     def log(details):
283         logger.log(details)
284
285     pool = Pool()
286     for s in suites:
287         if s.untouchable and not force:
288             utils.fubar("Refusing to touch %s (untouchable and not forced)" % s.suite_name)
289         for c in component_ids:
290             pool.apply_async(generate_sources, [s.suite_id, c], callback=log)
291             for a in s.architectures:
292                 #pool.apply_async(generate_packages, [s.suite_id, c, a.arch_id, 'deb'], callback=log)
293                 apply(generate_packages, [s.suite_id, c, a.arch_id, 'deb'])
294                 #pool.apply_async(generate_packages, [s.suite_id, c, a.arch_id, 'udeb'], callback=log)
295
296     pool.close()
297     pool.join()
298     # this script doesn't change the database
299     session.close()
300
301 if __name__ == '__main__':
302     main()