]>
code.delx.au - gnu-emacs-elpa/blob - admin/forward-diffs.py
2 ### forward-diffs.py --- forward emacs-diffs mails to maintainers
4 ## Copyright (C) 2012, 2013 Free Software Foundation, Inc.
6 ## Author: Glenn Morris <rgm@gnu.org>
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.
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.
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/>.
23 ## Forward emails from an emacs-diffs style mailing list to the
24 ## maintainer(s) of the modified files.
26 ## Two modes of operation:
28 ## 1) Create the maintfile (really this is just an optimization):
29 ## forward-diffs.py --create -p packagesdir -m maintfile
31 ## You can start with an empty maintfile and normal operation in 2)
32 ## will append information as needed.
34 ## 2) Call from eg procmail to forward diffs. Example usage:
37 ## * ^TO_emacs-elpa-diffs@gnu\.org
38 ## | forward-diffs.py -p packagedir -m maintfile -l logfile \
39 ## -o overmaint -s sender
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:
48 ## package1/file1 email1
49 ## package2/file2 email2,email3
52 ## Use "nomail" for the email field to not send a mail.
53 ## An entry that is a directory applies to all files in that directory
54 ## that do not have specific maintainers.
56 ## overmaint = like maintfile, but takes precedence over it.
69 ## Scan FILE for Author or Maintainer (preferred) headers.
70 ## Return a list of all email addresses found in MAINTS.
71 def scan_file(file, maints
):
75 except Exception as err
:
76 lfile
.write('Error opening file %s: %s\n' % (file, str(err
)))
79 ## Max number of lines to scan looking for a maintainer.
80 ## (20 seems to be the highest at present).
90 if ( nline
> max_lines
): break
92 ## Try and de-obfuscate. Worth it?
93 line
= re
.sub( '(?i) AT ', '@', line
)
94 line
= re
.sub( '(?i) DOT ', '.', line
)
96 if cont
: # continued header?
97 reg
= re
.match( ('%s[ \t]+[^:]*?<?([\w.-]+@[\w.-]+)>?' % prefix
), line
, re
.I
)
98 if not reg
: # not a continued header
101 if ( type == "maint" ): break
104 ## Check for one header immediately after another.
106 reg
= re
.match( '([^ ]+)? *(Author|Maintainer)s?: .*?<?([\w.-]+@[\w.-]+)>?', line
, re
.I
)
116 prefix
= reg
.group(1) or ""
119 type = "maint" if re
.search( 'Maintainer', type, re
.I
) else "auth"
120 ## maints = [] does the wrong thing.
121 if type == "maint": del maints
[:]
127 ## Scan all the files under dir for maintainer information.
128 ## Write to stdout, or optional argument outfile (which is overwritten).
129 def scan_dir(dir, outfile
=None):
131 dir = re
.sub( '/+$', '', dir) + '/' # ensure trailing /
133 if not os
.path
.isdir(dir):
134 sys
.stderr
.write('No such directory: %s\n' % dir)
140 fd
= open( outfile
, 'w' )
141 except Exception as err
:
142 sys
.stderr
.write("Error opening `%s': %s\n" % (outfile
, str(err
)))
146 for dirpath
, dirnames
, filenames
in os
.walk(dir):
147 for file in filenames
:
148 path
= os
.path
.join(dirpath
, file)
150 scan_file(path
, maints
)
151 ## This would skip printing empty maints.
152 ## That would mean we would scan the file each time for no reason.
153 ## But empty maintainers are an error at present.
154 if not maints
: continue
155 path
= re
.sub( '^%s' % dir, '', path
)
156 string
= "%-50s %s\n" % (path
, ",".join(maints
))
165 usage
="""usage: %prog <-p /path/to/packages> <-m maintfile>
166 <-l logfile -s sender|--create> [-o overmaintfile] [--prefix prefix]
167 [--sendmail] [--debug]
168 Take an emacs-diffs mail on stdin, and forward it to the maintainer(s)."""
170 parser
= optparse
.OptionParser()
171 parser
.set_usage ( usage
)
172 parser
.add_option( "-m", dest
="maintfile", default
=None,
173 help="file listing packages and maintainers")
174 parser
.add_option( "-l", dest
="logfile", default
=None,
175 help="file to append output to")
176 parser
.add_option( "-o", dest
="overmaintfile", default
=None,
177 help="override file listing packages and maintainers")
178 parser
.add_option( "-p", dest
="packagedir", default
=None,
179 help="path to packages directory")
180 parser
.add_option( "-s", dest
="sender", default
=None,
181 help="sender address for forwards")
182 parser
.add_option( "--create", dest
="create", default
=False,
183 action
="store_true", help="create maintfile")
184 parser
.add_option( "--no-scan", dest
="noscan", default
=True,
186 help="don't scan for maintainers; implies --no-update")
187 parser
.add_option( "--no-update", dest
="noupdate", default
=False,
189 help="do not update the maintfile")
190 parser
.add_option( "--prefix", dest
="prefix", default
="packages/",
191 help="prefix to remove from modified file name [default: %default]")
192 parser
.add_option( "--sendmail", dest
="sendmail", default
=False,
193 action
="store_true", help="use sendmail rather than smtp")
194 parser
.add_option( "--debug", dest
="debug", default
=False,
195 action
="store_true", help="debug only, do not send mail")
198 ( opts
, args
) = parser
.parse_args()
201 if not opts
.maintfile
:
202 parser
.error('No maintfile specified')
204 if not opts
.packagedir
:
205 parser
.error('No packagedir specified')
207 if not os
.path
.isdir(opts
.packagedir
):
208 sys
.stderr
.write('No such directory: %s\n' % opts
.packagedir
)
214 parser
.error('No logfile specified')
217 parser
.error('No sender specified')
221 lfile
= open( opts
.logfile
, 'a' )
222 except Exception as err
:
223 sys
.stderr
.write('Error opening logfile: %s\n' % str(err
))
228 mfile
= open( opts
.maintfile
, 'r' )
229 except Exception as err
:
230 lfile
.write('Error opening maintfile: %s\n' % str(err
))
233 ## Create the maintfile.
235 scan_dir( opts
.packagedir
, opts
.maintfile
)
239 ## Each element is package/file: maint1, maint2, ...
243 if re
.match( '#| *$', line
): continue
244 ## FIXME error here if empty maintainer.
245 (pfile
, maint
) = line
.split()
246 maints
[pfile
] = maint
.split(',')
251 if opts
.overmaintfile
:
253 ofile
= open( opts
.overmaintfile
, 'r' )
254 except Exception as err
:
255 lfile
.write('Error opening overmaintfile: %s\n' % str(err
))
259 if re
.match( '#| *$', line
): continue
260 (pfile
, maint
) = line
.split()
261 maints
[pfile
] = maint
.split(',')
271 resent_via
= 'GNU Emacs diff forwarder'
273 message
= email
.message_from_string( text
)
275 (msg_name
, msg_from
) = email
.utils
.parseaddr( message
['from'] )
277 lfile
.write('\nDate: %s\n' % str(datetime
.datetime
.now()))
278 lfile
.write('Message-ID: %s\n' % message
['message-id'])
279 lfile
.write('From: %s\n' % msg_from
)
281 if resent_via
== message
['x-resent-via']:
282 lfile
.write('Mail loop; aborting\n')
290 for line
in text
.splitlines():
292 # Look for and process things that look like (Git):
294 # Summary of changes:
295 # packages/vlf/vlf.el | 2 +-
296 # 1 files changed, 1 insertions(+), 1 deletions(-)
298 #BZR: if re.match( 'modified:$', line ):
299 if re
.match( 'Summary of changes:$', line
):
303 if not start
: continue
305 ## An empty line or a line with non-empty first character.
306 if re
.match( '( *$|[^ ])', line
): break
307 # Any line that doesn't match the diffstat format (Git).
308 if not re
.match( ' [^ ]+ +\| ', line
):
309 lfile
.write('Stop scanning at: %s\n' % line
)
313 #BZR: reg = re.match( '%s([^ ]+)' % opts.prefix, line.strip() )
314 reg
= re
.match( ' %s([^ ]+)' % opts
.prefix
, line
)
316 lfile
.write('Skip: %s\n' % line
)
323 lfile
.write('File: %s\n' % pfile
)
325 ## Should not be possible for files (rather than packages)...
326 if pfile
in pfiles_seen
:
327 lfile
.write('Already seen this file\n')
330 pfiles_seen
.append(pfile
)
333 if not pfile
in maints
:
335 lfile
.write('Unknown maintainer\n')
339 lfile
.write('Scanning file...\n')
341 thisfile
= os
.path
.join( opts
.packagedir
, pfile
)
342 # scan_file( thisfile, thismaint )
345 maints
[pfile
] = thismaint
347 ## Append maintainer to file.
348 if not opts
.noupdate
:
350 mfile
= open( opts
.maintfile
, 'a' )
351 string
= "%-50s %s\n" % (pfile
, ",".join(thismaint
))
354 lfile
.write('Appended to maintfile\n')
355 except Exception as err
:
356 lfile
.write('Error appending to maintfile: %s\n' %
359 ## Didn't scan, or scanning did not work.
360 ## Look for a directory maintainer.
361 if not pfile
in maints
:
362 lfile
.write('No file maintainer, trying directories...\n')
364 (pfile
, tail
) = os
.path
.split(pfile
)
366 if pfile
in maints
: break
369 if not pfile
in maints
:
370 lfile
.write('No maintainer, skipping\n')
374 for maint
in maints
[pfile
]:
376 lfile
.write('Maint: %s\n' % maint
)
379 if maint
in maints_seen
:
380 lfile
.write('Already seen this maintainer\n')
383 maints_seen
.append(maint
)
386 if maint
== "nomail":
387 lfile
.write('Not resending, no mail is requested\n')
391 if maint
== msg_from
:
392 lfile
.write('Not resending, since maintainer = committer\n')
397 forward
.add_header('X-Resent-Via', resent_via
)
398 forward
.add_header('Resent-To', maint
)
399 forward
.add_header('Resent-From', opts
.sender
)
401 lfile
.write('Resending via %s...\n' % ('sendmail'
402 if opts
.sendmail
else 'smtp') )
405 if opts
.debug
: continue
409 s
= os
.popen("/usr/sbin/sendmail -i -f %s %s" %
410 (opts
.sender
, maint
), "w")
411 s
.write(forward
.as_string())
414 lfile
.write('Sendmail exit status: %s\n' % status
)
419 s
= smtplib
.SMTP('localhost')
420 except Exception as err
:
421 lfile
.write('Error opening smtp: %s\n' % str(err
))
425 s
.sendmail(opts
.sender
, maint
, forward
.as_string())
426 except Exception as err
:
427 lfile
.write('Error sending smtp: %s\n' % str(err
))
431 ### forward-diffs.py ends here