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
|
2013-10-15 04:50:22 +00:00
|
|
|
import platform
|
2018-10-01 04:24:32 +00:00
|
|
|
from subprocess import PIPE
|
2013-07-06 07:51:09 +00:00
|
|
|
|
|
|
|
from .base import BaseProject
|
2018-10-01 04:24:32 +00:00
|
|
|
from ..compat import u, open, Popen
|
2013-07-30 06:04:43 +00:00
|
|
|
try:
|
|
|
|
from collections import OrderedDict
|
2015-09-27 04:11:21 +00:00
|
|
|
except ImportError: # pragma: nocover
|
2015-09-08 04:25:21 +00:00
|
|
|
from ..packages.ordereddict import OrderedDict # pragma: nocover
|
2013-07-06 07:51:09 +00:00
|
|
|
|
|
|
|
|
2014-07-25 07:59:25 +00:00
|
|
|
log = logging.getLogger('WakaTime')
|
2013-07-06 07:51:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Subversion(BaseProject):
|
2014-08-27 10:25:05 +00:00
|
|
|
binary_location = None
|
2013-07-06 07:51:09 +00:00
|
|
|
|
2013-07-10 03:11:49 +00:00
|
|
|
def process(self):
|
2013-07-20 21:42:19 +00:00
|
|
|
return self._find_project_base(self.path)
|
2013-07-10 03:11:49 +00:00
|
|
|
|
|
|
|
def name(self):
|
2015-09-08 04:28:19 +00:00
|
|
|
if 'Repository Root' not in self.info:
|
2015-09-27 04:11:21 +00:00
|
|
|
return None # pragma: nocover
|
2015-09-08 04:28:19 +00:00
|
|
|
return u(self.info['Repository Root'].split('/')[-1].split('\\')[-1])
|
2013-07-10 03:11:49 +00:00
|
|
|
|
2013-08-15 07:09:27 +00:00
|
|
|
def branch(self):
|
2015-09-08 04:28:19 +00:00
|
|
|
if 'URL' not in self.info:
|
2015-09-27 04:11:21 +00:00
|
|
|
return None # pragma: nocover
|
2015-09-08 04:28:19 +00:00
|
|
|
return u(self.info['URL'].split('/')[-1].split('\\')[-1])
|
2013-07-20 21:42:19 +00:00
|
|
|
|
2018-07-18 08:01:31 +00:00
|
|
|
def folder(self):
|
|
|
|
if 'Repository Root' not in self.info:
|
|
|
|
return None
|
|
|
|
return self.info['Repository Root']
|
|
|
|
|
2014-08-27 10:25:05 +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:
|
2015-08-24 01:49:34 +00:00
|
|
|
try:
|
|
|
|
with open(os.devnull, 'wb') as DEVNULL:
|
2014-08-29 19:45:52 +00:00
|
|
|
Popen([location, '--version'], stdout=DEVNULL, stderr=DEVNULL)
|
|
|
|
self.binary_location = location
|
|
|
|
return location
|
2015-08-24 01:49:34 +00:00
|
|
|
except:
|
|
|
|
pass
|
2014-08-27 10:25:05 +00:00
|
|
|
self.binary_location = 'svn'
|
|
|
|
return 'svn'
|
|
|
|
|
2013-07-20 21:42:19 +00:00
|
|
|
def _get_info(self, path):
|
2013-07-10 03:11:49 +00:00
|
|
|
info = OrderedDict()
|
2016-05-16 14:02:38 +00:00
|
|
|
if not self._is_mac() or self._has_xcode_tools():
|
|
|
|
stdout = None
|
|
|
|
try:
|
2018-10-03 07:28:16 +00:00
|
|
|
stdout, stderr = Popen(
|
|
|
|
[self._find_binary(), 'info', os.path.realpath(path)],
|
|
|
|
stdout=PIPE,
|
|
|
|
stderr=PIPE,
|
|
|
|
).communicate()
|
2016-05-16 14:02:38 +00:00
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if stdout:
|
|
|
|
for line in stdout.splitlines():
|
2018-10-03 07:28:16 +00:00
|
|
|
line = u(line).split(': ', 1)
|
2016-05-16 14:02:38 +00:00
|
|
|
if len(line) == 2:
|
|
|
|
info[line[0]] = line[1]
|
2013-07-10 03:11:49 +00:00
|
|
|
return info
|
2013-07-06 07:51:09 +00:00
|
|
|
|
2013-07-20 21:42:19 +00:00
|
|
|
def _find_project_base(self, path, found=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)
|
|
|
|
|
2016-05-16 14:02:38 +00:00
|
|
|
def _is_mac(self):
|
|
|
|
return platform.system() == 'Darwin'
|
|
|
|
|
|
|
|
def _has_xcode_tools(self):
|
|
|
|
try:
|
|
|
|
with open(os.devnull, 'wb') as DEVNULL:
|
|
|
|
proc = Popen(['/usr/bin/xcode-select', '-p'], stdout=DEVNULL, stderr=DEVNULL)
|
|
|
|
proc.communicate()
|
|
|
|
retval = proc.wait()
|
|
|
|
if retval == 0:
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return False
|