<commit_before># Copyright 2016 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import platform
import re
import shutil
import sys
from typing import Any, Mapping, Optional, Tuple, Union

import nox.command
import py
from nox.logger import logger

from . import _typing

# Problematic environment variables that are stripped from all commands inside
# of a virtualenv. See https://github.com/theacodes/nox/issues/44
_BLACKLISTED_ENV_VARS = frozenset(
    ["PIP_RESPECT_VIRTUALENV", "PIP_REQUIRE_VIRTUALENV", "__PYVENV_LAUNCHER__"]
)
_SYSTEM = platform.system()


class InterpreterNotFound(OSError):
    def __init__(self, interpreter: str) -> None:
        super().__init__("Python interpreter {} not found".format(interpreter))
        self.interpreter = interpreter


class ProcessEnv:
    """A environment with a 'bin' directory and a set of 'env' vars."""

    # Does this environment provide any process isolation?
    is_sandboxed = False

    # Special programs that aren't included in the environment.
    allowed_globals = ()  # type: _typing.ClassVar[Tuple[Any, ...]]

    def __init__(self, bin: None = None, env: Mapping[str, str] = None) -> None:
        self._bin = bin
        self.env = os.environ.copy()

        if env is not None:
            self.env.update(env)

        for key in _BLACKLISTED_ENV_VARS:
            self.env.pop(key, None)

        if self.bin:
            self.env["PATH"] = os.pathsep.join([self.bin, self.env.get("PATH", "")])

    @property
    def bin(self) -> Optional[str]:
        return self._bin

    def create(self) -> bool:
        raise NotImplementedError("ProcessEnv.create should be overwitten in subclass")


def locate_via_py(version: str) -> Optional[str]:
    """Find the Python executable using the Windows Launcher.

    This is based on :pep:397 which details that executing
    ``py.exe -{version}`` should execute python with the requested
    version. We then make the python process print out its full
    executable path which we use as the location for the version-
    specific Python interpreter.

    Args:
        version (str): The desired Python version to pass to ``py.exe``. Of the form
            ``X.Y`` or ``X.Y-32``. For example, a usage of the Windows Launcher might
            be ``py -3.6-32``.

    Returns:
        Optional[str]: The full executable path for the Python ``version``,
        if it is found.
    """
    script = "import sys; print(sys.executable)"
    py_exe = py.path.local.sysfind("py")
    if py_exe is not None:
        try:
            return py_exe.sysexec("-" + version, "-c", script).strip()
        except py.process.cmdexec.Error:
            return None
    return None


def locate_using_path_and_version(version: str) -> Optional[str]:
    """Check the PATH's python interpreter and return it if the version
    matches.

    On systems without version-named interpreters and with missing
    launcher (which is on all Windows Anaconda installations),
    we search the PATH for a plain "python" interpreter and accept it
    if its --version matches the specified interpreter version.

    Args:
        version (str): The desired Python version. Of the form ``X.Y``.

    Returns:
        Optional[str]: The full executable path for the Python ``version``,
        if it is found.
    """
    if not version:
        return None

    script = "import platform; print(platform.python_version())"
    path_python = py.path.local.sysfind("python")
    if path_python:
        try:
            prefix = "{}.".format(version)
            version_string = path_python.sysexec("-c", script).strip()
            if version_string.startswith(prefix):
                return str(path_python)
        except py.process.cmdexec.Error:
            return None

    return None


def _clean_location(self: "Union[CondaEnv, VirtualEnv]") -> bool:
    """Deletes any existing path-based environment"""
    if os.path.exists(self.location):
        if self.reuse_existing:
            return False
        else:
            shutil.rmtree(self.location)

    return True


class CondaEnv(ProcessEnv):
    """Conda environemnt management class.

    Args:
        location (str): The location on the filesystem where the conda environment
            should be created.
        interpreter (Optional[str]): The desired Python version. Of the form

            * ``X.Y``, e.g. ``3.5``
            * ``X.Y-32``. For example, a usage of the Windows Launcher might
              be ``py -3.6-32``
            * ``X.Y.Z``, e.g. ``3.4.9``
            * ``pythonX.Y``, e.g. ``python2.7``
            * A path in the filesystem to a Python executable

            If not specified, this will use the currently running Python.
        reuse_existing (Optional[bool]): Flag indicating if the conda environment
            should be reused if it already exists at ``location``.
    """

    is_sandboxed = True
    allowed_globals = ("conda",)

    def __init__(
        self,
        location: str,
        interpreter: Optional[str] = None,
        reuse_existing: bool = False,
        venv_params: Any = None,
    ):
        self.location_name = location
        self.location = os.path.abspath(location)<commit_msg>Fix overwritten and environment typos in virtualenv.py (#326)<commit_after># Copyright 2016 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import platform
import re
import shutil
import sys
from typing import Any, Mapping, Optional, Tuple, Union

import nox.command
import py
from nox.logger import logger

from . import _typing

# Problematic environment variables that are stripped from all commands inside
# of a virtualenv. See https://github.com/theacodes/nox/issues/44
_BLACKLISTED_ENV_VARS = frozenset(
    ["PIP_RESPECT_VIRTUALENV", "PIP_REQUIRE_VIRTUALENV", "__PYVENV_LAUNCHER__"]
)
_SYSTEM = platform.system()


class InterpreterNotFound(OSError):
    def __init__(self, interpreter: str) -> None:
        super().__init__("Python interpreter {} not found".format(interpreter))
        self.interpreter = interpreter


class ProcessEnv:
    """A environment with a 'bin' directory and a set of 'env' vars."""

    # Does this environment provide any process isolation?
    is_sandboxed = False

    # Special programs that aren't included in the environment.
    allowed_globals = ()  # type: _typing.ClassVar[Tuple[Any, ...]]

    def __init__(self, bin: None = None, env: Mapping[str, str] = None) -> None:
        self._bin = bin
        self.env = os.environ.copy()

        if env is not None:
            self.env.update(env)

        for key in _BLACKLISTED_ENV_VARS:
            self.env.pop(key, None)

        if self.bin:
            self.env["PATH"] = os.pathsep.join([self.bin, self.env.get("PATH", "")])

    @property
    def bin(self) -> Optional[str]:
        return self._bin

    def create(self) -> bool:
        raise NotImplementedError("ProcessEnv.create should be overwritten in subclass")


def locate_via_py(version: str) -> Optional[str]:
    """Find the Python executable using the Windows Launcher.

    This is based on :pep:397 which details that executing
    ``py.exe -{version}`` should execute python with the requested
    version. We then make the python process print out its full
    executable path which we use as the location for the version-
    specific Python interpreter.

    Args:
        version (str): The desired Python version to pass to ``py.exe``. Of the form
            ``X.Y`` or ``X.Y-32``. For example, a usage of the Windows Launcher might
            be ``py -3.6-32``.

    Returns:
        Optional[str]: The full executable path for the Python ``version``,
        if it is found.
    """
    script = "import sys; print(sys.executable)"
    py_exe = py.path.local.sysfind("py")
    if py_exe is not None:
        try:
            return py_exe.sysexec("-" + version, "-c", script).strip()
        except py.process.cmdexec.Error:
            return None
    return None


def locate_using_path_and_version(version: str) -> Optional[str]:
    """Check the PATH's python interpreter and return it if the version
    matches.

    On systems without version-named interpreters and with missing
    launcher (which is on all Windows Anaconda installations),
    we search the PATH for a plain "python" interpreter and accept it
    if its --version matches the specified interpreter version.

    Args:
        version (str): The desired Python version. Of the form ``X.Y``.

    Returns:
        Optional[str]: The full executable path for the Python ``version``,
        if it is found.
    """
    if not version:
        return None

    script = "import platform; print(platform.python_version())"
    path_python = py.path.local.sysfind("python")
    if path_python:
        try:
            prefix = "{}.".format(version)
            version_string = path_python.sysexec("-c", script).strip()
            if version_string.startswith(prefix):
                return str(path_python)
        except py.process.cmdexec.Error:
            return None

    return None


def _clean_location(self: "Union[CondaEnv, VirtualEnv]") -> bool:
    """Deletes any existing path-based environment"""
    if os.path.exists(self.location):
        if self.reuse_existing:
            return False
        else:
            shutil.rmtree(self.location)

    return True


class CondaEnv(ProcessEnv):
    """Conda environment management class.

    Args:
        location (str): The location on the filesystem where the conda environment
            should be created.
        interpreter (Optional[str]): The desired Python version. Of the form

            * ``X.Y``, e.g. ``3.5``
            * ``X.Y-32``. For example, a usage of the Windows Launcher might
              be ``py -3.6-32``
            * ``X.Y.Z``, e.g. ``3.4.9``
            * ``pythonX.Y``, e.g. ``python2.7``
            * A path in the filesystem to a Python executable

            If not specified, this will use the currently running Python.
        reuse_existing (Optional[bool]): Flag indicating if the conda environment
            should be reused if it already exists at ``location``.
    """

    is_sandboxed = True
    allowed_globals = ("conda",)

    def __init__(
        self,
        location: str,
        interpreter: Optional[str] = None,
        reuse_existing: bool = False,
        venv_params: Any = None,
    ):
        self.location_name = location
        self.location = os.path.abspath(location)
{'fraction_non_alphanumeric': 0.09915630164390711, 'fraction_numerical': 0.005827607201878751, 'mean_word_length': 2.674656439757111, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '303307', 'n_tokens_mistral': 3357, 'n_tokens_neox': 3208, 'n_words': 1172}