HEX
Server: LiteSpeed
System: Linux us-phx-web1284.main-hosting.eu 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: u300739242 (300739242)
PHP: 8.2.30
Disabled: system, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
Upload Files
File: //opt/alt/python311/lib/python3.11/site-packages/__pycache__/sh.cpython-311.pyc
�

�;f����\
�UdZddlZddlmZ	ddlmZn#e$r	ddlmZYnwxYwddlZddlZddl	Z	ddl
Z
ddlZddl
Z
ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddlm!Z"ddl#m$Z$ddl%m&Z&ddl'm(Z(dd	l)m*Z*m+Z+m,Z,dd
l)m-Z.ddl/m0Z0ddl1m2Z2m!Z!dd
l3m4Z5ddl6m7Z7m8Z8ddl9m:Z:m;Z;m<Z<m=Z=	e(j>d��Z?n#e(j@$rdZ?YnwxYwdZAdejB���C��vrede?�d����hd�ZDddhZEe0��pdZFejB��dvZGeHZIeJeKejL�Mdd������ZNeJeKejL�Mdd������ZOejP��ZQd�ZReSeTeU������ZVeWed��ZXd ZYdZZd!Z[d"Z\Gd#�d$e]��Z^Gd%�d&e]��Z_e_Z`e=e<e_e<e^fead'<eXreOse^Z`Gd(�d)eb��ZcGd*�d+ed��ZeGd,�d-eb��ZfGd.�d/ef��ZgGd0�d1eb��ZhejiejjejkejlejmejnejoejpejqejrejshZtGd2�d3eu��Zvejwd4��ZxiZye;eUe<effead5<ezd6�ej{�|��D����Z}d7�Z~d8�ZejZ�Gd9�d:e���Z�d�d<�Zee_d=�Z�d�d>�Z�d?�Z�d�d@�Z�GdA�dBe]��Z�d�dC�Z�GdD�dEe]��Z�dF�Z�dG�Z�dH�Z�dI�Z�dJ�Z�dK�Z�dL�Z�dM�Z�dN�Z�dO�Z�dP�Z�dQ�Z�GdR�dSe]��Z�dT�Z�d�dU�Z�dV�Z�dW�Z�dX�Z�dY�Z�dZ�Z�d[�Z�Gd\�d]e]��Z�d^�Z�d�d_�Z�d`�Z�da�Z�Gdb�dceb��Z�Gdd�deeb��Z�df�Z�dg�Z�dh�Z�di�Z�dj�Z�dk�Z�dl�Z�Gdm�dne]��Z�do�Z�dp�Z�dq�Z�dr�Z�ds�Z�dt�Z�Gdu�dve]��Z�Gdw�dxe]��Z�dy�Z�e�eQ��dz���Z�e$d{���Z�Gd|�d}ez��Z�Gd~�de8��Z�eHd�zZ�e�e���Z�e�ej�e�<e�d���d����Z�e�d���d����Z�e�d���d����Z�d��Z�Gd��d�e8��Z�eHd�kr e�eæ���Z�e�eĦ�dSe�ej�eH��ej�eH<dS)�z
http://amoffat.github.io/sh/
�N)�deque)�Mapping)�Queue��contextmanager)�partial)�metadata)�BytesIO�StringIO�UnsupportedOperation)�open)�getpreferredencoding)�Emptyr)�quote)�
GeneratorType�
ModuleType)�Any�Dict�Type�Union�sh�unknownzhttps://github.com/amoffat/sh�windowszsh z~ is currently only supported on linux and osx. please install pbs 0.110 (http://pypi.python.org/pypi/pbs) for windows support.>T�out�err�zUTF-8)�AIX�Darwin�SH_TESTS_RUNNING�0�SH_TESTS_USE_SELECTc�N�ttj|��j��S�N)�len�inspect�getfullargspec�args)�fns �5/opt/alt/python311/lib/python3.11/site-packages/sh.py�get_num_argsr*is���w�%�b�)�)�.�/�/�/��poll���c�\�eZdZd�Zd�Zd�Zd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�Zd�Z
d�Zd
�ZdS)�
PollPollerc�R�tj��|_i|_i|_dSr#)�selectr,�_poll�	fd_lookup�	fo_lookup��selfs r)�__init__zPollPoller.__init__ws!���[�]�]��
��������r+c�2�t|j��dkS�Nr�r$r5r7s r)�__nonzero__zPollPoller.__nonzero__}s���4�>�"�"�a�'�'r+c�*�t|j��Sr#r<r7s r)�__len__zPollPoller.__len__�s���4�>�"�"�"r+c��t|d��r*|���}||j|<||j|<dS||j|<||j|<dS�N�fileno��hasattrrBr5r6�r8�f�fds   r)�_set_fileobjectzPollPoller._set_fileobject�s]���1�h���	"������B�!"�D�N�2�� "�D�N�1���� !�D�N�1�� !�D�N�1���r+c��t|d��r&|���}|j|=|j|=dS|j|=|j|=dSrArCrEs   r)�_remove_fileobjectzPollPoller._remove_fileobject�sU���1�h���	"������B���r�"���q�!�!�!���q�!���q�!�!�!r+c�6�|j�|��Sr#)r6�get�r8rFs  r)�_get_file_descriptorzPollPoller._get_file_descriptor�s���~�!�!�!�$�$�$r+c�6�|j�|��Sr#)r5rL)r8rGs  r)�_get_file_objectzPollPoller._get_file_object�s���~�!�!�"�%�%�%r+c��|�|��|�|��}|j�||��dSr#)rHrNr4�register)r8rF�eventsrGs    r)�	_registerzPollPoller._register�sG�����Q����
�
&�
&�q�
)�
)���
���B��'�'�'�'�'r+c�`�|�|tjtjz��dSr#)rTr3�POLLIN�POLLPRIrMs  r)�
register_readzPollPoller.register_read�s%�����q�&�-�&�.�8�9�9�9�9�9r+c�F�|�|tj��dSr#)rTr3�POLLOUTrMs  r)�register_writezPollPoller.register_write�s�����q�&�.�)�)�)�)�)r+c�z�|�|tjtjztjz��dSr#)rTr3�POLLERR�POLLHUP�POLLNVALrMs  r)�register_errorzPollPoller.register_error�s,�����q�&�.�6�>�9�F�O�K�L�L�L�L�Lr+c��|�|��}|j�|��|�|��dSr#)rNr4�
unregisterrJrEs   r)rbzPollPoller.unregister�sE��
�
&�
&�q�
)�
)���
���b�!�!�!�����"�"�"�"�"r+c��|�|dz}|j�|��}g}|D]�\}}|�|��}|tjtjzzr|�|tf���S|tjzr|�|tf���|tj
zr|�|tf����|tjtj
zzr|�|tf����|S)Ni�)r4r,rPr3rVrW�append�POLLER_EVENT_READrZ�POLLER_EVENT_WRITEr^�POLLER_EVENT_HUPr]r_�POLLER_EVENT_ERROR)r8�timeout�changes�resultsrGrSrFs       r)r,zPollPoller.poll�s�����t�O�G��*�/�/�'�*�*����!�		8�		8�J�B���%�%�b�)�)�A������7�8�
8�����#4�5�6�6�6�6��&�.�(�
8�����#5�6�7�7�7�7��&�.�(�
8�����#3�4�5�5�5�5��6�>�F�O�;�<�
8�����#5�6�7�7�7���r+N)�__name__�
__module__�__qualname__r9r=r?rHrJrNrPrTrXr[r`rbr,�r+r)r1r1vs����������(�(�(�#�#�#�"�"�"�"�"�"�%�%�%�&�&�&�(�(�(�:�:�:�*�*�*�M�M�M�#�#�#�
����r+r1c�j�eZdZd�Zd�Zd�Zed���Zed���Zd�Z	d�Z
d�Zd	�Zd
�Z
dS)�SelectPollerc�0�g|_g|_g|_dSr#)�rlist�wlist�xlistr7s r)r9zSelectPoller.__init__�s����
���
���
�
�
r+c��t|j��t|j��zt|j��zdkSr;�r$rsrtrur7s r)r=zSelectPoller.__nonzero__�s/���4�:����T�Z���0�3�t�z�?�?�B�a�G�Gr+c�~�t|j��t|j��zt|j��zSr#rwr7s r)r?zSelectPoller.__len__�s*���4�:����T�Z���0�3�t�z�?�?�B�Br+c�<�||vr|�|��dSdSr#)rd�rFrSs  r)rTzSelectPoller._register�s*���F�?�?��M�M�!�������?r+c�<�||vr|�|��dSdSr#)�removerzs  r)�_unregisterzSelectPoller._unregister�s*����;�;��M�M�!�������;r+c�<�|�||j��dSr#)rTrsrMs  r)rXzSelectPoller.register_read�������q�$�*�%�%�%�%�%r+c�<�|�||j��dSr#)rTrtrMs  r)r[zSelectPoller.register_write�rr+c�<�|�||j��dSr#)rTrurMs  r)r`zSelectPoller.register_error�rr+c��|�||j��|�||j��|�||j��dSr#)r}rsrtrurMs  r)rbzSelectPoller.unregister�sP������D�J�'�'�'�����D�J�'�'�'�����D�J�'�'�'�'�'r+c�$�tj|j|j|j|��\}}}g}|D]}|�|t
f���|D]}|�|tf���|D]}|�|tf���|Sr#)r3rsrtrurdrerfrh)r8ri�_in�_out�_errrkrFs       r)r,zSelectPoller.poll�s��� �-��
�D�J��
�G�T�T���T�4����	3�	3�A��N�N�A�0�1�2�2�2�2��	4�	4�A��N�N�A�1�2�3�3�3�3��	4�	4�A��N�N�A�1�2�3�3�3�3��r+N)rlrmrnr9r=r?�staticmethodrTr}rXr[r`rbr,ror+r)rqrq�s����������
H�H�H�C�C�C�����\������\��&�&�&�&�&�&�&�&�&�(�(�(�
	�	�	�	�	r+rq�Pollerc��eZdZd�ZdS)�
ForkExceptionc�n�dtj|d���d�}t�||��dS)Nz+

Original exception:
===================

z    �
)�textwrap�indent�	Exceptionr9)r8�orig_exc�msgs   r)r9zForkException.__init__�sG���

���6�"�"�����	���4��%�%�%�%�%r+N�rlrmrnr9ror+r)r�r��s#������&�&�&�&�&r+r�c��eZdZdZd�ZdS)�ErrorReturnCodeMetaa�a metaclass which provides the ability for an ErrorReturnCode (or
    derived) instance, imported from one sh module, to be considered the
    subclass of ErrorReturnCode from another module.  this is mostly necessary
    in the tests, where we do assertRaises, but the ErrorReturnCode that the
    program we're testing throws may not be the same class that we pass to
    assertRaises
    c�p�td�|jD����}|j|vp|j|jkS)Nc��g|]	}|j��
Sro)rl)�.0�bs  r)�
<listcomp>z9ErrorReturnCodeMeta.__subclasscheck__.<locals>.<listcomp>s��;�;�;�!�1�:�;�;�;r+)�set�	__bases__rl)r8�o�other_basess   r)�__subclasscheck__z%ErrorReturnCodeMeta.__subclasscheck__s;���;�;�q�{�;�;�;�<�<���}��+�J�q�z�T�]�/J�Jr+N)rlrmrn�__doc__r�ror+r)r�r�s2��������K�K�K�K�Kr+r�c�0��eZdZeZ	dZd�Zd�fd�	Z�xZS)�ErrorReturnCodei�c�D�|j|j|j|j|jffSr#)�	__class__�full_cmd�stdout�stderr�truncater7s r)�
__reduce__zErrorReturnCode.__reduce__/s ���~��
�t�{�D�K���W�W�Wr+Tc	���|j|_||_||_||_||_|j}|rP|d|j�}t
|j��t
|��z
}|r|d|�d����z
}|j}|rP|d|j�}t
|j��t
|��z
}|r|d|�d����z
}d|j�d|�td���d|�td����}	tt|���|	��dS)Nz... (z more, please see e.stdout)z more, please see e.stderr)z	

  RAN: z

  STDOUT:
�replacez

  STDERR:
)
�	exit_coder�r�r�r��truncate_capr$�encode�decode�DEFAULT_ENCODING�superr�r9)r8r�r�r�r��
exc_stdout�	out_delta�
exc_stderr�	err_deltar�r�s          �r)r9zErrorReturnCode.__init__2sy������� ��
������� ��
��[�
��	X�#�$7�d�&7�$7�8�J��D�K�(�(�3�z�?�?�:�I��
X��M�y�M�M�M�U�U�W�W�W�
��[�
��	X�#�$7�d�&7�$7�8�J��D�K�(�(�3�z�?�?�:�I��
X��M�y�M�M�M�U�U�W�W�W�
�
O�$�-�
O�
O�(�/�/�0@�)�L�L�
O�
O�(�/�/�0@�)�L�L�
O�
O�	�	�o�t�$�$�-�-�c�2�2�2�2�2r+)T)	rlrmrnr��
__metaclass__r�r�r9�
__classcell__�r�s@r)r�r�sa�������'�M�S�(�L�X�X�X�3�3�3�3�3�3�3�3�3�3r+r�c��eZdZdS)�SignalExceptionN�rlrmrnror+r)r�r�P��������Dr+r�c�"��eZdZdZ�fd�Z�xZS)�TimeoutExceptionzvthe exception thrown when a command is killed because a specified
    timeout (via _timeout or .wait(timeout)) was hitc�r��||_||_tt|�����dSr#)r�r�r�r�r9)r8r�r�r�s   �r)r9zTimeoutException.__init__Xs3���"��� ��
�
�i����'�'�)�)�)�)�)r+)rlrmrnr�r9r�r�s@r)r�r�TsB�������8�8�*�*�*�*�*�*�*�*�*r+r�c��eZdZdS)�CommandNotFoundNr�ror+r)r�r�pr�r+r�z6(ErrorReturnCode|SignalException)_((\d+)|SIG[a-zA-Z]+)�rc_exc_cachec�F�g|]\}}tjd|���||f��S)zSIG[a-zA-Z]+)�re�match�r��k�vs   r)r�r�xs1��P�P�P���1�2�8�O�Q�3O�3O�P�a��V�P�P�Pr+c��d}	t|S#t$r�t�|��}|r�|�d��}|�d��}|dkr8	t|��}n5#t$rtt|��}YnwxYwt|��}t|��}YnwxYw|S)atakes an exception name, like:

        ErrorReturnCode_1
        SignalException_9
        SignalException_SIGHUP

    and returns the corresponding exception.  this is primarily used for
    importing exceptions from sh into user code, for instance, to capture those
    exceptionsNr-rr�)
r��KeyError�rc_exc_regexr��group�int�
ValueError�getattr�signal�
get_rc_exc)�name�exc�m�base�rc_or_sig_name�rcs      r)�get_exc_from_namer�|s����C�!��D�!�!���!�!�!����t�$�$���	!��7�7�1�:�:�D��W�W�Q�Z�Z�N��(�(�(�:��n�-�-�-�B�B��!�:�:�:�!�&�.�9�9�9�B�B�B�:������(�(���R�.�.�C���!�����Js4��AC�(A9�8C�9 B�C�B�!C�?Cc���	t|S#t$rYnwxYw|dkr
d|��}t}n$dtt	|����}t
}t
||fd|i��}|t|<|S)aGtakes a exit code or negative signal number and produces an exception
    that corresponds to that return code.  positive return codes yield
    ErrorReturnCode exception, negative return codes yield SignalException

    we also cache the generated exception so that only one signal of that type
    exists, preserving identityr�ErrorReturnCode_�SignalException_r�)r�r�r��SIGNAL_MAPPING�absr�r�)r�r�r�r�s    r)r�r��s���
��B�����
�
�
���
����
�Q�w�w�&�"�&�&�����;�.��R���"9�;�;����
�d�T�G�k�2�->�
?�
?�C��L����Js��
�c��eZdZd�ZdS)�GlobResultsc�J�||_t�||��dSr#)�path�listr9)r8r�rks   r)r9zGlobResults.__init__�s#����	��
�
�d�G�$�$�$�$�$r+Nr�ror+r)r�r��s#������%�%�%�%�%r+r�Fc�D�t|t||�����}|S)N)�	recursive)r��	_old_glob)r�r��expandeds   r)�globr��s#���4��4�9�!E�!E�!E�F�F�H��Or+c�z�tj�tj�|����Sr#)�osr��abspath�
expanduser)r�s r)�canonicalizer��s&��
�7�?�?�2�7�-�-�d�3�3�4�4�4r+c� �d�}d}tj�|��\}}|rt|��}||��r|}n�g}t	|t
tf��r|�|��nRtj�	dd���tj
��}|�|��|D]>}tj�t|��|��}	||	��r|	}n�?|S)a+takes a program name or full path, plus an optional collection of search
    paths, and returns the full path of the requested executable.  if paths is
    specified, it is the entire list of search paths, and the PATH env is not
    used at all.  otherwise, PATH env is used to look for the programc��tj�|��oZtj|tj��o;tj�tj�|����Sr#)r�r��exists�access�X_OK�isfile�realpath)�	file_paths r)�is_exez_which.<locals>.is_exe�sT���G�N�N�9�%�%�
<��	�)�R�W�-�-�
<�����r�w�/�/�	�:�:�;�;�	
r+N�PATH�)r�r��splitr��
isinstance�tupler��extend�environrL�pathsep�join)
�program�pathsr��
found_path�fpath�fname�paths_to_search�	env_pathsr��exe_files
          r)�_whichr�s��
�
�
��J��7�=�=��)�)�L�E�5�
���w�'�'���6�'�?�?�	!� �J��
���e�e�T�]�+�+�	.��"�"�5�)�)�)�)��
���v�r�2�2�8�8���D�D�I��"�"�9�-�-�-�#�	�	�D��w�|�|�L��$6�$6��@�@�H��v�h���
�%�
���
��r+c�~�t|��}|s+d|vr#t|�dd����}|sdS|S)N�_�-)rr�)r�r�s  r)�resolve_command_pathr
sM���'�?�?�D���
�'�>�>��'�/�/�#�s�3�3�4�4�D��	��4��Kr+c�`�t|��}d}|r||��}|r
|jdi|��}|S)Nro)r
�bake)r��command_cls�
baked_argsr��cmds     r)�resolve_commandrsO����%�%�D�
�C��)��k�$�����	)��#�(�(�(�Z�(�(�C��Jr+c�T�eZdZdZdd�Zd�Zed���Zd�Zd�Z	d�Z
d	�Zd
�ZdS)�LoggeraFprovides a memory-inexpensive logger.  a gotcha about python's builtin
    logger is that logger objects are never garbage collected.  if you create a
    thousand loggers with unique names, they'll sit there in memory until your
    script is done.  with sh, it's easy to create loggers with unique names if
    we want our loggers to include our command arguments.  for example, these
    are all unique loggers:

            ls -l
            ls -l /tmp
            ls /tmp

    so instead of creating unique loggers, and without sacrificing logging
    output, we use this class, which maintains as part of its state, the logging
    "context", which will be the very unique name.  this allows us to get a
    logger with a very general name, eg: "command", and have a unique name
    appended to it via the context, eg: "ls -l /tmp" Nc��||_tjt�d|����|_|�|��|_dS�N�.)r��logging�	getLogger�SH_LOGGER_NAME�log�sanitize_context�context)r8r�rs   r)r9zLogger.__init__-sB����	��$��%?�%?��%?�%?�@�@����,�,�W�5�5����r+c�2�|jr|j�d|��}||zS)N�: �r�r8r��as   r)�_format_msgzLogger._format_msg2s*���<�	+��\�*�*�S�*�*�C��Q�w�r+c�:�|r|�dd��}|pdS)N�%z%%r�)r�rs r)rzLogger.sanitize_context7s'���	1��o�o�c�4�0�0�G��}�"�r+c�V�|jdz|z}|jdz|z}t||��Sr)r�rr)r8r�r�new_name�new_contexts     r)�	get_childzLogger.get_child=s3���9�s�?�T�)���l�S�(�7�2���h��,�,�,r+c�R�|j�|j|g|�R���dSr#)r�infor!rs   r)r)zLogger.infoBs2����
�
�&�d�&�s�/�Q�/�/�/�0�0�0�0�0r+c�R�|j�|j|g|�R���dSr#)r�debugr!rs   r)r+zLogger.debugE�2������'�t�'��0�a�0�0�0�1�1�1�1�1r+c�R�|j�|j|g|�R���dSr#)r�errorr!rs   r)r.zLogger.errorHr,r+c�R�|j�|j|g|�R���dSr#)r�	exceptionr!rs   r)r0zLogger.exceptionKs4������+�4�+�C�4�!�4�4�4�5�5�5�5�5r+r#)
rlrmrnr�r9r!r�rr'r)r+r.r0ror+r)rrs�������9�9�"6�6�6�6�
���
����\��
-�-�-�
1�1�1�2�2�2�2�2�2�6�6�6�6�6r+rc�*�|r
d|�d|�d�}nd|�d�}|S)N�	<Command z, pid �>ro)r�	call_args�pid�ss    r)�default_logger_strr7Os9��
�!�+��+�+�S�+�+�+��� �� � � ���Hr+c���eZdZdZhd�Zd�Zdd�Zd�Zd�Ze	d���Z
e	d	���Ze	d
���Zd�Z
d�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�ZdS)�RunningCommanda�this represents an executing Command object.  it is returned as the
    result of __call__() being executed on a Command instance.  this creates a
    reference to a OProc instance, which is a low-level wrapper around the
    process that was exec'd

    this is the class that gets manipulated the most by user code, and so it
    implements various convenience methods and logical mechanisms for the
    underlying process.  for example, if a user tries to access a
    backgrounded-process's stdout/err, the RunningCommand object is smart enough
    to know to wait() on the process to finish first.  and when the process
    finishes, RunningCommand is smart enough to translate exit codes to
    exceptions.>r5�sid�ctty�kill�pgidr��	terminate�
kill_group�signal_group�
bg_thread_exc�input_thread_exc�output_thread_excc��d�d�|D����|_||_||_d|_d|_d}d}d|_	tj��tj	��|_
n#t$r
d|_
YnwxYwd|_|dr#d}t���|��|ds|ds|drd}|d	rd}|d
rd}|drtj}|d}|rt#||��|d<tj}	|dd
ks
|ddur
tj}	n|ddkrtj}	|dd
ks
|ddur
tj}	n|ddkrtj}	d|_|�r|dpt(}
|
|j|��}t+d|��|_|j�d��|rd|_t1j��}|5t||j|||||j|	|�	�	|_ddd��n#1swxYwY|
|j||jj��}|j�|��|j_|j�d��|r|���dSdSdS)N� c�F�g|]}tt|������Sro)�shlex_quote�str)r��args  r)r�z+RunningCommand.__init__.<locals>.<listcomp>ys&��B�B�B�s�[��S���2�2�B�B�Br+FT�with�piped�iter�iter_noblock�async�bg�
err_to_out�donerr�log_msg�commandzstarting processzprocess started)r��ranr4r�process�_waited_until_completion�_force_noblock_iter�asyncio�get_running_loop�Event�aio_output_complete�RuntimeError�_stopped_iteration�get_prepend_stackrd�OProc�STDOUTr�STDERR�_spawned_and_waitedr7rrr+�	threading�Lockr5rrr)�wait)
r8rr4�stdinr�r��should_wait�
spawn_process�
done_callback�pipe�log_str_factory�
logger_str�process_assign_locks
             r)r9zRunningCommand.__init__usr���8�8�B�B�c�B�B�B�C�C���"���������(-��%����
�$)�� �	7��$�&�&�&�(/�}���D�$�$���	,�	,�	,�'+�D�$�$�$�	,����#(����V��	-�!�M����&�&�t�,�,�,��W��	 ��6�!2�	 �i��6O�	 ��K��W��	 ��K��T�?�	 ��K��\�"�	"��\�F�!�&�)�
��	=� '�
�t� <� <�I�f��
�|���V���%�%��6�):�d�)B�)B��<�D�D�
�v�
�%�
'�
'��<�D��^�$��-�-��>�1J�d�1R�1R��<�D�D�
�~�
&�%�
/�
/��<�D�$)�� �� 	�'�	�2�H�6H�O�(����9�=�=�J��i��4�4�D�H��H�N�N�-�.�.�.��
0�+/��(�
#,�.�"2�"2��$�
�
�$���H������N��'�
 �
 ���
�
�
�
�
�
�
�
�
�
�
����
�
�
�
�)����9�d�l�>N�O�O�J�#�x�8�8��D�D�D�H���H�M�M�+�,�,�,��
��	�	������A 	� 	�>
�
s$�
A9�9B
�B
�'I
�
I�INc�\�|j�s#|��d}d}d}d}|dkrtd���||kr?|j���\}}|rt	j|��||z
}nn||k�?|rt
d|j���d|_n |j���}d|_|jj	rt
||j���|�
|��|jjr#|jjj���|j
�d��|S)a�waits for the running command to finish.  this is called on all
        running commands, eventually, except for ones that run in the background

        if timeout is a number, it is the number of seconds to wait for the process to
        resolve. otherwise block on wait.

        this function can raise a TimeoutException, either because of a `_timeout` on
        the command itself as it was
        launched, or because of a timeout passed into this method.
        Nr皙�����?Fztimeout cannot be negativeTzprocess completed)rVr\rU�is_alive�time�sleepr�rTre�	timed_out�handle_command_exit_code�_stdin_processrSrr+)r8ri�
waited_for�	sleep_amt�aliver�s      r)rezRunningCommand.wait�s`���,�:	0��"��
��	��� �	��Q�;�;�&�'C�D�D�D�!�G�+�+�'+�|�'<�'<�'>�'>�$�E�9����
�9�-�-�-�"�i�/�
�
��!�G�+�+��;�*�4���:�:�:�15��-�-�!�L�-�-�/�/�	�04��-��|�%�
?�'�	�z�4�8�<�<�<��-�-�i�8�8�8��<�.�?��L�/�7�<�<�>�>�>��H�N�N�.�/�/�/��r+c�@�|j���dS)zUreturns whether or not we're still alive. this call has side-effects on
        OProcr)rUrpr7s r)rpzRunningCommand.is_alive+s���|�$�$�&�&�q�)�)r+c��|j}t||d|d��}|r/||j|jj|jj|d��}|�dS)z�here we determine if we had an exception, or an error code that we
        weren't expecting to see.  if we did, we create and raise an exception
        �ok_coderK�truncate_excN)r4�get_exc_exit_code_would_raiserTrUr�r�)r8�code�ca�	exc_classr�s     r)rtz'RunningCommand.handle_command_exit_code0sh���^��1�$��9�
�r�'�{�S�S�	��	��)���$�,�-�t�|�/B�B�~�DV���C��I�		�	r+c�B�|���|jjSr#)rerUr�r7s r)r�zRunningCommand.stdout<����	�	�����|�"�"r+c�B�|���|jjSr#)rerUr�r7s r)r�zRunningCommand.stderrAr�r+c�B�|���|jjSr#)rerUr�r7s r)r�zRunningCommand.exit_codeFs���	�	�����|�%�%r+c�:�tt|����Sr#)r$rHr7s r)r?zRunningCommand.__len__Ks���3�t�9�9�~�~�r+c��dS)z�we don't actually do anything here because anything that should have
        been done would have been done in the Command.__call__ call.
        essentially all that has to happen is the command be pushed on the
        prepend stack.Nror7s r)�	__enter__zRunningCommand.__enter__Ns	��
	
�r+c��|Sr#ror7s r)�__iter__zRunningCommand.__iter__Us���r+c���|jrt���|jj}|j}		|�||jd��}|�)|���d|_t���	|�|jd|jd��S#t$r|cYSwxYw#t$r%|jds|jrtjcYSYnwxYw��)z2allow us to iterate over the output of our commandT�iter_poll_timeN�encoding�
decode_errorsrM)
r]�
StopIterationrU�_pipe_queuerWrLr4rer��UnicodeDecodeErrorr�errno�EWOULDBLOCK)r8�pq�
block_pq_read�chunks    r)�__next__zRunningCommand.__next__Xs#���"�	"��/�/�!�
�\�
%��!�4�4�
�	!�
!����}�d�n�=M�.N�O�O��
�=��I�I�K�K�K�.2�D�+�'�/�/�)�!� �<�<���z�2�D�N�?�4S�����*�!�!�!� �L�L�L�!������
-�
-�
-��>�.�1�-�T�5M�-� �,�,�,�,�-�-�
-����	!s#�!B7�9+B%�%B4�3B4�7*C&�%C&c�F���fd�}|�����S)Nc��d�K��j����d{V��t���Sr#)r[rerHr7s�r)�wait_for_completionz5RunningCommand.__await__.<locals>.wait_for_completion}s:������*�/�/�1�1�1�1�1�1�1�1�1��t�9�9�r+)�	__await__)r8r�s` r)r�zRunningCommand.__await__|s9���	�	�	�	�	�#�"�$�$�.�.�0�0�0r+c���td����_d�_�fd�}tj|����}|�_�S)Nr-)�maxsizeTc��:�K�	�D]M}|tjkrtjd���d{V���-�j�|���d{V���N	�j�d���d{V��dS#�j�d���d{V��wxYw)Ng{�G�z�?)r�r�rXrr�
_aio_queue�put)r�r8s �r)�queue_connectorz1RunningCommand.__aiter__.<locals>.queue_connector�s������

0�"�9�9�E��� 1�1�1�%�m�D�1�1�1�1�1�1�1�1�1�1�"�o�1�1�%�8�8�8�8�8�8�8�8�8�8�9��o�)�)�$�/�/�/�/�/�/�/�/�/�/�/��d�o�)�)�$�/�/�/�/�/�/�/�/�/�/���s�AA8�8"B)�AQueuer�rWrX�create_task�	_aio_task)r8r��tasks`  r)�	__aiter__zRunningCommand.__aiter__�s^���!��+�+�+���#'�� �
	0�	0�	0�	0�	0��"�?�?�#4�#4�5�5������r+c��K�|j����d{V��}|�|S|j���}|�|�t�r#)r�rLr�r0�StopAsyncIteration)r8r�r�s   r)�	__anext__zRunningCommand.__anext__�sY�����o�)�)�+�+�+�+�+�+�+�+�����L��.�*�*�,�,�C����	�$�$r+c��|jdr0t��r$t�����dSdSdS)NrJ)r4r^�pop�r8�exc_type�exc_val�exc_tbs    r)�__exit__zRunningCommand.__exit__�sQ���>�&�!�	&�&7�&9�&9�	&����#�#�%�%�%�%�%�	&�	&�	&�	&r+c��|jr8|jr1|j�|jd|jd��SdS)Nr�r�r�)rUr�r�r4r7s r)�__str__zRunningCommand.__str__�sI���<�	�D�K�	��;�%�%���z�*�D�N�?�,K���
��rr+c�B�t|��t|��kSr#)�id�r8�others  r)�__eq__zRunningCommand.__eq__�s���$�x�x�2�e�9�9�$�$r+c�$�|t|��vSr#�rH)r8�items  r)�__contains__zRunningCommand.__contains__�s���s�4�y�y� � r+c��||jvr#|jrt|j|��St�|tvrtt|��|��St�r#)�_OProc_attr_allowlistrUr��AttributeError�_unicode_methodsrH)r8�ps  r)�__getattr__zRunningCommand.__getattr__�s\����*�*�*��|�
%��t�|�Q�/�/�/�$�$�
� � � ��3�t�9�9�a�(�(�(��r+c��	t|��S#t$r6|jr|jrt	|j��cYSt	d��cYSwxYw�Nr�)rHr�rUr��reprr7s r)�__repr__zRunningCommand.__repr__�sg��	��t�9�9���!�	�	�	��|�
-��;�-����,�,�,�,�,���8�8�O�O�O�		���s��,A�A�Ac�^�tt|�������Sr#�r�rH�stripr7s r)�__long__zRunningCommand.__long__�� ���3�t�9�9�?�?�$�$�%�%�%r+c�^�tt|�������Sr#)�floatrHr�r7s r)�	__float__zRunningCommand.__float__�s ���S��Y�Y�_�_�&�&�'�'�'r+c�^�tt|�������Sr#r�r7s r)�__int__zRunningCommand.__int__�r�r+r#)rlrmrnr�r�r9rerprt�propertyr�r�r�r?r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�ror+r)r9r9Ws���������
�
�
��l�l�l�\F�F�F�F�P*�*�*�

�
�
��#�#��X�#��#�#��X�#��&�&��X�&����
�
�
����"!�"!�"!�H1�1�1����:%�%�%�&�&�&����%�%�%�!�!�!�������&�&�&�(�(�(�&�&�&�&�&r+r9c�L�t|t��pt|d��S)N�
__fspath__)r�rHrD)rs r)�output_redirect_is_filenamer��s!���c�3���=�7�3��#=�#=�=r+c�V�tj}t|d��sg|_|jS)N�_prepend_stack)�Command�thread_localrDr�)�tls r)r^r^�s.��	�	�B��2�'�(�(�����
��r+c��t|�����}g}|D]f}t|��r$|}|||��}|�|���5|\}}|�|��r|�||f���g|Sr#)r��keys�callabler��
issupersetrd)	�
passed_kwargs�
merged_kwargs�invalid_list�s1�invalid_args�elemr(�ret�	error_msgs	         r)�special_kwarg_validatorr��s���	�]�
�
�
!�
!�	"�	"�B��L��
7�
7���D�>�>�		7��B��"�]�M�2�2�C�����$�$�$�$�#�O�D�)��}�}�T�"�"�
7��#�#�T�9�$5�6�6�6���r+c��t|dd��}d}|r	|��}n.#t$rYn"wxYwt|tf��r|dkr|}|S)NrBr)r�rr�r�)�ob�fileno_methrBs   r)�
get_filenor��sz���"�h��-�-�K�
�F���	� �[�]�]�F�F��#�	�	�	��D�	����	�B���	�	��B�!�G�G����Ms�
"�
/�/c�$�t|��duSr#)r�)r�s r)�ob_is_fd_basedr�
s���b�>�>��%�%r+c�T�t|��}d}|�tj|��}|S)z7checks if an object (like a file-like object) is a tty.F)r�r��isatty)r�rB�is_ttys   r)�	ob_is_ttyr�s,��
��^�^�F�
�F�
����6�"�"���Mr+c��t|��}d}|r-tj|��}tj|j��}|S�NF)r�r��fstat�stat�S_ISFIFO�st_mode)r�rB�is_pipe�fd_stats    r)�
ob_is_piper�s?��
��^�^�F��G�
�1��(�6�"�"���-���0�0���Nr+c��g}|�d��r0|�d��dvrd}|�d|f��|S)N�no_outrL)Trz4You cannot iterate over output if there is no output)r�rL)rLrd)r�r��invalidr.s    r)�output_iterator_validatorr�#sW���G�����"�"�4�}�'8�'8��'@�'@�M�'Q�'Q�F�����*�E�2�3�3�3��Nr+c�
�d}g}|D]N\}}||vrEt|�|d����r"d|�d|�d�}|�||f|f���O|dr%|dr|ds|�d��|S)	N))�tty_in�in)�tty_outrz`_zG` is a TTY already, so so it doesn't make sense to set up a TTY with `_�`�
unify_ttysr�r�))rr�r�zC`_tty_in` and `_tty_out` must both be True if `_unify_ttys` is True)r�rLrd)r�r��pairsr��tty_type�stdr.s       r)�tty_in_validatorr+s���
3�E��G��5�5�
��#��}�$�$��=�3D�3D�S�$�3O�3O�)P�)P�$�+�S�+�+�'�+�+�+�
�
�N�N�X�s�O�U�3�4�4�4���\�"�
��h��
�$1�)�$<�
�	���
�	
�	
�	
��Nr+c��g}d}hd�}t|�����|z
}d|vr!|dr|r|�d|f��|S)z1fg is not valid with basically every other optionz�_fg is invalid with nearly every other option, see warning and workaround here:

    https://amoffat.github.io/sh/sections/special_arguments.html#fg>�fg�cwd�envr{r)r�r�rd)r�r�r�r��	allowlist�	offendings      r)�fg_validatorrFsq���G�G�C�0�/�/�I��M�&�&�(�(�)�)�I�5�I��}����t�!4��������c�{�#�#�#��Nr+c��g}|�dd��}|�dd��}|�dd��}|�dd��}t|��}t|��}d}	|r-|�+|�d|	�d���f��|r-|�+|�d	|	�d���f��|S)
a;a validator to prevent a user from saying that they want custom
    buffering when they're using an in/out object that will be os.dup'ed to the
    process, and has its own buffering.  an example is a pipe or a tty.  it
    doesn't make sense to tell them to have a custom buffering, since the os
    controls this.r�Nr�
in_bufsize�out_bufsizezICan't specify an {target} bufsize if the {target} target is a pipe or TTY)r�r)�target)rr)rLr�rd�format)
r�r�r��in_ob�out_ob�in_buf�out_buf�	in_no_buf�
out_no_bufrs
          r)�bufsize_validatorrVs����G����d�D�)�)�E�
�
�
�u�d�
+�
+�F�
�
�
�|�T�
2�
2�F����
�t�4�4�G��u�%�%�I���'�'�J�
U�C��H�V�'����,�c�j�j��j�.E�.E�F�G�G�G��K�g�)����.��
�
�%�
�0H�0H�I�J�J�J��Nr+c	��g}|�dd��}|�|St|t��s|�dd|��f��|S|d���D]h\}}t|t
��s|�dd|�d�f��t|t
��s|�dd|�d|�d�f���i|S)z�a validator to check that env is a dictionary and that all environment variable
    keys and values are strings. Otherwise, we would exit with a confusing exit code
    255.r	Nzenv must be dict-like. Got zenv key z must be a strzvalue z of env key )rLr�rrd�itemsrH)r�r�r�r	r�r�s      r)�
env_validatorrrs����G�
�
�
�E�4�
(�
(�C�
�{����c�7�#�#������D�S�D�D�E�F�F�F����e�$�*�*�,�,�S�S���1��!�S�!�!�	D��N�N�E�#A�a�#A�#A�#A�B�C�C�C��!�S�!�!�	S��N�N�E�#P�A�#P�#P�Q�#P�#P�#P�Q�R�R�R���Nr+c
��eZdZUdZej��ZeZidd�dd�dd�dd�dd	�d
d	�dd	�dd	�d
d�dd�dd�dd�dd	�dd	�dd	�dd	�dd�idd�dd	�dd�dd�d d�d!d�d"d�d#e	�d$d%�d&d	�d'e
j�d(d�d)d�d*d�d+d	�d,d	�d-d.��dd	d	ddd	d	de��ddd/��Z
eeefed0<d1d2d3ed4eeeef	Zd?d5�Zd6�Zed7���Zd8�Zd9�Zd:�Zd;�Zd<�Z d=�Z!d>�Z"d	S)@r�a�represents an un-run system program, like "ls" or "cd".  because it
    represents the program itself (and not a running instance of it), it should
    hold very little state.  in fact, the only state it does hold is baked
    arguments.

    when a Command object is called, the result that is returned is a
    RunningCommand object, which represents the Command put into an execution
    state.rFrO�bg_excTrJr�NrrrPrrrr-�err_bufsize�internal_bufsizei0r	rKrLrMr�ror{r�long_sep�=�long_prefixz--r�r�rr�r��strictri�timeout_signalr��no_err�no_pipe�teerQ�tty_size���P)r|�
preexec_fn�uid�new_session�	new_group�arg_preprocessrR�	close_fds�pass_fds�
return_cmdrN�
_call_args))rrPz"Stderr is already being redirected))rKrLz3You cannot iterate when this command is being piped))rKr&z;Using a pipe doesn't make sense if you've disabled the pipe))r1r2z0Passing `pass_fds` forces `close_fds` to be Truec���t||��}d|_d|_g|_i|_t|��|_|st|���||_t|��|_dS)Nr�F)r�_path�_partial�_partial_baked_args�_partial_call_argsrHrlr�)r8r��search_paths�founds    r)r9zCommand.__init__sp���t�\�*�*����
���
�#%�� �"$����D�	�	��
��	(�!�$�'�'�'���
��D�	�	��
�
�
r+c��ttj|��}d}|�d��r||��}n1|dkr|d��}n|�d��r
|dd�}|�|d��|��}|S)Nrr���)r�object�__getattribute__�
startswith�endswith)r8r��get_attr�vals    r)r?zCommand.__getattribute__$s����6�2�D�9�9�����?�?�3���
	��(�4�.�.�C�C�
�V�^�^��(�6�"�"�C�C�
�]�]�3�
�
�	�����9�D��;�"�(�(�6�"�"�4�(�(�C��
r+c���|���}i}|j���D]\}}d|z}||vr||||<||=�|j���}|�|��t	|||j��}|rMg}|D] \}	}
|�d|	�d|
�����!d�|��}td|�d����||fS)z�takes kwargs that were passed to a command's __call__ and extracts
        out the special keyword arguments, we return a tuple of special keyword
        args, and kwargs that will go to the exec'ed commandrz  rr�zInvalid special arguments:

)	�copyr4r�updater��_kwarg_validatorsrdr��	TypeError)�cls�kwargsr4�parg�default�key�merged_args�invalid_kwargs�exc_msg�kwargr�s           r)�_extract_call_argszCommand._extract_call_args:s���������	� �^�1�1�3�3�	 �	 �M�D�'���*�C��f�}�}�"(��+�	�$���3�K���n�)�)�+�+�����9�%�%�%�0��{�C�$9�
�
���	J��G�$2�
<�
<� ��y����:�E�:�:�y�:�:�;�;�;�;��i�i��(�(�G��H�W�H�H�H�I�I�I��&� � r+c��t|��|j��}d|_|�|��\}}|j�|j��|j�|��|j�|j��|�d|j	d��}|�d|j	d��}|j�t||||����|S)a.returns a new Command object after baking(freezing) the given
        command arguments which are used automatically when its exec'ed

        special keyword arguments can be temporary baked and additionally be
        overridden in __call__ or in subsequent bakes (basically setting
        defaults)Tr r")�typer6r7rRr9rFr8r�rLr4�compile_args)r8r'rJr(r4�sep�prefixs       r)rzCommand.bakeXs����T�$�Z�Z��
�
#�
#����� �3�3�F�;�;��	�6�
��$�$�T�%<�=�=�=�
��$�$�Y�/�/�/�
��%�%�d�&>�?�?�?��m�m�J���
�(C�D�D�����}�d�o�m�.L�M�M��
��%�%�l�4���f�&M�&M�N�N�N��	r+c�X�d�|j��}|rd|z}|j|zS)NrE)r�r8r6)r8rs  r)r�zCommand.__str__ns5���X�X�d�6�7�7�
��	*��z�)�J��z�J�&�&r+c�B�t|��t|��kSr#r�r�s  r)r�zCommand.__eq__ts���4�y�y�C��J�J�&�&r+c�(�dt|���d�S)Nr2r3r�r7s r)r�zCommand.__repr__ws��)�3�t�9�9�)�)�)�)r+c��|d���dS)NT)�_withror7s r)r�zCommand.__enter__zs����4������r+c�F�t�����dSr#)r^r�r�s    r)r�zCommand.__exit__}s �������!�!�!�!�!r+c�6�|���}t|��}g}|jj���}t	��D]v}|j���}|�dd��|�|��|�dd��s|�	|j
���w|�|j��|j
�dd��}|r|||��\}}|�|��\}}|�|j
��|�|��|d�d|d<t|ddd��s|dg|d<|d}	t!|	t"��r!|	jd	r|	j}	n|	jj}	t)|||d
|d��}
|j|
z}|}|�	|��|dr�|d
pt-j��}
t1|
��5|d�'t-jt,j|d|��}n-t-jt,j|d||d��}ddd��n#1swxYwYt9||d|d	��}|r+d�|��}||dd|d��}|�dS|d}t=|��rt?tA|��d��}|d}t=|��rt?tA|��d��}|j�!|||	||��}|j"r|dstA|��S|S)NrJr\Fr0r{rr�r�rKr r"rrr	rEr+r|r�wbrr3)#rEr�r�r4r^r4r�rFrLr�rrdr6r9rRr�r�r9rUr�rUr8r��getcwd�pushd�spawnv�P_WAIT�spawnver}r�r�r
rH�RunningCommandClsrb)r8r'rJrr4�prepend�
pcall_args�preprocessor�extracted_call_argsrf�processed_args�
split_args�
final_argsrr�r�rTr�r�r�r�s                     r)�__call__zCommand.__call__�s����������D�z�z�����N�-�2�2�4�4�	�)�*�*�		(�		(�G� �*�/�/�1�1�J��N�N�6�4�(�(�(����Z�(�(�(��:�:�g�u�-�-�
(��
�
�7�;�'�'�'���
�
�4�:�����.�2�2�3C�T�J�J���	6�'�<��f�5�5�L�D�&�'+�&=�&=�f�&E�&E�#��V�����0�1�1�1����,�-�-�-��Y��'�#$�I�i� ��y��+�Z��>�>�	:�$-�i�$8�#9�I�i� ��$����e�^�,�,�	2���w�'�
2��
����
�1��%��&�)�J�/��=�1I�
�
��
�-��>�
��
��
�
�:�����T�?�	��E�"�1�b�i�k�k�C��s���
U�
U��U�#�+� "�	�"�)�S��V�S� A� A�I�I� "�
�2�9�c�!�f�c�9�U�CS� T� T�I�	
U�
U�
U�
U�
U�
U�
U�
U�
U�
U�
U����
U�
U�
U�
U�6��9�Y�/��7�1C���I��
��h�h�s�m�m���i��S�#�y��/H�I�I���	��4��5�!��&�v�.�.�	-��#�f�+�+�t�,�,�F��5�!��&�v�.�.�	-��#�f�+�+�t�,�,�F�
�^�
-�
-�c�9�e�V�V�
T�
T��
�!�	�)�L�*A�	��r�7�7�N��Is�5AJ�J"�%J"r#)#rlrmrnr�rc�localr�r9rer�r��SIGKILLr�r4rrHr�__annotations__r�rrrrrGr9r?�classmethodrRrr�r�r�r�r�rmror+r)r�r��sh���������#�9�?�$�$�L�&��c"��e�c"�
	
�e�c"�	�$�c"�	��c"�	
�d�c"�	�t�c"�	�t�c"�	�d�c"�	�a�c"�"	�q�#c"�$	�q�%c"�8	�K�9c"�:	�t�;c"�<	��=c"�>	��?c"�@	��Ac"�D	�#�Ec"�c"�F	�1�Gc"�H	�t�Ic"�R	�C�Sc"�V	�t�Wc"�\	�%�]c"�^	�4�_c"�`	�e�ac"�b	�$�cc"�d	��ec"�h	�4�ic"�j	�&�.�kc"�v	�%�wc"�x	�%�yc"�z	�5�{c"�D	�t�Ec"�H	��Ic"�N	�H�Oc"�c"�R���������C�E�E���Ec"�c"�c"�J��S�#�X��c�c�c�P	F�R�	
�	"�W�����
��"�"�"�"�0���,�!�!��[�!�:���,'�'�'�'�'�'�*�*�*����"�"�"�h�h�h�h�hr+r�c��g}|D]�}t|ttf��r:t|t��r
|s|jg}|D]}|�|����Xt|t��r|t|||d���z
}��|�|dur��|�t|������|t|||��z
}|S)a�takes args and kwargs, as they were passed into the command instance
    being executed with __call__, and compose them into a flat list that
    will eventually be fed into exec.  example:

    with this call:

        sh.ls("-l", "/tmp", color="never")

    this function receives

        args = ['-l', '/tmp']
        kwargs = {'color': 'never'}

    and produces

        ['-l', '/tmp', '--color=geneticnever']

    T)�rawNF)	r�r�r�r�r�rd�dict�_aggregate_keywordsrH)r rJrVrWrjrI�sub_args       r)rUrU�s���&�N��,�,���c�D�%�=�)�)�
	,��#�{�+�+�
!�C�
!��x�j���
/�
/���%�%�g�.�.�.�.�
/�
��T�
"�
"�	,��1�#�s�F��M�M�M�M�N�N��[�C�5�L�L���!�!�#�c�(�(�+�+�+�+��)�&�#�v�>�>�>�N��r+c�r�g}|���D�]\}}|g}t|ttf��r|}|D]�}t	|��dkrC|dur>|�d|z��|dur"|�t
|�����X|s|�dd��}|dur|�||z����|dur��|�|dkr;|�||z��|�t
|������|�|�|�|��}	|�|	������ |S)atake our keyword arguments, and a separator, and compose the list of
    flat long (and short) arguments.  example

        {'color': 'never', 't': True, 'something': True} with sep '='

    becomes

        ['--color=never', '-t', '--something']

    the `raw` argument indicates whether or not we should leave the argument
    name alone, or whether we should replace "_" with "-".  if we pass in a
    dictionary, like this:

        sh.command({"some_option": 12})

    then `raw` gets set to True, because we want to leave the key as-is, to
    produce:

        ['--some_option=12']

    but if we just use a command's kwargs, `raw` is False, which means this:

        sh.command(some_option=12)

    becomes:

        ['--some-option=12']

    essentially, using kwargs is a convenience, but it lacks the ability to
    put a '-' in the name, so we do the replacement of '_' to '-' for you.
    but when you really don't want that to happen, you should use a
    dictionary instead with the exact names you want
    r-Fr	TrNrE)rr�r�r�r$rdrHr�)
�keywordsrVrWrs�	processedr��maybe_list_of_v�	list_of_vr�rIs
          r)rurus���F�I�&�n�n�.�.�#*�#*���?�%�%�	��o��e�}�5�5�	(�'�I��	*�	*�A��1�v�v��{�{��E�>�>��$�$�S�1�W�-�-�-���}�}�!�(�(��Q���0�0�0���,��	�	�#�s�+�+�A���9�9��$�$�V�a�Z�0�0�0�0��%�Z�Z���[�C�3�J�J��$�$�V�a�Z�0�0�0��$�$�S��V�V�,�,�,�,�$�0�Q�0��0�Q�0�0�C��$�$�S�)�)�)�)�9	*�<�r+c�z�����fd�}tj|||���}d|_|���|S)Nc�j��	�|i|��dS#t$r}��|���d}~wwxYwr#)r�r�)�rgsrJ�e�	exc_queuer(s   ��r)�wrapz"_start_daemon_thread.<locals>.wrapesW���	��B���v��������	�	�	��M�M�!���������	���s�
�
2�-�2)rr�r'T)rc�Thread�daemon�start)r(r�r�r r��threads` `   r)�_start_daemon_threadr�dsT�����������
�T��1�
=�
=�
=�F��F�M�
�L�L�N�N�N��Mr+c��|\}}ttdd��}tjd||dd��}t	j|||��dS)zSset the terminal size of a tty file descriptor.  borrowed logic
    from pexpect.py�
TIOCSWINSZigt��HHHHrN)r��termios�struct�pack�fcntl�ioctl)rG�	rows_cols�rows�cols�winsizer6s      r)�
setwinsizer�rsO���J�D�$��g�|�[�9�9�G���F�D�$��1�-�-�A�	�K��G�Q�����r+c�����d}d}�}t�t��rt�j��}�j}tj|��rd}t|��}n:tj|��rt|��}nd}t|j	��}||z
|z
}d�|dkrd�|dkr	|j
f�n!|dkr|j
tj|��f���fd�}|S)a^here we're constructing a closure for our streamreader callback.  this
    is used in the case that we pass a callback into _out or _err, meaning we
    want to our callback to handle each bit of output

    we construct the closure based on how many arguments it takes.  the reason
    for this is to make it as easy as possible for people to use, without
    limiting them.  a new user will assume the callback takes 1 argument (the
    data).  as they get more advanced, they may want to terminate the process,
    or pass some stdin back, and will realize that they can pass a callback of
    more argsrr-ror�c�p���}t|��dkr�d�d��f}�|g|�R�S)Nrrr-)r$)r�r �handler�handler_argss  ��r)r(z+construct_streamreader_callback.<locals>.fn�sK���
���q�6�6�Q�;�;��a��/�,�q�/�"3�"3�4�A��w�u�!�q�!�!�!�!r+)
r�rr$r'�funcr%�ismethodr*�
isfunctionrmrf�weakref�ref)	rUr��implied_arg�partial_args�handler_to_inspect�num_args�net_argsr(r�s	 `      @r)�construct_streamreader_callbackr�|s+�����K��L� ���'�7�#�#�*��7�<�(�(��$�\����*�+�+�A���� 2�3�3�����0�1�1�	A�#�$6�7�7�H�H��K�#�$6�$?�@�@�H��+�%��4�H��L��1�}�}����1�}�}��
�'���
�Q��� �
�w�{�7�';�';�<��"�"�"�"�"�"��Ir+c�z�d}||v}|tv}|r|tjkrd}d}|r|rt|��}|S)NFT)�#SIGNALS_THAT_SHOULD_THROW_EXCEPTIONr��SIGPIPEr�)r��ok_codes�
sigpipe_okr��success�bad_sigs      r)r}r}�sa��
�C��8�#�G��j�?�?�G�
��y�j�F�N�2�2������$�g�$���#�#���Jr+c���tj|��rtj|��}n8tj|��rtj|��}ntd���|S)z;this should only ever be called once for each child processzUnknown child exit status!)r��WIFSIGNALED�WTERMSIG�	WIFEXITED�WEXITSTATUSr\)r�s r)�handle_process_exit_coder��sb��
�~�i� � �9��[��+�+�+�	�	�	��i�	 �	 �9��N�9�-�-�	�	��7�8�8�8��r+c�z�d}		||i|��}n-#t$r }|jtjkrYd}~�/�d}~wwxYw|S)z0a helper for making system calls immune to EINTRN)�OSErrorr��EINTR)�syscallr'rJr�rs     r)�no_interruptr��so��
�C�	�	��'�4�*�6�*�*�C�
��
�	�	�	��w�%�+�%�%�����������		�����Js��
8�3�3�8c��eZdZdZdZdZdZd�Zd�Zd�Z	d�Z
d	�Zed
���Z
ed���Zd�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�ZdS)r_a"this class is instantiated by RunningCommand for a command to be exec'd.
    it handles all the nasty business involved with correctly setting up the
    input/output to the child process.  it gets its name for subprocess.Popen
    (process open) but we're calling ours OProc (open process)r)r=���c

�$#��	�=�>�|�_|�_�j�=�=d�Ptj��dkrt	d����=d}
tj�=d��}|j}nd\}
}�=drd�=d<d�_t|��}
t|��}t|��}t�=d	tttf��s�=d	�
�=d	h}nt�=d	��}t�|��}t"�|��}�=d
o�=do�=d}|r�t%j��\�_�_tj�j���_tj�j���_tj�j���_tj�j���_�n�t|t6��r(|jdr|j�_d�_|�_ny|
r.tjt;|�����_d�_nI�=d
r!t%j��\�_�_n tj��\�_�_|r0|s.tjt;|�����_d�_nI�=dr!t%j��\�_�_n tj��\�_�_|t6jurI|r
|sd�_ntj�j���_tj�j���_nR|r0|s.tjt;|�����_d�_n tj��\�_�_�=d}d�_|r-�j}|dkr�j}tj|���_�=d
}�=d}�=d
}|rd}d�_ |rtj!�j���_ tEj#��}|rtEj$��tj��\}}tj��\}}tJrtj��\}}nd\}}d�_&d�_'tj(���_)�j)dk�rtJr=tj*|d��tj+|��tj+|��tYj,|tXj-��} | tXj.z} tYj,|tXj/| ��	�=ddur)taj0t`j1t`j2��taj0t`j3t`j4��|rtj5��n|rtj6dd��tj7d��}!tj8d��}"|!�d|"���9tt��}#tj;||#���=dr|s|styj=�j���jrtj+�j���jrtj+�j���jrtj+�j��tj+|��tj+|���=d}$|$rtj>|$��tj?�jd��tj?�jd��tj?�jd��|rEtj@tj!d��tjA��}%tj+|%���=dr|st�d�=d���=d�(tjC|��tjD|
���=d}&t�|&��r
|&���=d}'�=drd}'|'r�ddd|h}(|(�F�=d��	tjGd��})n+#t�t�f$rtjGd��})YnwxYwtd�|)D����|(z
})|)D]'}*	tj+|*���#t�$rY�$wxYw�=fd�|D��}+�=d�tjJ|+d|+��n$tjK|+d|+�=d��dSdS#t�$r�t�jN���9dd��},	tj;||,��n:#t�$r-}-t�jP�;d |-�d!���Yd}-~-nd}-~-wwxYwtjQd"��n#tjQd"��wxYwYdSwxYw|rtEjR��tj+�j��tj+�j��tj+�j��tJrbtj+|��tj;|td���9tt����tj+|��tj+|��tj*|d#��}.tj+|��|.r)|.�Stt��}.t�|.���tj+|��tj*|d$���Stt���Ud��\}!}"tj+|��t|!���_&t|"���_'d�_Vt�jW���_X|�_Yd�_Z|�_[t��=d%��r�j[�t����_[t����_]t�j_���_`t��=d&�'���_bt��=d&�'���_c�=d
r|
st��j�=d��|�dd(t�������_f�jf�gd)���=d
r[|
sYt�ji�j��}/|/d*xxt�jjzcc<t�jk�jt�jl|/��d�_m�jrP�jf�dd+d,��}0t�|0�j�j[�=d-�=d.�=d
���_md}1|t6jur�=d/s�j]}1�=d0o|p|du}2�=dd1v}3�=dd2v}4d�_o|3sy�jrrt�|��rt��|��}t��jf�dd3d4���j|�jb�=d5�=d.�=d6|1|2�7�	�	�_on �jrtj+�j��d�_r|t6jur�|s�|4s��jr�d}5|t6jsur�=d/s�j]}5�=d8o|p|du}6t�|��rt��|��}t�t�d3���j|�jc�=d9�=d.�=d6|5|6�7�	�	�_rn �jrtj+�j���=�fd:�}7d�_ud�_v�=d;r[t�jw���_ut�jx�=d;�juj���_v�jv�y��d}8�jjzs�=d<r�=d=s�	�fd>�}9|9}8t�jw���_{d?�j)��}:t�d���_|t�t�|:�j||7�ju|8�j�j{���_�d�_�t�d���_��jmrG|};d@�j)��}:t��t|:�j��jf�jm�j�j{|;���_�t�jw���_�	�tj����>�>�fdA�}<n#t$rdB�}<YnwxYwt�d���_�dC�j)��}:t��t|:�j��jf�jo�jr�ju�j�j{�j�|<���_�dS)DaK
        cmd is the full list of arguments that will be exec'd.  it includes the program
        name and all its arguments.

        stdin, stdout, stderr are what the child will use for standard input/output/err.

        call_args is a mapping of all the special keyword arguments to apply to the
        child process.
        r-Nrz$UID setting requires root privileges)NNrKFr�r'r�rrr.r/Tr-rO�,rrr(r,r1r2z/dev/fdz
/proc/self/fdc3�4K�|]}t|��V��dSr#)r�)r�rGs  r)�	<genexpr>z!OProc.__init__.<locals>.<genexpr>0s(����'H�'H�B��B���'H�'H�'H�'H�'H�'Hr+c�F��g|]}|��d����S)r�)r�)r��crs  �r)r�z"OProc.__init__.<locals>.<listcomp>:s)���C�C�C�!�Q�X�X�b��n�5�5�C�C�Cr+r	�utf8�ignorez
FATAL SH ERROR: r��i�rr��maxlenrUzstarted processr��streamwriterrfrr�r&r�)rT)r�streamreaderr�rr�)�	save_datar%rc�L��d�_���d��dS)NTr$)rsr�)rr8s��r)�
timeout_fnz"OProc.__init__.<locals>.timeout_fn�s(���!%������B�/�0�1�1�1�1�1r+rirrNc�p���5�j�|��cddd��S#1swxYwYdSr#)rSrt)r�rmr8s ��r)r(zOProc.__init__.<locals>.fn	s����,�P�P�#�|�D�D�Y�O�O�P�P�P�P�P�P�P�P�P�P�P�P����P�P�P�P�P�Ps�+�/�/zbackground thread for pid zSTDIN thread for pid c�P�����jjj��dSr#)�call_soon_threadsaferSr[r�)�loopr8s��r)�output_completez'OProc.__init__.<locals>.output_completeV	s%����-�-�d�l�.N�.R�S�S�S�S�Sr+c��dSr#roror+r)r�z'OProc.__init__.<locals>.output_completeQ	s���Dr+zSTDOUT/ERR thread for pid )�rSr4r��getuidr\�pwd�getpwuid�pw_gidrur�r�rH�boolr�r��
TEE_STDOUT�intersection�
TEE_STDERR�pty�openpty�_stdin_parent_fd�_stdin_child_fd�dup�_stdout_parent_fd�_stdout_child_fd�_stderr_parent_fd�_stderr_child_fdr_�_pipe_fdr�rjr`r;�ttyname�gc�	isenabled�disable�IS_MACOSr:r=�forkr5�read�closer��F_GETFD�
FD_CLOEXEC�F_SETFDr��SIGHUP�SIG_IGNr��SIG_DFL�setsid�setpgid�getsid�getpgidr�r��write�tty�setraw�chdir�dup2r
�O_RDWRr��setgid�setuidr�rF�listdir�IOErrorr��execv�execver��	traceback�
format_exc�sysr��_exit�enabler�r�r�rsrq�startedrr�rfrr�rcrd�
_wait_lockr�_stdout�_stderrr'r�rr+r��	tcgetattr�ECHO�	tcsetattr�TCSANOW�
_stdin_stream�StreamWriter�_stdout_streamr��StreamReader�_stderr_streamrar�_timeout_event�_timeout_timerrZ�Timerr�rb�
_quit_threads�_bg_thread_exc_queuer��background_threadrp�_background_thread�
_input_thread�_input_thread_exc_queue�input_thread�_stop_output_eventrXrY�_output_thread_exc_queue�
output_thread�_output_thread)?r8rS�
parent_logrrfr�r�r4rjrm�
target_uid�pwrec�
target_gid�stdin_is_fd_based�stdout_is_fd_based�stderr_is_fd_basedr'�tee_out�tee_err�
single_ttyrK�	fd_to_user.r/�
needs_ctty�
gc_enabled�session_pipe_read�session_pipe_write�
exc_pipe_read�exc_pipe_write�close_pipe_read�close_pipe_write�flagsr:r=�payloadr�tmp_fdr,r1r2�
inherited_fdsrG�	bytes_cmd�tbr�fork_exc�attrr�stdout_pipe�save_stdout�pipe_out�pipe_err�stderr_pipe�save_stderrr��handle_exit_coder(�thread_name�close_before_termr�rr�s?`        `                                                   @@r)r9zOProc.__init__�s�������*���"����^��
�e�9� ��y�{�{�a���"�#I�J�J�J��E��J��L��E��+�+�E���J�J�%/�"�J�
��g�;�	"�!�B�y�M�"���+�5�1�1��+�F�3�3��+�F�3�3���b��i�#�t�S�!1�2�2�	!�b��i�6G��e�9�+�C�C��b��i�.�.�C��)�)�#�.�.���)�)�#�.�.����\�H�b��m�H��<�8H�
��Q	J�;>�+�-�-�7�D�!�4�#7�
&(�V�D�,A�%B�%B�D�"�
%'�F�4�+?�$@�$@�D�!�%'�V�D�,A�%B�%B�D�"�$&�F�4�+?�$@�$@�D�!�!��%��'�'�
H�E�O�G�,D�
H�',�~��$�(,��%�&+��#�#�"�	
H�')�v�j��.?�.?�'@�'@��$�(,��%�%��H��
H�>A�k�m�m�;��%�t�';�';�?A�g�i�i�;��$�d�&;�!�	
J�'�	
J�(*��z�&�/A�/A�(B�(B��%�)-��&�&��I��
J�@C��
�
�=��&��(=�(=�AC��	�	�=��&��(=����%�%�
&�L�g�L�-1�D�*�*�-/�V�D�4J�-K�-K�D�*�(*��t�/D�(E�(E��%�%�#�
J�G�
J�(*��z�&�/A�/A�(B�(B��%�)-��&�&�AC��	�	�=��&��(=��7�����
��	.��.�I���~�~� �2�	��F�9�-�-�D�M���'���{�O�	���\�
�
�	��K���	��	9��
�4�#7�8�8�D�I��\�^�^�
��	��J�L�L�L�13��	�	�-��-�(*��	�	�%�
�~��	;�02��	�	�-�O�-�-�0:�-�O�-������	��7�9�9����8�q�=�=��
+�����+�+�+����)�)�)���)�*�*�*��K���
�>�>�E��U�%�%�E��K���
�u�=�=�=�@
"��d�8�t�#�#��M�&�-���@�@�@��
�f�n�f�n�=�=�=�
�%��I�K�K�K�K��%��J�q�!�$�$�$��i��l�l���z�!�}�}��!�?�?�D�?�?�2�2�3C�D�D����+�W�5�5�5��i�=�6�);�6�J�6��J�t�4�5�5�5��(�4��H�T�2�3�3�3��)�5��H�T�3�4�4�4��)�5��H�T�3�4�4�4���*�+�+�+����'�'�'���i���"��H�S�M�M�M����,�a�0�0�0����-�q�1�1�1����-�q�1�1�1��%��W�R�Z��]�]�B�I�>�>�F��H�V�$�$�$��i�=�2�);�2��q�"�Z�.�1�1�1��e�9�(��I�j�)�)�)��I�j�)�)�)���-�
��J�'�'�!��J�L�L�L��{�O�	��j�>�%� $�I��!� !�1�a��8�H��O�O�B�z�N�3�3�3�D�(*�
�9�(=�(=�
�
��#�W�-�D�D�D�)+�
�?�(C�(C�
�
�
�	D����
%(�'H�'H�-�'H�'H�'H�$H�$H�8�$S�M�+�!�!��!��H�R�L�L�L�L��&�!�!�!� �D�!����D�C�C�C�s�C�C�C�	��e�9�$��H�Y�q�\�9�5�5�5�5��I�i��l�I�r�%�y�A�A�A�A�A�6�5���
"�
"�
"��)�+�+�2�2�6�8�D�D��"��H�^�R�0�0�0�0�� �A�A�A��J�$�$�%?�!�%?�%?�%?�@�@�@�@�@�@�@�@�����A����
�H�S�M�M�M�M��B�H�S�M�M�M�M����M�M�M�
"���� �
��	�����H�T�)�*�*�*��H�T�*�+�+�+��H�T�*�+�+�+�
�
+����)�)�)���)�3�q�6�6�=�=�9I�+J�+J�K�K�K���)�*�*�*��H�^�$�$�$��w�}�g�6�6�H��H�]�#�#�#��
.�#�?�?�+;�<�<��#�H�-�-�-��H�'�(�(�(���)�4�0�0�7�7�8H�I�I�O�O�PS�T�T�
�C��
�H�&�'�'�'��3�x�x�D�H��D�	�	�D�I�
#�D�N��9�;�;�D�L��D�H�"�D�N��D�J�
��5�	�"�"�
%�t�z�'9�"�W�W��
� %�w�w�D��
(�n�.�.�D�O�!��+=�(>�?�?�?�D�L� ��+=�(>�?�?�?�D�L��(�|�
B�$5�
B��4�0�"�Z�.�A�A�A�!�+�+�I�t�D�z�z�B�B�D�H��H�N�N�,�-�-�-��(�|�
P�$5�
P��(��)>�?�?���Q����G�L�=�(�����!�$�"7���$�O�O�O�"&�D���$�	
��h�(�(���A�A��%1���)��J��|�$��z�N��x�L�
&�&��"��K��u�|�#�#�B�y�M�#�"�.��!��l�*�J��0I�6�T�>�K��'�{�m�3�H��'�{�h�.�H�#'�D���
1�� 6�
1��F�#�#�K�<�T�6�J�J�F�&2��H�&�&�~�x�@�@��*���L��}�%��z�N���'��)�
'�
'�
'��#�#��'�
1����/�0�0�0�#'�D���e�l�*�*�"�+� �+��*�+�
#���5�<�'�'��9�
�'�"&�"2�K�"$�X�,�.�N�G�4M�v��~���F�#�#�K�<�T�6�J�J�F�&2��>�*�*��*���L��}�%��z�N���'��)�
'�
'�
'��#�#��'�
1����/�0�0�0�
2�
2�
2�
2�
2�
2�#'�D��"&�D���)�}�
,�&/�o�&7�&7��#�&/�o��y�M�4�#6�#:�'�'��#��#�)�)�+�+�+� $���L�4�
&��x�L�
&�
�7��
&�P�P�P�P�P�P�$&� �!*��!2�!2�D��A�t�x�A�A�K�(-�a���D�%�&:�!���)���#� ��
��"�	'�	'�D�#�"&�D��+0��8�8�D�(��!�
�(2�N�!�@�d�h�@�@��%9� ���0��H��&��M��&�%�	&�	&��"� '0�o�&7�&7�D�#�

T��/�1�1��T�T�T�T�T�T�T�� �
�
�
������
����-2�!�H�H�D�)�A�t�x�A�A�K�"6����-����#��#��#��
��"��'��#�#�D���s��K+e�2b�e�%b/�,e�.b/�/#e�c(�'e�(
c5�2e�4c5�5Ae�1h�f�g'�
g�"#g
�g'�
g�g'�h�'g=�=h�h�C8AD�D
AD#�D"AD#c�8�d|j�d|jdd��d�S)Nz	<Process rEi�r3)r5rr7s r)r�zOProc.__repr__i	s'��9�4�8�9�9�d�h�t��t�n�9�9�9�9r+c�D�|jj�|��dSr#)r�stream_bufferer�change_buffering�r8�bufs  r)�change_in_bufsizezOProc.change_in_bufsizel	s"����*�;�;�C�@�@�@�@�@r+c�D�|jj�|��dSr#)rrErFrGs  r)�change_out_bufsizezOProc.change_out_bufsizeo	�"����+�<�<�S�A�A�A�A�Ar+c�D�|jj�|��dSr#)rrErFrGs  r)�change_err_bufsizezOProc.change_err_bufsizer	rLr+c�r�d�|jd���|j��S�Nr�r�)r�r4r�rr7s r)r�zOProc.stdoutu	�+���y�y���
�3�4�4�9�9�$�,�G�G�Gr+c�r�d�|jd���|j��SrP)r�r4r�rr7s r)r�zOProc.stderry	rQr+c�4�tj|j��S)z�return the CURRENT group id of the process. this differs from
        self.pgid in that this reflects the current state of the process, where
        self.pgid is the group id at launch)r�r�r5r7s r)�get_pgidzOProc.get_pgid}	s���z�$�(�#�#�#r+c�4�tj|j��S)z�return the CURRENT session id of the process. this differs from
        self.sid in that this reflects the current state of the process, where
        self.sid is the session id at launch)r�r�r5r7s r)�get_sidz
OProc.get_sid�	s���y���"�"�"r+c��|j�d|��tj|���|��dS)Nzsending signal %d to group)rr+r��killpgrT�r8�sigs  r)r@zOProc.signal_group�	s9������3�S�9�9�9�
�	�$�-�-�/�/�3�'�'�'�'�'r+c�p�|j�d|��tj|j|��dS)Nzsending signal %d)rr+r�r<r5rYs  r)r�zOProc.signal�	s3������*�C�0�0�0�
����#�����r+c�x�|j�d��|�tj��dS)Nz
killing group)rr+r@r�ror7s r)r?zOProc.kill_group�	s2�������'�'�'����&�.�)�)�)�)�)r+c�x�|j�d��|�tj��dS)N�killing)rr+r�ror7s r)r<z
OProc.kill�	s0������y�!�!�!����F�N�#�#�#�#�#r+c�x�|j�d��|�tj��dS)N�terminating)rr+r��SIGTERMr7s r)r>zOProc.terminate�	s0������}�%�%�%����F�N�#�#�#�#�#r+c��|j�	d|jfS|j�d���}|s|j�	d|jfSd|jfSd}	ttj|jtj��\}}||jkrOt|��|_d}d|jf|j�	��|r|�
��SS	d|jf|j�	��|r|�
��SS#t$r<d|jfcY|j�	��|r|�
��SSwxYw#|j�	��|r|�
��wwxYw)z�polls if our child process has completed, without blocking.  this
        method has side-effects, such as setting our exit_code, if we happen to
        see our child exit while this is runningNFg�h㈵��>)riT)r�r�acquirer�r��waitpidr5�WNOHANGr��release�_process_just_endedr�)r8�acquired�
witnessed_endr5r�s     r)rpzOProc.is_alive�	s���
�>�%��$�.�(�(� �?�*�*�7�*�;�;���	(��~�)��d�n�,�,����'�'��
�	+�
*�"�*�d�h��
�K�K�N�C���d�h���!9�)�!D�!D��� $�
��d�n�,�
�O�#�#�%�%�%��
+��(�(�*�*�*�*�
+�����'��O�#�#�%�%�%��
+��(�(�*�*�*�*�
+��
�	)�	)�	)��$�.�(�(�(�
�O�#�#�%�%�%��
+��(�(�*�*�*�*�
+�
	)�����

�O�#�#�%�%�%��
+��(�(�*�*�*�*�
+���s+�
AD�E�E�E�E�E�2Fc��|jr|j���|jd}|r%|j|jdv}|||j��|jr"|jst
j|j��dSdSdS)NrQr{)r�cancelr4r�r�rr�r�)r8rir�s   r)rgzOProc._process_just_ended�	s�����	)���&�&�(�(�(���v�.�
��	3��n���y�(A�A�G��M�'�4�>�2�2�2�� �	,��);�	,��H�T�*�+�+�+�+�+�	,�	,�	,�	,r+c���|j�d��|j5|j�d��d}|j�T|j�d��t	t
j|jd��\}}t|��|_d}n |j�d|j��ddd��n#1swxYwY|�	|�	��|jS)
z8waits for the process to complete, handles the exit codez*acquiring wait lock to wait for completionz
got wait lockFNz!exit code not set, waiting on pidrTz+exit code already set (%d), no need to wait)ri)
rr+rr�r�r�rdr5r��_process_exit_cleanup)r8rir5r�s    r)rez
OProc.wait�	s��	
����C�D�D�D��_�
	�
	��H�N�N�?�+�+�+�!�M��~�%�����B�C�C�C�!-�b�j�$�(�A�!F�!F���Y�!9�)�!D�!D��� $�
�
�����A�4�>����
	�
	�
	�
	�
	�
	�
	�
	�
	�
	�
	����
	�
	�
	�
	�	
�"�"��"�?�?�?��~�s�BC�C
�
C
c��|j���|jr|j���t	jd|jj��}|���|j���|�	��|j
���|r|���dSdS)Ng@)rr�rr�rcrrr�rrkrrg)r8ri�timers   r)rmzOProc._process_exit_cleanup�	s������� � � ���	&���#�#�%�%�%�
���T�%<�%@�A�A��
���
�
�
�	
�� � �"�"�"�
��������$�$�&�&�&��	'��$�$�&�&�&�&�&�	'�	'r+N)rlrmrnr��_default_window_sizer`rar9r�rIrKrNr�r�r�rTrVr@r�r?r<r>rprgrermror+r)r_r_�sn������B�B�
$���F�
�F�k	�k	�k	�Z:�:�:�A�A�A�B�B�B�B�B�B��H�H��X�H��H�H��X�H�$�$�$�#�#�#�(�(�(����*�*�*�$�$�$�$�$�$�1+�1+�1+�f,�,�,� ���.'�'�'�'�'r+r_c�
�d}d}t��}|�|��|r�|r�|�d��}|D]p\}	}
|
ttzzrY|�d|��|���}|r-|�|��|r|���d}�q|��\}}|r|��|r$|�	d��|��\}}|�$|s|���dSdS)z�this is run in a separate thread.  it writes into our process's
    stdin (a streamwriter) and waits the process to end AND everything that
    can be written to be writtenFTr-z%r ready for more inputN)
r�r[r,rfrgr+r�rbr�re)
rrfrp�quit_threadrB�closedrx�poller�changedrGrSrQrs
             r)rr
sE��
�F��E�
�X�X�F�
���%� � � �
�
�U�
��+�+�a�.�.��!�		&�		&�J�B���+�.>�>�?�
&��	�	�3�U�;�;�;��{�{�}�}���&��%�%�e�,�,�,�(�&����
�
�
�!%����8�:�:���q��
�U�
�����������8�:�:���q�����
���
�
�
�
�
��r+c�0�|�|��}|Sr#)re)�evri�	triggereds   r)�
event_waitry1
s������ � �I��r+c��|rE|���s1t|d��}|r|��n|����1|r>ts9d}d}|r$|�d��|��\}}|�$||��dSdSdS)zhandles the timeout logicroTNr-)�is_setry�
RUNNING_TESTSre)r��
timeout_eventr@rprrrsrxr�s        r)rr6
s������$�$�&�&�	�"�=�#�6�6�I��
��
�����	�$�$�&�&�	��$�
�$����	��	*����Q����'�x�z�z��E�9��	*�	���#�#�#�#�#�$�$�$�$r+c��t��}|�|�|��|�|�|��|r�t|jd��}	|	D]d\}
}|tt
zzrB|�d|
��|
���}|r|�|
���Y|tzr	�e|r|�
��rn|�
��rn|��|��\}
}|
r$|�d��|��\}
}|
�$|r|���|r|���|��dS)z�this function is run in a separate thread.  it reads from the
    process's stdout stream (a streamreader), and waits for it to claim that
    its doneNroz%r ready to be read fromr-)
r�rXr�r,rergr+r�rbrhr{rer�)rr�r�r}rprr�stop_output_eventr�rtrurFrSrQrxrs               r)rrT
s����X�X�F�
�����V�$�$�$�
�����V�$�$�$����v�{�C�0�0�� �
	�
	�I�A�v��*�-=�=�>�	
��	�	�4�a�8�8�8��v�v�x�x���)��%�%�a�(�(�(���,�,�
����	�]�1�1�3�3�	���#�#�%�%�	��%��,�x�z�z�H�E�1�
����������8�:�:���q�����������
���������O�����r+c��eZdZdS)�DoneReadingForeverNr�ror+r)r�r��
r�r+r�c��eZdZdS)�NotYetReadyToReadNr�ror+r)r�r��
r�r+r�c�v�t|t��rd}t|��}�nt|��rd}t	|��}n�t|d��rd}t
|��}n�t|t��rd}t|��}n�t|t��rd}t|��}n}t|t��rd}tt|����}nI|�d	}d
�}|}n?	t|��}d}t|��}n#t$rtd���wxYw||fS)
a�given some kind of input object, return a function that knows how to
    read chunks of that input object.

    each reader function should return a chunk and raise a DoneReadingForever
    exception, or return None, when there's no more data to read

    NOTE: the function returned does not need to care much about the requested
    buffering type (eg, unbuffered vs newline-buffered).  the StreamBufferer
    will take care of that.  these functions just need to return a
    reasonably-sized chunk of data.�queuer�r�zfile descriptor�string�bytes�	generatorN�Nonec��t�r#�r�ror+r)�raise_z+determine_how_to_read_input.<locals>.raise_�
s��$�$r+zgeneral iterablezunknown input object)r�r�get_queue_chunk_readerr��get_callable_chunk_readerrD�get_file_chunk_readerrH�get_iter_string_readerr�r�get_iter_chunk_readerrLrHr�)�	input_objrR�	get_chunkr��its     r)�determine_how_to_read_inputr��
sp���)�U�#�#�(2���*�9�5�5�	�	�	�)�	�	�$2���-�i�8�8�	�	�
��F�	#�	#�2�#��)�)�4�4�	�	�	�I�s�	#�	#�2���*�9�5�5�	�	�	�I�u�	%�	%�2���*�9�5�5�	�	�	�I�}�	-�	-�2���)�$�y�/�/�:�:�	�	�	�	���	%�	%�	%��	�	�	2��i���B�)�G�-�b�1�1�I�I��	�	4�	4�	4��2�3�3�3�	4�����g��s�9D�D4c����fd�}|S)Nc�t��	��dd��}n#t$rt�wxYw|�t�|S)NTro)rLrr�r��r�rfs �r)r(z"get_queue_chunk_reader.<locals>.fn�
sN���	$��I�I�d�C�(�(�E�E���	$�	$�	$�#�#�	$�����=�$�$��s��,ro�rfr(s` r)r�r��
s#���������Ir+c����fd�}|S)Nc�P��	���}n#t$r�wxYw|st�|Sr#r�)�datarfs �r)r(z%get_callable_chunk_reader.<locals>.fn�
sH���	��5�7�7�D�D��!�	�	�	��	�����	%�$�$��s�
�ror�s` r)r�r��
s#���	�	�	�	�	��Ir+c�~���d���fd�tdt������D��}t|��S)a+return an iterator that returns a chunk of a string every time it is
    called.  notice that even though bufsize_type might be line buffered, we're
    not doing any line buffering here.  that's because our StreamBufferer
    handles all buffering.  we just need to return a reasonable-sized chunk.r�c3�2�K�|]}�||�z�V��dSr#ro)r��i�bufsizerfs  ��r)r�z)get_iter_string_reader.<locals>.<genexpr>�
s0�����N�N�1��a�!�g�+�o�&�N�N�N�N�N�Nr+r)�ranger$r�)rf�iter_strr�s` @r)r�r��
sG����
�G�N�N�N�N�N��a��U���W�0M�0M�N�N�N�H� ��*�*�*r+c����fd�}|S)Nc�\��	����}|S#t$rt�wxYwr#)r�r�r�r�s �r)r(z!get_iter_chunk_reader.<locals>.fn�
s=���	%��N�N�$�$�E��L���	%�	%�	%�$�$�	%���s��+ror�s` r)r�r��
s#���%�%�%�%�%��Ir+c����d���fd�}|S)Nr�c�z��d}	����n#t$rd}YnwxYw|rot�d��r_t��}|����|�d��}d}|D]\}}|ttzzrd}�|st���	���}|st�|S)NTFrBro)rBrrDr�rXr,rergr�r�r�)	�is_real_filertru�readyrGrSr�r�rfs	       ��r)r(z!get_file_chunk_reader.<locals>.fns������	!��L�L�N�N�N�N��#�	!�	!�	!� �L�L�L�	!����
�		(�G�E�8�4�4�		(��X�X�F�� � ��'�'�'��k�k�#�&�&�G��E�%�
!�
!�
��F��.�1A�A�B�!� �E���
(�'�'��
�
�7�#�#���	�$�$��Ls��)�)ro)rfr(r�s` @r)r�r�s.�����G�������:�Ir+c�.�|dkrd}n|dkrd}n|}|S)afor a given bufsize type, return the actual bufsize we will read.
    notice that although 1 means "newline-buffered", we're reading a chunk size
    of 1024.  this is because we have to read something.  we let a
    StreamBufferer instance handle splitting our chunk on newlinesr-r�rro)�bf_typer�s  r)�bufsize_type_to_bufsizer�&s0���!�|�|����	�A���������Nr+c�*�eZdZdZd�Zd�Zd�Zd�ZdS)r
z�StreamWriter reads from some input (the stdin param) and writes to a fd
    (the stream param).  the stdin may be a Queue, a callable, something with
    the "read" method, a string, or an iterablec���||_||_||_||_||_t||j��|_t|��\|_}|j�	d|��dS)Nzparsed stdin as a %s)
�streamrfrr�r��StreamBuffererrEr�r�r+)r8rr�rf�bufsize_typer�r�rRs        r)r9zStreamWriter.__init__>sj�������
���� ��
����-�l�D�M�J�J���"=�e�"D�"D���������-�w�7�7�7�7�7r+c��|jS�zGdefining this allows us to do poll on an instance of this
        class�r�r7s r)rBzStreamWriter.filenoJ����{�r+c���	|���}|�t�n�#t$r�|j�d��|jr�	tj|j��dt
j}n(#td���
��}YnxYwtj|j|��tj|j|��YdSt$r|j�d��YdSwxYwt|t��s|�
|j��}|j�|��D]�}|j�dt'|��|dd	���|j�d
��	tj|j|���i#t($r|j�d��YdSwxYwdS)z�attempt to get a chunk of data to write to our child process's
        stdin, then write it.  the return value answers the questions "are we
        done writing forever?" Nzdone reading�r.Tzreceived no dataF�got chunk size %d: %r�zwriting chunk to processzOSError writing stdin chunk)r�r�rr+r�r�rr��VEOF�chrr�r�r�r�r�r�r�rErUr$r�)r8r��char�
proc_chunks    r)r�zStreamWriter.writeOs���%	��N�N�$�$�E��}�(�(���"�	�	�	��H�N�N�>�*�*�*��{�
,�+�"�,�T�[�9�9�!�<�W�\�J�D�D��+��q�6�6�=�=�?�?�D�D�D����"����d�+�+�+�����d�+�+�+��4�4� �	�	�	��H�N�N�-�.�.�.��5�5�	����
�%��'�'�	0��L�L���/�/�E��.�6�6�u�=�=�	�	�J��H�N�N�2�C�
�O�O�Z�PS�QS�PS�_�U�U�U��H�N�N�5�6�6�6�
�����j�1�1�1�1���
�
�
�����<�=�=�=��t�t�t�
����
	�	s?� �+D�/A<�;D�<#B!�9D�#D�D�F7�7$G �G c�\�|j�d��|j���}|j�dt	|��|dd���	|rtj|j|��n#t$rYnwxYwtj	|j��dS)Nzclosing, but flushing first�got chunk size %d to flush: %rr�)
rr+rE�flushr$r�r�r�r�r��r8r�s  r)r�zStreamWriter.close�s�������4�5�5�5��$�*�*�,�,������7��U���U�3�B�3�Z�P�P�P�	��
-�����e�,�,�,����	�	�	��D�	����	��������s�&B�
B�BN)rlrmrnr�r9rBr�r�ror+r)r
r
9s[������3�3�
8�
8�
8����
;�;�;�z����r+r
c��t|��rt|||��\}}n�t|t��rt	|��\}}n�t|t
��rt
|||��\}}net|d��rt||��\}}nA	t|��}t||��\}}n#ttf$r	d�}d�}YnwxYw||fS)Nr�c��dSr�ro�r�s r)rUz-determine_how_to_feed_output.<locals>.process�s���ur+c��dSr#roror+r)�finishz,determine_how_to_feed_output.<locals>.finish�����tr+)
r��get_callback_chunk_consumerr�r
�get_cstringio_chunk_consumerr�get_stringio_chunk_consumerrD�get_file_chunk_consumerr��get_fd_chunk_consumerr�rH�r�r�r�rUr�s     r)�determine_how_to_feed_outputr��s$������L�5�g�x��W�W�����
�G�W�	%�	%�L�6�w�?�?�����
�G�X�	&�	&�L�5�g�x��W�W�����	��'�	"�	"�L�1�'�=�I�I�����	L��'�l�l�G�4�G�]�K�K�O�G�V�V���I�&�	�	�	�
�
�
�
�
�
�
�
�	�����F�?�s�B?�?C�Cc�F�t|dd���}t||��S)N�wF)�closefd)�fdopenr�)r�r�s  r)r�r��s&���W�c�5�1�1�1�G�"�7�M�:�:�:r+c������t�dd��r��fd��nd��t�d��r�j�nd�����fd�}�fd�}||fS)Nr�c�:��|��j���Sr#)r�r�)r�r�r�s ��r)r�z'get_file_chunk_consumer.<locals>.encode�s����<�<�� 0�-�@�@�@r+c��|Sr#ror�s r)r�z'get_file_chunk_consumer.<locals>.encode�s���Lr+r�c��dSr#roror+r)r�z&get_file_chunk_consumer.<locals>.flush�s���4r+c�X�����|�������dSr��r�)r�r�r�r�s ���r)rUz(get_file_chunk_consumer.<locals>.process�s/����
�
�f�f�U�m�m�$�$�$�	������ur+c������dSr#ro)r�s�r)r�z'get_file_chunk_consumer.<locals>.finish�s���
������r+)r�rDr�)r�r�rUr�r�r�s``  @@r)r�r��s��������w�
�D�)�)��	A�	A�	A�	A�	A�	A�	A�
	�	�	��w�� � ���
���	�	�	��������������F�?�r+c�$�������fd�}d�}||fS)Nc�j��	|�����}n#t$rYnwxYw�|��Sr#)r�r��r�r�r�r�s ���r)rUz,get_callback_chunk_consumer.<locals>.process�sK���	��L�L��=�9�9�E�E��!�	�	�	��D�	�����w�u�~�~�s��
'�'c��dSr#roror+r)r�z+get_callback_chunk_consumer.<locals>.finish�����r+ror�s```  r)r�r��sC������������
�
�
��F�?�r+c����fd�}d�}||fS)Nc�2����|��dSr�r�)r�r�s �r)rUz-get_cstringio_chunk_consumer.<locals>.process�s����
�
�e�����ur+c��dSr#roror+r)r�z,get_cstringio_chunk_consumer.<locals>.finish�r�r+ro)r�rUr�s`  r)r�r��s7��������
�
�
��F�?�r+c�$�������fd�}d�}||fS)Nc�Z����|�������dSr�)r�r�r�s ���r)rUz,get_stringio_chunk_consumer.<locals>.process�s(����
�
�e�l�l�8�]�;�;�<�<�<��ur+c��dSr#roror+r)r�z+get_stringio_chunk_consumer.<locals>.finish�r�r+ror�s```  r)r�r��sC������������
�
�
��F�?�r+c�6�eZdZdZ		d	d�Zd�Zd�Zd�Zd�ZdS)
rzSreads from some output (the stream) and sends what it just read to the
    handler.NTc
�P�||_||_|	|_||_||_d|_|rt
j|��|_||_t||j|j��|_
t|��|_t|||��\|_|_d|_dSr�)r��bufferr�r�r��
pipe_queuer�r�rr�rEr�r�r��
process_chunk�finish_chunk_processor�should_quit)
r8rr�r�r�r�r�r�r�r�s
          r)r9zStreamReader.__init__s���������"��� ��
�*�������	6�%�k�*�5�5�D�O����-��$�-��);� 
� 
���/�|�<�<���:V��X�}�;
�;
�7���D�7�!����r+c��|jSr�r�r7s r)rBzStreamReader.fileno+r�r+c��|j���}|j�dt	|��|dd���|r|�|��|���|jr.|jr'|����	d��tj|j��dS)Nr�r�)
rEr�rr+r$�write_chunkr�r�r�r�r�r�r�r�s  r)r�zStreamReader.close0s����$�*�*�,�,������7��U���U�3�B�3�Z�P�P�P��	$����U�#�#�#��#�#�%�%�%��?�	(�t�~�	(��O�O���!�!�$�'�'�'�
��������r+c�4�|js|�|��|_|jrm|j�|��|jrN|j�d|dd���|����|��dSdSdS)Nzputting chunk onto pipe: %rr�)	r�r�r�r�rdr�rr+r�r�s  r)r�zStreamReader.write_chunk=s�����	9�#�1�1�%�8�8�D���>�	-��K���u�%�%�%���
-�����<�e�C�R�C�j�I�I�I����!�!�%�%�e�,�,�,�,�,�	-�	-�
-�
-r+c���	ttj|j|j��}n8#t
$r+}|j�d|j��Yd}~dSd}~wwxYw|s|j�d��dS|j�dt|��|dd���|j
�|��D]}|�|���dS)Nzgot errno %d, done readingTzgot no chunk, done readingr�r�)
r�r�r�r�r�r�rr+r�r$rErUr�)r8r�rs   r)r�zStreamReader.readJs���	� ���$�+�t�|�D�D�E�E���	�	�	��H�N�N�7���A�A�A��4�4�4�4�4�����	�����	��H�N�N�7�8�8�8��4�����.��E�
�
�E�#�2�#�J�G�G�G��)�1�1�%�8�8�	$�	$�E����U�#�#�#�#�	$�	$s�%(�
A� A�A�NT)	rlrmrnr�r9rBr�r�r�ror+r)rrsw����������!!�!!�!!�!!�F���
���-�-�-�$�$�$�$�$r+rc�0�eZdZdZedfd�Zd�Zd�Zd�ZdS)r�a�this is used for feeding in chunks of stdout/stderr, and breaking it up
    into chunks that will actually be put into the internal buffers.  for
    example, if you have two processes, one being piped to the other, and you
    want that, first process to feed lines of data (instead of the chunks
    however they come in), OProc will use an instance of this class to chop up
    the data and feed it as lines to be sent down the piper#c��||_g|_d|_||_||_d|_t
j��|_td��|_
dS)NrFrE)rTr��n_buffer_countr�r��_use_up_buffer_firstrc�RLock�_buffering_lockrr)r8�buffer_typer�r�s    r)r9zStreamBufferer.__init__csW����	������� ��
�*���%*��!� )��0�0����+�,�,����r+c��|j�d��|j���|j�d��	|dkrd|_||_|j���|j�d��dS#|j���|j�d��wxYw)Nz/acquiring buffering lock for changing bufferingz)got buffering lock for changing bufferingrTz.released buffering lock for changing buffering)rr+r�rcr�rTrf)r8�new_types  r)rFzStreamBufferer.change_bufferingxs�������H�I�I�I���$�$�&�&�&�����B�C�C�C�	M��1�}�}�,0��)� �D�I�� �(�(�*�*�*��H�N�N�K�L�L�L�L�L��
� �(�(�*�*�*��H�N�N�K�L�L�L�L���s�B�5C
c���|j�d|j��|j���|j�d|j��	|jdkr�|jred|_|j}g|_|�|��||j���|j�d|j��S|g|j���|j�d|j��S|jdk�rg}d�	|j
��}	|�|��}|d	krnb|d|dz�}|jr+d
�|j��|z}g|_d|_
||dzd�}|�|���~|r7|j�|��|xj
t|��z
c_
||j���|j�d|j��Sg}	|j
t|��z|jz
}|dkrwd�	|j
���|j��|z}|d|j�}||jd�}|�|��g|_d|_
n8|j�|��|xj
t|��z
c_
n��||j���|j�d|j��S#|j���|j�d|j��wxYw)Nz9acquiring buffering lock to process chunk (buffering: %d)z3got buffering lock to process chunk (buffering: %d)rFz<released buffering lock for processing chunk (buffering: %d)r-r�Tr=r+r�)rr+rTr�rcr�r�rdrfr�r��findr�r�r$)	r8r��to_write�total_to_write�nl�newline�chunk_to_write�overager�s	         r)rUzStreamBufferer.process�s���
	
����G���	
�	
�	
�	
��$�$�&�&�&�����L�d�i�X�X�X�:	��y�A�~�~��,�$�05�D�-�#�{�H�"$�D�K��O�O�E�*�*�*�#�\
� �(�(�*�*�*��H�N�N�N��	�
�
�
�
�[�w�X
� �(�(�*�*�*��H�N�N�N��	�
�
�
�
�U��a���!#���[�[���/�/��
:�#�j�j��n�n�G��"�}�}��%*�=�W�q�[�=�%9�N��{�0�),���$�+�)>�)>��)O��&(���./��+�!�'�A�+�-�-�0�E�"�)�)�.�9�9�9�
:��6��K�&�&�u�-�-�-��'�'�3�u�:�:�5�'�'�%�(
� �(�(�*�*�*��H�N�N�N��	�
�
�
�
�#"$���"�1�C��J�J�>���J�G��!�|�|� �i�i��
�6�6�;�;�D�K�H�H�5�P��),�[�t�y�[�)9�� #�D�I�K�K� 0��&�-�-�n�=�=�=�&(���./��+�+���*�*�5�1�1�1��+�+�s�5�z�z�9�+�+���&�� �(�(�*�*�*��H�N�N�N��	�
�
�
�
��
� �(�(�*�*�*��H�N�N�N��	�
�
�
�
���s �=L<�L<�C!L<�)CL<�<;M7c���|j�d��|j���|j�d��	d�|j���|j��}g|_||j���|j�d��S#|j���|j�d��wxYw)Nz,acquiring buffering lock for flushing bufferz&got buffering lock for flushing bufferr�z+released buffering lock for flushing buffer)	rr+r�rcr�r�r�r�rf)r8r�s  r)r�zStreamBufferer.flush�s�������E�F�F�F���$�$�&�&�&�����?�@�@�@�	J��)�)�D�M�*�*�/�/���<�<�C��D�K��� �(�(�*�*�*��H�N�N�H�I�I�I�I��
� �(�(�*�*�*��H�N�N�H�I�I�I�I���s�:B=�=5C2N)	rlrmrnr�r�r9rFrUr�ror+r)r�r�[sr������>�>�.>�X�-�-�-�-�*M�M�M�D�D�D�L
J�
J�
J�
J�
Jr+r�c����fd�}|S)Nc�N���t����t��fd���}|S)Nc?��K��5�|i|��5dV�ddd��n#1swxYwYddd��dS#1swxYwYdSr#ro)r'rJr(�locks  ��r)�wrapped2z,with_lock.<locals>.wrapped.<locals>.wrapped2�s�������
�
��R��(��(�(����E�E�E�������������������
�
�
�
�
�
�
�
�
�
�
�
����
�
�
�
�
�
s$�	8� �8�$	�8�$	�8�<�<r)r(rrs` �r)�wrappedzwith_lock.<locals>.wrapped�s@����
�B�
�
��	�	�	�	�	�	�
��	�
�r+ro)rrs` r)�	with_lockr	�s#���	�	�	�	�	��Nr+c#�K�tj��}tj|��	dV�tj|��dS#tj|��wxYw)z�pushd changes the actual working directory for the duration of the
    context, unlike the _cwd arg this will work with other built-ins such as
    sh.glob correctlyN)r�r`r�)r��	orig_paths  r)rara�sZ����
�	���I��H�T�N�N�N��
����
������������������s�A�Ac��d�d�|���D����}td|�d|�d����)zZallows us to temporarily override all the special keyword parameters in
    a with contextr�c�"�g|]\}}|�d|����
S)r!ror�s   r)r�z_args.<locals>.<listcomp>�s&��C�C�C�D�A�q�a�<�<�!�<�<�C�C�Cr+zl

sh.args() has been deprecated because it was never thread safe.  use the
following instead:

    sh2 = sh(z+)
    sh2.your_command()

or

    sh2 = sh(z7)
    from sh2 import your_command
    your_command()

)r�r�DeprecationWarning)rJ�
kwargs_strs  r)�_argsr�sf��
���C�C�F�L�L�N�N�C�C�C�D�D�J�
�	�
�	�	��	�	�	���r+c�J��eZdZdZhd�Zd�fd�	Zd�Zedd���Z�xZ	S)�Environmenta�this allows lookups to names that aren't found in the global scope to be
    searched for as a program name.  for example, if "ls" isn't found in this
    module's scope, we consider it a system program and try to find it.

    we use a dict instead of just a regular object as the base class because the
    exec() statement used in the run_repl requires the "globals" argument to be a
    dictionary>r�rrar��contrib�__file__�__version__r�r9r�r�r�r��__project_url__r�r�r�r�ruNc�v��tt|�����||_|pi|_dS)z�baked_args are defaults for the 'sh' execution context.  for
        example:

            tmp = sh(_out=StringIO())

        'out' would end up in here as an entry in the baked_args dictN)r�rtr9�globsr)r8rrr�s   �r)r9zEnvironment.__init__2
s8���	�d�D���"�"�$�$�$���
�$�*�����r+c���|dkrd}||jvr
|j|S|dkrtjd��gSt	|��}|r|S|�d��r|�d��rt�t||jtj
|j��}|r|St|d|zd��}|r|S	tj|S#t$rYnwxYwt!|���)Nr'r�__all__zJCannot import * from sh. Please import sh or import programs individually.�__�b_)r
r�warnings�warnr�r@rAr�rr�rlrr�r�r�r�r�)r8r�r�r�builtins     r)�__getitem__zEnvironment.__getitem__=
s2����;�;��A�
������:�a�=� �
�	�>�>��M� �
�
�
��I� ��"�"���	��J�
�<�<����	!�!�*�*�T�"2�"2�	!� � ��a���G�,<�!=�t��O�O���	��J��$��q��$�/�/���	��N�	��:�a�=� ���	�	�	��D�	�����a� � � s�C�
C&�%C&c�"�t||��Sr#)r)r�r�s  r)�b_whichzEnvironment.b_whichr
s���g�u�%�%�%r+r#)
rlrmrnr�r
r9r r�r"r�r�s@r)rr
s�������������I�,	+�	+�	+�	+�	+�	+�1!�1!�1!�j�&�&�&��\�&�&�&�&�&r+rc�$�eZdZed���ZdS)�Contribc������fd�}|S)Nc�R���t��fd���}t��|���S)Nc�j��t�t��}|st�����|��}|Sr#)rr�r�)r8r�new_cmdr(r�s   ��r)�
cmd_getterz6Contrib.__call__.<locals>.wrapper1.<locals>.cmd_getter{
s:���%�d�G�4�4���0�)�$�/�/�/��"�S�'�'���r+)r��setattr)r(r)rIr�s` ��r)�wrapper1z"Contrib.__call__.<locals>.wrapper1z
sE����
�
�
�
�
�
��X�
�
�C��z�*�*�*��Ir+ro)rIr�r+s`` r)rmzContrib.__call__x
s)����	�	�	�	�	�	��r+N)rlrmrnrqrmror+r)r$r$w
s-����������[���r+r$z.contrib�gitc�2�|�d���}|S)z*most git commands play nicer without a TTYF)�_tty_out)r)�origrs  r)r,r,�
s���)�)�U�)�
#�
#�C��Jr+�sudoc�z���dtj���d���fd���fd�}|�d|���}|S)zya nicer version of sudo that uses getpass to ask for a password, or
    allows the first argument to be a string passwordz[sudo] password for rc3�D�K�tj����dz}|V�dS)N��promptr���getpass)�pwr4s �r)rfzsudo.<locals>.stdin�
s+�����
�_�F�
+�
+�
+�d�
2�������r+c���|�dd��}|����}n|�d��dz}||d<||fS)N�passwordr�r�)r��rstrip)r rJr9�pass_getterrfs    �r)rUzsudo.<locals>.process�
sR����:�:�j�$�/�/�����%�'�'�K�K�"�/�/�$�/�/�$�6�K�#��u�
��&�y�r+z-S)�_arg_preprocess)r6�getuserr)r/rUrr4rfs   @@r)r0r0�
sl����
:�G�O�$5�$5�
9�
9�
9�F������	�	�	�	�	��)�)�D�'�)�
2�
2�C��Jr+�sshc����Gd�dt���G�fd�dt����fd�}|�ddd|���}|S)	z+An ssh command for automatic password loginc�0�eZdZd�Zd�Zed���ZdS)�ssh.<locals>.SessionContentc��td���|_td���|_g|_d|_d|_dS)NiP�r�i�r�)r�chars�lines�
line_chars�	last_line�cur_charr7s r)r9z$ssh.<locals>.SessionContent.__init__�
s?���e�,�,�,�D�J��d�+�+�+�D�J� �D�O��D�N��D�M�M�Mr+c���|dkr0|j}||_|j�|��g|_n|j�|��|j�|��||_dS�Nr�)�cur_linerFrDrdrErCrG)r8r��lines   r)�append_charz'ssh.<locals>.SessionContent.append_char�
so���t�|�|��}��!%����
�!�!�$�'�'�'�"$������&�&�t�,�,�,��J���d�#�#�#� �D�M�M�Mr+c�:�d�|j��}|Sr�)r�rE)r8rKs  r)rJz$ssh.<locals>.SessionContent.cur_line�
s���7�7�4�?�+�+�D��Kr+N)rlrmrnr9rLr�rJror+r)�SessionContentrA�
sM������	�	�	�
	!�
	!�
	!�
�	�	�
��	�	�	r+rNc� ��eZdZ�fd�Zd�ZdS)�ssh.<locals>.SSHInteractc�z��||_||_||_||_���|_d|_d|_dSr�)�prompt_matchr;�out_handler�
login_success�content�
pw_enteredr�)r8rRr;rSrTrNs     �r)r9z!ssh.<locals>.SSHInteract.__init__�
sF��� ,�D��*�D��*�D��!.�D��)�>�+�+�D�L�$�D�O� �D�L�L�Lr+c�v�|j�|��|jr&|js|�|j��|_|jr|�|j|��S|�|j��r5|���}|�|dz��d|_dSdS)Nr�T)	rUrLrVr�rTrSrRr;r�)r8r�rfr9s    r)rmz!ssh.<locals>.SSHInteract.__call__�
s����L�$�$�T�*�*�*���
@�t�|�
@�#�1�1�$�,�?�?����|�
=��'�'���e�<�<�<�� � ���.�.�
'��+�+�-�-���	�	�(�T�/�*�*�*�"&�����
'�
'r+N)rlrmrnr9rm)rNs�r)�SSHInteractrP�
s=�������		!�		!�		!�		!�		!�	'�	'�	'�	'�	'r+rXc�����|�d��}|�dd���|�dd��}|�dd��}d�|�d�}���fd�}n�fd�}|�d	�}�||||��|d
<||fS)N�interactr9rTr4zPlease enter SSH password: c�6�|j�d��S)Nz
password: )rJrA�rUs r)rRz*ssh.<locals>.process.<locals>.prompt_match�
s���'�0�0��>�>�>r+c�.��tj����S)Nr3r5r3s�r)r;z)ssh.<locals>.process.<locals>.pass_getter�
s�����f�5�5�5�5r+c�.����d��SrI)r:)r9s�r)r;z)ssh.<locals>.process.<locals>.pass_getter�
s������t�,�,�,r+c��dSr�ror\s r)rTz+ssh.<locals>.process.<locals>.login_successr�r+r�)r�)	r rJ�real_out_handlerrTrRr;r9r4rXs	      @@�r)rUzssh.<locals>.process�
s������!�:�:�j�1�1���:�:�j�$�/�/���
�
�?�D�9�9�
��z�z�(�D�1�1��.����
?�
?�
?���
6�
6�
6�
6�
6�
6�

-�
-�
-�
-�
-�� �
�
�
�%���+�'7��
�
��v���&�y�r+rT)�_out_bufsize�_tty_in�_unify_ttysr<)r>r)r/rUrrXrNs   @@r)r>r>�
s��������������2'�'�'�'�'�'�'�f�'�'�'�4�����@�)�)���$�����C��Jr+c�P�tdt�d���		td��}n#ttf$rYnYwxYw	tt
|dd��||��n2#t$rYn'ttj	����YnxYw�}td��dS)Nz
>> sh vz"
>> https://github.com/amoffat/sh
Tzsh> z<dummy>�singler�)
�printr�inputr��EOFError�exec�compile�
SystemExitr�r)r	rKs  r)�run_replrls���	�
G�k�
G�
G�
G�H�H�H�*�	���=�=�D�D���H�%�	�	�	��E�	����	*����y�(�3�3�S�#�>�>�>�>���	�	�	��E�	*��)�&�(�(�)�)�)�)�)����*�
�"�I�I�I�I�Is�+�?�?� A$�$
B�0!Bc�,��eZdZd�fd�	Zd�Zd�Z�xZS)�SelfWrapperNc
���tt|���t|dd��t|dd�����dD]"}t	||t||d�����#g|_||_t}|j�	��}|�
dd��|rP|�|��\}}|d�	��|d<|d�|��t���	��}t|j|j|��||j<t#||���|_dS)Nrlr�)r��doc)�__builtins__r�__package__�__dict__r4)r)r�rnr9r�r*�__path__�_SelfWrapper__self_moduler�rsrEr�rRrF�globalsrTrlr�r�_SelfWrapper__env)
r8�self_modulerr9r
�	cls_attrsr4rrr�s
         �r)r9zSelfWrapper.__init__(sc���
	�k�4� � �)�)���j�$�7�7���Y��5�5�	*�	
�	
�	
�@�	B�	B�D��D�$���T�4� @� @�A�A�A�A���
�(������(�-�-�/�/�	��
�
�j�$�'�'�'��	6�&�9�9�*�E�E�L�I�q�&/��&=�&B�&B�&D�&D�I�l�#��l�#�*�*�9�5�5�5��	�	��� � ��&*�� �+�"7��'
�'
��k�"�#�!��:�>�>�>��
�
�
r+c��|j|Sr#)rw)r8r�s  r)r�zSelfWrapper.__getattr__Hs���z�$��r+c��|jj���}|�|��|�|j|��}|Sr#)rwrrErFr�ru)r8rJr�new_shs    r)rzSelfWrapper.bakeKsG���Z�*�/�/�1�1�
����&�!�!�!����� 2�J�?�?���
r+r#)rlrmrnr9r�rr�r�s@r)rnrn's\�������?�?�?�?�?�?�@ � � �������r+rn�__main__)Fr#)�r�rX�collectionsr�collections.abcr�ImportErrorr�r�r�r6r��glob_moduler%rr��platformr�r�r�r3r�r�r�rr�r�rcrqr�r�rr�rr��
contextlibr�	functoolsr�	importlibr	�ior
rrr
r��localerr�r�shlexrrG�typesrr�typingrrrr�versionr�PackageNotFoundErrorr�system�lowerr�r�r�r�rlrr�r�r�rLr|�FORCE_USE_SELECTr��
PUSHD_LOCKr*r��dirrHr�rD�HAS_POLLrerfrgrhr>r1rqr�rpr�r�rTr�r�r�r��SIGABRT�SIGBUS�SIGFPE�SIGILL�SIGINTror��SIGQUIT�SIGSEGVra�SIGSYSr�r�r�rjr�r�rtrsrr�r�r�r�r�r�r�rr
rrr7r9r�r^r�r�r�r�r�r�rrrrr�rUrur�r�r�r}r�r�r_rryrrr�r�r�r�r�r�r�r�r�r
r�r�r�r�r�r�rr�r	rarrr$�mod_namer�modulesr,r0r>rlrnrvr	ror+r)�<module>r�s������0����������$�'�'�'�'�'�'�'���$�$�$�#�#�#�#�#�#�#�#�$����
��������	�	�	�	�����������������	�	�	�	�����
�
�
�
�
�
�
�
�	�	�	�	�
�
�
�
�
�
�
�
�����
�
�
�
�
�
�
�
���������������������
�
�
�
���������#�#�#�#�#�#�%�%�%�%�%�%�������������6�6�6�6�6�6�6�6�6�6�������'�'�'�'�'�'���������&�&�&�&�&�&�+�+�+�+�+�+�+�+�)�)�)�)�)�)�)�)�)�)�)�)��"�(�"�4�(�(�K�K���$�����K�K�K�����1������!�!�'�'�)�)�)�)�
�+�	
�k�	
�	
�	
�����
�
�
��Q�Z�
�'�'�)�)�4�W���8�?��� 1�1������S�S�����(:�C�@�@�A�A�B�B�
��4���B�J�N�N�+@�#�F�F�G�G�H�H��
�Y�_�
�
�
�0�0�0��3�s�s�3�3�5�5�z�z�?�?���7�6�6�"�"����������I�I�I�I�I��I�I�I�X-�-�-�-�-�6�-�-�-�j7C���d�<� �$�z�"2�2�3�B�B�B���$��
�F�	&�	&�	&�	&�	&�I�	&�	&�	&�K�K�K�K�K�$�K�K�K�73�73�73�73�73�i�73�73�73�t	�	�	�	�	�o�	�	�	�*�*�*�*�*�y�*�*�*��N�
�M�
�M�
�M�
�M�
�N�
�N�
�N�
�N�
�N�
�M�'�#�$	�	�	�	�	�n�	�	�	��r�z�S�T�T��13��d�3��_�-�-�.�3�3�3���P�P���-�-�/�/�P�P�P����
���@���N
��	�%�%�%�%�%�$�%�%�%�����
���5�5�5�(�(�(�(�V�������16�16�16�16�16�V�16�16�16�h
�
�
�
�B&�B&�B&�B&�B&�V�B&�B&�B&�J>�>�>�������&���&&�&�&�������������6
�
�
� ���8���._�_�_�_�_�f�_�_�_�D)�)�)�XJ�J�J�J�Z��� � � �@�@�@�F���"������$]'�]'�]'�]'�]'�F�]'�]'�]'�@���B����
$�$�$�<:�:�:�z	�	�	�	�	��	�	�	�	�	�	�	�	�	�	�	�	�6�6�6�r
�
�
����+�+�+���� � � �F���&^�^�^�^�^�6�^�^�^�B���@;�;�;�
���>
�
�
� ������T$�T$�T$�T$�T$�6�T$�T$�T$�n{J�{J�{J�{J�{J�V�{J�{J�{J�|�����:���	�	���	�������2e&�e&�e&�e&�e&�$�e&�e&�e&�P�����j����(�j� ��
�'�(�
�
�����H��	����������	����������2	�����Y�Y���Y�x���2(�(�(�(�(�*�(�(�(�V�z���
�+�g�g�i�i�
 �
 �C��H�S�M�M�M�M�M�(�K���H�(=�>�>�C�K����s��$�$�C-�-C<�;C<