# -*- coding: utf-8 -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
import ulib.base.i_need_py23

__all__ = ('get_vcsdir', 'is_vcsdir', 'find_vcsdir', 'new_vcsdir')

from os import path

from cvs import is_cvsdir, CVSDir
from svn import is_svndir, SVNDir

def get_vcsdir(p):
    if is_cvsdir(p): return CVSDir(p)
    elif is_svndir(p): return SVNDir(p)
    else: return None

def is_vcsdir(p):
    return get_vcsdir(p) is not None

def find_vcsdir(p):
    p = path.abspath(p)
    vcsdir = get_vcsdir(p)
    while vcsdir is None:
        p = path.split(p)[0]
        if p in ('/', ''): break
        vcsdir = get_vcsdir(p)
    return vcsdir

def new_vcsdir(p, vcs_name):
    if vcs_name == 'cvs': return CVSDir(p)
    elif vcs_name == 'svn': return SVNDir(p)
    else: raise ValueError, u"Vcs non reconnu: %s" % vcs_name
