2013-07-08 05:38:01 +04: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
|
2013-10-15 08:51:35 +04:00
|
|
|
import platform
|
2013-07-10 11:14:44 +04:00
|
|
|
from subprocess import Popen, PIPE
|
2013-07-08 05:38:01 +04:00
|
|
|
|
|
|
|
from .base import BaseProject
|
2014-09-30 20:27:35 +04:00
|
|
|
from ..compat import u, open
|
2013-07-30 10:11:07 +04:00
|
|
|
try:
|
|
|
|
from collections import OrderedDict
|
2015-09-29 13:11:25 +03:00
|
|
|
except ImportError: # pragma: nocover
|
|
|
|
from ..packages.ordereddict import OrderedDict # pragma: nocover
|
2013-07-08 05:38:01 +04:00
|
|
|
|
|
|
|
|
2014-07-25 12:01:39 +04:00
|
|
|
log = logging.getLogger('WakaTime')
|
2013-07-08 05:38:01 +04:00
|
|
|
|
|
|
|
|
|
|
|
class Subversion(BaseProject):
|
2014-08-27 14:31:14 +04:00
|
|
|
binary_location = None
|
2013-07-08 05:38:01 +04:00
|
|
|
|
2013-07-10 11:14:44 +04:00
|
|
|
def process(self):
|
2013-07-21 02:28:38 +04:00
|
|
|
return self._find_project_base(self.path)
|
2013-07-10 11:14:44 +04:00
|
|
|
|
|
|
|
def name(self):
|
2015-09-29 13:11:25 +03:00
|
|
|
if 'Repository Root' not in self.info:
|
|
|
|
return None # pragma: nocover
|
|
|
|
return u(self.info['Repository Root'].split('/')[-1].split('\\')[-1])
|
2013-07-10 11:14:44 +04:00
|
|
|
|
2013-09-07 09:59:03 +04:00
|
|
|
def branch(self):
|
2015-09-29 13:11:25 +03:00
|
|
|
if 'URL' not in self.info:
|
|
|
|
return None # pragma: nocover
|
|
|
|
return u(self.info['URL'].split('/')[-1].split('\\')[-1])
|
2013-07-21 02:28:38 +04:00
|
|
|
|
2014-08-27 14:31:14 +04: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:
|
2015-08-25 10:42:37 +03:00
|
|
|
try:
|
|
|
|
with open(os.devnull, 'wb') as DEVNULL:
|
2014-08-29 23:49:51 +04:00
|
|
|
Popen([location, '--version'], stdout=DEVNULL, stderr=DEVNULL)
|
|
|
|
self.binary_location = location
|
|
|
|
return location
|
2015-08-25 10:42:37 +03:00
|
|
|
except:
|
|
|
|
pass
|
2014-08-27 14:31:14 +04:00
|
|
|
self.binary_location = 'svn'
|
|
|
|
return 'svn'
|
|
|
|
|
2013-07-21 02:28:38 +04:00
|
|
|
def _get_info(self, path):
|
2013-07-10 11:14:44 +04:00
|
|
|
info = OrderedDict()
|
|
|
|
stdout = None
|
|
|
|
try:
|
2014-01-14 17:11:00 +04:00
|
|
|
os.environ['LANG'] = 'en_US'
|
2013-07-10 11:14:44 +04:00
|
|
|
stdout, stderr = Popen([
|
2014-08-27 14:31:14 +04:00
|
|
|
self._find_binary(), 'info', os.path.realpath(path)
|
2013-07-10 12:34:26 +04:00
|
|
|
], stdout=PIPE, stderr=PIPE).communicate()
|
2013-07-10 11:14:44 +04:00
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if stdout:
|
|
|
|
for line in stdout.splitlines():
|
2015-09-29 13:11:25 +03:00
|
|
|
line = u(line)
|
2013-07-10 11:14:44 +04:00
|
|
|
line = line.split(': ', 1)
|
2014-11-19 03:00:10 +03:00
|
|
|
if len(line) == 2:
|
|
|
|
info[line[0]] = line[1]
|
2013-07-10 11:14:44 +04:00
|
|
|
return info
|
2013-07-08 05:38:01 +04:00
|
|
|
|
2013-07-21 02:28:38 +04:00
|
|
|
def _find_project_base(self, path, found=False):
|
2013-10-15 08:51:35 +04:00
|
|
|
if platform.system() == 'Windows':
|
2015-09-29 13:11:25 +03:00
|
|
|
return False # pragma: nocover
|
2013-07-21 02:28:38 +04: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)
|
|
|
|
|