Permalink
Browse files

Allow for creating gists using curl if SSL support isn't compiled int…

…o SublimeText's python (as in the case on Linux)

Fixes issue #2
  • Loading branch information...
1 parent b5887e1 commit f30db079935e73b58583a01389b1a144850e2343 @bgreenlee committed Sep 15, 2011
Showing with 90 additions and 12 deletions.
  1. +35 −0 commandline.py
  2. +27 −0 curl.py
  3. +28 −12 gist.py
View
@@ -0,0 +1,35 @@
+# adapted from https://github.com/wbond/sublime_package_control/blob/master/Package%20Control.py
+
+import os.path
+import subprocess
+
+class BinaryNotFoundError(Exception):
+ pass
+
+class NonCleanExitError(Exception):
+ def __init__(self, returncode):
+ self.returncode = returncode
+
+ def __str__(self):
+ return repr(self.returncode)
+
+def find_binary(name):
+ dirs = ['/usr/local/sbin', '/usr/local/bin', '/usr/sbin', '/usr/bin',
+ '/sbin', '/bin']
+ for dir in dirs:
+ path = os.path.join(dir, name)
+ if os.path.exists(path):
+ return path
+
+ raise BinaryNotFoundError('The binary ' + name + ' could not be ' + \
+ 'located')
+
+def execute(args):
+ proc = subprocess.Popen(args, stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+
+ output = proc.stdout.read()
+ returncode = proc.wait()
+ if returncode != 0:
+ raise NonCleanExitError(returncode)
+ return output
View
@@ -0,0 +1,27 @@
+# adapted from https://github.com/wbond/sublime_package_control/blob/master/Package%20Control.py
+import commandline
+
+def post(url, data, tries = 3):
+ curl = commandline.find_binary('curl')
+ if not curl:
+ return False
+ command = [curl, '-f', '--user-agent', 'Sublime Github', '-s',
+ '-d', data, url]
+
+ while tries > 1:
+ tries -= 1
+ try:
+ return commandline.execute(command)
+ except (commandline.NonCleanExitError) as (e):
+ if e.returncode == 22:
+ error_string = 'HTTP error 404'
+ elif e.returncode == 6:
+ error_string = 'URL error host not found'
+ else:
+ print "%s: Downloading %s timed out, trying again" % (__name__, url)
+ continue
+
+ sublime.error_message(__name__ + ': ' + error_message +
+ ' ' + error_string + ' posting ' + url + '.')
+ break
+ return False
View
@@ -3,6 +3,9 @@
import subprocess
import httplib
import urllib
+import re
+import curl
+from commandline import BinaryNotFoundError
import sublime
import sublime_plugin
@@ -33,7 +36,6 @@ def generate_from_environment(cls):
return cls(user, token)
-
class GistUnauthorizedException(Exception):
"Raised if we get a 401 from Github"
pass
@@ -71,17 +73,27 @@ def create(self):
}
if not self.public:
params['action_button'] = 'private'
- conn = httplib.HTTPSConnection("gist.github.com")
- req = conn.request("POST", "/gists", urllib.urlencode(params))
- response = conn.getresponse()
- conn.close()
- if response.status == 302: # success
- gist_url = response.getheader("Location")
- return gist_url
- elif response.status == 401: # unauthorized
- raise GistUnauthorizedException()
- else:
- raise GistCreationException(self.ERR_CREATING % (response.status, response.reason))
+ if hasattr(httplib, "HTTPSConnection"):
+ conn = httplib.HTTPSConnection("gist.github.com")
+ req = conn.request("POST", "/gists", urllib.urlencode(params))
+ response = conn.getresponse()
+ conn.close()
+ if response.status == 302: # success
+ gist_url = response.getheader("Location")
+ return gist_url
+ elif response.status == 401: # unauthorized
+ raise GistUnauthorizedException()
+ else:
+ raise GistCreationException(self.ERR_CREATING % (response.status, response.reason))
+ else: # try curl
+ curl_response = curl.post("https://gist.github.com/gists",
+ urllib.urlencode(params))
+ m = re.match(r'.*?You are being <a href="(.*?)">redirected', curl_response)
+ if m and m.group(1):
+ return m.group(1)
+ else:
+ raise GistCreationException(self.ERR_CREATING %
+ ("Got response:", curl_response))
class GistFromSelectionCommand(sublime_plugin.TextCommand):
@@ -101,6 +113,8 @@ class GistFromSelectionCommand(sublime_plugin.TextCommand):
"incorrect. Please check them and try again.\n\n"\
"See http://help.github.com/set-your-user-name-email-and-github-token/ "\
"for more information."
+ ERR_NO_CURL = "Couldn't find curl, which is required for this to work "\
+ "under Linux. Please install curl and try again."
def run(self, edit):
self.github_user = None
@@ -150,6 +164,8 @@ def on_done(self, value):
sublime.error_message(self.ERR_UNAUTHORIZED)
except GistCreationException, e:
sublime.error_message(e.message)
+ except BinaryNotFoundError, e:
+ sublime.error_message(self.ERR_NO_CURL)
class PrivateGistFromSelectionCommand(GistFromSelectionCommand):

0 comments on commit f30db07

Please sign in to comment.