always use external python binary because sublime text bundled binary does not fully support SSL with requests package

This commit is contained in:
Alan Hamlett 2015-03-31 16:19:30 -07:00
parent 919064200b
commit b801759cdf

View File

@ -6,8 +6,10 @@ License: BSD, see LICENSE for more details.
Website: https://wakatime.com/ Website: https://wakatime.com/
===========================================================""" ==========================================================="""
__version__ = '3.0.13' __version__ = '3.0.13'
import sublime import sublime
import sublime_plugin import sublime_plugin
@ -19,13 +21,14 @@ import time
import threading import threading
import webbrowser import webbrowser
from datetime import datetime from datetime import datetime
from os.path import expanduser, dirname, basename, realpath, join from subprocess import Popen
# globals # globals
ACTION_FREQUENCY = 2 ACTION_FREQUENCY = 2
ST_VERSION = int(sublime.version()) ST_VERSION = int(sublime.version())
PLUGIN_DIR = dirname(realpath(__file__)) PLUGIN_DIR = os.path.dirname(os.path.realpath(__file__))
API_CLIENT = '%s/packages/wakatime/cli.py' % PLUGIN_DIR API_CLIENT = os.path.join(PLUGIN_DIR, 'packages', 'wakatime', 'cli.py')
SETTINGS_FILE = 'WakaTime.sublime-settings' SETTINGS_FILE = 'WakaTime.sublime-settings'
SETTINGS = {} SETTINGS = {}
LAST_ACTION = { LAST_ACTION = {
@ -33,27 +36,16 @@ LAST_ACTION = {
'file': None, 'file': None,
'is_write': False, 'is_write': False,
} }
HAS_SSL = False
LOCK = threading.RLock() LOCK = threading.RLock()
PYTHON_LOCATION = None PYTHON_LOCATION = None
# add wakatime package to path # add wakatime package to path
sys.path.insert(0, join(PLUGIN_DIR, 'packages')) sys.path.insert(0, os.path.join(PLUGIN_DIR, 'packages'))
# check if we have SSL support
try: try:
import ssl from wakatime.base import parseConfigFile
import socket except ImportError:
assert ssl pass
assert socket.ssl
assert ssl.OPENSSL_VERSION
HAS_SSL = True
except (ImportError, AttributeError):
from subprocess import Popen
if HAS_SSL:
# import wakatime package so we can use built-in python
import wakatime
def createConfigFile(): def createConfigFile():
@ -81,7 +73,6 @@ def prompt_api_key():
default_key = '' default_key = ''
try: try:
from wakatime.base import parseConfigFile
configs = parseConfigFile() configs = parseConfigFile()
if configs is not None: if configs is not None:
if configs.has_option('settings', 'api_key'): if configs.has_option('settings', 'api_key'):
@ -123,7 +114,7 @@ def python_binary():
except: except:
pass pass
for path in glob.iglob('/python*'): for path in glob.iglob('/python*'):
path = realpath(join(path, 'pythonw')) path = os.path.realpath(os.path.join(path, 'pythonw'))
try: try:
Popen([path, '--version']) Popen([path, '--version'])
PYTHON_LOCATION = path PYTHON_LOCATION = path
@ -198,7 +189,7 @@ class SendActionThread(threading.Thread):
if self.is_write: if self.is_write:
cmd.append('--write') cmd.append('--write')
if self.project: if self.project:
self.project = basename(self.project).replace('.sublime-project', '', 1) self.project = os.path.basename(self.project).replace('.sublime-project', '', 1)
if self.project: if self.project:
cmd.extend(['--project', self.project]) cmd.extend(['--project', self.project])
elif self.folders: elif self.folders:
@ -209,24 +200,14 @@ class SendActionThread(threading.Thread):
cmd.extend(['--ignore', pattern]) cmd.extend(['--ignore', pattern])
if self.debug: if self.debug:
cmd.append('--verbose') cmd.append('--verbose')
if HAS_SSL: if python_binary():
cmd.insert(0, python_binary())
if self.debug: if self.debug:
print('[WakaTime] %s' % ' '.join(cmd)) print('[WakaTime] %s' % ' '.join(cmd))
code = wakatime.main(cmd)
if code != 0:
print('[WakaTime] Error: Response code %d from wakatime package.' % code)
else:
self.sent()
else:
python = python_binary()
if python:
cmd.insert(0, python)
if self.debug:
print('[WakaTime] %s %s' % (python, ' '.join(cmd)))
if platform.system() == 'Windows': if platform.system() == 'Windows':
Popen(cmd, shell=False) Popen(cmd, shell=False)
else: else:
with open(join(expanduser('~'), '.wakatime.log'), 'a') as stderr: with open(os.path.join(os.path.expanduser('~'), '.wakatime.log'), 'a') as stderr:
Popen(cmd, stderr=stderr) Popen(cmd, stderr=stderr)
self.sent() self.sent()
else: else:
@ -253,9 +234,7 @@ def plugin_loaded():
global SETTINGS global SETTINGS
print('[WakaTime] Initializing WakaTime plugin v%s' % __version__) print('[WakaTime] Initializing WakaTime plugin v%s' % __version__)
if not HAS_SSL: if not python_binary():
python = python_binary()
if not python:
sublime.error_message("Unable to find Python binary!\nWakaTime needs Python to work correctly.\n\nGo to https://www.python.org/downloads") sublime.error_message("Unable to find Python binary!\nWakaTime needs Python to work correctly.\n\nGo to https://www.python.org/downloads")
return return