view unixSoft/bin/hg-email-reply-selected-flags @ 336:ea73ef5dc38c

emacs: avoid weird package.el breakage with newish packages I've been toting around this package.el from 2009 or so, and something in the package format seems to have changed that broke me. Thanks to some related diagnostics by Lucas, I've grabbed the last package.el that worked with emacs 23 and stashed it here. This seems to work, modulo some things (notably js2-mode and smex) now seem to require emacs 24 if you install them using package.el, so this will end up being brittle on my last couple of emacs23 machines.
author Augie Fackler <raf@durin42.com>
date Thu, 29 May 2014 14:30:42 -0400
parents 678ed4e870f1
children
line wrap: on
line source

#!/usr/bin/python
# shebang for system python explicitly so we're sure to have pyobjc and
# the scripting bridge.
"""apply-patchbomb: apply selected messages in Mail.app to an hg repo in pwd.

This uses the scripting bridge to talk to Mail and subprocess to pass the
patch to Mercurial on stdin.
"""
import cStringIO
import email
import optparse
import subprocess
import sys

import ScriptingBridge

# TODO: use a real shell escape
shell_escape = repr

def main(argv=sys.argv):
    p = optparse.OptionParser()
    p.add_option('--from', '-f', dest='from_', help='From address to use for the message.')
    options, args = p.parse_args(argv)
    assert options.from_
    mail = ScriptingBridge.SBApplication.applicationWithBundleIdentifier_(
        'com.apple.Mail')
    messages = list(sorted(mail.selection(),
                           cmp=lambda x,y: cmp(x.subject(), y.subject())))
    data = str(messages[0].source())
    m = email.message_from_string(data)
    msgid = m['Message-ID']
    to = m['To']
    cc = m['CC']
    from_ = m['From']
    response_addrs = from_.split(',') + cc.split(',') + to.split(',')
    response_addrs = [a for a in response_addrs if options.from_ not in a]

    print '--in-reply-to', shell_escape(msgid),
    print '--to', shell_escape(response_addrs[0]),
    if len(response_addrs) > 1:
        print '--cc', shell_escape(', '.join(response_addrs[1:])),
    print '--from', shell_escape(options.from_)


if __name__ == '__main__':
    sys.exit(main())