]> git.decadent.org.uk Git - dak.git/blob - dak/make_maintainers.py
convert make_maintainers to new DB API
[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   -h, --help                 show this help and exit
55 """
56     sys.exit(exit_code)
57
58 ################################################################################
59
60 def fix_maintainer (maintainer):
61     """
62     Fixup maintainer entry, cache the result.
63
64     @type maintainer: string
65     @param maintainer: A maintainer entry as passed to L{daklib.textutils.fix_maintainer}
66
67     @rtype: tuple
68     @returns: fixed maintainer tuple
69     """
70     global fixed_maintainer_cache
71
72     if not fixed_maintainer_cache.has_key(maintainer):
73         fixed_maintainer_cache[maintainer] = textutils.fix_maintainer(maintainer)[0]
74
75     return fixed_maintainer_cache[maintainer]
76
77 def get_maintainer(maintainer, session):
78     """
79     Retrieves maintainer name from database, passes it through fix_maintainer and
80     passes on whatever that returns.
81
82     @type maintainer: int
83     @param maintainer: maintainer_id
84     """
85     q = session.execute("SELECT name FROM maintainer WHERE id = :id", {'id': maintainer}).fetchall()
86     return fix_maintainer(q[0][0])
87
88 def get_maintainer_from_source(source_id, session):
89     """
90     Returns maintainer name for given source_id.
91
92     @type source_id: int
93     @param source_id: source package id
94
95     @rtype: string
96     @return: maintainer name/email
97     """
98     global maintainer_from_source_cache
99
100     if not maintainer_from_source_cache.has_key(source_id):
101         q = session.execute("""SELECT m.name FROM maintainer m, source s
102                                 WHERE s.id = :sourceid AND s.maintainer = m.id""",
103                             {'sourceid': source_id})
104         maintainer = q.fetchall()[0][0]
105         maintainer_from_source_cache[source_id] = fix_maintainer(maintainer)
106
107     return maintainer_from_source_cache[source_id]
108
109 ################################################################################
110
111 def main():
112     cnf = Config()
113
114     Arguments = [('h',"help","Make-Maintainers::Options::Help")]
115     if not cnf.has_key("Make-Maintainers::Options::Help"):
116         cnf["Make-Maintainers::Options::Help"] = ""
117
118     extra_files = apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv)
119     Options = cnf.SubTree("Make-Maintainers::Options")
120
121     if Options["Help"]:
122         usage()
123
124     session = DBConn().session()
125
126     for suite in cnf.SubTree("Suite").List():
127         suite = suite.lower()
128         suite_priority = int(cnf["Suite::%s::Priority" % (suite)])
129
130         # Source packages
131         q = session.execute("""SELECT s.source, s.version, m.name
132                                  FROM src_associations sa, source s, suite su, maintainer m
133                                 WHERE su.suite_name = :suite_name
134                                   AND sa.suite = su.id AND sa.source = s.id
135                                   AND m.id = s.maintainer""",
136                                 {'suite_name': suite})
137         for source in q.fetchall():
138             package = source[0]
139             version = source[1]
140             maintainer = fix_maintainer(source[2])
141             if packages.has_key(package):
142                 if packages[package]["priority"] <= suite_priority:
143                     if apt_pkg.VersionCompare(packages[package]["version"], version) < 0:
144                         packages[package] = { "maintainer": maintainer, "priority": suite_priority, "version": version }
145             else:
146                 packages[package] = { "maintainer": maintainer, "priority": suite_priority, "version": version }
147
148         # Binary packages
149         q = session.execute("""SELECT b.package, b.source, b.maintainer, b.version
150                                  FROM bin_associations ba, binaries b, suite s
151                                 WHERE s.suite_name = :suite_name
152                                   AND ba.suite = s.id AND ba.bin = b.id""",
153                                {'suite_name': suite})
154         for binary in q.fetchall():
155             package = binary[0]
156             source_id = binary[1]
157             version = binary[3]
158             # Use the source maintainer first; falling back on the binary maintainer as a last resort only
159             if source_id:
160                 maintainer = get_maintainer_from_source(source_id, session)
161             else:
162                 maintainer = get_maintainer(binary[2], session)
163             if packages.has_key(package):
164                 if packages[package]["priority"] <= suite_priority:
165                     if apt_pkg.VersionCompare(packages[package]["version"], version) < 0:
166                         packages[package] = { "maintainer": maintainer, "priority": suite_priority, "version": version }
167             else:
168                 packages[package] = { "maintainer": maintainer, "priority": suite_priority, "version": version }
169
170     # Process any additional Maintainer files (e.g. from pseudo packages)
171     for filename in extra_files:
172         extrafile = utils.open_file(filename)
173         for line in extrafile.readlines():
174             line = re_comments.sub('', line).strip()
175             if line == "":
176                 continue
177             split = line.split()
178             lhs = split[0]
179             maintainer = fix_maintainer(" ".join(split[1:]))
180             if lhs.find('~') != -1:
181                 (package, version) = lhs.split('~', 1)
182             else:
183                 package = lhs
184                 version = '*'
185             # A version of '*' overwhelms all real version numbers
186             if not packages.has_key(package) or version == '*' \
187                or apt_pkg.VersionCompare(packages[package]["version"], version) < 0:
188                 packages[package] = { "maintainer": maintainer, "version": version }
189         extrafile.close()
190
191     package_keys = packages.keys()
192     package_keys.sort()
193     for package in package_keys:
194         lhs = "~".join([package, packages[package]["version"]])
195         print "%-30s %s" % (lhs, packages[package]["maintainer"])
196
197 ################################################################################
198
199 if __name__ == '__main__':
200     main()