]> git.decadent.org.uk Git - dak.git/blob - dak/make_maintainers.py
Merge remote-tracking branch 'nthykier/auto-decruft'
[dak.git] / dak / make_maintainers.py
1 #!/usr/bin/env python
2
3 """
4 Generate Maintainers file used by e.g. the Debian Bug Tracking System
5 @contact: Debian FTP Master <ftpmaster@debian.org>
6 @copyright: 2000, 2001, 2002, 2003, 2004, 2006  James Troup <james@nocrew.org>
7 @copyright: 2011 Torsten Werner <twerner@debian.org>
8 @license: GNU General Public License version 2 or later
9
10 """
11
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
26 ################################################################################
27
28 # ``As opposed to "Linux sucks. Respect my academic authoritah, damn
29 #   you!" or whatever all this hot air amounts to.''
30 #                             -- ajt@ in _that_ thread on debian-devel@
31
32 ################################################################################
33
34 from daklib import daklog
35 from daklib import utils
36 from daklib.config import Config
37 from daklib.dbconn import *
38 from daklib.regexes import re_comments
39
40 import apt_pkg
41 import sys
42
43 ################################################################################
44
45 def usage (exit_code=0):
46     print """Usage: dak make-maintainers [OPTION] -a ARCHIVE EXTRA_FILE[...]
47 Generate an index of packages <=> Maintainers / Uploaders.
48
49   -a, --archive=ARCHIVE      archive to take packages from
50   -h, --help                 show this help and exit
51 """
52     sys.exit(exit_code)
53
54 ################################################################################
55
56 def format(package, person):
57     '''Return a string nicely formatted for writing to the output file.'''
58     return '%-20s %s\n' % (package, person)
59
60 ################################################################################
61
62 def uploader_list(source):
63     '''Return a sorted list of uploader names for source package.'''
64     return sorted([uploader.name for uploader in source.uploaders])
65
66 ################################################################################
67
68 def main():
69     cnf = Config()
70
71     Arguments = [('h',"help","Make-Maintainers::Options::Help"),
72                  ('a','archive','Make-Maintainers::Options::Archive','HasArg')]
73     if not cnf.has_key("Make-Maintainers::Options::Help"):
74         cnf["Make-Maintainers::Options::Help"] = ""
75
76     extra_files = apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv)
77     Options = cnf.subtree("Make-Maintainers::Options")
78
79     if Options["Help"] or not Options.get('Archive'):
80         usage()
81
82     Logger = daklog.Logger('make-maintainers')
83     session = DBConn().session()
84
85     archive = session.query(Archive).filter_by(archive_name=Options['Archive']).one()
86
87     # dictionary packages to maintainer names
88     maintainers = dict()
89     # dictionary packages to list of uploader names
90     uploaders = dict()
91
92     source_query = session.query(DBSource).from_statement('''
93         select distinct on (source.source) source.* from source
94             join src_associations sa on source.id = sa.source
95             join suite on sa.suite = suite.id
96             where suite.archive_id = :archive_id
97             order by source.source, source.version desc''') \
98         .params(archive_id=archive.archive_id)
99
100     binary_query = session.query(DBBinary).from_statement('''
101         select distinct on (binaries.package) binaries.* from binaries
102             join bin_associations ba on binaries.id = ba.bin
103             join suite on ba.suite = suite.id
104             where suite.archive_id = :archive_id
105             order by binaries.package, binaries.version desc''') \
106         .params(archive_id=archive.archive_id)
107
108     Logger.log(['sources'])
109     for source in source_query:
110         maintainers[source.source] = source.maintainer.name
111         uploaders[source.source] = uploader_list(source)
112
113     Logger.log(['binaries'])
114     for binary in binary_query:
115         if binary.package not in maintainers:
116             maintainers[binary.package] = binary.maintainer.name
117             uploaders[binary.package] = uploader_list(binary.source)
118
119     Logger.log(['files'])
120     # Process any additional Maintainer files (e.g. from pseudo
121     # packages)
122     for filename in extra_files:
123         extrafile = utils.open_file(filename)
124         for line in extrafile.readlines():
125             line = re_comments.sub('', line).strip()
126             if line == "":
127                 continue
128             (package, maintainer) = line.split(None, 1)
129             maintainers[package] = maintainer
130             uploaders[package] = [maintainer]
131
132     maintainer_file = open('Maintainers', 'w')
133     uploader_file = open('Uploaders', 'w')
134     for package in sorted(uploaders):
135         maintainer_file.write(format(package, maintainers[package]))
136         for uploader in uploaders[package]:
137             uploader_file.write(format(package, uploader))
138     uploader_file.close()
139     maintainer_file.close()
140     Logger.close()
141
142 ################################################################################
143
144 if __name__ == '__main__':
145     main()