comparison hgext/patchbomb.py @ 15560:cc58c228503e

mail: mbox handling as a part of mail handling, refactored from patchbomb
author Mads Kiilerich <mads@kiilerich.com>
date Wed, 23 Nov 2011 02:11:24 +0100
parents 1830d0cc4bc1
children 3f79b110c4f0
comparison
equal deleted inserted replaced
15559:1830d0cc4bc1 15560:cc58c228503e
43 that the patchbomb extension can automatically send patchbombs 43 that the patchbomb extension can automatically send patchbombs
44 directly from the commandline. See the [email] and [smtp] sections in 44 directly from the commandline. See the [email] and [smtp] sections in
45 hgrc(5) for details. 45 hgrc(5) for details.
46 ''' 46 '''
47 47
48 import os, errno, socket, tempfile, cStringIO, time 48 import os, errno, socket, tempfile, cStringIO
49 import email.MIMEMultipart, email.MIMEBase 49 import email.MIMEMultipart, email.MIMEBase
50 import email.Utils, email.Encoders, email.Generator 50 import email.Utils, email.Encoders, email.Generator
51 from mercurial import cmdutil, commands, hg, mail, patch, util, discovery 51 from mercurial import cmdutil, commands, hg, mail, patch, util, discovery
52 from mercurial import scmutil 52 from mercurial import scmutil
53 from mercurial.i18n import _ 53 from mercurial.i18n import _
530 except IOError, inst: 530 except IOError, inst:
531 if inst.errno != errno.EPIPE: 531 if inst.errno != errno.EPIPE:
532 raise 532 raise
533 if fp is not ui: 533 if fp is not ui:
534 fp.close() 534 fp.close()
535 elif mbox: 535 else:
536 if not sendmail:
537 sendmail = mail.connect(ui, mbox=mbox)
536 ui.status(_('Sending '), subj, ' ...\n') 538 ui.status(_('Sending '), subj, ' ...\n')
537 ui.progress(_('sending'), i, item=subj, total=len(msgs)) 539 ui.progress(_('sending'), i, item=subj, total=len(msgs))
538 fp = open(mbox, i > 0 and 'ab+' or 'wb+') 540 if not mbox:
539 generator = email.Generator.Generator(fp, mangle_from_=True) 541 # Exim does not remove the Bcc field
540 # Should be time.asctime(), but Windows prints 2-characters day 542 del m['Bcc']
541 # of month instead of one. Make them print the same thing.
542 date = time.strftime('%a %b %d %H:%M:%S %Y', time.localtime())
543 fp.write('From %s %s\n' % (sender_addr, date))
544 generator.flatten(m, 0)
545 fp.write('\n\n')
546 fp.close()
547 else:
548 if not sendmail:
549 sendmail = mail.connect(ui)
550 ui.status(_('Sending '), subj, ' ...\n')
551 ui.progress(_('sending'), i, item=subj, total=len(msgs))
552 # Exim does not remove the Bcc field
553 del m['Bcc']
554 fp = cStringIO.StringIO() 543 fp = cStringIO.StringIO()
555 generator = email.Generator.Generator(fp, mangle_from_=False) 544 generator = email.Generator.Generator(fp, mangle_from_=False)
556 generator.flatten(m, 0) 545 generator.flatten(m, 0)
557 sendmail(sender_addr, to + bcc + cc, fp.getvalue()) 546 sendmail(sender_addr, to + bcc + cc, fp.getvalue())
558 547