<s> from __future__ import absolute_import <EOL> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> import contextlib <EOL> import io <EOL> import os . path <EOL> import traceback <EOL> from pre_commit import five <EOL> from pre_commit . errors import FatalError <EOL> from pre_commit . output import sys_stdout_write_wrapper <EOL> from pre_commit . store import Store <EOL> class PreCommitSystemExit ( SystemExit ) : <EOL> pass <EOL> def _to_bytes ( exc ) : <EOL> try : <EOL> return bytes ( exc ) <EOL> except Exception : <EOL> return five . text ( exc ) . encode ( '<STR_LIT>' ) <EOL> def _log_and_exit ( msg , exc , formatted , write_fn = sys_stdout_write_wrapper ) : <EOL> error_msg = b'<STR_LIT>' . join ( ( <EOL> five . to_bytes ( msg ) , b'<STR_LIT>' , <EOL> five . to_bytes ( type ( exc ) . __name__ ) , b'<STR_LIT>' , <EOL> _to_bytes ( exc ) , b'<STR_LIT:\n>' , <EOL> ) ) <EOL> write_fn ( error_msg ) <EOL> write_fn ( '<STR_LIT>' ) <EOL> store = Store ( ) <EOL> store . require_created ( ) <EOL> with io . open ( os . path . join ( store . directory , '<STR_LIT>' ) , '<STR_LIT:wb>' ) as log : <EOL> log . write ( five . to_bytes ( error_msg ) ) <EOL> log . write ( five . to_bytes ( formatted ) + b'<STR_LIT:\n>' ) <EOL> raise PreCommitSystemExit ( <NUM_LIT:1> ) <EOL> @ contextlib . contextmanager <EOL> def error_handler ( ) : <EOL> try : <EOL> yield <EOL> except FatalError as e : <EOL> _log_and_exit ( '<STR_LIT>' , e , traceback . format_exc ( ) ) <EOL> except Exception as e : <EOL> _log_and_exit ( <EOL> '<STR_LIT>' , <EOL> e , <EOL> traceback . format_exc ( ) , <EOL> ) </s>
