-
Notifications
You must be signed in to change notification settings - Fork 45
/
WakaTime.py
1008 lines (818 loc) · 30.3 KB
/
WakaTime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
""" ==========================================================
File: WakaTime.py
Description: Automatic time tracking for Sublime Text 2 and 3.
Maintainer: WakaTime <[email protected]>
License: BSD, see LICENSE for more details.
Website: https://wakatime.com/
==========================================================="""
__version__ = '11.1.1'
import sublime
import sublime_plugin
import json
import os
import platform
import re
import shutil
import ssl
import subprocess
import sys
import time
import threading
import traceback
import webbrowser
from subprocess import STDOUT, PIPE
from zipfile import ZipFile
try:
import Queue as queue # py2
except ImportError:
import queue # py3
try:
from ConfigParser import SafeConfigParser as ConfigParser
from ConfigParser import Error as ConfigParserError
except ImportError:
from configparser import ConfigParser, Error as ConfigParserError
try:
from urllib2 import Request, urlopen, HTTPError
except ImportError:
from urllib.request import Request, urlopen
from urllib.error import HTTPError
is_py2 = (sys.version_info[0] == 2)
is_py3 = (sys.version_info[0] == 3)
is_win = platform.system() == 'Windows'
if is_py2:
import codecs
open = codecs.open
def u(text):
if text is None:
return None
if isinstance(text, unicode): # noqa: F821
return text
try:
return text.decode('utf-8')
except:
try:
return text.decode(sys.getdefaultencoding())
except:
try:
return unicode(text) # noqa: F821
except:
try:
return text.decode('utf-8', 'replace')
except:
try:
return unicode(str(text)) # noqa: F821
except:
return unicode('') # noqa: F821
elif is_py3:
def u(text):
if text is None:
return None
if isinstance(text, bytes):
try:
return text.decode('utf-8')
except:
try:
return text.decode(sys.getdefaultencoding())
except:
pass
try:
return str(text)
except:
return text.decode('utf-8', 'replace')
else:
raise Exception('Unsupported Python version: {0}.{1}.{2}'.format(
sys.version_info[0],
sys.version_info[1],
sys.version_info[2],
))
class Popen(subprocess.Popen):
"""Patched Popen to prevent opening cmd window on Windows platform."""
def __init__(self, *args, **kwargs):
if is_win:
startupinfo = kwargs.get('startupinfo')
try:
startupinfo = startupinfo or subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
except AttributeError:
pass
kwargs['startupinfo'] = startupinfo
super(Popen, self).__init__(*args, **kwargs)
# globals
ST_VERSION = int(sublime.version())
HOME_FOLDER = os.path.realpath(os.environ.get('WAKATIME_HOME') or os.path.expanduser('~'))
RESOURCES_FOLDER = os.path.join(HOME_FOLDER, '.wakatime')
CONFIG_FILE = os.path.join(HOME_FOLDER, '.wakatime.cfg')
INTERNAL_CONFIG_FILE = os.path.join(HOME_FOLDER, '.wakatime-internal.cfg')
GITHUB_RELEASES_STABLE_URL = 'https://api.github.com/repos/wakatime/wakatime-cli/releases/latest'
GITHUB_DOWNLOAD_PREFIX = 'https://github.com/wakatime/wakatime-cli/releases/download'
SETTINGS_FILE = 'WakaTime.sublime-settings'
SETTINGS = {}
LAST_HEARTBEAT = {
'time': 0,
'file': None,
'is_write': False,
}
LAST_HEARTBEAT_SENT_AT = 0
LAST_FETCH_TODAY_CODING_TIME = 0
FETCH_TODAY_DEBOUNCE_COUNTER = 0
FETCH_TODAY_DEBOUNCE_SECONDS = 60
LATEST_CLI_VERSION = None
WAKATIME_CLI_LOCATION = None
HEARTBEATS = queue.Queue()
HEARTBEAT_FREQUENCY = 2 # minutes between logging heartbeat when editing same file
SEND_BUFFER_SECONDS = 30 # seconds between sending buffered heartbeats to API
# Log Levels
DEBUG = 'DEBUG'
INFO = 'INFO'
WARNING = 'WARNING'
ERROR = 'ERROR'
def parseConfigFile(configFile):
"""Returns a configparser.SafeConfigParser instance with configs
read from the config file. Default location of the config file is
at ~/.wakatime.cfg.
"""
kwargs = {} if is_py2 else {'strict': False}
configs = ConfigParser(**kwargs)
try:
with open(configFile, 'r', encoding='utf-8') as fh:
try:
if is_py2:
configs.readfp(fh)
else:
configs.read_file(fh)
return configs
except ConfigParserError:
log(ERROR, traceback.format_exc())
return None
except IOError:
log(DEBUG, "Error: Could not read from config file {0}\n".format(configFile))
return configs
class ApiKey(object):
_key = None
def read(self):
if self._key:
return self._key
key = SETTINGS.get('api_key')
if key:
self._key = key
return self._key
configs = None
try:
configs = parseConfigFile(CONFIG_FILE)
if configs:
if configs.has_option('settings', 'api_key'):
key = configs.get('settings', 'api_key')
if key:
self._key = key
return self._key
except:
pass
key = self.api_key_from_vault_cmd(configs)
if key:
self._key = key
return self._key
return self._key
def api_key_from_vault_cmd(self, configs):
vault_cmd = SETTINGS.get('api_key_vault_cmd')
if not vault_cmd and configs:
try:
if configs.has_option('settings', 'api_key_vault_cmd'):
vault_cmd = configs.get('settings', 'api_key_vault_cmd')
except:
pass
if not vault_cmd or not vault_cmd.strip():
return None
try:
process = Popen(vault_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
retcode = process.poll()
if retcode:
log(ERROR, 'Vault command error ({retcode}): {stderr}'.format(retcode=retcode, stderr=u(stderr)))
return None
return stdout.strip() or None
except:
log(ERROR, traceback.format_exc())
return None
def write(self, key):
global SETTINGS
self._key = key
SETTINGS.set('api_key', str(key))
sublime.save_settings(SETTINGS_FILE)
APIKEY = ApiKey()
def set_timeout(callback, seconds):
"""Runs the callback after the given seconds delay.
If this is Sublime Text 3, runs the callback on an alternate thread. If this
is Sublime Text 2, runs the callback in the main thread.
"""
milliseconds = int(seconds * 1000)
try:
sublime.set_timeout_async(callback, milliseconds)
except AttributeError:
sublime.set_timeout(callback, milliseconds)
def log(lvl, message, *args, **kwargs):
try:
if lvl == DEBUG and not SETTINGS.get('debug'):
return
msg = message
if len(args) > 0:
msg = message.format(*args)
elif len(kwargs) > 0:
msg = message.format(**kwargs)
try:
print('[WakaTime] [{lvl}] {msg}'.format(lvl=lvl, msg=msg))
except UnicodeDecodeError:
print(u('[WakaTime] [{lvl}] {msg}').format(lvl=lvl, msg=u(msg)))
except RuntimeError:
set_timeout(lambda: log(lvl, message, *args, **kwargs), 0)
def update_status_bar(status=None, debounced=False, msg=None):
"""Updates the status bar."""
global LAST_FETCH_TODAY_CODING_TIME, FETCH_TODAY_DEBOUNCE_COUNTER
try:
if not msg and SETTINGS.get('status_bar_message') is not False and SETTINGS.get('status_bar_enabled'):
if SETTINGS.get('status_bar_coding_activity') and status == 'OK':
if debounced:
FETCH_TODAY_DEBOUNCE_COUNTER -= 1
if debounced or not LAST_FETCH_TODAY_CODING_TIME:
now = int(time.time())
if LAST_FETCH_TODAY_CODING_TIME and (FETCH_TODAY_DEBOUNCE_COUNTER > 0 or LAST_FETCH_TODAY_CODING_TIME > now - FETCH_TODAY_DEBOUNCE_SECONDS):
return
LAST_FETCH_TODAY_CODING_TIME = now
FetchStatusBarCodingTime().start()
return
else:
FETCH_TODAY_DEBOUNCE_COUNTER += 1
set_timeout(lambda: update_status_bar(status, debounced=True), FETCH_TODAY_DEBOUNCE_SECONDS)
return
else:
msg = 'WakaTime: {status}'.format(status=status)
if msg:
active_window = sublime.active_window()
if active_window:
for view in active_window.views():
view.set_status('wakatime', msg)
except RuntimeError:
set_timeout(lambda: update_status_bar(status=status, debounced=debounced, msg=msg), 0)
class FetchStatusBarCodingTime(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.debug = SETTINGS.get('debug')
self.api_key = APIKEY.read() or ''
self.proxy = SETTINGS.get('proxy')
def run(self):
if not self.api_key:
log(DEBUG, 'Missing WakaTime API key.')
return
if not isCliInstalled():
return
ua = 'sublime/%d sublime-wakatime/%s' % (ST_VERSION, __version__)
cmd = [
getCliLocation(),
'--today',
'--key', str(bytes.decode(self.api_key.encode('utf8'))),
'--plugin', ua,
]
if self.debug:
cmd.append('--verbose')
if self.proxy:
cmd.extend(['--proxy', self.proxy])
log(DEBUG, ' '.join(obfuscate_apikey(cmd)))
try:
process = Popen(cmd, stdout=PIPE, stderr=STDOUT)
output, err = process.communicate()
output = u(output)
if output:
output = output.strip()
retcode = process.poll()
if not retcode and output:
msg = 'Today: {output}'.format(output=output)
update_status_bar(msg=msg)
else:
log(DEBUG, 'wakatime-core today exited with status: {0}'.format(retcode))
if output:
log(DEBUG, u('wakatime-core today output: {0}').format(output))
except:
pass
def prompt_api_key():
if APIKEY.read():
return True
window = sublime.active_window()
if window:
def got_key(text):
if text:
APIKEY.write(text)
window.show_input_panel('[WakaTime] Enter your wakatime.com api key:', '', got_key, None, None)
return True
else:
log(ERROR, 'Could not prompt for api key because no window found.')
return False
def obfuscate_apikey(command_list):
cmd = list(command_list)
apikey_index = None
for num in range(len(cmd)):
if cmd[num] == '--key':
apikey_index = num + 1
break
if apikey_index is not None and apikey_index < len(cmd):
cmd[apikey_index] = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX' + cmd[apikey_index][-4:]
return cmd
def enough_time_passed(now, is_write):
if now - LAST_HEARTBEAT['time'] > HEARTBEAT_FREQUENCY * 60:
return True
if is_write and now - LAST_HEARTBEAT['time'] > 2:
return True
return False
def find_folder_containing_file(folders, current_file):
"""Returns absolute path to folder containing the file.
"""
parent_folder = None
current_folder = current_file
while True:
for folder in folders:
if os.path.realpath(os.path.dirname(current_folder)) == os.path.realpath(folder):
parent_folder = folder
break
if parent_folder is not None:
break
if not current_folder or os.path.dirname(current_folder) == current_folder:
break
current_folder = os.path.dirname(current_folder)
return parent_folder
def find_project_from_folders(folders, current_file):
"""Find project name from open folders.
"""
folder = find_folder_containing_file(folders, current_file)
return os.path.basename(folder) if folder else None
def is_view_active(view):
if view:
active_window = sublime.active_window()
if active_window:
active_view = active_window.active_view()
if active_view:
return active_view.buffer_id() == view.buffer_id()
return False
def handle_activity(view, is_write=False):
window = view.window()
if window is not None:
entity = view.file_name()
if entity:
timestamp = time.time()
last_file = LAST_HEARTBEAT['file']
if entity != last_file or enough_time_passed(timestamp, is_write):
project = window.project_data() if hasattr(window, 'project_data') else None
folders = window.folders()
append_heartbeat(entity, timestamp, is_write, view, project, folders)
def append_heartbeat(entity, timestamp, is_write, view, project, folders):
global LAST_HEARTBEAT
# add this heartbeat to queue
heartbeat = {
'entity': entity,
'timestamp': timestamp,
'is_write': is_write,
'project': project,
'folders': folders,
'lines_in_file': view.rowcol(view.size())[0] + 1,
}
selections = view.sel()
if selections and len(selections) > 0:
rowcol = view.rowcol(selections[0].begin())
row, col = rowcol[0] + 1, rowcol[1] + 1
heartbeat['lineno'] = row
heartbeat['cursorpos'] = col
HEARTBEATS.put_nowait(heartbeat)
# make this heartbeat the LAST_HEARTBEAT
LAST_HEARTBEAT = {
'file': entity,
'time': timestamp,
'is_write': is_write,
}
# process the queue of heartbeats in the future
set_timeout(lambda: process_queue(timestamp), SEND_BUFFER_SECONDS)
def process_queue(timestamp):
global LAST_HEARTBEAT_SENT_AT
if not isCliInstalled():
return
# Prevent sending heartbeats more often than SEND_BUFFER_SECONDS
now = int(time.time())
if timestamp != LAST_HEARTBEAT['time'] and LAST_HEARTBEAT_SENT_AT > now - SEND_BUFFER_SECONDS:
return
LAST_HEARTBEAT_SENT_AT = now
try:
heartbeat = HEARTBEATS.get_nowait()
except queue.Empty:
return
has_extra_heartbeats = False
extra_heartbeats = []
try:
while True:
extra_heartbeats.append(HEARTBEATS.get_nowait())
has_extra_heartbeats = True
except queue.Empty:
pass
thread = SendHeartbeatsThread(heartbeat)
if has_extra_heartbeats:
thread.add_extra_heartbeats(extra_heartbeats)
thread.start()
class SendHeartbeatsThread(threading.Thread):
"""Non-blocking thread for sending heartbeats to api.
"""
def __init__(self, heartbeat):
threading.Thread.__init__(self)
self.debug = SETTINGS.get('debug')
self.api_key = APIKEY.read() or ''
self.ignore = SETTINGS.get('ignore', [])
self.include = SETTINGS.get('include', [])
self.hidefilenames = SETTINGS.get('hidefilenames')
self.proxy = SETTINGS.get('proxy')
self.heartbeat = heartbeat
self.has_extra_heartbeats = False
def add_extra_heartbeats(self, extra_heartbeats):
self.has_extra_heartbeats = True
self.extra_heartbeats = extra_heartbeats
def run(self):
"""Running in background thread."""
self.send_heartbeats()
def build_heartbeat(self, entity=None, timestamp=None, is_write=None,
lineno=None, cursorpos=None, lines_in_file=None,
project=None, folders=None):
"""Returns a dict for passing to wakatime-cli as arguments."""
heartbeat = {
'entity': entity,
'timestamp': timestamp,
'is_write': is_write,
}
if project and project.get('name'):
heartbeat['alternate_project'] = project.get('name')
elif folders:
project_name = find_project_from_folders(folders, entity)
if project_name:
heartbeat['alternate_project'] = project_name
if lineno is not None:
heartbeat['lineno'] = lineno
if cursorpos is not None:
heartbeat['cursorpos'] = cursorpos
if lines_in_file is not None:
heartbeat['lines'] = lines_in_file
return heartbeat
def send_heartbeats(self):
heartbeat = self.build_heartbeat(**self.heartbeat)
ua = 'sublime/%d sublime-wakatime/%s' % (ST_VERSION, __version__)
cmd = [
getCliLocation(),
'--entity', heartbeat['entity'],
'--time', str('%f' % heartbeat['timestamp']),
'--plugin', ua,
]
if self.api_key:
cmd.extend(['--key', str(bytes.decode(self.api_key.encode('utf8')))])
if heartbeat['is_write']:
cmd.append('--write')
if heartbeat.get('alternate_project'):
cmd.extend(['--alternate-project', heartbeat['alternate_project']])
if heartbeat.get('lineno') is not None:
cmd.extend(['--lineno', '{0}'.format(heartbeat['lineno'])])
if heartbeat.get('cursorpos') is not None:
cmd.extend(['--cursorpos', '{0}'.format(heartbeat['cursorpos'])])
if heartbeat.get('lines') is not None:
cmd.extend(['--lines-in-file', '{0}'.format(heartbeat['lines'])])
for pattern in self.ignore:
cmd.extend(['--exclude', pattern])
for pattern in self.include:
cmd.extend(['--include', pattern])
if self.debug:
cmd.append('--verbose')
if self.hidefilenames:
cmd.append('--hidefilenames')
if self.proxy:
cmd.extend(['--proxy', self.proxy])
if self.has_extra_heartbeats:
cmd.append('--extra-heartbeats')
stdin = PIPE
extra_heartbeats = json.dumps([self.build_heartbeat(**x) for x in self.extra_heartbeats])
inp = "{0}\n".format(extra_heartbeats).encode('utf-8')
else:
extra_heartbeats = None
stdin = None
inp = None
log(DEBUG, ' '.join(obfuscate_apikey(cmd)))
try:
process = Popen(cmd, stdin=stdin, stdout=PIPE, stderr=STDOUT)
output, _err = process.communicate(input=inp)
retcode = process.poll()
if (not retcode or retcode == 102 or retcode == 112) and not output:
self.sent()
else:
update_status_bar('Error')
if retcode:
log(DEBUG if retcode == 102 or retcode == 112 else ERROR, 'wakatime-core exited with status: {0}'.format(retcode))
if output:
log(ERROR, u('wakatime-core output: {0}').format(output))
except:
log(ERROR, u(sys.exc_info()[1]))
update_status_bar('Error')
def sent(self):
update_status_bar('OK')
def plugin_loaded():
global SETTINGS
SETTINGS = sublime.load_settings(SETTINGS_FILE)
log(INFO, 'Initializing WakaTime plugin v%s' % __version__)
update_status_bar('Initializing...')
UpdateCLI().start()
after_loaded()
def after_loaded():
if not prompt_api_key():
set_timeout(after_loaded, 0.5)
update_status_bar('OK')
# need to call plugin_loaded because only ST3 will auto-call it
if ST_VERSION < 3000:
plugin_loaded()
class WakatimeListener(sublime_plugin.EventListener):
def on_post_save(self, view):
handle_activity(view, is_write=True)
def on_selection_modified(self, view):
if is_view_active(view):
handle_activity(view)
def on_modified(self, view):
if is_view_active(view):
handle_activity(view)
class WakatimeDashboardCommand(sublime_plugin.ApplicationCommand):
def run(self):
webbrowser.open_new_tab('https://wakatime.com/dashboard')
class UpdateCLI(threading.Thread):
"""Non-blocking thread for downloading latest wakatime-cli from GitHub.
"""
def run(self):
if isCliLatest():
return
log(INFO, 'Downloading wakatime-cli...')
if os.path.isdir(os.path.join(RESOURCES_FOLDER, 'wakatime-cli')):
shutil.rmtree(os.path.join(RESOURCES_FOLDER, 'wakatime-cli'))
if not os.path.exists(RESOURCES_FOLDER):
os.makedirs(RESOURCES_FOLDER)
try:
url = cliDownloadUrl()
log(DEBUG, 'Downloading wakatime-cli from {url}'.format(url=url))
zip_file = os.path.join(RESOURCES_FOLDER, 'wakatime-cli.zip')
download(url, zip_file)
if isCliInstalled():
try:
os.remove(getCliLocation())
except:
log(DEBUG, traceback.format_exc())
log(INFO, 'Extracting wakatime-cli...')
with ZipFile(zip_file) as zf:
zf.extractall(RESOURCES_FOLDER)
if not is_win:
os.chmod(getCliLocation(), 509) # 755
try:
os.remove(os.path.join(RESOURCES_FOLDER, 'wakatime-cli.zip'))
except:
log(DEBUG, traceback.format_exc())
except:
log(DEBUG, traceback.format_exc())
createSymlink()
log(INFO, 'Finished extracting wakatime-cli.')
def getCliLocation():
global WAKATIME_CLI_LOCATION
if not WAKATIME_CLI_LOCATION:
binary = 'wakatime-cli-{osname}-{arch}{ext}'.format(
osname=platform.system().lower(),
arch=architecture(),
ext='.exe' if is_win else '',
)
WAKATIME_CLI_LOCATION = os.path.join(RESOURCES_FOLDER, binary)
return WAKATIME_CLI_LOCATION
def architecture():
arch = platform.machine() or platform.processor()
if arch == 'armv7l':
return 'arm'
if arch == 'aarch64':
return 'arm64'
if 'arm' in arch:
return 'arm64' if sys.maxsize > 2**32 else 'arm'
return 'amd64' if sys.maxsize > 2**32 else '386'
def isCliInstalled():
return os.path.exists(getCliLocation())
def isCliLatest():
if not isCliInstalled():
return False
args = [getCliLocation(), '--version']
try:
stdout, stderr = Popen(args, stdout=PIPE, stderr=PIPE).communicate()
except:
return False
stdout = (stdout or b'') + (stderr or b'')
localVer = extractVersion(stdout.decode('utf-8'))
if not localVer:
log(DEBUG, 'Local wakatime-cli version not found.')
return False
log(INFO, 'Current wakatime-cli version is %s' % localVer)
log(INFO, 'Checking for updates to wakatime-cli...')
remoteVer = getLatestCliVersion()
if not remoteVer:
return True
if remoteVer == localVer:
log(INFO, 'wakatime-cli is up to date.')
return True
log(INFO, 'Found an updated wakatime-cli %s' % remoteVer)
return False
def getLatestCliVersion():
global LATEST_CLI_VERSION
if LATEST_CLI_VERSION:
return LATEST_CLI_VERSION
configs, last_modified, last_version = None, None, None
try:
configs = parseConfigFile(INTERNAL_CONFIG_FILE)
if configs:
last_modified, last_version = lastModifiedAndVersion(configs)
except:
log(DEBUG, traceback.format_exc())
try:
headers, contents, code = request(GITHUB_RELEASES_STABLE_URL, last_modified=last_modified)
log(DEBUG, 'GitHub API Response {0}'.format(code))
if code == 304:
LATEST_CLI_VERSION = last_version
return last_version
data = json.loads(contents.decode('utf-8'))
ver = data['tag_name']
log(DEBUG, 'Latest wakatime-cli version from GitHub: {0}'.format(ver))
if configs:
last_modified = headers.get('Last-Modified')
if not configs.has_section('internal'):
configs.add_section('internal')
configs.set('internal', 'cli_version', ver)
configs.set('internal', 'cli_version_last_modified', last_modified)
with open(INTERNAL_CONFIG_FILE, 'w', encoding='utf-8') as fh:
configs.write(fh)
LATEST_CLI_VERSION = ver
return ver
except:
log(DEBUG, traceback.format_exc())
return None
def lastModifiedAndVersion(configs):
last_modified, last_version = None, None
if configs.has_option('internal', 'cli_version'):
last_version = configs.get('internal', 'cli_version')
if last_version and configs.has_option('internal', 'cli_version_last_modified'):
last_modified = configs.get('internal', 'cli_version_last_modified')
if last_modified and last_version and extractVersion(last_version):
return last_modified, last_version
return None, None
def extractVersion(text):
pattern = re.compile(r"([0-9]+\.[0-9]+\.[0-9]+)")
match = pattern.search(text)
if match:
return 'v{ver}'.format(ver=match.group(1))
return None
def cliDownloadUrl():
osname = platform.system().lower()
arch = architecture()
validCombinations = [
'darwin-amd64',
'darwin-arm64',
'freebsd-386',
'freebsd-amd64',
'freebsd-arm',
'linux-386',
'linux-amd64',
'linux-arm',
'linux-arm64',
'netbsd-386',
'netbsd-amd64',
'netbsd-arm',
'openbsd-386',
'openbsd-amd64',
'openbsd-arm',
'openbsd-arm64',
'windows-386',
'windows-amd64',
'windows-arm64',
]
check = '{osname}-{arch}'.format(osname=osname, arch=arch)
if check not in validCombinations:
reportMissingPlatformSupport(osname, arch)
version = getLatestCliVersion()
return '{prefix}/{version}/wakatime-cli-{osname}-{arch}.zip'.format(
prefix=GITHUB_DOWNLOAD_PREFIX,
version=version,
osname=osname,
arch=arch,
)
def reportMissingPlatformSupport(osname, arch):
url = 'https://api.wakatime.com/api/v1/cli-missing?osname={osname}&architecture={arch}&plugin=sublime'.format(
osname=osname,
arch=arch,
)
request(url)
def request(url, last_modified=None):
req = Request(url)
req.add_header('User-Agent', 'github.com/wakatime/sublime-wakatime')
proxy = SETTINGS.get('proxy')
if proxy:
req.set_proxy(proxy, 'https')
if last_modified:
req.add_header('If-Modified-Since', last_modified)
try:
resp = urlopen(req)
headers = dict(resp.getheaders()) if is_py2 else resp.headers
return headers, resp.read(), resp.getcode()
except HTTPError as err:
if err.code == 304:
return None, None, 304
if is_py2:
with SSLCertVerificationDisabled():
try:
resp = urlopen(req)
headers = dict(resp.getheaders()) if is_py2 else resp.headers
return headers, resp.read(), resp.getcode()
except HTTPError as err2:
if err2.code == 304:
return None, None, 304
log(DEBUG, err.read().decode())
log(DEBUG, err2.read().decode())
raise
except IOError:
raise
log(DEBUG, err.read().decode())
raise
except IOError:
if is_py2:
with SSLCertVerificationDisabled():
try:
resp = urlopen(url)
headers = dict(resp.getheaders()) if is_py2 else resp.headers
return headers, resp.read(), resp.getcode()
except HTTPError as err:
if err.code == 304:
return None, None, 304
log(DEBUG, err.read().decode())
raise
except IOError:
raise
raise
def download(url, filePath):
req = Request(url)
req.add_header('User-Agent', 'github.com/wakatime/sublime-wakatime')
proxy = SETTINGS.get('proxy')
if proxy:
req.set_proxy(proxy, 'https')
with open(filePath, 'wb') as fh:
try:
resp = urlopen(req)
fh.write(resp.read())
except HTTPError as err:
if err.code == 304:
return None, None, 304
if is_py2:
with SSLCertVerificationDisabled():
try:
resp = urlopen(req)
fh.write(resp.read())
return
except HTTPError as err2:
log(DEBUG, err.read().decode())
log(DEBUG, err2.read().decode())
raise
except IOError:
raise
log(DEBUG, err.read().decode())
raise
except IOError:
if is_py2:
with SSLCertVerificationDisabled():
try:
resp = urlopen(url)
fh.write(resp.read())
return
except HTTPError as err:
log(DEBUG, err.read().decode())
raise
except IOError:
raise
raise
def is_symlink(path):
try:
return os.is_symlink(path)
except:
return False
def createSymlink():
link = os.path.join(RESOURCES_FOLDER, 'wakatime-cli')
if is_win:
link = link + '.exe'
elif os.path.exists(link) and is_symlink(link):
return # don't re-create symlink on Unix-like platforms
try:
os.symlink(getCliLocation(), link)
except:
try:
shutil.copy2(getCliLocation(), link)
if not is_win:
os.chmod(link, 509) # 755
except:
log(WARNING, traceback.format_exc())