<commit_before>from __future__ import unicode_literals

import contextlib
import os
import sys

import pre_commit.constants as C
from pre_commit.envcontext import envcontext
from pre_commit.envcontext import Var
from pre_commit.languages import helpers
from pre_commit.languages.python import bin_dir
from pre_commit.util import clean_path_on_failure
from pre_commit.util import cmd_output


ENVIRONMENT_DIR = 'node_env'
get_default_version = helpers.basic_get_default_version
healthy = helpers.basic_healthy


def _envdir(prefix, version):
    directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
    return prefix.path(directory)


def get_env_patch(venv):
    if sys.platform == 'cygwin':  # pragma: no cover
        _, win_venv, _ = cmd_output('cygpath', '-w', venv)
        install_prefix = r'{}\bin'.format(win_venv.strip())
    elif sys.platform == 'win32':  # pragma: no cover
        install_prefix = bin_dir(venv)
    else:  # pragma: windows no cover
        install_prefix = venv
    return (
        ('NODE_VIRTUAL_ENV', venv),
        ('NPM_CONFIG_PREFIX', install_prefix),
        ('npm_config_prefix', install_prefix),
        ('NODE_PATH', os.path.join(venv, 'lib', 'node_modules')),
        ('PATH', (bin_dir(venv), os.pathsep, Var('PATH'))),
    )


@contextlib.contextmanager
def in_env(prefix, language_version):
    with envcontext(get_env_patch(_envdir(prefix, language_version))):
        yield


def install_environment(prefix, version, additional_dependencies):
    additional_dependencies = tuple(additional_dependencies)
    assert prefix.exists('package.json')
    envdir = _envdir(prefix, version)

    # https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx?f=255&MSPPError=-2147217396#maxpath
    if sys.platform == 'win32':  # pragma: no cover
        envdir = '\\\\?\\' + os.path.normpath(envdir)
    with clean_path_on_failure(envdir):
        cmd = [
            sys.executable, '-mnodeenv', '--prebuilt', '--clean-src', envdir,
        ]
        if version != C.DEFAULT:
            cmd.extend(['-n', version])
        cmd_output(*cmd)

        dep = 'git+file://{}'.format(prefix.prefix_dir)
        with in_env(prefix, version):
            helpers.run_setup_cmd(
                prefix,
                ('npm', 'install', '-g', dep) + additional_dependencies,
            )


def run_hook(hook, file_args):
    with in_env(hook.prefix, hook.language_version):
        return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)<commit_msg>3 slashes works around an npm bug<commit_after>from __future__ import unicode_literals

import contextlib
import os
import sys

import pre_commit.constants as C
from pre_commit.envcontext import envcontext
from pre_commit.envcontext import Var
from pre_commit.languages import helpers
from pre_commit.languages.python import bin_dir
from pre_commit.util import clean_path_on_failure
from pre_commit.util import cmd_output


ENVIRONMENT_DIR = 'node_env'
get_default_version = helpers.basic_get_default_version
healthy = helpers.basic_healthy


def _envdir(prefix, version):
    directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
    return prefix.path(directory)


def get_env_patch(venv):
    if sys.platform == 'cygwin':  # pragma: no cover
        _, win_venv, _ = cmd_output('cygpath', '-w', venv)
        install_prefix = r'{}\bin'.format(win_venv.strip())
    elif sys.platform == 'win32':  # pragma: no cover
        install_prefix = bin_dir(venv)
    else:  # pragma: windows no cover
        install_prefix = venv
    return (
        ('NODE_VIRTUAL_ENV', venv),
        ('NPM_CONFIG_PREFIX', install_prefix),
        ('npm_config_prefix', install_prefix),
        ('NODE_PATH', os.path.join(venv, 'lib', 'node_modules')),
        ('PATH', (bin_dir(venv), os.pathsep, Var('PATH'))),
    )


@contextlib.contextmanager
def in_env(prefix, language_version):
    with envcontext(get_env_patch(_envdir(prefix, language_version))):
        yield


def install_environment(prefix, version, additional_dependencies):
    additional_dependencies = tuple(additional_dependencies)
    assert prefix.exists('package.json')
    envdir = _envdir(prefix, version)

    # https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx?f=255&MSPPError=-2147217396#maxpath
    if sys.platform == 'win32':  # pragma: no cover
        envdir = '\\\\?\\' + os.path.normpath(envdir)
    with clean_path_on_failure(envdir):
        cmd = [
            sys.executable, '-mnodeenv', '--prebuilt', '--clean-src', envdir,
        ]
        if version != C.DEFAULT:
            cmd.extend(['-n', version])
        cmd_output(*cmd)

        dep = 'git+file:///{}'.format(prefix.prefix_dir)
        with in_env(prefix, version):
            helpers.run_setup_cmd(
                prefix,
                ('npm', 'install', '-g', dep) + additional_dependencies,
            )


def run_hook(hook, file_args):
    with in_env(hook.prefix, hook.language_version):
        return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)
{'fraction_non_alphanumeric': 0.12042713070990706, 'fraction_numerical': 0.01008503065058335, 'mean_word_length': 3.4761061946902654, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1853515', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1599, 'n_words': 325}