o
    i                  	   @   s   d dl Z d dlZd dlZd dlZd dlmZmZmZmZ ddl	m
Z
 ddlmZ dedee fdd	Zdee defd
dZddddeeee f dee dedejfddZdS )    N)AnyListOptionalUnion   )
is_windows)Errorscommandreturnc                 C   s   t j| t dS )zSplit a string command using shlex. Handles platform compatibility.

    command (str) : The command to split
    RETURNS (List[str]): The split command.
    )posix)shlexsplitr   r	    r   H/home/ubuntu/.local/lib/python3.10/site-packages/weasel/util/commands.pysplit_command   s   r   c                 C   s   d dd | D S )zJoin a command using shlex. shlex.join is only available for Python 3.8+,
    so we're using a workaround here.

    command (List[str]): The command to join.
    RETURNS (str): The joined command
     c                 s   s    | ]}t |V  qd S )N)r   quote).0cmdr   r   r   	<genexpr>   s    zjoin_command.<locals>.<genexpr>)joinr   r   r   r   join_command   s   r   F)stdincapturer   r   c             	   C   s   t | trt| }| }n| }d| }ztj|tj |dd|r#tj	nd|r)tj
ndd}W n tyB   ttjj||d ddw |jdkrs|rsd| d	}|d
|j 7 }|jdurf|d7 }||j7 }t|}||_||_||jdkr~t|j |S )a  Run a command on the command line as a subprocess. If the subprocess
    returns a non-zero exit code, a system exit is performed.

    command (str / List[str]): The command. If provided as a string, the
        string will be split using shlex.split.
    stdin (Optional[Any]): stdin to read from or None.
    capture (bool): Whether to capture the output and errors. If False,
        the stdout and stderr will not be redirected, and if there's an error,
        sys.exit will be called with the return code. You should use capture=False
        when you want to turn over execution to the command, and capture=True
        when you want to run the command more like a function.
    RETURNS (Optional[CompletedProcess]): The process object.
    r   utf8FN)envinputencodingcheckstdoutstderrr   )str_commandtoolzError running command:

z

zSubprocess exited with status z$

Process log (stdout and stderr):

)
isinstancestrr   r   
subprocessrunosenvironcopyPIPESTDOUTFileNotFoundErrorr   E501format
returncoder    SubprocessErrorretr	   sysexit)r	   r   r   cmd_listcmd_strr2   messageerrorr   r   r   run_command   sF   


	



r9   )r(   r   r&   r3   typingr   r   r   r   compatr   errorsr   r%   r   r   boolCompletedProcessr9   r   r   r   r   <module>   s(    	