rana-cli/wakatime/projects/mercurial.py

60 lines
1.8 KiB
Python
Raw Normal View History

2013-07-06 07:51:09 +00:00
# -*- coding: utf-8 -*-
"""
wakatime.projects.mercurial
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Information about the mercurial project for a given file.
:copyright: (c) 2013 Alan Hamlett.
:license: BSD, see LICENSE for more details.
"""
import logging
import os
import sys
2013-07-06 07:51:09 +00:00
from .base import BaseProject
from ..compat import u, open
2013-07-06 07:51:09 +00:00
log = logging.getLogger('WakaTime')
2013-07-06 07:51:09 +00:00
class Mercurial(BaseProject):
def process(self):
self.configDir = self._find_hg_config_dir(self.path)
return self.configDir is not None
def name(self):
if self.configDir:
return u(os.path.basename(os.path.dirname(self.configDir)))
2015-09-08 04:40:01 +00:00
return None # pragma: nocover
2013-07-06 07:51:09 +00:00
2013-08-15 07:09:27 +00:00
def branch(self):
if self.configDir:
branch_file = os.path.join(self.configDir, 'branch')
try:
with open(branch_file, 'r', encoding='utf-8') as fh:
return u(fh.readline().strip().rsplit('/', 1)[-1])
2015-09-08 04:40:01 +00:00
except UnicodeDecodeError: # pragma: nocover
try:
with open(branch_file, 'r', encoding=sys.getfilesystemencoding()) as fh:
return u(fh.readline().strip().rsplit('/', 1)[-1])
except:
2015-10-23 06:15:57 +00:00
log.traceback()
2015-09-08 04:40:01 +00:00
except IOError: # pragma: nocover
2015-10-23 06:15:57 +00:00
log.traceback()
return u('default')
def _find_hg_config_dir(self, path):
path = os.path.realpath(path)
if os.path.isfile(path):
path = os.path.split(path)[0]
if os.path.isdir(os.path.join(path, '.hg')):
return os.path.join(path, '.hg')
split_path = os.path.split(path)
if split_path[1] == '':
return None
return self._find_hg_config_dir(split_path[0])