move wakatime-cli to parent folder for faster startup on Windows
This commit is contained in:
parent
f9e5760e72
commit
e0e7621f18
1005 changed files with 1 additions and 1 deletions
0
packages/wakatime/projects/__init__.py
Normal file
0
packages/wakatime/projects/__init__.py
Normal file
45
packages/wakatime/projects/base.py
Normal file
45
packages/wakatime/projects/base.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
wakatime.projects.base
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Base project for use when no other project can be found.
|
||||
|
||||
:copyright: (c) 2013 Alan Hamlett.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
from ..exceptions import NotYetImplemented
|
||||
|
||||
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
class BaseProject(object):
|
||||
""" Parent project class only
|
||||
used when no valid project can
|
||||
be found for the current path.
|
||||
"""
|
||||
|
||||
def __init__(self, path, configs=None):
|
||||
self.path = path
|
||||
self._configs = configs
|
||||
|
||||
def process(self):
|
||||
""" Processes self.path into a project and
|
||||
returns True if project is valid, otherwise
|
||||
returns False.
|
||||
"""
|
||||
raise NotYetImplemented()
|
||||
|
||||
def name(self):
|
||||
""" Returns the project's name.
|
||||
"""
|
||||
raise NotYetImplemented()
|
||||
|
||||
def branch(self):
|
||||
""" Returns the current branch.
|
||||
"""
|
||||
raise NotYetImplemented()
|
128
packages/wakatime/projects/git.py
Normal file
128
packages/wakatime/projects/git.py
Normal file
|
@ -0,0 +1,128 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
wakatime.projects.git
|
||||
~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Information about the git project for a given file.
|
||||
|
||||
:copyright: (c) 2013 Alan Hamlett.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
from .base import BaseProject
|
||||
from ..compat import u, open
|
||||
|
||||
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
class Git(BaseProject):
|
||||
_submodule = None
|
||||
_project_name = None
|
||||
_head_file = None
|
||||
|
||||
def process(self):
|
||||
return self._find_git_config_file(self.path)
|
||||
|
||||
def name(self):
|
||||
return u(self._project_name) if self._project_name else None
|
||||
|
||||
def branch(self):
|
||||
head = self._head_file
|
||||
if head:
|
||||
try:
|
||||
with open(head, 'r', encoding='utf-8') as fh:
|
||||
return self._get_branch_from_head_file(fh.readline())
|
||||
except UnicodeDecodeError: # pragma: nocover
|
||||
try:
|
||||
with open(head, 'r', encoding=sys.getfilesystemencoding()) as fh:
|
||||
return self._get_branch_from_head_file(fh.readline())
|
||||
except:
|
||||
log.traceback(logging.WARNING)
|
||||
except IOError: # pragma: nocover
|
||||
log.traceback(logging.WARNING)
|
||||
return u('master')
|
||||
|
||||
def _find_git_config_file(self, path):
|
||||
path = os.path.realpath(path)
|
||||
if os.path.isfile(path):
|
||||
path = os.path.split(path)[0]
|
||||
if os.path.isfile(os.path.join(path, '.git', 'config')):
|
||||
self._project_name = os.path.basename(path)
|
||||
self._head_file = os.path.join(path, '.git', 'HEAD')
|
||||
return True
|
||||
if self._submodules_supported_for_path(path):
|
||||
submodule_path = self._find_path_from_submodule(path)
|
||||
if submodule_path:
|
||||
self._project_name = os.path.basename(path)
|
||||
self._head_file = os.path.join(submodule_path, 'HEAD')
|
||||
return True
|
||||
split_path = os.path.split(path)
|
||||
if split_path[1] == '':
|
||||
return False
|
||||
return self._find_git_config_file(split_path[0])
|
||||
|
||||
def _get_branch_from_head_file(self, line):
|
||||
if u(line.strip()).startswith('ref: '):
|
||||
return u(line.strip().rsplit('/', 1)[-1])
|
||||
return None
|
||||
|
||||
def _submodules_supported_for_path(self, path):
|
||||
if not self._configs:
|
||||
return True
|
||||
|
||||
disabled = self._configs.get('submodules_disabled')
|
||||
if not disabled:
|
||||
return True
|
||||
|
||||
if disabled.strip().lower() == 'true':
|
||||
return False
|
||||
if disabled.strip().lower() == 'false':
|
||||
return True
|
||||
|
||||
for pattern in disabled.split("\n"):
|
||||
if pattern.strip():
|
||||
try:
|
||||
compiled = re.compile(pattern, re.IGNORECASE)
|
||||
if compiled.search(path):
|
||||
return False
|
||||
except re.error as ex:
|
||||
log.warning(u('Regex error ({msg}) for disable git submodules pattern: {pattern}').format(
|
||||
msg=u(ex),
|
||||
pattern=u(pattern),
|
||||
))
|
||||
|
||||
return True
|
||||
|
||||
def _find_path_from_submodule(self, path):
|
||||
link = os.path.join(path, '.git')
|
||||
if not os.path.isfile(link):
|
||||
return None
|
||||
|
||||
try:
|
||||
with open(link, 'r', encoding='utf-8') as fh:
|
||||
return self._get_path_from_submodule_link(path, fh.readline())
|
||||
except UnicodeDecodeError:
|
||||
try:
|
||||
with open(link, 'r', encoding=sys.getfilesystemencoding()) as fh:
|
||||
return self._get_path_from_submodule_link(path, fh.readline())
|
||||
except:
|
||||
log.traceback(logging.WARNING)
|
||||
except IOError:
|
||||
log.traceback(logging.WARNING)
|
||||
|
||||
return None
|
||||
|
||||
def _get_path_from_submodule_link(self, path, line):
|
||||
if line.startswith('gitdir: '):
|
||||
subpath = line[len('gitdir: '):].strip()
|
||||
if os.path.isfile(os.path.join(path, subpath, 'config')) and \
|
||||
os.path.isfile(os.path.join(path, subpath, 'HEAD')):
|
||||
return os.path.realpath(os.path.join(path, subpath))
|
||||
|
||||
return None
|
59
packages/wakatime/projects/mercurial.py
Normal file
59
packages/wakatime/projects/mercurial.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
# -*- 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
|
||||
|
||||
from .base import BaseProject
|
||||
from ..compat import u, open
|
||||
|
||||
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
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)))
|
||||
return None # pragma: nocover
|
||||
|
||||
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])
|
||||
except UnicodeDecodeError: # pragma: nocover
|
||||
try:
|
||||
with open(branch_file, 'r', encoding=sys.getfilesystemencoding()) as fh:
|
||||
return u(fh.readline().strip().rsplit('/', 1)[-1])
|
||||
except:
|
||||
log.traceback(logging.WARNING)
|
||||
except IOError: # pragma: nocover
|
||||
log.traceback(logging.WARNING)
|
||||
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])
|
55
packages/wakatime/projects/projectfile.py
Normal file
55
packages/wakatime/projects/projectfile.py
Normal file
|
@ -0,0 +1,55 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
wakatime.projects.projectfile
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Information from a .wakatime-project file about the project for
|
||||
a given file. First line of .wakatime-project sets the project
|
||||
name. Second line sets the current branch name.
|
||||
|
||||
:copyright: (c) 2013 Alan Hamlett.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import sys
|
||||
|
||||
from .base import BaseProject
|
||||
from ..compat import u, open
|
||||
from ..utils import find_project_file
|
||||
|
||||
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
class ProjectFile(BaseProject):
|
||||
|
||||
def process(self):
|
||||
self.config = find_project_file(self.path)
|
||||
self._project_name = None
|
||||
self._project_branch = None
|
||||
|
||||
if self.config:
|
||||
|
||||
try:
|
||||
with open(self.config, 'r', encoding='utf-8') as fh:
|
||||
self._project_name = u(fh.readline().strip()) or None
|
||||
self._project_branch = u(fh.readline().strip()) or None
|
||||
except UnicodeDecodeError: # pragma: nocover
|
||||
try:
|
||||
with open(self.config, 'r', encoding=sys.getfilesystemencoding()) as fh:
|
||||
self._project_name = u(fh.readline().strip()) or None
|
||||
self._project_branch = u(fh.readline().strip()) or None
|
||||
except:
|
||||
log.traceback(logging.WARNING)
|
||||
except IOError: # pragma: nocover
|
||||
log.traceback(logging.WARNING)
|
||||
|
||||
return True
|
||||
return False
|
||||
|
||||
def name(self):
|
||||
return self._project_name
|
||||
|
||||
def branch(self):
|
||||
return self._project_branch
|
71
packages/wakatime/projects/projectmap.py
Normal file
71
packages/wakatime/projects/projectmap.py
Normal file
|
@ -0,0 +1,71 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
wakatime.projects.projectmap
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Use the ~/.wakatime.cfg file to set custom project names by matching files
|
||||
with regex patterns. Project maps go under the [projectmap] config section.
|
||||
|
||||
For example:
|
||||
|
||||
[projectmap]
|
||||
/home/user/projects/foo = new project name
|
||||
/home/user/projects/bar(\d+)/ = project{0}
|
||||
|
||||
Will result in file `/home/user/projects/foo/src/main.c` to have
|
||||
project name `new project name` and file `/home/user/projects/bar42/main.c`
|
||||
to have project name `project42`.
|
||||
|
||||
:copyright: (c) 2013 Alan Hamlett.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
|
||||
from .base import BaseProject
|
||||
from ..compat import u
|
||||
|
||||
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
class ProjectMap(BaseProject):
|
||||
|
||||
def process(self):
|
||||
if not self._configs:
|
||||
return False
|
||||
|
||||
self.project = self._find_project(self.path)
|
||||
|
||||
return self.project is not None
|
||||
|
||||
def _find_project(self, path):
|
||||
path = os.path.realpath(path)
|
||||
|
||||
for pattern, new_proj_name in self._configs.items():
|
||||
try:
|
||||
compiled = re.compile(pattern, re.IGNORECASE)
|
||||
match = compiled.search(path)
|
||||
if match:
|
||||
try:
|
||||
return new_proj_name.format(*match.groups())
|
||||
except IndexError as ex:
|
||||
log.warning(u('Regex error ({msg}) for projectmap pattern: {pattern}').format(
|
||||
msg=u(ex),
|
||||
pattern=u(new_proj_name),
|
||||
))
|
||||
except re.error as ex:
|
||||
log.warning(u('Regex error ({msg}) for projectmap pattern: {pattern}').format(
|
||||
msg=u(ex),
|
||||
pattern=u(pattern),
|
||||
))
|
||||
|
||||
return None
|
||||
|
||||
def branch(self):
|
||||
return None
|
||||
|
||||
def name(self):
|
||||
return u(self.project) if self.project else None
|
114
packages/wakatime/projects/subversion.py
Normal file
114
packages/wakatime/projects/subversion.py
Normal file
|
@ -0,0 +1,114 @@
|
|||
# -*- 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
|
||||
from subprocess import Popen, PIPE
|
||||
|
||||
from .base import BaseProject
|
||||
from ..compat import u, open
|
||||
try:
|
||||
from collections import OrderedDict
|
||||
except ImportError: # pragma: nocover
|
||||
from ..packages.ordereddict import OrderedDict # pragma: nocover
|
||||
|
||||
|
||||
log = logging.getLogger('WakaTime')
|
||||
|
||||
|
||||
class Subversion(BaseProject):
|
||||
binary_location = None
|
||||
|
||||
def process(self):
|
||||
return self._find_project_base(self.path)
|
||||
|
||||
def name(self):
|
||||
if 'Repository Root' not in self.info:
|
||||
return None # pragma: nocover
|
||||
return u(self.info['Repository Root'].split('/')[-1].split('\\')[-1])
|
||||
|
||||
def branch(self):
|
||||
if 'URL' not in self.info:
|
||||
return None # pragma: nocover
|
||||
return u(self.info['URL'].split('/')[-1].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:
|
||||
try:
|
||||
with open(os.devnull, 'wb') as DEVNULL:
|
||||
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()
|
||||
if not self._is_mac() or self._has_xcode_tools():
|
||||
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():
|
||||
line = u(line)
|
||||
line = line.split(': ', 1)
|
||||
if len(line) == 2:
|
||||
info[line[0]] = line[1]
|
||||
return info
|
||||
|
||||
def _find_project_base(self, path, found=False):
|
||||
if platform.system() == 'Windows':
|
||||
return False # pragma: nocover
|
||||
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)
|
||||
|
||||
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
|
Loading…
Add table
Add a link
Reference in a new issue