<s> from __future__ import print_function <EOL> import os <EOL> import sys <EOL> import subprocess <EOL> from os import environ as env <EOL> from shellpython import config <EOL> _colorama_intialized = False <EOL> _colorama_available = True <EOL> try : <EOL> import colorama <EOL> from colorama import Fore , Style <EOL> except ImportError : <EOL> _colorama_available = False <EOL> def _is_colorama_enabled ( ) : <EOL> return _colorama_available and config . COLORAMA_ENABLED <EOL> def _print_stdout ( text ) : <EOL> print ( text ) <EOL> def _print_stderr ( text ) : <EOL> print ( text , file = sys . stderr ) <EOL> _PARAM_PRINT_STDOUT = '<STR_LIT:p>' <EOL> _PARAM_PRINT_STDERR = '<STR_LIT:e>' <EOL> _PARAM_INTERACTIVE = '<STR_LIT:i>' <EOL> _PARAM_NO_THROW = '<STR_LIT:n>' <EOL> def exe ( cmd , params ) : <EOL> """<STR_LIT>""" <EOL> global _colorama_intialized <EOL> if _is_colorama_enabled ( ) and not _colorama_intialized : <EOL> _colorama_intialized = True <EOL> colorama . init ( ) <EOL> if config . PRINT_ALL_COMMANDS : <EOL> if _is_colorama_enabled ( ) : <EOL> _print_stdout ( Fore . GREEN + '<STR_LIT>' + cmd + Style . RESET_ALL ) <EOL> else : <EOL> _print_stdout ( '<STR_LIT>' + cmd ) <EOL> if _is_param_set ( params , _PARAM_INTERACTIVE ) : <EOL> return _create_interactive_result ( cmd , params ) <EOL> else : <EOL> return _create_result ( cmd , params ) <EOL> def _is_param_set ( params , param ) : <EOL> return True if params . find ( param ) != - <NUM_LIT:1> else False <EOL> class ShellpyError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class NonZeroReturnCodeError ( ShellpyError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cmd , result ) : <EOL> self . cmd = cmd <EOL> self . result = result <EOL> def __str__ ( self ) : <EOL> if _is_colorama_enabled ( ) : <EOL> return '<STR_LIT>' . format ( red = Fore . RED , end = Style . RESET_ALL , cmd = self . cmd , code = self . result . returncode , <EOL> stderr = self . result . stderr ) <EOL> else : <EOL> return '<STR_LIT>' . format ( <EOL> cmd = self . cmd , code = self . result . returncode , stderr = self . result . stderr ) <EOL> class Stream : <EOL> def __init__ ( self , file , encoding , print_out_stream = False , color = None ) : <EOL> self . _file = file <EOL> self . _encoding = encoding <EOL> self . _print_out_stream = print_out_stream <EOL> self . _color = color <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> return self . sreadline ( ) <EOL> __next__ = next <EOL> def sreadline ( self ) : <EOL> line = self . _file . readline ( ) <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> : <EOL> line = line . decode ( self . _encoding ) <EOL> if line == '<STR_LIT>' : <EOL> raise StopIteration <EOL> else : <EOL> line = line . rstrip ( os . linesep ) <EOL> if self . _print_out_stream : <EOL> if self . _color is None : <EOL> _print_stdout ( line ) <EOL> else : <EOL> _print_stdout ( self . _color + line + Style . RESET_ALL ) <EOL> return line <EOL> def swriteline ( self , text ) : <EOL> text_with_linesep = text + os . linesep <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> : <EOL> text_with_linesep = text_with_linesep . encode ( self . _encoding ) <EOL> self . _file . write ( text_with_linesep ) <EOL> self . _file . flush ( ) <EOL> class InteractiveResult : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , process , params ) : <EOL> self . _process = process <EOL> self . _params = params <EOL> self . stdin = Stream ( process . stdin , sys . stdin . encoding ) <EOL> print_stdout = _is_param_set ( params , _PARAM_PRINT_STDOUT ) or config . PRINT_STDOUT_ALWAYS <EOL> self . stdout = Stream ( process . stdout , sys . stdout . encoding , print_stdout ) <EOL> print_stderr = _is_param_set ( params , _PARAM_PRINT_STDERR ) or config . PRINT_STDERR_ALWAYS <EOL> color = None if not _is_colorama_enabled ( ) else Fore . RED <EOL> self . stderr = Stream ( process . stderr , sys . stderr . encoding , print_stderr , color ) <EOL> def sreadline ( self ) : <EOL> return self . stdout . sreadline ( ) <EOL> def swriteline ( self , text ) : <EOL> self . stdin . swriteline ( text ) <EOL> @ property <EOL> def returncode ( self ) : <EOL> self . _process . wait ( ) <EOL> return self . _process . returncode <EOL> def __iter__ ( self ) : <EOL> return iter ( self . stdout ) <EOL> def __bool__ ( self ) : <EOL> return self . returncode == <NUM_LIT:0> <EOL> __nonzero__ = __bool__ <EOL> class Result : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _stdout_lines = [ ] <EOL> self . _stderr_lines = [ ] <EOL> self . returncode = None <EOL> @ property <EOL> def stdout ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . linesep . join ( self . _stdout_lines ) <EOL> @ property <EOL> def stderr ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . linesep . join ( self . _stderr_lines ) <EOL> @ property <EOL> def stdout_lines ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _stdout_lines <EOL> @ property <EOL> def stderr_lines ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _stderr_lines <EOL> def _add_stdout_line ( self , line ) : <EOL> line = line . rstrip ( os . linesep ) <EOL> self . _stdout_lines . append ( line ) <EOL> def _add_stderr_line ( self , line ) : <EOL> line = line . rstrip ( os . linesep ) <EOL> self . _stderr_lines . append ( line ) <EOL> def __str__ ( self ) : <EOL> return self . stdout <EOL> def __iter__ ( self ) : <EOL> return iter ( self . _stdout_lines ) <EOL> def __eq__ ( self , other ) : <EOL> return self . __str__ ( ) == other . __str__ ( ) <EOL> def __bool__ ( self ) : <EOL> return self . returncode == <NUM_LIT:0> <EOL> __nonzero__ = __bool__ <EOL> def _create_result ( cmd , params ) : <EOL> p = subprocess . Popen ( cmd , <EOL> shell = True , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE , <EOL> env = os . environ ) <EOL> result = Result ( ) <EOL> for line in p . stdout . readlines ( ) : <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> : <EOL> line = line . decode ( sys . stdout . encoding ) <EOL> result . _add_stdout_line ( line ) <EOL> for line in p . stderr . readlines ( ) : <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> : <EOL> line = line . decode ( sys . stderr . encoding ) <EOL> result . _add_stderr_line ( line ) <EOL> p . wait ( ) <EOL> if ( _is_param_set ( params , _PARAM_PRINT_STDOUT ) or config . PRINT_STDOUT_ALWAYS ) and len ( result . stdout ) > <NUM_LIT:0> : <EOL> _print_stdout ( result . stdout ) <EOL> if ( _is_param_set ( params , _PARAM_PRINT_STDERR ) or config . PRINT_STDERR_ALWAYS ) and len ( result . stderr ) > <NUM_LIT:0> : <EOL> if _is_colorama_enabled ( ) : <EOL> _print_stderr ( Fore . RED + result . stderr + Style . RESET_ALL ) <EOL> else : <EOL> _print_stderr ( result . stderr ) <EOL> result . returncode = p . returncode <EOL> if p . returncode != <NUM_LIT:0> and not _is_param_set ( params , _PARAM_NO_THROW ) : <EOL> raise NonZeroReturnCodeError ( cmd , result ) <EOL> return result <EOL> def _create_interactive_result ( cmd , params ) : <EOL> p = subprocess . Popen ( cmd , shell = True , stdout = subprocess . PIPE , stderr = subprocess . PIPE , stdin = subprocess . PIPE ) <EOL> result = InteractiveResult ( p , params ) <EOL> return result </s>
