annotate unixSoft/bin/hg-email-reply-selected-flags @ 525:b0fa9e7cadac

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