vim-rana-local/plugin/packages/wakatime/wakatime/projects/subversion.py

110 lines
2.9 KiB
Python
Raw Normal View History

# -*- 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
2013-10-26 04:32:30 +00:00
import platform
2014-08-29 19:46:28 +00:00
from subprocess import Popen, PIPE
from .base import BaseProject
try:
from collections import OrderedDict
except ImportError:
from ..packages.ordereddict import OrderedDict
2014-07-25 09:45:35 +00:00
log = logging.getLogger('WakaTime')
2014-01-16 00:44:03 +00:00
# str is unicode in Python3
try:
unicode
except NameError:
unicode = str
class Subversion(BaseProject):
2014-08-27 10:34:31 +00:00
binary_location = None
2013-07-10 03:15:01 +00:00
def process(self):
2013-07-20 22:25:11 +00:00
return self._find_project_base(self.path)
2013-07-10 03:15:01 +00:00
def name(self):
return unicode(self.info['Repository Root'].split('/')[-1])
2013-07-10 03:15:01 +00:00
def branch(self):
2013-07-20 22:25:11 +00:00
if self.base:
unicode(os.path.basename(self.base))
return None
2013-07-20 22:25:11 +00:00
2014-08-27 10:34:31 +00:00
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-08-29 19:46:28 +00:00
with open(os.devnull, 'wb') as DEVNULL:
try:
Popen([location, '--version'], stdout=DEVNULL, stderr=DEVNULL)
self.binary_location = location
return location
except:
pass
2014-08-27 10:34:31 +00:00
self.binary_location = 'svn'
return 'svn'
2013-07-20 22:25:11 +00:00
def _get_info(self, path):
2013-07-10 03:15:01 +00:00
info = OrderedDict()
stdout = None
try:
2014-01-14 13:09:54 +00:00
os.environ['LANG'] = 'en_US'
2013-07-10 03:15:01 +00:00
stdout, stderr = Popen([
2014-08-27 10:34:31 +00:00
self._find_binary(), 'info', os.path.realpath(path)
2013-07-10 08:34:18 +00:00
], stdout=PIPE, stderr=PIPE).communicate()
2013-07-10 03:15:01 +00:00
except OSError:
pass
else:
if stdout:
interesting = [
'Repository Root',
'Repository UUID',
'URL',
]
for line in stdout.splitlines():
if isinstance(line, bytes):
line = bytes.decode(line)
2013-07-10 03:15:01 +00:00
line = line.split(': ', 1)
if line[0] in interesting:
info[line[0]] = line[1]
return info
2013-07-20 22:25:11 +00:00
def _find_project_base(self, path, found=False):
2013-10-26 04:32:30 +00:00
if platform.system() == 'Windows':
return False
2013-07-20 22:25:11 +00:00
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)