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

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
@ -237,6 +235,8 @@ class FetchStatusBarCodingTime(threading.Thread):
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 = [
@ -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
@ -604,6 +607,7 @@ 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
@ -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,6 +670,7 @@ 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)