Changeset - 291be8fa4d4f
[Not reviewed]
beta
0 2 0
Mads Kiilerich - 13 years ago 2012-12-12 18:12:18
madski@unity3d.com
Grafted from: 44b57042f4bd
rename search_recursively to search_up - that is what it is
2 files changed with 4 insertions and 4 deletions:
0 comments (0 inline, 0 general)
rhodecode/lib/vcs/backends/__init__.py
Show inline comments
 
@@ -9,49 +9,49 @@
 
    :copyright: (c) 2010-2011 by Marcin Kuzminski, Lukasz Balcerzak.
 
"""
 
import os
 
from pprint import pformat
 
from rhodecode.lib.vcs.conf import settings
 
from rhodecode.lib.vcs.exceptions import VCSError
 
from rhodecode.lib.vcs.utils.helpers import get_scm
 
from rhodecode.lib.vcs.utils.paths import abspath
 
from rhodecode.lib.vcs.utils.imports import import_class
 

	
 

	
 
def get_repo(path=None, alias=None, create=False):
 
    """
 
    Returns ``Repository`` object of type linked with given ``alias`` at
 
    the specified ``path``. If ``alias`` is not given it will try to guess it
 
    using get_scm method
 
    """
 
    if create:
 
        if not (path or alias):
 
            raise TypeError("If create is specified, we need path and scm type")
 
        return get_backend(alias)(path, create=True)
 
    if path is None:
 
        path = abspath(os.path.curdir)
 
    try:
 
        scm, path = get_scm(path, search_recursively=True)
 
        scm, path = get_scm(path, search_up=True)
 
        path = abspath(path)
 
        alias = scm
 
    except VCSError:
 
        raise VCSError("No scm found at %s" % path)
 
    if alias is None:
 
        alias = get_scm(path)[0]
 

	
 
    backend = get_backend(alias)
 
    repo = backend(path, create=create)
 
    return repo
 

	
 

	
 
def get_backend(alias):
 
    """
 
    Returns ``Repository`` class identified by the given alias or raises
 
    VCSError if alias is not recognized or backend class cannot be imported.
 
    """
 
    if alias not in settings.BACKENDS:
 
        raise VCSError("Given alias '%s' is not recognized! Allowed aliases:\n"
 
            "%s" % (alias, pformat(settings.BACKENDS.keys())))
 
    backend_path = settings.BACKENDS[alias]
 
    klass = import_class(backend_path)
 
    return klass
 

	
rhodecode/lib/vcs/utils/helpers.py
Show inline comments
 
"""
 
Utitlites aimed to help achieve mostly basic tasks.
 
"""
 
from __future__ import division
 

	
 
import re
 
import time
 
import datetime
 
import os.path
 
from subprocess import Popen, PIPE
 
from rhodecode.lib.vcs.exceptions import VCSError
 
from rhodecode.lib.vcs.exceptions import RepositoryError
 
from rhodecode.lib.vcs.utils.paths import abspath
 

	
 
ALIASES = ['hg', 'git']
 

	
 

	
 
def get_scm(path, search_recursively=False, explicit_alias=None):
 
def get_scm(path, search_up=False, explicit_alias=None):
 
    """
 
    Returns one of alias from ``ALIASES`` (in order of precedence same as
 
    shortcuts given in ``ALIASES``) and top working dir path for the given
 
    argument. If no scm-specific directory is found or more than one scm is
 
    found at that directory, ``VCSError`` is raised.
 

	
 
    :param search_recursively: if set to ``True``, this function would try to
 
    :param search_up: if set to ``True``, this function would try to
 
      move up to parent directory every time no scm is recognized for the
 
      currently checked path. Default: ``False``.
 
    :param explicit_alias: can be one of available backend aliases, when given
 
      it will return given explicit alias in repositories under more than one
 
      version control, if explicit_alias is different than found it will raise
 
      VCSError
 
    """
 
    if not os.path.isdir(path):
 
        raise VCSError("Given path %s is not a directory" % path)
 

	
 
    def get_scms(path):
 
        return [(scm, path) for scm in get_scms_for_path(path)]
 

	
 
    found_scms = get_scms(path)
 
    while  not found_scms and search_recursively:
 
    while not found_scms and search_up:
 
        newpath = abspath(path, '..')
 
        if newpath == path:
 
            break
 
        path = newpath
 
        found_scms = get_scms(path)
 

	
 
    if len(found_scms) > 1:
 
        for scm in found_scms:
 
            if scm[0] == explicit_alias:
 
                return scm
 
        raise VCSError('More than one [%s] scm found at given path %s'
 
                       % (','.join((x[0] for x in found_scms)), path))
 

	
 
    if len(found_scms) is 0:
 
        raise VCSError('No scm found at given path %s' % path)
 

	
 
    return found_scms[0]
 

	
 

	
 
def get_scms_for_path(path):
 
    """
 
    Returns all scm's found at the given path. If no scm is recognized
 
    - empty list is returned.
 

	
0 comments (0 inline, 0 general)