]> code.delx.au - gnu-emacs-elpa/blob - admin/forward-diffs.py
Add --no-update, --no-scan options for forward-diffs.py
[gnu-emacs-elpa] / admin / forward-diffs.py
1 #!/usr/bin/python
2 ### forward-diffs.py --- forward emacs-elpa-diffs mails to maintainers
3
4 ## Copyright (C) 2012 Free Software Foundation, Inc.
5
6 ## Author: Glenn Morris <rgm@gnu.org>
7
8 ## This program is free software; you can redistribute it and/or modify
9 ## it under the terms of the GNU General Public License as published by
10 ## the Free Software Foundation, either version 3 of the License, or
11 ## (at your option) any later version.
12
13 ## This program is distributed in the hope that it will be useful,
14 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ## GNU General Public License for more details.
17
18 ## You should have received a copy of the GNU General Public License
19 ## along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ### Commentary:
22
23 ## Forward emails from the emacs-elpa-diffs mailing list to the
24 ## maintainer(s) of the modified files.
25
26 ## Two modes of operation:
27
28 ## 1) Create the maintfile (really this is just an optimization):
29 ## forward-diffs.py --create -p packagesdir -m maintfile
30
31 ## You can start with an empty maintfile and normal operation in 2)
32 ## will append information as needed.
33
34 ## 2) Call from eg procmail to forward diffs. Example usage:
35
36 ## :0c
37 ## * ^TO_emacs-elpa-diffs@gnu\.org
38 ## | forward-diffs.py -p packagedir -m maintfile -l logfile \
39 ## -o overmaint -s sender
40
41 ## where
42
43 ## packagedir = /path/to/packages
44 ## sender = your email address
45 ## logfile = file to write log to (you might want to rotate/compress/examine it)
46 ## maintfile = file listing files and their maintainers, with format:
47 ##
48 ## package1/file1 email1
49 ## package2/file2 email2,email3
50 ##
51 ## Use "nomail" for the email field to not send a mail.
52 ##
53 ## overmaint = like maintfile, but takes precedence over it.
54
55 ### Code:
56
57 import optparse
58 import sys
59 import re
60 import email
61 import smtplib
62 import datetime
63 import os
64
65
66 ## Scan FILE for Author or Maintainer (preferred) headers.
67 ## Return a list of all email addresses found in MAINTS.
68 def scan_file(file, maints):
69
70 try:
71 fd = open( file, 'r')
72 except Exception as err:
73 lfile.write('Error opening file %s: %s\n' % (file, str(err)))
74 return 1
75
76 ## Max number of lines to scan looking for a maintainer.
77 ## (20 seems to be the highest at present).
78 max_lines = 50
79 nline = 0
80 cont = 0
81 type = ""
82
83 for line in fd:
84
85 nline += 1
86
87 if ( nline > max_lines ): break
88
89 ## Try and de-obfuscate. Worth it?
90 line = re.sub( '(?i) AT ', '@', line )
91 line = re.sub( '(?i) DOT ', '.', line )
92
93 if cont: # continued header?
94 reg = re.match( ('%s[ \t]+[^:]*?<?([\w.-]+@[\w.-]+)>?' % prefix), line, re.I )
95 if not reg: # not a continued header
96 cont = 0
97 prefix = ""
98 if ( type == "maint" ): break
99 type = ""
100
101 ## Check for one header immediately after another.
102 if not cont:
103 reg = re.match( '([^ ]+)? *(Author|Maintainer)s?: .*?<?([\w.-]+@[\w.-]+)>?', line, re.I )
104
105
106 if not reg: continue
107
108 if cont:
109 email = reg.group(1)
110 maints.append(email)
111 else:
112 cont = 1
113 prefix = reg.group(1) or ""
114 type = reg.group(2)
115 email = reg.group(3)
116 type = "maint" if re.search( 'Maintainer', type, re.I ) else "auth"
117 ## maints = [] does the wrong thing.
118 if type == "maint": del maints[:]
119 maints.append(email)
120
121 fd.close()
122
123
124 ## Scan all the files under dir for maintainer information.
125 ## Write to stdout, or optional argument outfile (which is overwritten).
126 def scan_dir(dir, outfile=None):
127
128 dir = re.sub( '/+$', '', dir) + '/' # ensure trailing /
129
130 if not os.path.isdir(dir):
131 sys.stderr.write('No such directory: %s\n' % dir)
132 sys.exit(1)
133
134 fd = 0
135 if outfile:
136 try:
137 fd = open( outfile, 'w' )
138 except Exception as err:
139 sys.stderr.write("Error opening `%s': %s\n" % (outfile, str(err)))
140 sys.exit(1)
141
142
143 for dirpath, dirnames, filenames in os.walk(dir):
144 for file in filenames:
145 path = os.path.join(dirpath, file)
146 maints = []
147 scan_file(path, maints)
148 ## This would skip printing empty maints.
149 ## That would mean we would scan the file each time for no reason.
150 ## But empty maintainers are an error at present.
151 if not maints: continue
152 path = re.sub( '^%s' % dir, '', path )
153 string = "%-50s %s\n" % (path, ",".join(maints))
154 if fd:
155 fd.write(string)
156 else:
157 print string,
158
159 if fd: fd.close()
160
161
162 usage="""usage: %prog <-p /path/to/packages> <-m maintfile>
163 <-l logfile -s sender|--create> [-o overmaintfile] [--sendmail] [--debug]
164 Take a GNU ELPA diff on stdin, and forward it to the maintainer(s)."""
165
166 parser = optparse.OptionParser()
167 parser.set_usage ( usage )
168 parser.add_option( "-m", dest="maintfile", default=None,
169 help="file listing packages and maintainers")
170 parser.add_option( "-l", dest="logfile", default=None,
171 help="file to append output to")
172 parser.add_option( "-o", dest="overmaintfile", default=None,
173 help="override file listing packages and maintainers")
174 parser.add_option( "-p", dest="packagedir", default=None,
175 help="path to packages directory")
176 parser.add_option( "-s", dest="sender", default=None,
177 help="sender address for forwards")
178 parser.add_option( "--create", dest="create", default=False,
179 action="store_true", help="create maintfile")
180 parser.add_option( "--no-scan", dest="noscan", default=False,
181 action="store_true",
182 help="don't scan for maintainers; implies --no-update")
183 parser.add_option( "--no-update", dest="noupdate", default=False,
184 action="store_true",
185 help="do not update the maintfile")
186 parser.add_option( "--sendmail", dest="sendmail", default=False,
187 action="store_true", help="use sendmail rather than smtp")
188 parser.add_option( "--debug", dest="debug", default=False,
189 action="store_true", help="debug only, do not send mail")
190
191
192 ( opts, args ) = parser.parse_args()
193
194
195 if not opts.maintfile:
196 parser.error('No maintfile specified')
197
198 if not opts.packagedir:
199 parser.error('No packagedir specified')
200
201 if not os.path.isdir(opts.packagedir):
202 sys.stderr.write('No such directory: %s\n' % opts.packagedir)
203 sys.exit(1)
204
205
206 if not opts.create:
207 if not opts.logfile:
208 parser.error('No logfile specified')
209
210 if not opts.sender:
211 parser.error('No sender specified')
212
213
214 ## Create the maintfile.
215 if opts.create:
216 scan_dir( opts.packagedir, opts.maintfile )
217 sys.exit()
218
219
220 try:
221 lfile = open( opts.logfile, 'a' )
222 except Exception as err:
223 sys.stderr.write('Error opening logfile: %s\n' % str(err))
224 sys.exit(1)
225
226
227 try:
228 mfile = open( opts.maintfile, 'r' )
229 except Exception as err:
230 lfile.write('Error opening maintfile: %s\n' % str(err))
231 sys.exit(1)
232
233 ## Each element is package/file: maint1, maint2, ...
234 maints = {}
235
236 for line in mfile:
237 if re.match( '#| *$', line ): continue
238 ## FIXME error here if empty maintainer.
239 (pfile, maint) = line.split()
240 maints[pfile] = maint.split(',')
241
242 mfile.close()
243
244
245 if opts.overmaintfile:
246 try:
247 ofile = open( opts.overmaintfile, 'r' )
248 except Exception as err:
249 lfile.write('Error opening overmaintfile: %s\n' % str(err))
250 sys.exit(1)
251
252 for line in ofile:
253 if re.match( '#| *$', line ): continue
254 (pfile, maint) = line.split()
255 maints[pfile] = maint.split(',')
256
257 ofile.close()
258
259
260 stdin = sys.stdin
261
262 text = stdin.read()
263
264
265 resent_via = 'GNU ELPA diff forwarder'
266
267 message = email.message_from_string( text )
268
269 (msg_name, msg_from) = email.utils.parseaddr( message['from'] )
270
271 lfile.write('\nDate: %s\n' % str(datetime.datetime.now()))
272 lfile.write('Message-ID: %s\n' % message['message-id'])
273 lfile.write('From: %s\n' % msg_from)
274
275 if resent_via == message['x-resent-via']:
276 lfile.write('Mail loop; aborting\n')
277 sys.exit(1)
278
279
280 start = False
281 pfiles_seen = []
282 maints_seen = []
283
284 for line in text.splitlines():
285
286 if re.match( 'modified:$', line ):
287 start = True
288 continue
289
290 if not start: continue
291
292 if re.match( ' *$', line ): break
293
294
295 reg = re.match( 'packages/([^ ]+)', line.strip() )
296 if not reg: break
297
298
299 pfile = reg.group(1)
300
301 lfile.write('File: %s\n' % pfile)
302
303 ## Should not be possible for files (rather than packages)...
304 if pfile in pfiles_seen:
305 lfile.write('Already seen this file\n')
306 continue
307
308 pfiles_seen.append(pfile)
309
310
311 if not pfile in maints:
312
313 lfile.write('Unknown maintainer\n')
314
315 if opts.noscan: continue
316
317 lfile.write('Scanning file...\n')
318 thismaint = []
319 thisfile = os.path.join( opts.packagedir, pfile )
320 scan_file( thisfile, thismaint )
321 if not thismaint: continue
322
323 maints[pfile] = thismaint
324
325 ## Append maintainer to file.
326 if not opts.noupdate:
327 try:
328 mfile = open( opts.maintfile, 'a' )
329 string = "%-50s %s\n" % (pfile, ",".join(thismaint))
330 mfile.write(string)
331 mfile.close()
332 lfile.write('Appended to maintfile\n')
333 except Exception as err:
334 lfile.write('Error appending to maintfile: %s\n' % str(err))
335
336
337 for maint in maints[pfile]:
338
339 lfile.write('Maint: %s\n' % maint)
340
341
342 if maint in maints_seen:
343 lfile.write('Already seen this maintainer\n')
344 continue
345
346 maints_seen.append(maint)
347
348
349 if maint == "nomail":
350 lfile.write('Not resending, no mail is requested\n')
351 continue
352
353
354 if maint == msg_from:
355 lfile.write('Not resending, since maintainer = committer\n')
356 continue
357
358
359 forward = message
360 forward.add_header('X-Resent-Via', resent_via)
361 forward.add_header('Resent-To', maint)
362 forward.add_header('Resent-From', opts.sender)
363
364 lfile.write('Resending via %s...\n' % ('sendmail'
365 if opts.sendmail else 'smtp') )
366
367
368 if opts.debug: continue
369
370
371 if opts.sendmail:
372 s = os.popen("/usr/sbin/sendmail -i -f %s %s" %
373 (opts.sender, maint), "w")
374 s.write(forward.as_string())
375 status = s.close()
376 if status:
377 lfile.write('Sendmail exit status: %s\n' % status)
378
379 else:
380
381 try:
382 s = smtplib.SMTP('localhost')
383 except Exception as err:
384 lfile.write('Error opening smtp: %s\n' % str(err))
385 sys.exit(1)
386
387 try:
388 s.sendmail(opts.sender, maint, forward.as_string())
389 except Exception as err:
390 lfile.write('Error sending smtp: %s\n' % str(err))
391
392 s.quit()
393
394 ### forward-diffs.py ends here