|
29 | 29 | import pydoc |
30 | 30 | import re |
31 | 31 | import rlcompleter |
| 32 | +import subprocess |
32 | 33 | import sys |
33 | 34 | import textwrap |
34 | 35 | import traceback |
@@ -770,10 +771,13 @@ def pastebin(self, s=None): |
770 | 771 | not self.interact.confirm("Pastebin buffer? (y/N) ")): |
771 | 772 | self.interact.notify("Pastebin aborted") |
772 | 773 | return |
773 | | - return self.do_pastebin(s) |
| 774 | + if self.config.pastebin_helper: |
| 775 | + return self.do_pastebin_helper(s) |
| 776 | + else: |
| 777 | + return self.do_pastebin_xmlrpc(s) |
774 | 778 |
|
775 | | - def do_pastebin(self, s): |
776 | | - """Actually perform the upload.""" |
| 779 | + def do_pastebin_xmlrpc(self, s): |
| 780 | + """Upload to pastebin via XML-RPC.""" |
777 | 781 | try: |
778 | 782 | pasteservice = ServerProxy(self.config.pastebin_url) |
779 | 783 | except IOError, e: |
@@ -803,6 +807,40 @@ def do_pastebin(self, s): |
803 | 807 | self.interact.notify('Pastebin URL: %s' % (paste_url, ), 10) |
804 | 808 | return paste_url |
805 | 809 |
|
| 810 | + def do_pastebin_helper(self, s): |
| 811 | + """Call out to helper program for pastebin upload.""" |
| 812 | + if s == self.prev_pastebin_content: |
| 813 | + self.interact.notify('Duplicate pastebin. Previous URL: ' + |
| 814 | + self.prev_pastebin_url) |
| 815 | + return self.prev_pastebin_url |
| 816 | + |
| 817 | + self.interact.notify('Posting data to pastebin...') |
| 818 | + |
| 819 | + try: |
| 820 | + helper = subprocess.Popen('', executable=self.config.pastebin_helper, |
| 821 | + stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| 822 | + helper.stdin.write(s.encode()) |
| 823 | + paste_url = helper.communicate()[0].decode().strip() |
| 824 | + except OSError, e: |
| 825 | + if e.errno == 2: |
| 826 | + self.interact.notify('Upload failed: Helper program not found.') |
| 827 | + else: |
| 828 | + self.interact.notify('Upload failed: Helper program could not be run.') |
| 829 | + return |
| 830 | + |
| 831 | + if helper.returncode != 0: |
| 832 | + self.interact.notify('Upload failed: Helper program returned non-zero exit status %s.' % |
| 833 | + (helper.returncode, )) |
| 834 | + return |
| 835 | + |
| 836 | + if not paste_url: |
| 837 | + self.interact.notify('Upload failed: No output from helper program.') |
| 838 | + return |
| 839 | + |
| 840 | + self.prev_pastebin_content = s |
| 841 | + self.interact.notify('Pastebin URL: %s' % (paste_url, ), 10) |
| 842 | + return paste_url |
| 843 | + |
806 | 844 | def push(self, s, insert_into_history=True): |
807 | 845 | """Push a line of code onto the buffer so it can process it all |
808 | 846 | at once when a code block ends""" |
|
0 commit comments