o
    vaH
                     @   s4   d Z ddlmZ ddlmZmZ dd Zdd ZdS )	zSecures functions for Glances    )	nativestr)PopenPIPEc                 C   s$   d}|  dD ]}|t|7 }q|S )zA more or less secure way to execute system commands
    Multiple command should be seprated with a &&

    Return: the result of the commands
     z&&)split__secure_popen)cmdretc r   0/usr/lib/python3/dist-packages/glances/secure.pysecure_popen   s   r   c                 C   s  d}|  d}t|dkrd| S t|dkr#|d  }|d } nd}d}d}|  dD ]#}d	d
 | dD }t|d|ttd}|durL|j  |}|j}q.| }	t	|	d dkrt	|	d }|durt
|d}
|
| W d   |S 1 s~w   Y  |S t	|	d }|S )zaA more or less secure way to execute system command
    Manage redirection (>) and pipes (|)
    r   >   z5Glances error: Only one file redirection allowed ({})   r   N|c                 S   s   g | ]}|r|qS r   r   ).0ir   r   r   
<listcomp>>   s    z"__secure_popen.<locals>.<listcomp> F)shellstdinstdoutstderrw)r   lenformatstripr   r   r   closeZcommunicater   openwrite)r   r	   Zcmd_split_redirectZstdout_redirectZsub_cmd_stdinZp_lastZsub_cmdZsub_cmd_splitpZp_retZstdout_redirect_filer   r   r   r   )   sD   




r   N)__doc__Zglances.compatr   
subprocessr   r   r   r   r   r   r   r   <module>   s
   