<commit_before>import os

from ..temp import create_temp_dir


class PythonBuilder(object):
    def __init__(self, console):
        self._console = console

    def matrix(self, project_config):
        return project_config.get_list("python", ["2.7"])
    
    def create_runtime(self, project_dir, entry):
        python_version = entry
        temp_dir = create_temp_dir()
        try:
            working_dir = temp_dir.path
            virtualenv_dir = os.path.join(working_dir, "virtualenv")
            self._create_virtualenv(virtualenv_dir, python_version)
        except Exception:
            temp_dir.close()
            raise
        return PythonRuntime(temp_dir, virtualenv_dir)
    
    def _create_virtualenv(self, path, python_version):
        python_binary = self._python_binary(python_version)
        
        def _pip_upgrade(package_name):
            pip = os.path.join(path, "bin", "pip")
            return [pip, "install", "--upgrade", package_name]
        
        commands = [
            ["virtualenv", path, "--python={0}".format(python_binary)],
            _pip_upgrade("pip"),
            _pip_upgrade("setuptools"),
            _pip_upgrade("virtualenv"),
        ]
        
        self._console.run_all(
            "Creating virtualenv for {0}".format(python_version),
            commands,
            quiet=True,
        )
        
        
    def _python_binary(self, python_version):
        if python_version == "pypy":
            return "pypy"
        else:
            return "python{0}".format(python_version)


class PythonRuntime(object):
    def __init__(self, temp_dir, virtualenv_dir):<commit_msg>Fix: cannot create python2.7 virtualenv under pypy<commit_after>import os

import spur

from ..temp import create_temp_dir


class PythonBuilder(object):
    def __init__(self, console):
        self._console = console

    def matrix(self, project_config):
        return project_config.get_list("python", ["2.7"])
    
    def create_runtime(self, project_dir, entry):
        python_version = entry
        temp_dir = create_temp_dir()
        try:
            working_dir = temp_dir.path
            virtualenv_dir = os.path.join(working_dir, "virtualenv")
            self._create_virtualenv(virtualenv_dir, python_version)
        except Exception:
            temp_dir.close()
            raise
        return PythonRuntime(temp_dir, virtualenv_dir)
    
    def _create_virtualenv(self, path, python_version):
        python_binary = self._python_binary(python_version)
        
        def _pip_upgrade(package_name):
            pip = os.path.join(path, "bin", "pip")
            return [pip, "install", "--upgrade", package_name]
        
        commands = [
            ["virtualenv", path, "--python={0}".format(python_binary)],
            _pip_upgrade("pip"),
            _pip_upgrade("setuptools"),
            _pip_upgrade("virtualenv"),
        ]
        
        self._console.run_all(
            "Creating virtualenv for {0}".format(python_version),
            commands,
            quiet=True,
        )
        
        
    def _python_binary(self, python_version):
        if python_version == "pypy":
            binary_name = "pypy"
        else:
            binary_name = "python{0}".format(python_version)
        
        return self._which(binary_name)
    
    def _which(self, binary_name):
        shell = spur.LocalShell()
        path = shell.run(["bash", "-lc", "echo $PATH"]).output.strip()
        return shell.run(["which", binary_name], update_env={"PATH": path}).output.strip()


class PythonRuntime(object):
    def __init__(self, temp_dir, virtualenv_dir):
{'fraction_non_alphanumeric': 0.10834473324213406, 'fraction_numerical': 0.003283173734610123, 'mean_word_length': 2.2325375773651635, 'pattern_counts': {'":': 3, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6061482', 'n_tokens_mistral': 1074, 'n_tokens_neox': 1036, 'n_words': 205}