10
10
import ssl
11
11
import subprocess
12
12
import sys
13
+ import time
13
14
import traceback
14
15
from subprocess import PIPE
15
16
from zipfile import ZipFile
@@ -36,7 +37,7 @@ def getOsName():
36
37
37
38
38
39
GITHUB_RELEASES_STABLE_URL = 'https://api.github.com/repos/wakatime/wakatime-cli/releases/latest'
39
- GITHUB_DOWNLOAD_PREFIX = 'https://github.com/wakatime/wakatime-cli/releases/download'
40
+ GITHUB_DOWNLOAD_URL = 'https://github.com/wakatime/wakatime-cli/releases/latest /download'
40
41
PLUGIN = 'vim'
41
42
42
43
is_py2 = (sys .version_info [0 ] == 2 )
@@ -298,8 +299,27 @@ def isCliLatest():
298
299
return True
299
300
300
301
log ('Current wakatime-cli version is %s' % localVer )
301
- log ('Checking for updates to wakatime-cli...' )
302
302
303
+ configs , last_accessed = None , None
304
+ try :
305
+ configs = parseConfigFile (getConfigFile (True ))
306
+ if configs and configs .has_option ('internal' , 'cli_version_last_accessed' ):
307
+ last_accessed = configs .get ('internal' , 'cli_version_last_accessed' )
308
+ except :
309
+ log (traceback .format_exc ())
310
+
311
+ if last_accessed :
312
+ try :
313
+ last_accessed = int (float (last_accessed ))
314
+ except :
315
+ last_accessed = None
316
+ now = round (time .time ())
317
+ four_hours = 4 * 3600
318
+ if last_accessed and last_accessed + four_hours > now :
319
+ log ('Skip checking for wakatime-cli updates because recently checked {0} seconds ago.' .format (now - last_accessed ))
320
+ return True
321
+
322
+ log ('Checking for updates to wakatime-cli...' )
303
323
remoteVer = getLatestCliVersion ()
304
324
305
325
if not remoteVer :
@@ -313,50 +333,31 @@ def isCliLatest():
313
333
return False
314
334
315
335
316
- LATEST_CLI_VERSION = None
317
-
318
-
319
336
def getLatestCliVersion ():
320
- global LATEST_CLI_VERSION
321
-
322
- if LATEST_CLI_VERSION :
323
- return LATEST_CLI_VERSION
324
-
325
- configs , last_modified , last_version = None , None , None
326
- try :
327
- configs = parseConfigFile (getConfigFile (True ))
328
- if configs :
329
- if configs .has_option ('internal' , 'cli_version' ):
330
- last_version = configs .get ('internal' , 'cli_version' )
331
- if last_version and configs .has_option ('internal' , 'cli_version_last_modified' ):
332
- last_modified = configs .get ('internal' , 'cli_version_last_modified' )
333
- except :
334
- log (traceback .format_exc ())
335
-
336
337
try :
337
- headers , contents , code = request (GITHUB_RELEASES_STABLE_URL , last_modified = last_modified )
338
+ headers , contents , code = request (GITHUB_RELEASES_STABLE_URL )
338
339
339
340
log ('GitHub API Response {0}' .format (code ))
340
341
341
- if code == 304 :
342
- LATEST_CLI_VERSION = last_version
343
- return last_version
344
-
345
342
data = json .loads (contents .decode ('utf-8' ))
346
343
347
344
ver = data ['tag_name' ]
348
345
log ('Latest wakatime-cli version from GitHub: {0}' .format (ver ))
349
346
347
+ try :
348
+ configs = parseConfigFile (getConfigFile (True ))
349
+ except :
350
+ log (traceback .format_exc ())
350
351
if configs :
351
352
last_modified = headers .get ('Last-Modified' )
352
353
if not configs .has_section ('internal' ):
353
354
configs .add_section ('internal' )
354
355
configs .set ('internal' , 'cli_version' , str (u (ver )))
355
356
configs .set ('internal' , 'cli_version_last_modified' , str (u (last_modified )))
357
+ configs .set ('internal' , 'cli_version_last_accessed' , str (round (time .time ())))
356
358
with open (getConfigFile (True ), 'w' , encoding = 'utf-8' ) as fh :
357
359
configs .write (fh )
358
360
359
- LATEST_CLI_VERSION = ver
360
361
return ver
361
362
except :
362
363
log (traceback .format_exc ())
@@ -404,11 +405,8 @@ def cliDownloadUrl():
404
405
if check not in validCombinations :
405
406
reportMissingPlatformSupport (osname , arch )
406
407
407
- version = getLatestCliVersion ()
408
-
409
- return '{prefix}/{version}/wakatime-cli-{osname}-{arch}.zip' .format (
410
- prefix = GITHUB_DOWNLOAD_PREFIX ,
411
- version = version ,
408
+ return '{prefix}/wakatime-cli-{osname}-{arch}.zip' .format (
409
+ prefix = GITHUB_DOWNLOAD_URL ,
412
410
osname = osname ,
413
411
arch = arch ,
414
412
)
@@ -423,17 +421,14 @@ def reportMissingPlatformSupport(osname, arch):
423
421
request (url )
424
422
425
423
426
- def request (url , last_modified = None ):
424
+ def request (url ):
427
425
req = Request (url )
428
426
req .add_header ('User-Agent' , 'github.com/wakatime/{plugin}-wakatime' .format (plugin = PLUGIN ))
429
427
430
428
proxy = CONFIGS .get ('settings' , 'proxy' ) if CONFIGS .has_option ('settings' , 'proxy' ) else None
431
429
if proxy :
432
430
req .set_proxy (proxy , 'https' )
433
431
434
- if last_modified :
435
- req .add_header ('If-Modified-Since' , last_modified )
436
-
437
432
try :
438
433
resp = urlopen (req )
439
434
try :
0 commit comments