upgrade wakatime-cli to v10.0.4

This commit is contained in:
Alan Hamlett 2017-11-23 12:41:59 -08:00
parent c4a88541d0
commit e0040e185b
7 changed files with 61 additions and 41 deletions

View file

@ -1,7 +1,7 @@
__title__ = 'wakatime' __title__ = 'wakatime'
__description__ = 'Common interface to the WakaTime api.' __description__ = 'Common interface to the WakaTime api.'
__url__ = 'https://github.com/wakatime/wakatime' __url__ = 'https://github.com/wakatime/wakatime'
__version_info__ = ('10', '0', '3') __version_info__ = ('10', '0', '4')
__version__ = '.'.join(__version_info__) __version__ = '.'.join(__version_info__)
__author__ = 'Alan Hamlett' __author__ = 'Alan Hamlett'
__author_email__ = 'alan@wakatime.com' __author_email__ = 'alan@wakatime.com'

View file

@ -30,7 +30,7 @@ log = logging.getLogger('WakaTime')
try: try:
from .packages import requests from .packages import requests
except ImportError: except ImportError: # pragma: nocover
log.traceback(logging.ERROR) log.traceback(logging.ERROR)
print(traceback.format_exc()) print(traceback.format_exc())
log.error('Please upgrade Python to the latest version.') log.error('Please upgrade Python to the latest version.')
@ -138,50 +138,53 @@ def send_heartbeats(heartbeats, args, configs, use_ntlm_proxy=False):
else: else:
code = response.status_code if response is not None else None code = response.status_code if response is not None else None
content = response.text if response is not None else None content = response.text if response is not None else None
try:
results = response.json() if response is not None else []
except:
if log.isEnabledFor(logging.DEBUG):
log.traceback(logging.WARNING)
results = []
if code == requests.codes.created or code == requests.codes.accepted:
log.debug({
'response_code': code,
})
for i in range(len(results)):
if len(heartbeats) <= i:
log.debug('Results from server do not match heartbeats sent.')
break
try:
c = results[i][1]
except:
c = 0
try:
text = json.dumps(results[i][0])
except:
if log.isEnabledFor(logging.DEBUG):
log.traceback(logging.WARNING)
text = ''
handle_result([heartbeats[i]], c, text, args, configs)
if _success(code):
results = _get_results(response)
_process_server_results(heartbeats, code, content, results, args, configs)
session_cache.save(session) session_cache.save(session)
return SUCCESS return SUCCESS
if should_try_ntlm: if should_try_ntlm:
return send_heartbeats(heartbeats, args, configs, use_ntlm_proxy=True) return send_heartbeats(heartbeats, args, configs, use_ntlm_proxy=True)
else:
handle_result(heartbeats, code, content, args, configs) _handle_unsent_heartbeats(heartbeats, code, content, args, configs)
session_cache.delete() session_cache.delete()
return AUTH_ERROR if code == 401 else API_ERROR return AUTH_ERROR if code == 401 else API_ERROR
def handle_result(h, code, content, args, configs): def _process_server_results(heartbeats, code, content, results, args, configs):
if code == requests.codes.created or code == requests.codes.accepted: log.debug({
return 'response_code': code,
})
for i in range(len(results)):
if len(heartbeats) <= i:
log.warn('Results from api not matching heartbeats sent.')
break
try:
c = results[i][1]
except:
log.traceback(logging.WARNING)
c = 0
try:
text = json.dumps(results[i][0])
except:
log.traceback(logging.WARNING)
text = ''
if not _success(c):
_handle_unsent_heartbeats([heartbeats[i]], c, text, args, configs)
leftover = len(heartbeats) - len(results)
if leftover > 0:
log.warn('Missing {0} results from api.'.format(leftover))
start = len(heartbeats) - leftover
_handle_unsent_heartbeats(heartbeats[start:], code, content, args, configs)
def _handle_unsent_heartbeats(heartbeats, code, content, args, configs):
if args.offline: if args.offline:
if code == 400: if code == 400:
log.error({ log.error({
@ -195,9 +198,23 @@ def handle_result(h, code, content, args, configs):
'response_content': content, 'response_content': content,
}) })
queue = Queue(args, configs) queue = Queue(args, configs)
queue.push_many(h) queue.push_many(heartbeats)
else: else:
log.error({ log.error({
'response_code': code, 'response_code': code,
'response_content': content, 'response_content': content,
}) })
def _get_results(response):
results = []
if response is not None:
try:
results = response.json()
except:
log.traceback(logging.WARNING)
return results
def _success(code):
return code == requests.codes.created or code == requests.codes.accepted

View file

@ -96,5 +96,5 @@ except ImportError: # pragma: nocover
try: try:
from .packages import simplejson as json from .packages import simplejson as json
except (ImportError, SyntaxError): except (ImportError, SyntaxError): # pragma: nocover
import json import json

View file

@ -21,7 +21,7 @@ from .constants import CONFIG_FILE_PARSE_ERROR
try: try:
import configparser import configparser
except ImportError: except ImportError: # pragma: nocover
from .packages import configparser from .packages import configparser

View file

@ -43,7 +43,7 @@ class JavaParser(TokenParser):
self._process_other(token, content) self._process_other(token, content)
def _process_namespace(self, token, content): def _process_namespace(self, token, content):
if u(content) == u('import'): if u(content).split() and u(content).split()[0] == u('import'):
self.state = 'import' self.state = 'import'
elif self.state == 'import': elif self.state == 'import':

View file

@ -41,6 +41,10 @@ class Heartbeat(object):
user_agent = None user_agent = None
def __init__(self, data, args, configs, _clone=None): def __init__(self, data, args, configs, _clone=None):
if not data:
self.skip = u('Skipping because heartbeat data is missing.')
return
self.args = args self.args = args
self.configs = configs self.configs = configs
@ -62,7 +66,7 @@ class Heartbeat(object):
return return
if self.type == 'file': if self.type == 'file':
self.entity = format_file_path(self.entity) self.entity = format_file_path(self.entity)
if self.type == 'file' and not os.path.isfile(self.entity): if self.type == 'file' and (not self.entity or not os.path.isfile(self.entity)):
self.skip = u('File does not exist; ignoring this heartbeat.') self.skip = u('File does not exist; ignoring this heartbeat.')
return return
@ -91,7 +95,6 @@ class Heartbeat(object):
data = self.dict() data = self.dict()
data.update(attrs) data.update(attrs)
heartbeat = Heartbeat(data, self.args, self.configs, _clone=True) heartbeat = Heartbeat(data, self.args, self.configs, _clone=True)
heartbeat.skip = self.skip
return heartbeat return heartbeat
def sanitize(self): def sanitize(self):

View file

@ -123,6 +123,6 @@ class Git(BaseProject):
subpath = line[len('gitdir: '):].strip() subpath = line[len('gitdir: '):].strip()
if os.path.isfile(os.path.join(path, subpath, 'config')) and \ if os.path.isfile(os.path.join(path, subpath, 'config')) and \
os.path.isfile(os.path.join(path, subpath, 'HEAD')): os.path.isfile(os.path.join(path, subpath, 'HEAD')):
return os.path.join(path, subpath) return os.path.realpath(os.path.join(path, subpath))
return None return None