]> git.decadent.org.uk Git - dak.git/blob - dak/make_maintainers.py
make_maintainers.py: Support creating an uploaders index, closes: #120262
[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 @license: GNU General Public License version 2 or later
8
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 # ``As opposed to "Linux sucks. Respect my academic authoritah, damn
28 #   you!" or whatever all this hot air amounts to.''
29 #                             -- ajt@ in _that_ thread on debian-devel@
30
31 ################################################################################
32
33 import sys
34 import apt_pkg
35
36 from daklib.config import Config
37 from daklib.dbconn import *
38 from daklib import utils
39 from daklib import textutils
40 from daklib.regexes import re_comments
41
42 ################################################################################
43
44 maintainer_from_source_cache = {}   #: caches the maintainer name <email> per source_id
45 packages = {}                       #: packages data to write out
46 fixed_maintainer_cache = {}         #: caches fixed ( L{daklib.textutils.fix_maintainer} ) maintainer data
47
48 ################################################################################
49
50 def usage (exit_code=0):
51     print """Usage: dak make-maintainers [OPTION] EXTRA_FILE[...]
52 Generate an index of packages <=> Maintainers.
53
54   -u, --uploaders            create uploaders index
55   -h, --help                 show this help and exit
56 """
57     sys.exit(exit_code)
58
59 ################################################################################
60
61 def fix_maintainer (maintainer):
62     """
63     Fixup maintainer entry, cache the result.
64
65     @type maintainer: string
66     @param maintainer: A maintainer entry as passed to L{daklib.textutils.fix_maintainer}
67
68     @rtype: tuple
69     @returns: fixed maintainer tuple
70     """
71     global fixed_maintainer_cache
72
73     if not fixed_maintainer_cache.has_key(maintainer):
74         fixed_maintainer_cache[maintainer] = textutils.fix_maintainer(maintainer)[0]
75
76     return fixed_maintainer_cache[maintainer]
77
78 def get_maintainer(maintainer, session):
79     """
80     Retrieves maintainer name from database, passes it through fix_maintainer and
81     passes on whatever that returns.
82
83     @type maintainer: int
84     @param maintainer: maintainer_id
85     """
86     q = session.execute("SELECT name FROM maintainer WHERE id = :id", {'id': maintainer}).fetchall()
87     return fix_maintainer(q[0][0])
88
89 def get_maintainer_from_source(source_id, session):
90     """
91     Returns maintainer name for given source_id.
92
93     @type source_id: int
94     @param source_id: source package id
95
96     @rtype: string
97     @return: maintainer name/email
98     """
99     global maintainer_from_source_cache
100
101     if not maintainer_from_source_cache.has_key(source_id):
102         q = session.execute("""SELECT m.name FROM maintainer m, source s
103                                 WHERE s.id = :sourceid AND s.maintainer = m.id""",
104                             {'sourceid': source_id})
105         maintainer = q.fetchall()[0][0]
106         maintainer_from_source_cache[source_id] = fix_maintainer(maintainer)
107
108     return maintainer_from_source_cache[source_id]
109
110 ################################################################################
111
112 def main():
113     cnf = Config()
114
115     Arguments = [('h',"help","Make-Maintainers::Options::Help"),
116                  ('u',"uploaders","Make-Maintainers::Options::Uploaders")]
117     if not cnf.has_key("Make-Maintainers::Options::Help"):
118         cnf["Make-Maintainers::Options::Help"] = ""
119     if not cnf.has_key("Make-Maintainers::Options::Uploaders"):
120         cnf["Make-Maintainers::Options::Uploaders"] = ""
121
122     extra_files = apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv)
123     Options = cnf.SubTree("Make-Maintainers::Options")
124
125     if Options["Help"]:
126         usage()
127
128     gen_uploaders = False
129     if Options["Uploaders"]:
130         gen_uploaders = True
131
132     session = DBConn().session()
133
134     for suite in cnf.SubTree("Suite").List():
135         suite = suite.lower()
136         suite_priority = int(cnf["Suite::%s::Priority" % (suite)])
137
138         # Source packages
139         if not gen_uploaders:
140             q = session.execute("""SELECT s.source, s.version, m.name
141                                      FROM src_associations sa, source s, suite su, maintainer m
142                                     WHERE su.suite_name = :suite_name
143                                       AND sa.suite = su.id AND sa.source = s.id
144                                       AND m.id = s.maintainer""",
145                                     {'suite_name': suite})
146         else:
147             q = session.execute("""SELECT s.source, s.version, m.name
148                                      FROM src_associations sa, source s, suite su, maintainer m, src_uploaders srcu
149                                     WHERE su.suite_name = :suite_name
150                                       AND sa.suite = su.id AND sa.source = s.id
151                                       AND m.id = srcu.maintainer
152                                       AND srcu.source = s.id""",
153                                     {'suite_name': suite})
154
155         for source in q.fetchall():
156             package = source[0]
157             version = source[1]
158             maintainer = fix_maintainer(source[2])
159             if not gen_uploaders:
160                 key = package
161             else:
162                 key = (package, maintainer)
163
164             if packages.has_key(key):
165                 if packages[key]["priority"] <= suite_priority:
166                     if apt_pkg.VersionCompare(packages[key]["version"], version) < 0:
167                         packages[key] = { "maintainer": maintainer, "priority": suite_priority, "version": version }
168             else:
169                 packages[key] = { "maintainer": maintainer, "priority": suite_priority, "version": version }
170
171         # Binary packages
172         if not gen_uploaders:
173             q = session.execute("""SELECT b.package, b.source, b.maintainer, b.version
174                                      FROM bin_associations ba, binaries b, suite s
175                                     WHERE s.suite_name = :suite_name
176                                       AND ba.suite = s.id AND ba.bin = b.id""",
177                                    {'suite_name': suite})
178         else:
179             q = session.execute("""SELECT b.package, b.source, srcu.maintainer, b.version
180                                      FROM bin_associations ba, binaries b, suite s, src_uploaders srcu
181                                     WHERE s.suite_name = :suite_name
182                                       AND ba.suite = s.id AND ba.bin = b.id
183                                       AND b.source = srcu.source""",
184                                    {'suite_name': suite})
185
186
187         for binary in q.fetchall():
188             package = binary[0]
189             source_id = binary[1]
190             version = binary[3]
191             # Use the source maintainer first; falling back on the binary maintainer as a last resort only
192             if source_id and not gen_uploaders:
193                 maintainer = get_maintainer_from_source(source_id, session)
194             else:
195                 maintainer = get_maintainer(binary[2], session)
196             if not gen_uploaders:
197                 key = package
198             else:
199                 key = (package, maintainer)
200
201             if packages.has_key(key):
202                 if packages[key]["priority"] <= suite_priority:
203                     if apt_pkg.VersionCompare(packages[key]["version"], version) < 0:
204                         packages[key] = { "maintainer": maintainer, "priority": suite_priority, "version": version }
205             else:
206                 packages[key] = { "maintainer": maintainer, "priority": suite_priority, "version": version }
207
208     # Process any additional Maintainer files (e.g. from pseudo packages)
209     for filename in extra_files:
210         extrafile = utils.open_file(filename)
211         for line in extrafile.readlines():
212             line = re_comments.sub('', line).strip()
213             if line == "":
214                 continue
215             split = line.split()
216             lhs = split[0]
217             maintainer = fix_maintainer(" ".join(split[1:]))
218             if lhs.find('~') != -1:
219                 (package, version) = lhs.split('~', 1)
220             else:
221                 package = lhs
222                 version = '*'
223             if not gen_uploaders:
224                 key = package
225             else:
226                 key = (package, maintainer)
227             # A version of '*' overwhelms all real version numbers
228             if not packages.has_key(key) or version == '*' \
229                or apt_pkg.VersionCompare(packages[key]["version"], version) < 0:
230                 packages[key] = { "maintainer": maintainer, "version": version }
231         extrafile.close()
232
233     package_keys = packages.keys()
234     package_keys.sort()
235     if not gen_uploaders:
236         for package in package_keys:
237             lhs = "~".join([package, packages[package]["version"]])
238             print "%-30s %s" % (lhs, packages[package]["maintainer"])
239     else:
240         for (package, maintainer) in package_keys:
241             lhs = "~".join([package, packages[package]["version"]])
242             print "%-30s %s" % (lhs, maintainer)
243
244 ################################################################################
245
246 if __name__ == '__main__':
247     main()