]> git.decadent.org.uk Git - dak.git/blob - dak/make_changelog.py
Define changelogs export path into projectb
[dak.git] / dak / make_changelog.py
1 #!/usr/bin/env python
2
3 """
4 Generate changelog entry between two suites
5
6 @contact: Debian FTP Master <ftpmaster@debian.org>
7 @copyright: 2010 Luca Falavigna <dktrkranz@debian.org>
8 @license: GNU General Public License version 2 or later
9 """
10
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
25 ################################################################################
26
27 # <bdefreese> !dinstall
28 # <dak> bdefreese: I guess the next dinstall will be in 0hr 1min 35sec
29 # <bdefreese> Wow I have great timing
30 # <DktrKranz> dating with dinstall, part II
31 # <bdefreese> heh
32 # <Ganneff> dating with that monster? do you have good combat armor?
33 # <bdefreese> +5 Plate :)
34 # <Ganneff> not a good one then
35 # <Ganneff> so you wont even manage to bypass the lesser monster in front, unchecked
36 # <DktrKranz> asbesto belt
37 # <Ganneff> helps only a step
38 # <DktrKranz> the Ultimate Weapon: cron_turned_off
39 # <bdefreese> heh
40 # <Ganneff> thats debadmin limited
41 # <Ganneff> no option for you
42 # <DktrKranz> bdefreese: it seems ftp-masters want dinstall to sexual harass us, are you good in running?
43 # <Ganneff> you can run but you can not hide
44 # <bdefreese> No, I'm old and fat :)
45 # <Ganneff> you can roll but you can not hide
46 # <Ganneff> :)
47 # <bdefreese> haha
48 # <DktrKranz> damn dinstall, you racist bastard
49
50 ################################################################################
51
52 import os
53 import sys
54 import apt_pkg
55 from commands import getstatusoutput
56 from glob import glob
57 from re import split
58 from shutil import rmtree
59 from daklib.dbconn import *
60 from daklib import utils
61 from daklib.config import Config
62
63 ################################################################################
64
65 def usage (exit_code=0):
66     print """Generate changelog between two suites
67
68        Usage:
69        make-changelog -s <suite> -b <base_suite> [OPTION]...
70        make-changelog -e
71        make-changelog -T
72
73 Options:
74
75   -h, --help                show this help and exit
76   -s, --suite               suite providing packages to compare
77   -b, --base-suite          suite to be taken as reference for comparison
78   -n, --binnmu              display binNMUs uploads instead of source ones
79
80   -e, --export              export interesting files from source packages
81
82   -T, --testing             display changes entering testing"""
83
84     sys.exit(exit_code)
85
86 def get_source_uploads(suite, base_suite, session):
87     """
88     Returns changelogs for source uploads where version is newer than base.
89     """
90
91     query = """WITH base AS (
92                  SELECT source, max(version) AS version
93                  FROM source_suite
94                  WHERE suite_name = :base_suite
95                  GROUP BY source
96                  UNION (SELECT source, CAST(0 AS debversion) AS version
97                  FROM source_suite
98                  WHERE suite_name = :suite
99                  EXCEPT SELECT source, CAST(0 AS debversion) AS version
100                  FROM source_suite
101                  WHERE suite_name = :base_suite
102                  ORDER BY source)),
103                cur_suite AS (
104                  SELECT source, max(version) AS version
105                  FROM source_suite
106                  WHERE suite_name = :suite
107                  GROUP BY source)
108                SELECT DISTINCT c.source, c.version, c.changelog
109                FROM changelogs c
110                JOIN base b ON b.source = c.source
111                JOIN cur_suite cs ON cs.source = c.source
112                WHERE c.version > b.version
113                AND c.version <= cs.version
114                AND c.architecture LIKE '%source%'
115                ORDER BY c.source, c.version DESC"""
116
117     return session.execute(query, {'suite': suite, 'base_suite': base_suite})
118
119 def get_binary_uploads(suite, base_suite, session):
120     """
121     Returns changelogs for binary uploads where version is newer than base.
122     """
123
124     query = """WITH base as (
125                  SELECT s.source, max(b.version) AS version, a.arch_string
126                  FROM source s
127                  JOIN binaries b ON b.source = s.id
128                  JOIN bin_associations ba ON ba.bin = b.id
129                  JOIN architecture a ON a.id = b.architecture
130                  WHERE ba.suite = (
131                    SELECT id
132                    FROM suite
133                    WHERE suite_name = :base_suite)
134                  GROUP BY s.source, a.arch_string),
135                cur_suite as (
136                  SELECT s.source, max(b.version) AS version, a.arch_string
137                  FROM source s
138                  JOIN binaries b ON b.source = s.id
139                  JOIN bin_associations ba ON ba.bin = b.id
140                  JOIN architecture a ON a.id = b.architecture
141                  WHERE ba.suite = (
142                    SELECT id
143                    FROM suite
144                    WHERE suite_name = :suite)
145                  GROUP BY s.source, a.arch_string)
146                SELECT DISTINCT c.source, c.version, c.architecture, c.changelog
147                FROM changelogs c
148                JOIN base b on b.source = c.source
149                JOIN cur_suite cs ON cs.source = c.source
150                WHERE c.version > b.version
151                AND c.version <= cs.version
152                AND c.architecture = b.arch_string
153                AND c.architecture = cs.arch_string
154                ORDER BY c.source, c.version DESC, c.architecture"""
155
156     return session.execute(query, {'suite': suite, 'base_suite': base_suite})
157
158 def testing_summary(summary, session):
159     """
160     Returns changes introduced in packages entering testing.
161     """
162
163     query =  'SELECT source, changelog FROM changelogs WHERE'
164     fd = open(summary, 'r')
165     for package in fd.read().splitlines():
166         package = package.split()
167         if package[1] != package[2]:
168             if package[1] == '(not_in_testing)':
169                 package[1] = 0
170             query += " source = '%s' AND version > '%s' AND version <= '%s'" \
171                      % (package[0], package[1], package[2])
172             query += " AND architecture LIKE '%source%' OR"
173     fd.close()
174     query += ' False ORDER BY source, version DESC;'
175
176     return session.execute(query)
177
178 def display_changes(uploads, index):
179     prev_upload = None
180     for upload in uploads:
181         if prev_upload and prev_upload != upload[0]:
182             print
183         print upload[index]
184         prev_upload = upload[0]
185
186 def export_files(session, pool, clpool, temppath):
187     """
188     Export interesting files from source packages.
189     """
190
191     sources = {}
192     query = """SELECT s.source, su.suite_name AS suite, s.version, f.filename
193                FROM source s
194                JOIN src_associations sa ON sa.source = s.id
195                JOIN suite su ON su.id = sa.suite
196                JOIN files f ON f.id = s.file
197                ORDER BY s.source, suite"""
198
199     for p in session.execute(query):
200         if not sources.has_key(p[0]):
201             sources[p[0]] = {}
202         sources[p[0]][p[1]] = (p[2], p[3])
203
204     tempdir = utils.temp_dirname(parent=temppath)
205     os.rmdir(tempdir)
206
207     for p in sources.keys():
208         for s in sources[p].keys():
209             files = (('changelog', True),
210                      ('copyright', True),
211                      ('NEWS.Debian', False),
212                      ('README.Debian', False))
213             path = os.path.join(clpool, sources[p][s][1].split('/')[0], \
214                                 split('(^lib\S|^\S)', p)[1], p)
215             if not os.path.exists(path):
216                 os.makedirs(path)
217             for file in files:
218                 for f in glob(os.path.join(path, s + '.*')):
219                     os.unlink(f)
220             try:
221                 for file in files:
222                     t = os.path.join(path, '%s_%s.*%s' % (p, sources[p][s][0], file[0]))
223                     if file[1] and not glob(t):
224                         raise OSError
225                     else:
226                         for f in glob(t):
227                             os.link(f, os.path.join(path, '%s.%s' % \
228                                     (s, os.path.basename(f).split('%s_%s.' \
229                                     % (p, sources[p][s][0]))[1])))
230             except OSError:
231                 cmd = 'dpkg-source --no-check --no-copy -x %s %s' \
232                       % (os.path.join(pool, sources[p][s][1]), tempdir)
233                 (result, output) = getstatusoutput(cmd)
234                 if not result:
235                     for file in files:
236                         try:
237                             for f in glob(os.path.join(tempdir, 'debian', '*' + file[0])):
238                                 for dest in os.path.join(path, '%s_%s.%s' \
239                                             % (p, sources[p][s][0], os.path.basename(f))), \
240                                             os.path.join(path, '%s.%s' % (s, os.path.basename(f))):
241                                     if not os.path.exists(dest):
242                                         os.link(f, dest)
243                         except:
244                             print 'make-changelog: unable to extract %s for %s_%s' \
245                                    % (os.path.basename(f), p, sources[p][s][0])
246                 else:
247                     print 'make-changelog: unable to unpack %s_%s' % (p, sources[p][s][0])
248                     continue
249
250                 rmtree(tempdir)
251
252     for root, dirs, files in os.walk(clpool):
253         if len(files):
254             if root.split('/')[-1] not in sources.keys():
255                 if os.path.exists(root):
256                     rmtree(root)
257             for file in files:
258                 if os.path.exists(os.path.join(root, file)):
259                     if os.stat(os.path.join(root, file)).st_nlink ==  1:
260                         os.unlink(os.path.join(root, file))
261
262 def main():
263     Cnf = utils.get_conf()
264     cnf = Config()
265     Arguments = [('h','help','Make-Changelog::Options::Help'),
266                  ('s','suite','Make-Changelog::Options::Suite','HasArg'),
267                  ('b','base-suite','Make-Changelog::Options::Base-Suite','HasArg'),
268                  ('n','binnmu','Make-Changelog::Options::binNMU'),
269                  ('e','export','Make-Changelog::Options::export'),
270                  ('T', 'testing','Make-Changelog::Options::Testing')]
271
272     for i in ['help', 'suite', 'base-suite', 'binnmu', 'export', 'testing']:
273         if not Cnf.has_key('Make-Changelog::Options::%s' % (i)):
274             Cnf['Make-Changelog::Options::%s' % (i)] = ''
275
276     apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv)
277     Options = Cnf.SubTree('Make-Changelog::Options')
278     suite = Cnf['Make-Changelog::Options::Suite']
279     base_suite = Cnf['Make-Changelog::Options::Base-Suite']
280     binnmu = Cnf['Make-Changelog::Options::binNMU']
281     export = Cnf['Make-Changelog::Options::export']
282     testing = Cnf['Make-Changelog::Options::Testing']
283
284     if Options['help'] or not (suite and base_suite) and not testing and not export:
285         usage()
286
287     for s in suite, base_suite:
288         if not testing and not export and not get_suite(s):
289             utils.fubar('Invalid suite "%s"' % s)
290
291     session = DBConn().session()
292
293     if testing:
294         display_changes(testing_summary(Cnf['Changelogs::Testing'], session), 1)
295     elif export:
296         if cnf.exportpath:
297             export_files(session, Cnf['Dir::Pool'], cnf.exportpath, Cnf['Dir::TempPath'])
298         else:
299             utils.fubar('No changelog export path defined')
300     elif binnmu:
301         display_changes(get_binary_uploads(suite, base_suite, session), 3)
302     else:
303         display_changes(get_source_uploads(suite, base_suite, session), 2)
304
305     session.commit()
306
307 if __name__ == '__main__':
308     main()