view unixSoft/bin/apply-patchbomb @ 357:c7a9cd793e37

patchpipe: handle base64-encoded message bodies It looks like the new version of mailman on selenic now base64s message bodies, so detect that and unpack the base64'd body.
author Augie Fackler <raf@durin42.com>
date Mon, 22 Dec 2014 17:12:53 -0500
parents 4b661cede8ad
children 0b8457903012
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 email
import subprocess
import sys

import ScriptingBridge

def main():
    mail = ScriptingBridge.SBApplication.applicationWithBundleIdentifier_(
        'com.apple.Mail')
    messages = list(sorted(mail.selection(),
                           cmp=lambda x,y: cmp(x.subject(), y.subject())))
    for m in messages:
        print 'Applying', m.subject()
        p = subprocess.Popen(['hg', 'import', '--partial', '--obsolete', '-'], stdin=subprocess.PIPE)
        p.stdin.write(m.source())
        p.stdin.close()
        if p.wait() != 0:
            print 'hg import failed, bailing'
            return 2
    return 0

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