comparison unixSoft/bin/hg-email-reply-selected-flags @ 235:678ed4e870f1

hg-email-reply-selected-flags: new script to automate patchbomb replies
author Augie Fackler <durin42@gmail.com>
date Sun, 05 Dec 2010 10:00:31 -0600
parents
children
comparison
equal deleted inserted replaced
234:0cd241f35c40 235:678ed4e870f1
1 #!/usr/bin/python
2 # shebang for system python explicitly so we're sure to have pyobjc and
3 # the scripting bridge.
4 """apply-patchbomb: apply selected messages in Mail.app to an hg repo in pwd.
5
6 This uses the scripting bridge to talk to Mail and subprocess to pass the
7 patch to Mercurial on stdin.
8 """
9 import cStringIO
10 import email
11 import optparse
12 import subprocess
13 import sys
14
15 import ScriptingBridge
16
17 # TODO: use a real shell escape
18 shell_escape = repr
19
20 def main(argv=sys.argv):
21 p = optparse.OptionParser()
22 p.add_option('--from', '-f', dest='from_', help='From address to use for the message.')
23 options, args = p.parse_args(argv)
24 assert options.from_
25 mail = ScriptingBridge.SBApplication.applicationWithBundleIdentifier_(
26 'com.apple.Mail')
27 messages = list(sorted(mail.selection(),
28 cmp=lambda x,y: cmp(x.subject(), y.subject())))
29 data = str(messages[0].source())
30 m = email.message_from_string(data)
31 msgid = m['Message-ID']
32 to = m['To']
33 cc = m['CC']
34 from_ = m['From']
35 response_addrs = from_.split(',') + cc.split(',') + to.split(',')
36 response_addrs = [a for a in response_addrs if options.from_ not in a]
37
38 print '--in-reply-to', shell_escape(msgid),
39 print '--to', shell_escape(response_addrs[0]),
40 if len(response_addrs) > 1:
41 print '--cc', shell_escape(', '.join(response_addrs[1:])),
42 print '--from', shell_escape(options.from_)
43
44
45 if __name__ == '__main__':
46 sys.exit(main())