a
    dg                     @  sf   d Z ddlmZ ddlZddlZddlZddlmZmZm	Z	 ddl
mZ erTddlZG dd dZdS )z*Wrapper around subprocess to run commands.    )annotationsN)TYPE_CHECKINGListUnion)uuid4c                   @  s   e Zd ZU dZdZded< dZded< dZded< d dddddd	Ze	d
dddZ
e	d dddddZdddddZdddddZddddddZdddddZdS )!BashProcessa<  Wrapper for starting subprocesses.

    Uses the python built-in subprocesses.run()
    Persistent processes are **not** available
    on Windows systems, as pexpect makes use of
    Unix pseudoterminals (ptys). MacOS and Linux
    are okay.

    Example:
        .. code-block:: python

            from langchain_community.utilities.bash import BashProcess

            bash = BashProcess(
                strip_newlines = False,
                return_err_output = False,
                persistent = False
            )
            bash.run('echo 'hello world'')

    Fboolstrip_newlinesreturn_err_output
persistent)r	   r
   r   c                 C  s<   || _ || _d| _d| _|r8tt | _| | | j| _dS )z3
        Initializes with default settings
         N)r	   r
   promptprocessstrr   _initialize_persistent_process)selfr	   r
   r    r   r/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_experimental/llm_bash/bash.py__init__/   s    	zBashProcess.__init__pexpect)returnc                  C  s@   t  dkrtdzddl} W n ty:   tdY n0 | S )z Import pexpect only when needed.Windowsz;Persistent bash processes are not yet supported on Windows.r   NzVpexpect required for persistent bash processes. To install, run `pip install pexpect`.)platformsystem
ValueErrorr   ImportError)r   r   r   r   _lazy_import_pexpect@   s    
z BashProcess._lazy_import_pexpectr   zpexpect.spawn)r   r   r   c                 C  s<   |   }|jdg ddd}|d|  |j|dd |S )z
        Initializes a persistent bash setting in a
        clean environment.
        NOTE: Unavailable on Windows

        Args:
            Prompt(str): the bash command to execute
        env)z-ibashz--norcz--noprofilezutf-8)encodingzPS1=
   timeout)r   spawnsendlineZexpect_exact)r   r   r   r   r   r   r   r   Q   s    
z*BashProcess._initialize_persistent_processzUnion[str, List[str]])commandsr   c                 C  s<   t |tr|g}d|}| jdur.| |S | |S dS )z
        Run commands in either an existing persistent
        subprocess or on in a new subprocess environment.

        Args:
            commands(List[str]): a list of commands to
                execute in the session
        ;N)
isinstancer   joinr   _run_persistent_run)r   r%   r   r   r   rung   s    	


zBashProcess.run)commandr   c              
   C  s   z"t j|ddt jt jdj }W nJ t jyl } z0| jrP|j W  Y d}~S t|W  Y d}~S d}~0 0 | j	r||
 }|S )z
        Runs a command in a subprocess and returns
        the output.

        Args:
            command: The command to run
        T)shellcheckstdoutstderrN)
subprocessr+   PIPESTDOUTr/   decodeCalledProcessErrorr
   r   r	   strip)r   r,   outputerrorr   r   r   r*   z   s    zBashProcess._run)r7   r,   r   c                 C  s(   t |d }t j|d|dd}| S )z
        Uses regex to remove the command from the output

        Args:
            output: a process' output string
            command: the executed command
        z\s*\nr      )count)reescapesubr6   )r   r7   r,   patternr   r   r   process_output   s    zBashProcess.process_outputc                 C  s   |   }| jdu rtd| j| | jj| jdd | jd z| jj| j|jgdd W n |jy~   d|  Y S 0 | jj|jkrd| jj	 S | jj
}| ||}| jr| S |S )z
        Runs commands in a persistent environment
        and returns the output.

        Args:
            command: the command to execute
        NzProcess not initializedr    r!   r   z&Timeout error while executing command zExited with error status: )r   r   r   r$   expectr   ZEOFTIMEOUTafterZ
exitstatusbeforer?   r	   r6   )r   r,   r   r7   r   r   r   r)      s"    
zBashProcess._run_persistentN)FFF)__name__
__module____qualname____doc__r	   __annotations__r
   r   r   staticmethodr   r   r+   r*   r?   r)   r   r   r   r   r      s    
   r   )rG   
__future__r   r   r;   r1   typingr   r   r   uuidr   r   r   r   r   r   r   <module>   s   