prevent executing wakatime-cli before it has been downloaded

This commit is contained in:
Alan Hamlett 2020-03-01 10:01:52 -08:00
parent 01503b1c20
commit 2e6a87c67e
1 changed files with 17 additions and 9 deletions

View File

@ -14,7 +14,6 @@ __version__ = '10.0.0'
import sublime import sublime
import sublime_plugin import sublime_plugin
import contextlib
import json import json
import os import os
import platform import platform
@ -24,7 +23,6 @@ import sys
import time import time
import threading import threading
import traceback import traceback
import urllib
import webbrowser import webbrowser
import ssl import ssl
import shutil import shutil
@ -231,15 +229,17 @@ class FetchStatusBarCodingTime(threading.Thread):
self.debug = SETTINGS.get('debug') self.debug = SETTINGS.get('debug')
self.api_key = APIKEY.read() or '' self.api_key = APIKEY.read() or ''
self.proxy = SETTINGS.get('proxy') self.proxy = SETTINGS.get('proxy')
def run(self): def run(self):
if not self.api_key: if not self.api_key:
log(DEBUG, 'Missing WakaTime API key.') log(DEBUG, 'Missing WakaTime API key.')
return return
if not isCliInstalled():
return
ua = 'sublime/%d sublime-wakatime/%s' % (ST_VERSION, __version__) ua = 'sublime/%d sublime-wakatime/%s' % (ST_VERSION, __version__)
cmd = [ cmd = [
API_CLIENT, API_CLIENT,
'--today', '--today',
'--key', str(bytes.decode(self.api_key.encode('utf8'))), '--key', str(bytes.decode(self.api_key.encode('utf8'))),
@ -382,9 +382,12 @@ def append_heartbeat(entity, timestamp, is_write, view, project, folders):
def process_queue(timestamp): def process_queue(timestamp):
global LAST_HEARTBEAT_SENT_AT global LAST_HEARTBEAT_SENT_AT
if not isCliInstalled():
return
# Prevent sending heartbeats more often than SEND_BUFFER_SECONDS # Prevent sending heartbeats more often than SEND_BUFFER_SECONDS
now = int(time.time()) now = int(time.time())
if isCliInstalled() and timestamp != LAST_HEARTBEAT['time'] and LAST_HEARTBEAT_SENT_AT > now - SEND_BUFFER_SECONDS: if timestamp != LAST_HEARTBEAT['time'] and LAST_HEARTBEAT_SENT_AT > now - SEND_BUFFER_SECONDS:
return return
LAST_HEARTBEAT_SENT_AT = now LAST_HEARTBEAT_SENT_AT = now
@ -524,7 +527,7 @@ def plugin_loaded():
if not isCliLatest(): if not isCliLatest():
thread = DownloadCLI() thread = DownloadCLI()
thread.start() thread.start()
log(INFO, 'Finished initializing WakaTime v%s' % __version__) log(INFO, 'Finished initializing WakaTime v%s' % __version__)
after_loaded() after_loaded()
@ -604,10 +607,11 @@ class DownloadCLI(threading.Thread):
def isCliInstalled(): def isCliInstalled():
return os.path.exists(API_CLIENT) return os.path.exists(API_CLIENT)
def isCliLatest(): def isCliLatest():
if not isCliInstalled(): if not isCliInstalled():
return False return False
args = [API_CLIENT, '--version'] args = [API_CLIENT, '--version']
stdout, stderr = Popen(args, stdout=PIPE, stderr=PIPE).communicate() stdout, stderr = Popen(args, stdout=PIPE, stderr=PIPE).communicate()
stdout = (stdout or b'') + (stderr or b'') stdout = (stdout or b'') + (stderr or b'')
@ -618,7 +622,7 @@ def isCliLatest():
log(INFO, 'Current wakatime-cli version is %s' % localVer) log(INFO, 'Current wakatime-cli version is %s' % localVer)
log(INFO, 'Checking for updates to wakatime-cli...') log(INFO, 'Checking for updates to wakatime-cli...')
remoteVer = getLatestCliVersion() remoteVer = getLatestCliVersion()
if not remoteVer: if not remoteVer:
return True return True
@ -630,6 +634,7 @@ def isCliLatest():
log(INFO, 'Found an updated wakatime-cli v%s' % remoteVer) log(INFO, 'Found an updated wakatime-cli v%s' % remoteVer)
return False return False
def getLatestCliVersion(): def getLatestCliVersion():
url = getCliVersionUrl() url = getCliVersionUrl()
try: try:
@ -646,6 +651,7 @@ def getLatestCliVersion():
except: except:
return None return None
def getCliVersionUrl(): def getCliVersionUrl():
os = platform.system().lower().replace('darwin', 'mac') os = platform.system().lower().replace('darwin', 'mac')
arch = '64' if sys.maxsize > 2**32 else '32' arch = '64' if sys.maxsize > 2**32 else '32'
@ -655,6 +661,7 @@ def getCliVersionUrl():
arch=arch, arch=arch,
) )
def extractVersion(text): def extractVersion(text):
log(DEBUG, 'extracting version.') log(DEBUG, 'extracting version.')
pattern = re.compile(r"([0-9]+\.[0-9]+\.[0-9]+)") pattern = re.compile(r"([0-9]+\.[0-9]+\.[0-9]+)")
@ -663,9 +670,10 @@ def extractVersion(text):
return match.group(1) return match.group(1)
return None return None
def download(url, filePath): def download(url, filePath):
try: try:
urlretrieve(url, filePath) urlretrieve(url, filePath)
except IOError: except IOError:
ssl._create_default_https_context = ssl._create_unverified_context ssl._create_default_https_context = ssl._create_unverified_context
urlretrieve(url, filePath) urlretrieve(url, filePath)