]> git.decadent.org.uk Git - dak.git/blob - dak/make_maintainers.py
move fix_maintainer support routines to textutils
[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 pg
34 import sys
35 import apt_pkg
36 from daklib import database
37 from daklib import utils
38 from daklib import textutils
39 from daklib.regexes import re_comments
40
41 ################################################################################
42
43 Cnf = None                          #: Configuration, apt_pkg.Configuration
44 projectB = None                     #: database connection, pgobject
45 maintainer_from_source_cache = {}   #: caches the maintainer name <email> per source_id
46 packages = {}                       #: packages data to write out
47 fixed_maintainer_cache = {}         #: caches fixed ( L{daklib.textutils.fix_maintainer} ) maintainer data
48
49 ################################################################################
50
51 def usage (exit_code=0):
52     print """Usage: dak make-maintainers [OPTION] EXTRA_FILE[...]
53 Generate an index of packages <=> Maintainers.
54
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):
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     return fix_maintainer(database.get_maintainer(maintainer))
87
88 def get_maintainer_from_source (source_id):
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 = projectB.query("SELECT m.name FROM maintainer m, source s WHERE s.id = %s and s.maintainer = m.id" % (source_id))
102         maintainer = q.getresult()[0][0]
103         maintainer_from_source_cache[source_id] = fix_maintainer(maintainer)
104
105     return maintainer_from_source_cache[source_id]
106
107 ################################################################################
108
109 def main():
110     global Cnf, projectB
111
112     Cnf = utils.get_conf()
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,Arguments,sys.argv)
119     Options = Cnf.SubTree("Make-Maintainers::Options")
120
121     if Options["Help"]:
122         usage()
123
124     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
125     database.init(Cnf, projectB)
126
127     for suite in Cnf.SubTree("Suite").List():
128         suite = suite.lower()
129         suite_priority = int(Cnf["Suite::%s::Priority" % (suite)])
130
131         # Source packages
132         q = projectB.query("SELECT s.source, s.version, m.name FROM src_associations sa, source s, suite su, maintainer m WHERE su.suite_name = '%s' AND sa.suite = su.id AND sa.source = s.id AND m.id = s.maintainer" % (suite))
133         sources = q.getresult()
134         for source in sources:
135             package = source[0]
136             version = source[1]
137             maintainer = fix_maintainer(source[2])
138             if packages.has_key(package):
139                 if packages[package]["priority"] <= suite_priority:
140                     if apt_pkg.VersionCompare(packages[package]["version"], version) < 0:
141                         packages[package] = { "maintainer": maintainer, "priority": suite_priority, "version": version }
142             else:
143                 packages[package] = { "maintainer": maintainer, "priority": suite_priority, "version": version }
144
145         # Binary packages
146         q = projectB.query("SELECT b.package, b.source, b.maintainer, b.version FROM bin_associations ba, binaries b, suite s WHERE s.suite_name = '%s' AND ba.suite = s.id AND ba.bin = b.id" % (suite))
147         binaries = q.getresult()
148         for binary in binaries:
149             package = binary[0]
150             source_id = binary[1]
151             version = binary[3]
152             # Use the source maintainer first; falling back on the binary maintainer as a last resort only
153             if source_id:
154                 maintainer = get_maintainer_from_source(source_id)
155             else:
156                 maintainer = get_maintainer(binary[2])
157             if packages.has_key(package):
158                 if packages[package]["priority"] <= suite_priority:
159                     if apt_pkg.VersionCompare(packages[package]["version"], version) < 0:
160                         packages[package] = { "maintainer": maintainer, "priority": suite_priority, "version": version }
161             else:
162                 packages[package] = { "maintainer": maintainer, "priority": suite_priority, "version": version }
163
164     # Process any additional Maintainer files (e.g. from pseudo packages)
165     for filename in extra_files:
166         extrafile = utils.open_file(filename)
167         for line in extrafile.readlines():
168             line = re_comments.sub('', line).strip()
169             if line == "":
170                 continue
171             split = line.split()
172             lhs = split[0]
173             maintainer = fix_maintainer(" ".join(split[1:]))
174             if lhs.find('~') != -1:
175                 (package, version) = lhs.split('~', 1)
176             else:
177                 package = lhs
178                 version = '*'
179             # A version of '*' overwhelms all real version numbers
180             if not packages.has_key(package) or version == '*' \
181                or apt_pkg.VersionCompare(packages[package]["version"], version) < 0:
182                 packages[package] = { "maintainer": maintainer, "version": version }
183         extrafile.close()
184
185     package_keys = packages.keys()
186     package_keys.sort()
187     for package in package_keys:
188         lhs = "~".join([package, packages[package]["version"]])
189         print "%-30s %s" % (lhs, packages[package]["maintainer"])
190
191 ################################################################################
192
193 if __name__ == '__main__':
194     main()