]> git.decadent.org.uk Git - dak.git/blob - dak/generate_packages_sources2.py
dont output tag lines from metadata table, only external overrides
[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 daklib.dakmultiprocessing import DakProcessPool, PROC_STATUS_SUCCESS, PROC_STATUS_SIGNALRAISED
35 from daklib.filewriter import PackagesFileWriter, SourcesFileWriter
36
37 import apt_pkg, os, stat, sys
38
39 def usage():
40     print """Usage: dak generate-packages-sources2 [OPTIONS]
41 Generate the Packages/Sources files
42
43   -s, --suite=SUITE            process this suite
44                                Default: All suites not marked 'untouchable'
45   -f, --force                  Allow processing of untouchable suites
46                                CAREFUL: Only to be used at point release time!
47   -h, --help                   show this help and exit
48
49 SUITE can be a space seperated list, e.g.
50    --suite=unstable testing
51 """
52     sys.exit()
53
54 #############################################################################
55
56 # Here be dragons.
57 _sources_query = R"""
58 SELECT
59
60   (SELECT
61      STRING_AGG(
62        CASE
63          WHEN key = 'Source' THEN 'Package\: '
64          WHEN key = 'Files' THEN E'Files\:\n ' || f.md5sum || ' ' || f.size || ' ' || SUBSTRING(f.filename FROM E'/([^/]*)\\Z')
65          WHEN key = 'Checksums-Sha1' THEN E'Checksums-Sha1\:\n ' || f.sha1sum || ' ' || f.size || ' ' || SUBSTRING(f.filename FROM E'/([^/]*)\\Z')
66          WHEN key = 'Checksums-Sha256' THEN E'Checksums-Sha256\:\n ' || f.sha256sum || ' ' || f.size || ' ' || SUBSTRING(f.filename FROM E'/([^/]*)\\Z')
67          ELSE key || '\: '
68        END || value, E'\n' ORDER BY mk.ordering, mk.key)
69    FROM
70      source_metadata sm
71      JOIN metadata_keys mk ON mk.key_id = sm.key_id
72    WHERE s.id=sm.src_id
73   )
74   ||
75   E'\nDirectory\: pool/' || SUBSTRING(f.filename FROM E'\\A(.*)/[^/]*\\Z')
76   ||
77   E'\nPriority\: ' || pri.priority
78   ||
79   E'\nSection\: ' || sec.section
80
81 FROM
82
83 source s
84 JOIN src_associations sa ON s.id = sa.source
85 JOIN files f ON s.file=f.id
86 JOIN override o ON o.package = s.source
87 JOIN section sec ON o.section = sec.id
88 JOIN priority pri ON o.priority = pri.id
89
90 WHERE
91   sa.suite = :suite
92   AND o.suite = :overridesuite AND o.component = :component AND o.type = :dsc_type
93
94 ORDER BY
95 s.source, s.version
96 """
97
98 def generate_sources(suite_id, component_id):
99     global _sources_query
100
101     session = DBConn().session()
102     dsc_type = session.query(OverrideType).filter_by(overridetype='dsc').one().overridetype_id
103
104     suite = session.query(Suite).get(suite_id)
105     component = session.query(Component).get(component_id)
106
107     overridesuite_id = suite.get_overridesuite().suite_id
108
109     writer = SourcesFileWriter(suite=suite.suite_name, component=component.component_name)
110     output = writer.open()
111
112     # run query and write Sources
113     r = session.execute(_sources_query, {"suite": suite_id, "component": component_id, "dsc_type": dsc_type, "overridesuite": overridesuite_id})
114     for (stanza,) in r:
115         print >>output, stanza
116         print >>output, ""
117
118     writer.close()
119
120     message = ["generate sources", suite.suite_name, component.component_name]
121     session.rollback()
122     return (PROC_STATUS_SUCCESS, message)
123
124 #############################################################################
125
126 # We currently filter out the "Tag" line. They are set by external overrides and
127 # NOT by the maintainer. And actually having it set by maintainer means we output
128 # it twice at the moment -> which breaks dselect.
129 # Here be large dragons.
130 _packages_query = R"""
131 WITH
132
133   tmp AS (
134     SELECT
135       b.id AS binary_id,
136       b.package AS package,
137       b.version AS version,
138       b.architecture AS architecture,
139       b.source AS source_id,
140       s.source AS source,
141       f.filename AS filename,
142       f.size AS size,
143       f.md5sum AS md5sum,
144       f.sha1sum AS sha1sum,
145       f.sha256sum AS sha256sum
146     FROM
147       binaries b
148       JOIN bin_associations ba ON b.id = ba.bin
149       JOIN files f ON f.id = b.file
150       JOIN location l ON l.id = f.location
151       JOIN source s ON b.source = s.id
152     WHERE
153       (b.architecture = :arch_all OR b.architecture = :arch) AND b.type = :type_name
154       AND ba.suite = :suite
155       AND l.component = :component
156   )
157
158 SELECT
159   (SELECT
160      STRING_AGG(key || '\: ' || value, E'\n' ORDER BY mk.ordering, mk.key)
161    FROM
162      binaries_metadata bm
163      JOIN metadata_keys mk ON mk.key_id = bm.key_id
164    WHERE
165      bm.bin_id = tmp.binary_id
166      AND key != 'Section' AND key != 'Priority' AND key != 'Tag'
167   )
168   || COALESCE(E'\n' || (SELECT
169      STRING_AGG(key || '\: ' || value, E'\n' ORDER BY key)
170    FROM external_overrides eo
171    WHERE
172      eo.package = tmp.package
173      AND eo.suite = :overridesuite AND eo.component = :component
174   ), '')
175   || E'\nSection\: ' || sec.section
176   || E'\nPriority\: ' || pri.priority
177   || E'\nFilename\: pool/' || tmp.filename
178   || E'\nSize\: ' || tmp.size
179   || E'\nMD5sum\: ' || tmp.md5sum
180   || E'\nSHA1\: ' || tmp.sha1sum
181   || E'\nSHA256\: ' || tmp.sha256sum
182
183 FROM
184   tmp
185   JOIN override o ON o.package = tmp.package
186   JOIN section sec ON sec.id = o.section
187   JOIN priority pri ON pri.id = o.priority
188
189 WHERE
190   (
191       architecture <> :arch_all
192     OR
193       (architecture = :arch_all AND source_id IN (SELECT source_id FROM tmp WHERE architecture <> :arch_all))
194     OR
195       (architecture = :arch_all AND source NOT IN (SELECT DISTINCT source FROM tmp WHERE architecture <> :arch_all))
196   )
197   AND
198     o.type = :type_id AND o.suite = :overridesuite AND o.component = :component
199
200 ORDER BY tmp.source, tmp.package, tmp.version
201 """
202
203 def generate_packages(suite_id, component_id, architecture_id, type_name):
204     global _packages_query
205
206     session = DBConn().session()
207     arch_all_id = session.query(Architecture).filter_by(arch_string='all').one().arch_id
208     type_id = session.query(OverrideType).filter_by(overridetype=type_name).one().overridetype_id
209
210     suite = session.query(Suite).get(suite_id)
211     component = session.query(Component).get(component_id)
212     architecture = session.query(Architecture).get(architecture_id)
213
214     overridesuite_id = suite.get_overridesuite().suite_id
215
216     writer = PackagesFileWriter(suite=suite.suite_name, component=component.component_name,
217             architecture=architecture.arch_string, debtype=type_name)
218     output = writer.open()
219
220     r = session.execute(_packages_query, {"suite": suite_id, "component": component_id,
221         "arch": architecture_id, "type_id": type_id, "type_name": type_name, "arch_all": arch_all_id,
222         "overridesuite": overridesuite_id})
223     for (stanza,) in r:
224         print >>output, stanza
225         print >>output, ""
226
227     writer.close()
228
229     message = ["generate-packages", suite.suite_name, component.component_name, architecture.arch_string]
230     session.rollback()
231     return (PROC_STATUS_SUCCESS, message)
232
233 #############################################################################
234
235 def main():
236     cnf = Config()
237
238     Arguments = [('h',"help","Generate-Packages-Sources::Options::Help"),
239                  ('s',"suite","Generate-Packages-Sources::Options::Suite"),
240                  ('f',"force","Generate-Packages-Sources::Options::Force")]
241
242     suite_names = apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv)
243     try:
244         Options = cnf.SubTree("Generate-Packages-Sources::Options")
245     except KeyError:
246         Options = {}
247
248     if Options.has_key("Help"):
249         usage()
250
251     logger = daklog.Logger(cnf, 'generate-packages-sources2')
252
253     session = DBConn().session()
254
255     if Options.has_key("Suite"):
256         suites = []
257         for s in suite_names:
258             suite = get_suite(s.lower(), session)
259             if suite:
260                 suites.append(suite)
261             else:
262                 print "I: Cannot find suite %s" % s
263                 logger.log(['Cannot find suite %s' % s])
264     else:
265         suites = session.query(Suite).filter(Suite.untouchable == False).all()
266
267     force = Options.has_key("Force") and Options["Force"]
268
269     component_ids = [ c.component_id for c in session.query(Component).all() ]
270
271     def parse_results(message):
272         # Split out into (code, msg)
273         code, msg = message
274         if code == PROC_STATUS_SUCCESS:
275             logger.log([msg])
276         elif code == PROC_STATUS_SIGNALRAISED:
277             logger.log(['E: Subprocess recieved signal ', msg])
278         else:
279             logger.log(['E: ', msg])
280
281     pool = DakProcessPool()
282     for s in suites:
283         if s.untouchable and not force:
284             utils.fubar("Refusing to touch %s (untouchable and not forced)" % s.suite_name)
285         for c in component_ids:
286             pool.apply_async(generate_sources, [s.suite_id, c], callback=parse_results)
287             for a in s.architectures:
288                 if a == 'source':
289                     continue
290                 pool.apply_async(generate_packages, [s.suite_id, c, a.arch_id, 'deb'], callback=parse_results)
291                 pool.apply_async(generate_packages, [s.suite_id, c, a.arch_id, 'udeb'], callback=parse_results)
292
293     pool.close()
294     pool.join()
295
296     # this script doesn't change the database
297     session.close()
298
299     logger.close()
300
301     sys.exit(pool.overall_status())
302
303 if __name__ == '__main__':
304     main()