<commit_before>"""Gather version information for BB."""

from os import path
from plumbum import local
from benchbuild.utils.cmd import git
from benchbuild.settings import CFG
from benchbuild.utils.downloader import get_hash_of_dirs


def get_version_from_cache_dir(src_file):
    """
    Creates a version for a project out of the hash.

    The hash is taken from the directory of the source file.

    Args:
        src_file: The source file of the project using this function.

    Returns:
        Either returns the first 8 digits of the hash as string,
        the entire hash as a string if the hash consists out of less
        than 7 digits or None if the path is incorrect.
    """
    tmp_dir = CFG["tmp_dir"].value()
    if path.exists(tmp_dir):
        cache_file = path.join(tmp_dir, src_file)
        dir_hash = get_hash_of_dirs(cache_file)
        if dir_hash is None:
            return None
        elif len(str(dir_hash)) <= 7:
            return str(dir_hash)
        else:
            return str(dir_hash)[:7]
    else:
        return None


def get_git_hash(from_url):
    """
    Get the git commit hash of HEAD from :from_url.

    Args:
        from_url: The file system url of our git repository.

    Returns:
        git commit hash of HEAD, or empty string.
    """
    if from_url is None:
        return ""

    if not path.exists(from_url):
        return ""

    with local.cwd(from_url):
        return git("rev-parse", "HEAD", retcode=None)


LLVM_VERSION = get_git_hash(str(CFG["llvm"]["src"]))
CLANG_VERSION = get_git_hash(path.join(str(CFG["llvm"]["src"]), "tools",
                                       "clang"))
POLLY_VERSION = get_git_hash(path.join(str(CFG["llvm"]["src"]), "tools",
                                       "polly"))
POLLI_VERSION = get_git_hash(path.join(str(CFG["llvm"]["src"]), "tools",
                                       "polly", "tools", "polli"))<commit_msg>versions: use git-import only when necessary<commit_after>"""Gather version information for BB."""

from os import path
from plumbum import local
from benchbuild.settings import CFG
from benchbuild.utils.downloader import get_hash_of_dirs


def get_version_from_cache_dir(src_file):
    """
    Creates a version for a project out of the hash.

    The hash is taken from the directory of the source file.

    Args:
        src_file: The source file of the project using this function.

    Returns:
        Either returns the first 8 digits of the hash as string,
        the entire hash as a string if the hash consists out of less
        than 7 digits or None if the path is incorrect.
    """
    tmp_dir = CFG["tmp_dir"].value()
    if path.exists(tmp_dir):
        cache_file = path.join(tmp_dir, src_file)
        dir_hash = get_hash_of_dirs(cache_file)
        if dir_hash is None:
            return None
        elif len(str(dir_hash)) <= 7:
            return str(dir_hash)
        else:
            return str(dir_hash)[:7]
    else:
        return None


def get_git_hash(from_url):
    """
    Get the git commit hash of HEAD from :from_url.

    Args:
        from_url: The file system url of our git repository.

    Returns:
        git commit hash of HEAD, or empty string.
    """
    from benchbuild.utils.cmd import git
    if from_url is None:
        return ""

    if not path.exists(from_url):
        return ""

    with local.cwd(from_url):
        return git("rev-parse", "HEAD", retcode=None)
{'fraction_non_alphanumeric': 0.08840579710144927, 'fraction_numerical': 0.002318840579710145, 'mean_word_length': 2.4134520276953513, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5421284', 'n_tokens_mistral': 1044, 'n_tokens_neox': 1018, 'n_words': 350}