Exemplo n.º 1
0
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)
        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
Exemplo n.º 2
0
def is_valid_repos_group(repos_group_name, base_path):
    """
    Returns True if given path is a repos group False otherwise

    :param repo_name:
    :param base_path:
    """
    full_path = os.path.join(safe_str(base_path), safe_str(repos_group_name))

    # check if it's not a repo
    if is_valid_repo(repos_group_name, base_path):
        return False

    try:
        # we need to check bare git repos at higher level
        # since we might match branches/hooks/info/objects or possible
        # other things inside bare git repo
        get_scm(os.path.dirname(full_path))
        return False
    except VCSError:
        pass

    # check if it's a valid path
    if os.path.isdir(full_path):
        return True

    return False
Exemplo n.º 3
0
def is_valid_repos_group(repos_group_name, base_path, skip_path_check=False):
    """
    Returns True if given path is a repository group False otherwise

    :param repo_name:
    :param base_path:
    """
    full_path = os.path.join(safe_str(base_path), safe_str(repos_group_name))

    # check if it's not a repo
    if is_valid_repo(repos_group_name, base_path):
        return False

    try:
        # we need to check bare git repos at higher level
        # since we might match branches/hooks/info/objects or possible
        # other things inside bare git repo
        get_scm(os.path.dirname(full_path))
        return False
    except VCSError:
        pass

    # check if it's a valid path
    if skip_path_check or os.path.isdir(full_path):
        return True

    return False
Exemplo n.º 4
0
    def test_get_two_scms_for_path(self, tmpdir):
        multialias_repo_path = str(tmpdir)

        subprocess.check_call(['hg', 'init', multialias_repo_path])
        subprocess.check_call(['git', 'init', multialias_repo_path])

        with pytest.raises(VCSError):
            get_scm(multialias_repo_path)
Exemplo n.º 5
0
def is_valid_repo(repo_name, base_path):
    """
    Returns True if given path is a valid repository False otherwise

    :param repo_name:
    :param base_path:

    :return True: if given path is a valid repository
    """
    full_path = os.path.join(safe_str(base_path), safe_str(repo_name))

    try:
        get_scm(full_path)
        return True
    except VCSError:
        return False
Exemplo n.º 6
0
def is_valid_repo_group(repo_group_name, base_path, skip_path_check=False):
    """
    Returns True if given path is a repository group, False otherwise

    :param repo_name:
    :param base_path:
    """
    full_path = os.path.join(safe_str(base_path), safe_str(repo_group_name))
    log.debug('Checking if `%s` is a valid path for repository group',
              repo_group_name)

    # check if it's not a repo
    if is_valid_repo(repo_group_name, base_path):
        log.debug('Repo called %s exist, it is not a valid '
                  'repo group' % repo_group_name)
        return False

    try:
        # we need to check bare git repos at higher level
        # since we might match branches/hooks/info/objects or possible
        # other things inside bare git repo
        scm_ = get_scm(os.path.dirname(full_path))
        log.debug('path: %s is a vcs object:%s, not valid '
                  'repo group' % (full_path, scm_))
        return False
    except VCSError:
        pass

    # check if it's a valid path
    if skip_path_check or os.path.isdir(full_path):
        log.debug('path: %s is a valid repo group !', full_path)
        return True

    log.debug('path: %s is not a valid repo group !', full_path)
    return False
Exemplo n.º 7
0
    def _get_repos(p):
        if not os.access(p, os.W_OK):
            log.warn('ignoring repo path without write access: %s', p)
            return
        for dirpath in os.listdir(p):
            if os.path.isfile(os.path.join(p, dirpath)):
                continue
            cur_path = os.path.join(p, dirpath)

            # skip removed repos
            if skip_removed_repos and REMOVED_REPO_PAT.match(dirpath):
                continue

            #skip .<somethin> dirs
            if dirpath.startswith('.'):
                continue

            try:
                scm_info = get_scm(cur_path)
                yield scm_info[1].split(path, 1)[-1].lstrip(os.sep), scm_info
            except VCSError:
                if not recursive:
                    continue
                #check if this dir containts other repos for recursive scan
                rec_path = os.path.join(p, dirpath)
                if os.path.isdir(rec_path):
                    for inner_scm in _get_repos(rec_path):
                        yield inner_scm
Exemplo n.º 8
0
    def __get_instance(self):

        repo_full_path = self.repo_full_path

        try:
            alias = get_scm(repo_full_path)[0]
            log.debug("Creating instance of %s repository" % alias)
            backend = get_backend(alias)
        except VCSError:
            log.error(traceback.format_exc())
            log.error(
                "Perhaps this repository is in db and not in "
                "filesystem run rescan repositories with "
                '"destroy old data " option from admin panel'
            )
            return

        if alias == "hg":

            repo = backend(safe_str(repo_full_path), create=False, baseui=self._ui)
            # skip hidden web repository
            if repo._get_hidden():
                return
        else:
            repo = backend(repo_full_path, create=False)

        return repo
Exemplo n.º 9
0
def is_valid_repo(repo_name, base_path, expect_scm=None, explicit_scm=None):
    """
    Returns True if given path is a valid repository False otherwise.
    If expect_scm param is given also, compare if given scm is the same
    as expected from scm parameter. If explicit_scm is given don't try to
    detect the scm, just use the given one to check if repo is valid

    :param repo_name:
    :param base_path:
    :param expect_scm:
    :param explicit_scm:

    :return True: if given path is a valid repository
    """
    full_path = os.path.join(safe_str(base_path), safe_str(repo_name))
    log.debug('Checking if `%s` is a valid path for repository', repo_name)

    try:
        if explicit_scm:
            detected_scms = [get_scm_backend(explicit_scm)]
        else:
            detected_scms = get_scm(full_path)

        if expect_scm:
            return detected_scms[0] == expect_scm
        log.debug('path: %s is an vcs object:%s', full_path, detected_scms)
        return True
    except VCSError:
        log.debug('path: %s is not a valid repo !', full_path)
        return False
Exemplo n.º 10
0
    def _get_repos(p):
        if not os.access(p, os.R_OK) or not os.access(p, os.X_OK):
            log.warn('ignoring repo path without access: %s' % (p, ))
            return
        if not os.access(p, os.W_OK):
            log.warn('repo path without write access: %s' % (p, ))
        for dirpath in os.listdir(p):
            if os.path.isfile(os.path.join(p, dirpath)):
                continue
            cur_path = os.path.join(p, dirpath)

            # skip removed repos
            if skip_removed_repos and REMOVED_REPO_PAT.match(dirpath):
                continue

            #skip .<somethin> dirs
            if dirpath.startswith('.'):
                continue

            try:
                scm_info = get_scm(cur_path)
                yield scm_info[1].split(path, 1)[-1].lstrip(os.sep), scm_info
            except VCSError:
                if not recursive:
                    continue
                #check if this dir containts other repos for recursive scan
                rec_path = os.path.join(p, dirpath)
                if os.path.isdir(rec_path):
                    for inner_scm in _get_repos(rec_path):
                        yield inner_scm
Exemplo n.º 11
0
    def _get_repos(p):
        dirpaths = _get_dirpaths(p)
        if not _is_dir_writable(p):
            log.warning('repo path without write access: %s', p)

        for dirpath in dirpaths:
            if os.path.isfile(os.path.join(p, dirpath)):
                continue
            cur_path = os.path.join(p, dirpath)

            # skip removed repos
            if skip_removed_repos and REMOVED_REPO_PAT.match(dirpath):
                continue

            #skip .<somethin> dirs
            if dirpath.startswith('.'):
                continue

            try:
                scm_info = get_scm(cur_path)
                yield scm_info[1].split(path, 1)[-1].lstrip(os.sep), scm_info
            except VCSError:
                if not recursive:
                    continue
                #check if this dir containts other repos for recursive scan
                rec_path = os.path.join(p, dirpath)
                if os.path.isdir(rec_path):
                    for inner_scm in _get_repos(rec_path):
                        yield inner_scm
Exemplo n.º 12
0
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_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
Exemplo n.º 13
0
 def _get_repos(p):
     if not os.access(p, os.W_OK):
         return
     for dirpath in os.listdir(p):
         if os.path.isfile(os.path.join(p, dirpath)):
             continue
         cur_path = os.path.join(p, dirpath)
         try:
             scm_info = get_scm(cur_path)
             yield scm_info[1].split(path, 1)[-1].lstrip(os.sep), scm_info
         except VCSError:
             if not recursive:
                 continue
             #check if this dir containts other repos for recursive scan
             rec_path = os.path.join(p, dirpath)
             if os.path.isdir(rec_path):
                 for inner_scm in _get_repos(rec_path):
                     yield inner_scm
Exemplo n.º 14
0
def is_valid_repo(repo_name, base_path, scm=None):
    """
    Returns True if given path is a valid repository False otherwise.
    If scm param is given also compare if given scm is the same as expected
    from scm parameter

    :param repo_name:
    :param base_path:
    :param scm:

    :return True: if given path is a valid repository
    """
    full_path = os.path.join(safe_str(base_path), safe_str(repo_name))

    try:
        scm_ = get_scm(full_path)
        if scm:
            return scm_[0] == scm
        return True
    except VCSError:
        return False
Exemplo n.º 15
0
    def __get_instance(self):
        repo_full_path = self.repo_full_path
        try:
            alias = get_scm(repo_full_path)[0]
            log.debug('Creating instance of %s repository' % alias)
            backend = get_backend(alias)
        except VCSError:
            log.error(traceback.format_exc())
            log.error('Perhaps this repository is in db and not in '
                      'filesystem run rescan repositories with '
                      '"destroy old data " option from admin panel')
            return

        if alias == 'hg':

            repo = backend(safe_str(repo_full_path), create=False,
                           config=self._config)
        else:
            repo = backend(repo_full_path, create=False)

        return repo
Exemplo n.º 16
0
def is_valid_repo(repo_name, base_path, scm=None):
    """
    Returns True if given path is a valid repository False otherwise.
    If scm param is given also compare if given scm is the same as expected
    from scm parameter

    :param repo_name:
    :param base_path:
    :param scm:

    :return True: if given path is a valid repository
    """
    full_path = os.path.join(safe_str(base_path), safe_str(repo_name))

    try:
        scm_ = get_scm(full_path)
        if scm:
            return scm_[0] == scm
        return True
    except VCSError:
        return False
Exemplo n.º 17
0
def get_current_revision(quiet=False):
    """
    Returns tuple of (number, id) from repository containing this package
    or None if repository could not be found.

    :param quiet: prints error for fetching revision if True
    """

    try:
        from rhodecode.lib.vcs import get_repo
        from rhodecode.lib.vcs.utils.helpers import get_scm
        repopath = os.path.join(os.path.dirname(__file__), '..', '..')
        scm = get_scm(repopath)[0]
        repo = get_repo(path=repopath, alias=scm)
        tip = repo.get_changeset()
        return (tip.revision, tip.short_id)
    except Exception, err:
        if not quiet:
            print ("Cannot retrieve rhodecode's revision. Original error "
                   "was: %s" % err)
        return None
Exemplo n.º 18
0
def get_current_revision(quiet=False):
    """
    Returns tuple of (number, id) from repository containing this package
    or None if repository could not be found.

    :param quiet: prints error for fetching revision if True
    """

    try:
        from rhodecode.lib.vcs import get_repo
        from rhodecode.lib.vcs.utils.helpers import get_scm
        repopath = os.path.join(os.path.dirname(__file__), '..', '..')
        scm = get_scm(repopath)[0]
        repo = get_repo(path=repopath, alias=scm)
        tip = repo.get_changeset()
        return (tip.revision, tip.short_id)
    except Exception, err:
        if not quiet:
            print("Cannot retrieve rhodecode's revision. Original error "
                  "was: %s" % err)
        return None
Exemplo n.º 19
0
def get_current_revision(quiet=False):
    """
    Returns tuple of (number, id) from repository containing this package
    or None if repository could not be found.

    :param quiet: prints error for fetching revision if True
    """

    try:
        from rhodecode.lib.vcs import get_repo
        from rhodecode.lib.vcs.utils.helpers import get_scm
        repopath = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                                '..', '..'))
        scm = get_scm(repopath)[0]
        repo = get_repo(path=repopath, alias=scm)
        wk_dir = repo.workdir
        cur_rev = wk_dir.get_changeset()
        return (cur_rev.revision, cur_rev.short_id)
    except Exception, err:
        if not quiet:
            print ("WARNING: Cannot retrieve rhodecode's revision. "
                   "disregard this if you don't know what that means. "
                   "Original error was: %s" % err)
        return None
Exemplo n.º 20
0
 def test_get_scm_error_path(self):
     with pytest.raises(VCSError):
         get_scm('err')
Exemplo n.º 21
0
 def test_ignores_svn_working_copy(self, tmpdir):
     tmpdir.mkdir('.svn')
     with pytest.raises(VCSError):
         get_scm(tmpdir.strpath)
Exemplo n.º 22
0
 def test_get_scm(self):
     self.assertEqual(('hg', TEST_HG_REPO), get_scm(TEST_HG_REPO))
     self.assertEqual(('git', TEST_GIT_REPO), get_scm(TEST_GIT_REPO))
Exemplo n.º 23
0
 def test_raises_if_path_is_empty(self, tmpdir):
     with pytest.raises(VCSError):
         get_scm(str(tmpdir))
Exemplo n.º 24
0
 def test_existing_repository(self, vcs_repository_support):
     alias, repo = vcs_repository_support
     assert (alias, repo.path) == get_scm(repo.path)
Exemplo n.º 25
0
 def test_get_scm(self):
     self.assertEqual(('hg', TEST_HG_REPO), get_scm(TEST_HG_REPO))
     self.assertEqual(('git', TEST_GIT_REPO), get_scm(TEST_GIT_REPO))