]>
code.delx.au - gnu-emacs-elpa/blob - admin/forward-diffs.py
2 ### forward-diffs.py --- forward emacs-elpa-diffs mails to maintainers
4 ## Copyright (C) 2012 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 the emacs-elpa-diffs 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 -s sender
42 ## packagedir = /path/to/packages
43 ## sender = your email address
44 ## logfile = file to write log to (you might want to rotate/compress/examine it)
45 ## maintfile = file listing files and their maintainers, with format:
47 ## package1/file1 email1
48 ## package2/file2 email2,email3
50 ## Use "nomail" for the email field to not send a mail.
52 ## overmaintfile = like maintfile, but takes precedence over it.
65 ## Scan FILE for Author or Maintainer (preferred) headers.
66 ## Return a list of all email addresses found in MAINTS.
67 def scan_file(file, maints
):
71 except Exception as err
:
72 lfile
.write('Error opening file %s: %s\n' % (file, str(err
)))
75 ## Max number of lines to scan looking for a maintainer.
76 ## (20 seems to be the highest at present).
86 if ( nline
> max_lines
): break
88 ## Try and de-obfuscate. Worth it?
89 line
= re
.sub( '(?i) AT ', '@', line
)
90 line
= re
.sub( '(?i) DOT ', '.', line
)
92 if cont
: # continued header?
93 reg
= re
.match( ('%s[ \t]+[^:]*?<?([\w.-]+@[\w.-]+)>?' % prefix
), line
, re
.I
)
94 if not reg
: # not a continued header
97 if ( type == "maint" ): break
100 ## Check for one header immediately after another.
102 reg
= re
.match( '([^ ]+)? *(Author|Maintainer)s?: .*?<?([\w.-]+@[\w.-]+)>?', line
, re
.I
)
112 prefix
= reg
.group(1) or ""
115 type = "maint" if re
.search( 'Maintainer', type, re
.I
) else "auth"
116 ## maints = [] does the wrong thing.
117 if type == "maint": del maints
[:]
123 ## Scan all the files under dir for maintainer information.
124 ## Write to stdout, or optional argument outfile (which is overwritten).
125 def scan_dir(dir, outfile
=None):
127 dir = re
.sub( '/+$', '', dir) + '/' # ensure trailing /
129 if not os
.path
.isdir(dir):
130 sys
.stderr
.write('No such directory: %s\n' % dir)
136 fd
= open( outfile
, 'w' )
137 except Exception as err
:
138 sys
.stderr
.write("Error opening `%s': %s\n" % (outfile
, str(err
)))
142 for dirpath
, dirnames
, filenames
in os
.walk(dir):
143 for file in filenames
:
144 path
= os
.path
.join(dirpath
, file)
146 scan_file(path
, maints
)
147 ## This would skip printing empty maints.
148 ## That would mean we would scan the file each time for no reason.
149 ## if not maints: continue
150 path
= re
.sub( '^%s' % dir, '', path
)
151 string
= "%-50s %s\n" % (path
, ",".join(maints
))
160 usage
="""usage: %prog <-p /path/to/packages> <-m maintfile>
161 <-l logfile -s sender|--create> [-o overmaintfile] [--sendmail] [--debug]
162 Take a GNU ELPA diff on stdin, and forward it to the maintainer(s)."""
164 parser
= optparse
.OptionParser()
165 parser
.set_usage ( usage
)
166 parser
.add_option( "-m", dest
="maintfile", default
=None,
167 help="file listing packages and maintainers")
168 parser
.add_option( "-l", dest
="logfile", default
=None,
169 help="file to append output to")
170 parser
.add_option( "-o", dest
="overmaintfile", default
=None,
171 help="override file listing packages and maintainers")
172 parser
.add_option( "-p", dest
="packagedir", default
=None,
173 help="path to packages directory")
174 parser
.add_option( "-s", dest
="sender", default
=None,
175 help="sender address for forwards")
176 parser
.add_option( "--create", dest
="create", default
=False,
177 action
="store_true", help="create maintfile")
178 parser
.add_option( "--sendmail", dest
="sendmail", default
=False,
179 action
="store_true", help="use sendmail rather than smtp")
180 parser
.add_option( "--debug", dest
="debug", default
=False,
181 action
="store_true", help="debug only, do not send mail")
184 ( opts
, args
) = parser
.parse_args()
187 if not opts
.maintfile
:
188 parser
.error('No maintfile specified')
190 if not opts
.packagedir
:
191 parser
.error('No packagedir specified')
193 if not os
.path
.isdir(opts
.packagedir
):
194 sys
.stderr
.write('No such directory: %s\n' % opts
.packagedir
)
200 parser
.error('No logfile specified')
203 parser
.error('No sender specified')
206 ## Create the maintfile.
208 scan_dir( opts
.packagedir
, opts
.maintfile
)
213 lfile
= open( opts
.logfile
, 'a' )
214 except Exception as err
:
215 sys
.stderr
.write('Error opening logfile: %s\n' % str(err
))
220 mfile
= open( opts
.maintfile
, 'r' )
221 except Exception as err
:
222 lfile
.write('Error opening maintfile: %s\n' % str(err
))
225 ## Each element is package/file: maint1, maint2, ...
229 if re
.match( '#| *$', line
): continue
230 (pfile
, maint
) = line
.split()
231 maints
[pfile
] = maint
.split(',')
236 if opts
.overmaintfile
:
238 ofile
= open( opts
.overmaintfile
, 'r' )
239 except Exception as err
:
240 lfile
.write('Error opening overmaintfile: %s\n' % str(err
))
244 if re
.match( '#| *$', line
): continue
245 (pfile
, maint
) = line
.split()
246 maints
[pfile
] = maint
.split(',')
256 resent_via
= 'GNU ELPA diff forwarder'
258 message
= email
.message_from_string( text
)
260 (msg_name
, msg_from
) = email
.utils
.parseaddr( message
['from'] )
262 lfile
.write('\nDate: %s\n' % str(datetime
.datetime
.now()))
263 lfile
.write('Message-ID: %s\n' % message
['message-id'])
264 lfile
.write('From: %s\n' % msg_from
)
266 if resent_via
== message
['x-resent-via']:
267 lfile
.write('Mail loop; aborting\n')
275 for line
in text
.splitlines():
277 if re
.match( 'modified:$', line
):
281 if not start
: continue
283 if re
.match( ' *$', line
): break
286 reg
= re
.match( 'packages/([^ ]+)', line
.strip() )
292 lfile
.write('File: %s\n' % pfile
)
294 ## Should not be possible for files (rather than packages)...
295 if pfile
in pfiles_seen
:
296 lfile
.write('Already seen this file\n')
299 pfiles_seen
.append(pfile
)
302 if not pfile
in maints
:
304 lfile
.write('Unknown maintainer, scanning file...\n')
307 thisfile
= os
.path
.join( opts
.packagedir
, pfile
)
309 scan_file( thisfile
, thismaint
)
311 if not thismaint
: continue
313 maints
[pfile
] = thismaint
315 ## Append maintainer to file.
317 mfile
= open( opts
.maintfile
, 'a' )
318 string
= "%-50s %s\n" % (pfile
, ",".join(thismaint
))
321 lfile
.write('Appended to maintfile\n')
322 except Exception as err
:
323 lfile
.write('Error appending to maintfile: %s\n' % str(err
))
326 for maint
in maints
[pfile
]:
328 lfile
.write('Maint: %s\n' % maint
)
331 if maint
in maints_seen
:
332 lfile
.write('Already seen this maintainer\n')
335 maints_seen
.append(maint
)
338 if maint
== "nomail":
339 lfile
.write('Not resending, no mail is requested\n')
343 if maint
== msg_from
:
344 lfile
.write('Not resending, since maintainer = committer\n')
349 forward
.add_header('X-Resent-Via', resent_via
)
350 forward
.add_header('Resent-To', maint
)
351 forward
.add_header('Resent-From', opts
.sender
)
353 lfile
.write('Resending via %s...\n' % ('sendmail'
354 if opts
.sendmail
else 'smtp') )
357 if opts
.debug
: continue
361 s
= os
.popen("/usr/sbin/sendmail -i -f %s %s" %
362 (opts
.sender
, maint
), "w")
363 s
.write(forward
.as_string())
366 lfile
.write('Sendmail exit status: %s\n' % status
)
371 s
= smtplib
.SMTP('localhost')
372 except Exception as err
:
373 lfile
.write('Error opening smtp: %s\n' % str(err
))
377 s
.sendmail(opts
.sender
, maint
, forward
.as_string())
378 except Exception as err
:
379 lfile
.write('Error sending smtp: %s\n' % str(err
))
383 ### forward-diffs.py ends here