2013-07-08 01:38:01 +00:00
|
|
|
""" ==========================================================
|
2013-08-05 05:30:24 +00:00
|
|
|
File: WakaTime.py
|
2013-07-08 01:38:01 +00:00
|
|
|
Description: Automatic time tracking for Sublime Text 2 and 3.
|
2014-03-05 21:55:08 +00:00
|
|
|
Maintainer: WakaTime <support@wakatime.com>
|
2014-03-13 00:03:14 +00:00
|
|
|
License: BSD, see LICENSE for more details.
|
2014-03-14 20:36:20 +00:00
|
|
|
Website: https://wakatime.com/
|
2013-07-08 01:38:01 +00:00
|
|
|
==========================================================="""
|
2013-07-02 09:21:13 +00:00
|
|
|
|
2015-03-31 23:19:30 +00:00
|
|
|
|
2016-01-02 07:09:08 +00:00
|
|
|
__version__ = '6.0.1'
|
2013-07-02 09:21:13 +00:00
|
|
|
|
2015-03-31 23:19:30 +00:00
|
|
|
|
2013-07-23 19:09:58 +00:00
|
|
|
import sublime
|
|
|
|
import sublime_plugin
|
|
|
|
|
2013-08-05 05:11:22 +00:00
|
|
|
import os
|
2013-07-23 19:09:58 +00:00
|
|
|
import platform
|
2015-10-01 22:07:55 +00:00
|
|
|
import re
|
2013-08-08 09:17:47 +00:00
|
|
|
import sys
|
2013-07-02 09:21:13 +00:00
|
|
|
import time
|
2013-08-08 09:17:47 +00:00
|
|
|
import threading
|
2015-07-31 22:19:56 +00:00
|
|
|
import urllib
|
2015-02-24 06:20:27 +00:00
|
|
|
import webbrowser
|
2015-03-20 08:14:53 +00:00
|
|
|
from datetime import datetime
|
2015-12-01 08:51:09 +00:00
|
|
|
from zipfile import ZipFile
|
2016-01-02 06:20:02 +00:00
|
|
|
from subprocess import Popen, STDOUT, PIPE
|
2015-10-01 22:07:55 +00:00
|
|
|
try:
|
|
|
|
import _winreg as winreg # py2
|
|
|
|
except ImportError:
|
2015-10-01 22:14:36 +00:00
|
|
|
try:
|
|
|
|
import winreg # py3
|
|
|
|
except ImportError:
|
|
|
|
winreg = None
|
2015-03-31 23:19:30 +00:00
|
|
|
|
2013-07-02 09:21:13 +00:00
|
|
|
|
2016-01-02 06:20:02 +00:00
|
|
|
is_py2 = (sys.version_info[0] == 2)
|
|
|
|
is_py3 = (sys.version_info[0] == 3)
|
|
|
|
|
|
|
|
if is_py2:
|
|
|
|
def u(text):
|
|
|
|
if text is None:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
return text.decode('utf-8')
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
return text.decode(sys.getdefaultencoding())
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
return unicode(text)
|
|
|
|
except:
|
|
|
|
return text
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise Exception('Unsupported Python version: {0}.{1}.{2}'.format(
|
|
|
|
sys.version_info[0],
|
|
|
|
sys.version_info[1],
|
|
|
|
sys.version_info[2],
|
|
|
|
))
|
|
|
|
|
|
|
|
|
2013-07-08 01:38:01 +00:00
|
|
|
# globals
|
2015-05-06 19:25:52 +00:00
|
|
|
HEARTBEAT_FREQUENCY = 2
|
2013-08-08 09:17:47 +00:00
|
|
|
ST_VERSION = int(sublime.version())
|
2015-03-31 23:19:30 +00:00
|
|
|
PLUGIN_DIR = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
API_CLIENT = os.path.join(PLUGIN_DIR, 'packages', 'wakatime', 'cli.py')
|
2013-08-08 09:17:47 +00:00
|
|
|
SETTINGS_FILE = 'WakaTime.sublime-settings'
|
|
|
|
SETTINGS = {}
|
2015-05-06 19:25:52 +00:00
|
|
|
LAST_HEARTBEAT = {
|
2013-11-28 11:16:43 +00:00
|
|
|
'time': 0,
|
|
|
|
'file': None,
|
|
|
|
'is_write': False,
|
|
|
|
}
|
2013-08-14 09:36:17 +00:00
|
|
|
LOCK = threading.RLock()
|
2015-03-09 21:32:27 +00:00
|
|
|
PYTHON_LOCATION = None
|
2013-07-08 01:38:01 +00:00
|
|
|
|
2014-12-24 18:01:56 +00:00
|
|
|
|
2015-10-02 19:09:37 +00:00
|
|
|
# Log Levels
|
|
|
|
DEBUG = 'DEBUG'
|
|
|
|
INFO = 'INFO'
|
|
|
|
WARNING = 'WARNING'
|
|
|
|
ERROR = 'ERROR'
|
|
|
|
|
|
|
|
|
2015-03-31 23:19:30 +00:00
|
|
|
# add wakatime package to path
|
|
|
|
sys.path.insert(0, os.path.join(PLUGIN_DIR, 'packages'))
|
2013-08-08 09:41:29 +00:00
|
|
|
try:
|
2015-03-31 23:19:30 +00:00
|
|
|
from wakatime.base import parseConfigFile
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2013-08-13 03:30:06 +00:00
|
|
|
|
2013-08-08 09:17:47 +00:00
|
|
|
|
2015-10-02 19:09:37 +00:00
|
|
|
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)
|
|
|
|
print('[WakaTime] [{lvl}] {msg}'.format(lvl=lvl, msg=msg))
|
|
|
|
except RuntimeError:
|
|
|
|
sublime.set_timeout(lambda: log(lvl, message, *args, **kwargs), 0)
|
|
|
|
|
|
|
|
|
2014-12-24 18:01:56 +00:00
|
|
|
def createConfigFile():
|
|
|
|
"""Creates the .wakatime.cfg INI file in $HOME directory, if it does
|
|
|
|
not already exist.
|
|
|
|
"""
|
|
|
|
configFile = os.path.join(os.path.expanduser('~'), '.wakatime.cfg')
|
|
|
|
try:
|
2014-12-27 00:18:52 +00:00
|
|
|
with open(configFile) as fh:
|
2014-12-24 18:01:56 +00:00
|
|
|
pass
|
|
|
|
except IOError:
|
|
|
|
try:
|
2014-12-27 00:18:52 +00:00
|
|
|
with open(configFile, 'w') as fh:
|
2014-12-24 18:01:56 +00:00
|
|
|
fh.write("[settings]\n")
|
|
|
|
fh.write("debug = false\n")
|
|
|
|
fh.write("hidefilenames = false\n")
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-08-14 09:36:17 +00:00
|
|
|
def prompt_api_key():
|
2013-08-08 09:17:47 +00:00
|
|
|
global SETTINGS
|
2014-12-24 18:01:56 +00:00
|
|
|
|
|
|
|
createConfigFile()
|
|
|
|
|
|
|
|
default_key = ''
|
2015-03-09 22:23:29 +00:00
|
|
|
try:
|
|
|
|
configs = parseConfigFile()
|
|
|
|
if configs is not None:
|
|
|
|
if configs.has_option('settings', 'api_key'):
|
|
|
|
default_key = configs.get('settings', 'api_key')
|
|
|
|
except:
|
|
|
|
pass
|
2014-12-24 18:01:56 +00:00
|
|
|
|
2014-06-18 17:10:28 +00:00
|
|
|
if SETTINGS.get('api_key'):
|
|
|
|
return True
|
|
|
|
else:
|
2013-08-05 05:11:22 +00:00
|
|
|
def got_key(text):
|
|
|
|
if text:
|
2013-08-14 09:36:17 +00:00
|
|
|
SETTINGS.set('api_key', str(text))
|
2013-08-08 09:17:47 +00:00
|
|
|
sublime.save_settings(SETTINGS_FILE)
|
2013-08-07 00:21:32 +00:00
|
|
|
window = sublime.active_window()
|
2013-08-08 09:17:47 +00:00
|
|
|
if window:
|
2014-12-24 18:01:56 +00:00
|
|
|
window.show_input_panel('[WakaTime] Enter your wakatime.com api key:', default_key, got_key, None, None)
|
2013-08-14 09:36:17 +00:00
|
|
|
return True
|
2013-07-23 22:10:19 +00:00
|
|
|
else:
|
2015-10-02 19:09:37 +00:00
|
|
|
log(ERROR, 'Could not prompt for api key because no window found.')
|
2013-08-14 09:36:17 +00:00
|
|
|
return False
|
2013-07-10 07:14:44 +00:00
|
|
|
|
2013-08-09 01:52:45 +00:00
|
|
|
|
2013-08-08 09:41:29 +00:00
|
|
|
def python_binary():
|
2015-03-09 21:32:27 +00:00
|
|
|
if PYTHON_LOCATION is not None:
|
|
|
|
return PYTHON_LOCATION
|
2015-10-01 22:07:55 +00:00
|
|
|
|
|
|
|
# look for python in PATH and common install locations
|
2015-03-09 21:32:27 +00:00
|
|
|
paths = [
|
2015-12-01 08:51:09 +00:00
|
|
|
os.path.join(os.path.expanduser('~'), '.wakatime', 'python'),
|
2015-10-06 10:59:04 +00:00
|
|
|
None,
|
2015-10-01 22:19:58 +00:00
|
|
|
'/',
|
|
|
|
'/usr/local/bin/',
|
|
|
|
'/usr/bin/',
|
2015-03-09 21:32:27 +00:00
|
|
|
]
|
|
|
|
for path in paths:
|
2015-10-01 22:07:55 +00:00
|
|
|
path = find_python_in_folder(path)
|
|
|
|
if path is not None:
|
2015-10-02 19:09:37 +00:00
|
|
|
set_python_binary_location(path)
|
2015-03-09 21:32:27 +00:00
|
|
|
return path
|
2015-10-01 22:07:55 +00:00
|
|
|
|
|
|
|
# look for python in windows registry
|
|
|
|
path = find_python_from_registry(r'SOFTWARE\Python\PythonCore')
|
|
|
|
if path is not None:
|
2015-10-02 19:09:37 +00:00
|
|
|
set_python_binary_location(path)
|
2015-10-01 22:07:55 +00:00
|
|
|
return path
|
|
|
|
path = find_python_from_registry(r'SOFTWARE\Wow6432Node\Python\PythonCore')
|
|
|
|
if path is not None:
|
2015-10-02 19:09:37 +00:00
|
|
|
set_python_binary_location(path)
|
2015-10-01 22:07:55 +00:00
|
|
|
return path
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2015-10-02 19:09:37 +00:00
|
|
|
def set_python_binary_location(path):
|
|
|
|
global PYTHON_LOCATION
|
|
|
|
PYTHON_LOCATION = path
|
2016-01-02 06:20:02 +00:00
|
|
|
log(DEBUG, 'Found Python at: {0}'.format(path))
|
2015-10-02 19:09:37 +00:00
|
|
|
|
|
|
|
|
2015-10-01 22:07:55 +00:00
|
|
|
def find_python_from_registry(location, reg=None):
|
2015-10-01 22:14:36 +00:00
|
|
|
if platform.system() != 'Windows' or winreg is None:
|
2015-10-01 22:07:55 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
if reg is None:
|
|
|
|
path = find_python_from_registry(location, reg=winreg.HKEY_CURRENT_USER)
|
|
|
|
if path is None:
|
|
|
|
path = find_python_from_registry(location, reg=winreg.HKEY_LOCAL_MACHINE)
|
|
|
|
return path
|
|
|
|
|
|
|
|
val = None
|
|
|
|
sub_key = 'InstallPath'
|
|
|
|
compiled = re.compile(r'^\d+\.\d+$')
|
|
|
|
|
|
|
|
try:
|
|
|
|
with winreg.OpenKey(reg, location) as handle:
|
|
|
|
versions = []
|
|
|
|
try:
|
|
|
|
for index in range(1024):
|
|
|
|
version = winreg.EnumKey(handle, index)
|
|
|
|
try:
|
|
|
|
if compiled.search(version):
|
|
|
|
versions.append(version)
|
|
|
|
except re.error:
|
|
|
|
pass
|
|
|
|
except EnvironmentError:
|
|
|
|
pass
|
|
|
|
versions.sort(reverse=True)
|
|
|
|
for version in versions:
|
|
|
|
try:
|
|
|
|
path = winreg.QueryValue(handle, version + '\\' + sub_key)
|
|
|
|
if path is not None:
|
|
|
|
path = find_python_in_folder(path)
|
|
|
|
if path is not None:
|
2015-10-02 19:09:37 +00:00
|
|
|
log(DEBUG, 'Found python from {reg}\\{key}\\{version}\\{sub_key}.'.format(
|
|
|
|
reg=reg,
|
|
|
|
key=location,
|
|
|
|
version=version,
|
|
|
|
sub_key=sub_key,
|
|
|
|
))
|
2015-10-01 22:07:55 +00:00
|
|
|
return path
|
|
|
|
except WindowsError:
|
2015-10-02 19:09:37 +00:00
|
|
|
log(DEBUG, 'Could not read registry value "{reg}\\{key}\\{version}\\{sub_key}".'.format(
|
|
|
|
reg=reg,
|
2015-10-01 22:07:55 +00:00
|
|
|
key=location,
|
|
|
|
version=version,
|
|
|
|
sub_key=sub_key,
|
|
|
|
))
|
|
|
|
except WindowsError:
|
2015-10-02 19:09:37 +00:00
|
|
|
if SETTINGS.get('debug'):
|
|
|
|
log(DEBUG, 'Could not read registry value "{reg}\\{key}".'.format(
|
|
|
|
reg=reg,
|
|
|
|
key=location,
|
|
|
|
))
|
2015-10-01 22:07:55 +00:00
|
|
|
|
|
|
|
return val
|
|
|
|
|
|
|
|
|
2016-01-02 06:20:02 +00:00
|
|
|
def find_python_in_folder(folder, headless=True):
|
|
|
|
pattern = re.compile(r'\d+\.\d+')
|
|
|
|
|
2015-10-06 10:59:42 +00:00
|
|
|
path = 'python'
|
2015-10-06 10:59:04 +00:00
|
|
|
if folder is not None:
|
|
|
|
path = os.path.realpath(os.path.join(folder, 'python'))
|
2016-01-02 06:20:02 +00:00
|
|
|
if headless:
|
2016-01-02 07:06:03 +00:00
|
|
|
path = u(path) + u('w')
|
|
|
|
log(DEBUG, u('Looking for Python at: {0}').format(path))
|
2015-10-01 22:07:55 +00:00
|
|
|
try:
|
2016-01-02 06:20:02 +00:00
|
|
|
process = Popen([path, '--version'], stdout=PIPE, stderr=STDOUT)
|
|
|
|
output, err = process.communicate()
|
|
|
|
output = u(output).strip()
|
|
|
|
retcode = process.poll()
|
2016-01-02 07:06:03 +00:00
|
|
|
log(DEBUG, u('Python Version Output: {0}').format(output))
|
2016-01-02 06:20:02 +00:00
|
|
|
if not retcode and pattern.search(output):
|
|
|
|
return path
|
2015-10-01 22:07:55 +00:00
|
|
|
except:
|
2016-01-02 07:06:03 +00:00
|
|
|
log(DEBUG, u('Python Version Output: {0}').format(u(sys.exc_info()[1])))
|
2016-01-02 06:20:02 +00:00
|
|
|
|
|
|
|
if headless:
|
|
|
|
path = find_python_in_folder(folder, headless=False)
|
|
|
|
if path is not None:
|
|
|
|
return path
|
|
|
|
|
2015-03-09 21:32:27 +00:00
|
|
|
return None
|
2013-07-10 07:14:44 +00:00
|
|
|
|
2013-08-09 01:52:45 +00:00
|
|
|
|
2015-04-02 22:04:05 +00:00
|
|
|
def obfuscate_apikey(command_list):
|
|
|
|
cmd = list(command_list)
|
2015-04-01 20:01:10 +00:00
|
|
|
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):
|
2015-10-01 18:24:13 +00:00
|
|
|
cmd[apikey_index] = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX' + cmd[apikey_index][-4:]
|
2015-04-01 20:01:10 +00:00
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
2015-05-06 19:27:39 +00:00
|
|
|
def enough_time_passed(now, last_heartbeat, is_write):
|
|
|
|
if now - last_heartbeat['time'] > HEARTBEAT_FREQUENCY * 60:
|
2013-07-10 07:14:44 +00:00
|
|
|
return True
|
2015-05-06 19:27:39 +00:00
|
|
|
if is_write and now - last_heartbeat['time'] > 2:
|
2015-05-06 19:22:42 +00:00
|
|
|
return True
|
2013-07-10 07:14:44 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2015-04-02 23:41:55 +00:00
|
|
|
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:
|
2015-02-06 01:40:45 +00:00
|
|
|
for folder in folders:
|
2015-04-02 23:41:55 +00:00
|
|
|
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)
|
2015-04-07 21:20:21 +00:00
|
|
|
return os.path.basename(folder) if folder else None
|
2014-06-18 17:35:59 +00:00
|
|
|
|
|
|
|
|
2015-05-06 21:06:06 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2015-05-06 19:25:52 +00:00
|
|
|
def handle_heartbeat(view, is_write=False):
|
2015-03-25 18:08:27 +00:00
|
|
|
window = view.window()
|
|
|
|
if window is not None:
|
|
|
|
target_file = view.file_name()
|
2015-04-02 23:41:55 +00:00
|
|
|
project = window.project_data() if hasattr(window, 'project_data') else None
|
2015-03-25 18:08:27 +00:00
|
|
|
folders = window.folders()
|
2015-05-06 19:25:52 +00:00
|
|
|
thread = SendHeartbeatThread(target_file, view, is_write=is_write, project=project, folders=folders)
|
2015-03-25 18:08:27 +00:00
|
|
|
thread.start()
|
2013-08-08 09:17:47 +00:00
|
|
|
|
|
|
|
|
2015-05-06 19:25:52 +00:00
|
|
|
class SendHeartbeatThread(threading.Thread):
|
2015-10-01 19:39:28 +00:00
|
|
|
"""Non-blocking thread for sending heartbeats to api.
|
|
|
|
"""
|
2013-08-08 09:17:47 +00:00
|
|
|
|
2015-03-20 08:14:53 +00:00
|
|
|
def __init__(self, target_file, view, is_write=False, project=None, folders=None, force=False):
|
2013-08-08 09:17:47 +00:00
|
|
|
threading.Thread.__init__(self)
|
2015-03-20 08:14:53 +00:00
|
|
|
self.lock = LOCK
|
2013-10-28 04:30:10 +00:00
|
|
|
self.target_file = target_file
|
|
|
|
self.is_write = is_write
|
2014-06-18 17:10:28 +00:00
|
|
|
self.project = project
|
2014-06-18 17:35:59 +00:00
|
|
|
self.folders = folders
|
2013-08-08 09:17:47 +00:00
|
|
|
self.force = force
|
2013-08-14 08:49:08 +00:00
|
|
|
self.debug = SETTINGS.get('debug')
|
2013-08-14 09:36:17 +00:00
|
|
|
self.api_key = SETTINGS.get('api_key', '')
|
2013-10-28 04:30:10 +00:00
|
|
|
self.ignore = SETTINGS.get('ignore', [])
|
2015-05-06 19:25:52 +00:00
|
|
|
self.last_heartbeat = LAST_HEARTBEAT.copy()
|
2015-05-06 23:34:15 +00:00
|
|
|
self.cursorpos = view.sel()[0].begin() if view.sel() else None
|
2015-03-20 08:14:53 +00:00
|
|
|
self.view = view
|
2013-08-08 09:17:47 +00:00
|
|
|
|
|
|
|
def run(self):
|
2015-03-20 08:14:53 +00:00
|
|
|
with self.lock:
|
|
|
|
if self.target_file:
|
|
|
|
self.timestamp = time.time()
|
2015-05-06 19:27:39 +00:00
|
|
|
if self.force or self.target_file != self.last_heartbeat['file'] or enough_time_passed(self.timestamp, self.last_heartbeat, self.is_write):
|
2015-03-20 08:14:53 +00:00
|
|
|
self.send_heartbeat()
|
2013-08-08 09:17:47 +00:00
|
|
|
|
2015-03-20 08:14:53 +00:00
|
|
|
def send_heartbeat(self):
|
2013-08-14 08:49:08 +00:00
|
|
|
if not self.api_key:
|
2015-10-02 19:09:37 +00:00
|
|
|
log(ERROR, 'missing api key.')
|
2013-08-08 09:17:47 +00:00
|
|
|
return
|
2013-09-05 14:39:33 +00:00
|
|
|
ua = 'sublime/%d sublime-wakatime/%s' % (ST_VERSION, __version__)
|
2013-08-08 09:17:47 +00:00
|
|
|
cmd = [
|
|
|
|
API_CLIENT,
|
2013-10-28 04:30:10 +00:00
|
|
|
'--file', self.target_file,
|
2013-08-08 09:17:47 +00:00
|
|
|
'--time', str('%f' % self.timestamp),
|
2013-09-05 14:39:33 +00:00
|
|
|
'--plugin', ua,
|
2013-08-14 08:49:08 +00:00
|
|
|
'--key', str(bytes.decode(self.api_key.encode('utf8'))),
|
2013-08-08 09:17:47 +00:00
|
|
|
]
|
2013-10-28 04:30:10 +00:00
|
|
|
if self.is_write:
|
2013-08-08 09:17:47 +00:00
|
|
|
cmd.append('--write')
|
2015-04-02 23:41:55 +00:00
|
|
|
if self.project and self.project.get('name'):
|
2015-05-15 22:32:03 +00:00
|
|
|
cmd.extend(['--alternate-project', self.project.get('name')])
|
2014-06-18 17:35:59 +00:00
|
|
|
elif self.folders:
|
2015-04-02 23:41:55 +00:00
|
|
|
project_name = find_project_from_folders(self.folders, self.target_file)
|
2014-06-18 17:35:59 +00:00
|
|
|
if project_name:
|
2015-05-15 22:32:03 +00:00
|
|
|
cmd.extend(['--alternate-project', project_name])
|
2015-05-06 23:34:15 +00:00
|
|
|
if self.cursorpos is not None:
|
|
|
|
cmd.extend(['--cursorpos', '{0}'.format(self.cursorpos)])
|
2013-10-28 04:30:10 +00:00
|
|
|
for pattern in self.ignore:
|
|
|
|
cmd.extend(['--ignore', pattern])
|
2013-08-14 08:49:08 +00:00
|
|
|
if self.debug:
|
2013-08-09 01:52:45 +00:00
|
|
|
cmd.append('--verbose')
|
2015-03-31 23:19:30 +00:00
|
|
|
if python_binary():
|
|
|
|
cmd.insert(0, python_binary())
|
2015-10-02 19:09:37 +00:00
|
|
|
log(DEBUG, ' '.join(obfuscate_apikey(cmd)))
|
2016-01-02 07:06:03 +00:00
|
|
|
try:
|
|
|
|
if not self.debug:
|
|
|
|
Popen(cmd)
|
|
|
|
self.sent()
|
|
|
|
else:
|
|
|
|
process = Popen(cmd, stdout=PIPE, stderr=STDOUT)
|
|
|
|
output, err = process.communicate()
|
|
|
|
output = u(output)
|
|
|
|
retcode = process.poll()
|
|
|
|
if (not retcode or retcode == 102) and not output:
|
|
|
|
self.sent()
|
|
|
|
if retcode:
|
|
|
|
log(DEBUG if retcode == 102 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]))
|
2013-08-13 03:30:06 +00:00
|
|
|
else:
|
2015-10-02 19:09:37 +00:00
|
|
|
log(ERROR, 'Unable to find python binary.')
|
2013-08-08 09:17:47 +00:00
|
|
|
|
2015-03-20 08:14:53 +00:00
|
|
|
def sent(self):
|
2015-03-24 17:05:23 +00:00
|
|
|
sublime.set_timeout(self.set_status_bar, 0)
|
2015-05-06 19:25:52 +00:00
|
|
|
sublime.set_timeout(self.set_last_heartbeat, 0)
|
2015-03-20 08:14:53 +00:00
|
|
|
|
2015-03-24 17:05:23 +00:00
|
|
|
def set_status_bar(self):
|
|
|
|
if SETTINGS.get('status_bar_message'):
|
2015-06-21 17:42:31 +00:00
|
|
|
self.view.set_status('wakatime', datetime.now().strftime(SETTINGS.get('status_bar_message_fmt')))
|
2015-03-24 17:05:23 +00:00
|
|
|
|
2015-05-06 19:25:52 +00:00
|
|
|
def set_last_heartbeat(self):
|
|
|
|
global LAST_HEARTBEAT
|
|
|
|
LAST_HEARTBEAT = {
|
2015-03-20 08:14:53 +00:00
|
|
|
'file': self.target_file,
|
|
|
|
'time': self.timestamp,
|
|
|
|
'is_write': self.is_write,
|
|
|
|
}
|
|
|
|
|
2013-08-08 09:17:47 +00:00
|
|
|
|
2015-12-01 09:04:41 +00:00
|
|
|
class DownloadPython(threading.Thread):
|
|
|
|
"""Non-blocking thread for extracting embeddable Python on Windows machines.
|
2015-10-01 19:39:28 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def run(self):
|
2015-12-01 09:04:41 +00:00
|
|
|
log(INFO, 'Downloading embeddable Python...')
|
2015-12-01 08:51:09 +00:00
|
|
|
|
|
|
|
ver = '3.5.0'
|
|
|
|
arch = 'amd64' if platform.architecture()[0] == '64bit' else 'win32'
|
|
|
|
url = 'https://www.python.org/ftp/python/{ver}/python-{ver}-embed-{arch}.zip'.format(
|
|
|
|
ver=ver,
|
|
|
|
arch=arch,
|
|
|
|
)
|
|
|
|
|
|
|
|
if not os.path.exists(os.path.join(os.path.expanduser('~'), '.wakatime')):
|
|
|
|
os.makedirs(os.path.join(os.path.expanduser('~'), '.wakatime'))
|
|
|
|
|
|
|
|
zip_file = os.path.join(os.path.expanduser('~'), '.wakatime', 'python.zip')
|
2015-10-01 19:39:28 +00:00
|
|
|
try:
|
2015-12-01 08:51:09 +00:00
|
|
|
urllib.urlretrieve(url, zip_file)
|
2015-10-01 19:39:28 +00:00
|
|
|
except AttributeError:
|
2015-12-01 08:51:09 +00:00
|
|
|
urllib.request.urlretrieve(url, zip_file)
|
|
|
|
|
2015-12-01 09:04:41 +00:00
|
|
|
log(INFO, 'Extracting Python...')
|
2015-12-01 08:51:09 +00:00
|
|
|
with ZipFile(zip_file) as zf:
|
|
|
|
path = os.path.join(os.path.expanduser('~'), '.wakatime', 'python')
|
|
|
|
zf.extractall(path)
|
2015-10-01 19:39:28 +00:00
|
|
|
|
2015-12-01 09:00:48 +00:00
|
|
|
try:
|
|
|
|
os.remove(zip_file)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2015-12-01 09:04:41 +00:00
|
|
|
log(INFO, 'Finished extracting Python.')
|
|
|
|
|
2015-10-01 19:39:28 +00:00
|
|
|
|
2013-08-08 09:17:47 +00:00
|
|
|
def plugin_loaded():
|
2013-10-28 04:30:10 +00:00
|
|
|
global SETTINGS
|
2015-10-02 19:09:37 +00:00
|
|
|
log(INFO, 'Initializing WakaTime plugin v%s' % __version__)
|
|
|
|
|
|
|
|
SETTINGS = sublime.load_settings(SETTINGS_FILE)
|
2014-10-14 18:02:52 +00:00
|
|
|
|
2015-03-31 23:19:30 +00:00
|
|
|
if not python_binary():
|
2015-10-02 19:09:37 +00:00
|
|
|
log(WARNING, 'Python binary not found.')
|
2015-07-31 22:19:56 +00:00
|
|
|
if platform.system() == 'Windows':
|
2015-12-01 09:04:41 +00:00
|
|
|
thread = DownloadPython()
|
2015-10-01 19:39:28 +00:00
|
|
|
thread.start()
|
2015-07-31 22:19:56 +00:00
|
|
|
else:
|
|
|
|
sublime.error_message("Unable to find Python binary!\nWakaTime needs Python to work correctly.\n\nGo to https://www.python.org/downloads")
|
|
|
|
return
|
2014-10-14 18:02:52 +00:00
|
|
|
|
2013-08-14 09:36:17 +00:00
|
|
|
after_loaded()
|
|
|
|
|
|
|
|
|
|
|
|
def after_loaded():
|
|
|
|
if not prompt_api_key():
|
|
|
|
sublime.set_timeout(after_loaded, 500)
|
2013-08-08 09:17:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
# need to call plugin_loaded because only ST3 will auto-call it
|
|
|
|
if ST_VERSION < 3000:
|
|
|
|
plugin_loaded()
|
2013-07-02 09:21:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WakatimeListener(sublime_plugin.EventListener):
|
|
|
|
|
|
|
|
def on_post_save(self, view):
|
2015-05-06 19:25:52 +00:00
|
|
|
handle_heartbeat(view, is_write=True)
|
2013-07-02 09:21:13 +00:00
|
|
|
|
2015-04-12 23:45:16 +00:00
|
|
|
def on_selection_modified(self, view):
|
2015-05-06 21:06:06 +00:00
|
|
|
if is_view_active(view):
|
2015-05-06 21:00:33 +00:00
|
|
|
handle_heartbeat(view)
|
2013-07-08 01:38:01 +00:00
|
|
|
|
2013-07-16 11:02:30 +00:00
|
|
|
def on_modified(self, view):
|
2015-05-06 21:06:06 +00:00
|
|
|
if is_view_active(view):
|
2015-05-06 21:00:33 +00:00
|
|
|
handle_heartbeat(view)
|
2015-02-23 22:59:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WakatimeDashboardCommand(sublime_plugin.ApplicationCommand):
|
2015-02-24 06:20:27 +00:00
|
|
|
|
2015-02-23 22:59:10 +00:00
|
|
|
def run(self):
|
|
|
|
webbrowser.open_new_tab('https://wakatime.com/dashboard')
|