Example #1
0
def is_repo_dir(path, existence=True):
    """
        Checks if a directory contains a MathHub repository.

        Optimised to exit as soon as possible when the directory is not
        a repository.

        @param path {string} Path to check.
        @param existence {boolean} Do we do a theoretical test only or do we check for existence as well?

        @returns {boolean}
    """

    if re.match('^[^\/]+\/[^\/]+$', path):
        # it is already a name.
        name = path
    else:
        # it is not a name, so we need to find the relative_path.
        name = os.path.relpath(os.path.abspath(path), lmh_locate("content"))

        # if the name ends with a /, remove it.
        if name.endswith("/"):
            name = name[:-1]

        # if the path does not match, we need to exit.
        if not re.match('^[^\/]+\/[^\/]+$', name):
            return False

    # if we need existence, check it is a git directory
    if existence:
        return is_repo(path)

    # we have passed all the tests
    return True
Example #2
0
def is_repo_dir(path, existence = True):
    """
        Checks if a directory contains a MathHub repository.

        Optimised to exit as soon as possible when the directory is not
        a repository.

        @param path {string} Path to check.
        @param existence {boolean} Do we do a theoretical test only or do we check for existence as well?

        @returns {boolean}
    """

    if re.match('^[^\/]+\/[^\/]+$', path):
        # it is already a name.
        name = path
    else:
        # it is not a name, so we need to find the relative_path.
        name = os.path.relpath(
            os.path.abspath(path),
            lmh_locate("content")
        )

        # if the name ends with a /, remove it.
        if name.endswith("/"):
            name = name[:-1]

        # if the path does not match, we need to exit.
        if not re.match('^[^\/]+\/[^\/]+$', name):
            return False

    # if we need existence, check it is a git directory
    if existence:
        return is_repo(path)

    # we have passed all the tests
    return True
Example #3
0
def find_repo_dir(path, existence=True):
    """
        Returns the absolute path to the repository contained in PATH or False.

        If you need to only check if you are in a repository, use is_in_repo
        instead.

        @param {string} path - Path to check
        @param {boolean} [existence = True] - Do we need to check for existence?

        @returns {string|boolean}
    """

    # we figure out the relative path
    namecheck = os.path.relpath(
        lmh_locate("content"),
        os.path.abspath(path),
    )

    # if we go into a directory, we

    if namecheck.startswith('../../'):
        path = lmh_locate("content", path)

    # find the relative path
    # by going relatively from the path to the data directory.
    name = os.path.relpath(os.path.abspath(path), lmh_locate("content"))

    # Check that the path does not leave the DATA directory.
    # This has to be the first component of the path
    # or the entire path.
    if name.startswith("../") or name == "..":
        return False

    # the repository name should be everything until the second slash
    # so go through the path and find it
    num_slash_correct = False
    abs_name = lmh_locate("content", "")
    for (i, c) in enumerate(name):
        if c == "/":
            if num_slash_correct:
                # we have found the second slash
                # we want to check for existence now.
                if existence and not is_repo(abs_name):
                    return False

                # and then return the name.
                return abs_name
            else:
                # we have found the first slash
                num_slash_correct = True
        # add the caracter to the name
        abs_name += c

    # do we have the correct number of slashes?
    # if so, we can return the path.
    if num_slash_correct:
        # again, we might have to check for existence.
        if existence and not is_repo(abs_name):
            return False
        return abs_name
    else:
        return False
Example #4
0
def find_repo_dir(path, existence=True):
    """
        Returns the absolute path to the repository contained in PATH or False.

        If you need to only check if you are in a repository, use is_in_repo
        instead.

        @param {string} path - Path to check
        @param {boolean} [existence = True] - Do we need to check for existence?

        @returns {string|boolean}
    """

    # we figure out the relative path
    namecheck = os.path.relpath(
        lmh_locate("content"),
        os.path.abspath(path),
    )

    # if we go into a directory, we

    if namecheck.startswith('../../'):
        path = lmh_locate("content", path)

    # find the relative path
    # by going relatively from the path to the data directory.
    name = os.path.relpath(
        os.path.abspath(path),
        lmh_locate("content")
    )



    # Check that the path does not leave the DATA directory.
    # This has to be the first component of the path
    # or the entire path.
    if name.startswith("../") or name == "..":
        return False

    # the repository name should be everything until the second slash
    # so go through the path and find it
    num_slash_correct = False
    abs_name = lmh_locate("content", "")
    for (i, c) in enumerate(name):
        if c == "/":
            if num_slash_correct:
                # we have found the second slash
                # we want to check for existence now.
                if existence and not is_repo(abs_name):
                    return False

                # and then return the name.
                return abs_name
            else:
                # we have found the first slash
                num_slash_correct = True
        # add the caracter to the name
        abs_name += c

    # do we have the correct number of slashes?
    # if so, we can return the path.
    if num_slash_correct:
        # again, we might have to check for existence.
        if existence and not is_repo(abs_name):
            return False
        return abs_name
    else:
        return False