<s> from __future__ import unicode_literals <EOL> import contextlib <EOL> import io <EOL> import os . path <EOL> import shutil <EOL> from pre_commit . envcontext import envcontext <EOL> from pre_commit . envcontext import Var <EOL> from pre_commit . languages import helpers <EOL> from pre_commit . util import CalledProcessError <EOL> from pre_commit . util import clean_path_on_failure <EOL> from pre_commit . util import resource_filename <EOL> from pre_commit . util import tarfile_open <EOL> from pre_commit . xargs import xargs <EOL> ENVIRONMENT_DIR = '<STR_LIT>' <EOL> def get_env_patch ( venv , language_version ) : <EOL> return ( <EOL> ( '<STR_LIT>' , os . path . join ( venv , '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , venv ) , <EOL> ( '<STR_LIT>' , language_version ) , <EOL> ( '<STR_LIT>' , ( <EOL> os . path . join ( venv , '<STR_LIT>' , '<STR_LIT>' ) , os . pathsep , <EOL> os . path . join ( venv , '<STR_LIT>' ) , os . pathsep , <EOL> os . path . join ( venv , '<STR_LIT>' ) , os . pathsep , Var ( '<STR_LIT>' ) , <EOL> ) ) , <EOL> ) <EOL> @ contextlib . contextmanager <EOL> def in_env ( repo_cmd_runner , language_version ) : <EOL> envdir = os . path . join ( <EOL> repo_cmd_runner . prefix_dir , <EOL> helpers . environment_dir ( ENVIRONMENT_DIR , language_version ) , <EOL> ) <EOL> with envcontext ( get_env_patch ( envdir , language_version ) ) : <EOL> yield <EOL> def _install_rbenv ( repo_cmd_runner , version = '<STR_LIT:default>' ) : <EOL> directory = helpers . environment_dir ( ENVIRONMENT_DIR , version ) <EOL> with tarfile_open ( resource_filename ( '<STR_LIT>' ) ) as tf : <EOL> tf . extractall ( repo_cmd_runner . path ( '<STR_LIT:.>' ) ) <EOL> shutil . move ( <EOL> repo_cmd_runner . path ( '<STR_LIT>' ) , repo_cmd_runner . path ( directory ) , <EOL> ) <EOL> if version != '<STR_LIT:default>' : <EOL> with tarfile_open ( resource_filename ( '<STR_LIT>' ) ) as tf : <EOL> tf . extractall ( repo_cmd_runner . path ( directory , '<STR_LIT>' ) ) <EOL> with tarfile_open ( resource_filename ( '<STR_LIT>' ) ) as tf : <EOL> tf . extractall ( repo_cmd_runner . path ( directory , '<STR_LIT>' ) ) <EOL> activate_path = repo_cmd_runner . path ( directory , '<STR_LIT>' , '<STR_LIT>' ) <EOL> with io . open ( activate_path , '<STR_LIT:w>' ) as activate_file : <EOL> activate_file . write ( <EOL> '<STR_LIT>' <EOL> "<STR_LIT>" <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> "<STR_LIT>" <EOL> '<STR_LIT>' <EOL> '<STR_LIT:\n>' . format ( repo_cmd_runner . path ( directory ) ) <EOL> ) <EOL> if version != '<STR_LIT:default>' : <EOL> activate_file . write ( '<STR_LIT>' . format ( version ) ) <EOL> def _install_ruby ( runner , version ) : <EOL> try : <EOL> helpers . run_setup_cmd ( runner , ( '<STR_LIT>' , '<STR_LIT>' , version ) ) <EOL> except CalledProcessError : <EOL> helpers . run_setup_cmd ( runner , ( '<STR_LIT>' , '<STR_LIT>' , version ) ) <EOL> def install_environment ( <EOL> repo_cmd_runner , <EOL> version = '<STR_LIT:default>' , <EOL> additional_dependencies = ( ) , <EOL> ) : <EOL> additional_dependencies = tuple ( additional_dependencies ) <EOL> directory = helpers . environment_dir ( ENVIRONMENT_DIR , version ) <EOL> with clean_path_on_failure ( repo_cmd_runner . path ( directory ) ) : <EOL> _install_rbenv ( repo_cmd_runner , version = version ) <EOL> with in_env ( repo_cmd_runner , version ) : <EOL> helpers . run_setup_cmd ( repo_cmd_runner , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:->' ) ) <EOL> if version != '<STR_LIT:default>' : <EOL> _install_ruby ( repo_cmd_runner , version ) <EOL> helpers . run_setup_cmd ( repo_cmd_runner , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> helpers . run_setup_cmd ( <EOL> repo_cmd_runner , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) + repo_cmd_runner . star ( '<STR_LIT>' ) , <EOL> ) <EOL> helpers . run_setup_cmd ( <EOL> repo_cmd_runner , <EOL> ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) + <EOL> repo_cmd_runner . star ( '<STR_LIT>' ) + additional_dependencies <EOL> ) , <EOL> ) <EOL> def run_hook ( repo_cmd_runner , hook , file_args ) : <EOL> with in_env ( repo_cmd_runner , hook [ '<STR_LIT>' ] ) : <EOL> return xargs ( ( hook [ '<STR_LIT>' ] , ) + tuple ( hook [ '<STR_LIT:args>' ] ) , file_args ) </s>
