comparison hgext/patchbomb.py @ 1702:e291d9a30bef

add --mbox output to patchbomb add option to save generated mails in an mbox file for later processing with formail, instead of sending them directly
author Johannes Stezenbach <js@linuxtv.org>
date Mon, 06 Feb 2006 17:34:39 -0600
parents e70e1ed66093
children 26dd4ae77b7b
comparison
equal deleted inserted replaced
1701:4ba8fe499df2 1702:e291d9a30bef
24 # changes. 24 # changes.
25 # 25 #
26 # It is best to run this script with the "-n" (test only) flag before 26 # It is best to run this script with the "-n" (test only) flag before
27 # firing it up "for real", in which case it will use your pager to 27 # firing it up "for real", in which case it will use your pager to
28 # display each of the messages that it would send. 28 # display each of the messages that it would send.
29 #
30 # The "-m" (mbox) option will create an mbox file instead of sending
31 # the messages directly. This can be reviewed e.g. with "mutt -R -f mbox",
32 # and finally sent with "formail -s sendmail -bm -t < mbox".
29 # 33 #
30 # To configure a default mail host, add a section like this to your 34 # To configure a default mail host, add a section like this to your
31 # hgrc file: 35 # hgrc file:
32 # 36 #
33 # [smtp] 37 # [smtp]
45 # to = recipient1, recipient2, ... 49 # to = recipient1, recipient2, ...
46 # cc = cc1, cc2, ... 50 # cc = cc1, cc2, ...
47 51
48 from email.MIMEMultipart import MIMEMultipart 52 from email.MIMEMultipart import MIMEMultipart
49 from email.MIMEText import MIMEText 53 from email.MIMEText import MIMEText
54 from email.Utils import parseaddr
50 from mercurial import commands 55 from mercurial import commands
51 from mercurial import hg 56 from mercurial import hg
52 from mercurial import ui 57 from mercurial import ui
53 from mercurial.i18n import gettext as _ 58 from mercurial.i18n import gettext as _
54 import os 59 import os
220 d = cdiffstat(_('Final summary:\n'), jumbo) 225 d = cdiffstat(_('Final summary:\n'), jumbo)
221 if d: msg.attach(MIMEText(d)) 226 if d: msg.attach(MIMEText(d))
222 227
223 msgs.insert(0, msg) 228 msgs.insert(0, msg)
224 229
225 if not opts['test']: 230 if not opts['test'] and not opts['mbox']:
226 s = smtplib.SMTP() 231 s = smtplib.SMTP()
227 s.connect(host = ui.config('smtp', 'host', 'mail'), 232 s.connect(host = ui.config('smtp', 'host', 'mail'),
228 port = int(ui.config('smtp', 'port', 25))) 233 port = int(ui.config('smtp', 'port', 25)))
229 if ui.configbool('smtp', 'tls'): 234 if ui.configbool('smtp', 'tls'):
230 s.ehlo() 235 s.ehlo()
234 password = ui.config('smtp', 'password') 239 password = ui.config('smtp', 'password')
235 if username and password: 240 if username and password:
236 s.login(username, password) 241 s.login(username, password)
237 parent = None 242 parent = None
238 tz = time.strftime('%z') 243 tz = time.strftime('%z')
244 sender_addr = parseaddr(sender)[1]
239 for m in msgs: 245 for m in msgs:
240 try: 246 try:
241 m['Message-Id'] = genmsgid(m['X-Mercurial-Node']) 247 m['Message-Id'] = genmsgid(m['X-Mercurial-Node'])
242 except TypeError: 248 except TypeError:
243 m['Message-Id'] = genmsgid('patchbomb') 249 m['Message-Id'] = genmsgid('patchbomb')
248 m['Date'] = time.strftime('%a, %e %b %Y %T ', time.localtime(start_time)) + tz 254 m['Date'] = time.strftime('%a, %e %b %Y %T ', time.localtime(start_time)) + tz
249 start_time += 1 255 start_time += 1
250 m['From'] = sender 256 m['From'] = sender
251 m['To'] = ', '.join(to) 257 m['To'] = ', '.join(to)
252 if cc: m['Cc'] = ', '.join(cc) 258 if cc: m['Cc'] = ', '.join(cc)
253 ui.status('Sending ', m['Subject'], ' ...\n')
254 if opts['test']: 259 if opts['test']:
260 ui.status('Displaying ', m['Subject'], ' ...\n')
255 fp = os.popen(os.getenv('PAGER', 'more'), 'w') 261 fp = os.popen(os.getenv('PAGER', 'more'), 'w')
256 fp.write(m.as_string(0)) 262 fp.write(m.as_string(0))
257 fp.write('\n') 263 fp.write('\n')
258 fp.close() 264 fp.close()
265 elif opts['mbox']:
266 ui.status('Writing ', m['Subject'], ' ...\n')
267 fp = open(opts['mbox'], m.has_key('In-Reply-To') and 'ab+' or 'wb+')
268 date = time.asctime(time.localtime(start_time))
269 fp.write('From %s %s\n' % (sender_addr, date))
270 fp.write(m.as_string(0))
271 fp.write('\n\n')
272 fp.close()
259 else: 273 else:
274 ui.status('Sending ', m['Subject'], ' ...\n')
260 s.sendmail(sender, to + cc, m.as_string(0)) 275 s.sendmail(sender, to + cc, m.as_string(0))
261 if not opts['test']: 276 if not opts['test'] and not opts['mbox']:
262 s.close() 277 s.close()
263 278
264 cmdtable = { 279 cmdtable = {
265 'email': 280 'email':
266 (patchbomb, 281 (patchbomb,
267 [('c', 'cc', [], 'email addresses of copy recipients'), 282 [('c', 'cc', [], 'email addresses of copy recipients'),
268 ('d', 'diffstat', None, 'add diffstat output to messages'), 283 ('d', 'diffstat', None, 'add diffstat output to messages'),
269 ('f', 'from', '', 'email address of sender'), 284 ('f', 'from', '', 'email address of sender'),
270 ('', 'plain', None, 'omit hg patch header'), 285 ('', 'plain', None, 'omit hg patch header'),
271 ('n', 'test', None, 'print messages that would be sent'), 286 ('n', 'test', None, 'print messages that would be sent'),
287 ('m', 'mbox', '', 'write messages to mbox file instead of sending them'),
272 ('s', 'subject', '', 'subject of introductory message'), 288 ('s', 'subject', '', 'subject of introductory message'),
273 ('t', 'to', [], 'email addresses of recipients')], 289 ('t', 'to', [], 'email addresses of recipients')],
274 "hg email [OPTION]... [REV]...") 290 "hg email [OPTION]... [REV]...")
275 } 291 }