<commit_before>"""
Autogenerated Problem Facilitation
"""

import api

import shutil
import os
from os import path
from string import Template

from api.common import InternalException

log = api.logger.use(__name__)

_build_directories = []

def generate_build_directory(base=os.getcwd()):
    """
    Generates a random writable directory for generators to build instances with.
    All directories generated will be removed in the call to clear_build_directories()

    Args:
        base: base path to create the directory. defaults to the current working directory.
    Returns:
        The path to the directory
    """

    build_path = path.join(base, api.common.token()[:6])

    try:
        os.makedirs(build_path)
    except Exception:
        raise InternalException("Unable to create build path: "+build_path)

    _build_directories.append(build_path)

    return build_path

def clear_build_directories():
    """
    Removes everything in the generated build directories.
    """

    problems = []

    for directory in _build_directories:
        if path.isdir(directory):
            try:
                shutil.rmtree(directory)
            except Exception:
                problems.append(directory)

    problem_string = ", ".join(problems)

    if len(problems) > 0:
        raise InternalException("Unable to delete these directories: "+problem_string)
            

def replace_source_tokens(file_path, lookup, out_path):
    """
    Replace tokens in a file and write out the substitution.

    Args:
        file_path: the path of the file to be read.
        lookup: a dictionary containing the substitutions.
        out_path: file in which the substitutions will be written.<commit_msg>Clears build list on sucessful deletion.<commit_after>"""
Autogenerated Problem Facilitation
"""

import api

import shutil
import os
from os import path
from string import Template

from api.common import InternalException

log = api.logger.use(__name__)

_build_directories = []

def generate_build_directory(base=os.getcwd()):
    """
    Generates a random writable directory for generators to build instances with.
    All directories generated will be removed in the call to clear_build_directories()

    Args:
        base: base path to create the directory. defaults to the current working directory.
    Returns:
        The path to the directory
    """

    build_path = path.join(base, api.common.token()[:6])

    try:
        os.makedirs(build_path)
    except Exception:
        raise InternalException("Unable to create build path: "+build_path)

    _build_directories.append(build_path)

    return build_path

def clear_build_directories():
    """
    Removes everything in the generated build directories.
    """

    problems = []

    for directory in _build_directories:
        if path.isdir(directory):
            try:
                shutil.rmtree(directory)
            except Exception:
                problems.append(directory)

    problem_string = ", ".join(problems)

    if len(problems) > 0:
        raise InternalException("Unable to delete these directories: "+problem_string)
    else:
        _build_directories.clear()

def replace_source_tokens(file_path, lookup, out_path):
    """
    Replace tokens in a file and write out the substitution.

    Args:
        file_path: the path of the file to be read.
        lookup: a dictionary containing the substitutions.
        out_path: file in which the substitutions will be written.
{'fraction_non_alphanumeric': 0.07238912732474964, 'fraction_numerical': 0.0011444921316165952, 'mean_word_length': 2.968217934165721, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7572035', 'n_tokens_mistral': 964, 'n_tokens_neox': 911, 'n_words': 322}