rana-cli/wakatime/projects/subversion.py

96 lines
2.6 KiB
Python
Raw Normal View History

2013-07-06 07:51:09 +00:00
# -*- coding: utf-8 -*-
"""
wakatime.projects.subversion
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Information about the svn project for a given file.
:copyright: (c) 2013 Alan Hamlett.
:license: BSD, see LICENSE for more details.
"""
import logging
import os
import platform
2014-08-29 19:45:52 +00:00
from subprocess import Popen, PIPE
2013-07-06 07:51:09 +00:00
from .base import BaseProject
from ..compat import u, open
2013-07-30 06:04:43 +00:00
try:
from collections import OrderedDict
except ImportError:
from ..packages.ordereddict import OrderedDict
2013-07-06 07:51:09 +00:00
log = logging.getLogger('WakaTime')
2013-07-06 07:51:09 +00:00
class Subversion(BaseProject):
binary_location = None
2013-07-06 07:51:09 +00:00
def process(self):
return self._find_project_base(self.path)
def name(self):
return u(self.info['Repository Root'].split('/')[-1])
2013-08-15 07:09:27 +00:00
def branch(self):
return u(self.info['URL'].split('/')[-1])
def _find_binary(self):
if self.binary_location:
return self.binary_location
locations = [
'svn',
'/usr/bin/svn',
'/usr/local/bin/svn',
]
for location in locations:
2014-09-30 17:11:44 +00:00
with open(os.devnull, 'wb') as DEVNULL:
2014-08-29 19:45:52 +00:00
try:
Popen([location, '--version'], stdout=DEVNULL, stderr=DEVNULL)
self.binary_location = location
return location
except:
pass
self.binary_location = 'svn'
return 'svn'
def _get_info(self, path):
info = OrderedDict()
stdout = None
try:
os.environ['LANG'] = 'en_US'
stdout, stderr = Popen([
self._find_binary(), 'info', os.path.realpath(path)
], stdout=PIPE, stderr=PIPE).communicate()
except OSError:
pass
else:
if stdout:
for line in stdout.splitlines():
2013-07-30 06:04:43 +00:00
if isinstance(line, bytes):
line = bytes.decode(line)
line = line.split(': ', 1)
info[line[0]] = line[1]
return info
2013-07-06 07:51:09 +00:00
def _find_project_base(self, path, found=False):
if platform.system() == 'Windows':
return False
path = os.path.realpath(path)
if os.path.isfile(path):
path = os.path.split(path)[0]
info = self._get_info(path)
if len(info) > 0:
found = True
self.base = path
self.info = info
elif found:
return True
split_path = os.path.split(path)
if split_path[1] == '':
return found
return self._find_project_base(split_path[0], found)