B
    ~dD                 @   sl   d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 dZ
e	dd	d
d Ze	dd	eedfddZdS )z'Utilities for calling out to the shell
    N)find_executable)PopenPIPECalledProcessError   )deprecated_functionz(Duncan Macleod <duncan.macleod@ligo.org>zgwpy.utils.shell.which is deprecated in favour of shutil.which from the Python standard library, and will be removed in a future release)messagec             C   s    t | }|dkrtd|  |S )a;  Find full path of executable program

    Parameters
    ----------
    program : `str`
        path of executable name for which to search

    Returns
    -------
    programpath
        the full absolute path of the executable

    Raises
    ------
    ValueError
        if not executable program is found
    Nz No executable '%s' found in PATH)r   
ValueError)programexe r   ]/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/gwpy/utils/shell.pywhich   s    r   zgwpy.utils.shell.call is deprecated in favour of subprocess.call from the Python standard library, and will be removed in a future release.raisec       
      K   s   t | ttfr&d| }|dd nt| }|dd t| f||d|}| \}}|jr|dkrln0|dkrt	|j|}	t
t|	 nt	|j||d|dfS )	a.  Call out to the shell using `subprocess.Popen`

    Parameters
    ----------
    stdout : `file-like`, optional
        stream for stdout

    stderr : `file-like`, optional
        stderr for stderr

    on_error : `str`, optional
        what to do when the command fails, one of

        - 'ignore' - do nothing
        - 'warn' - print a warning
        - 'raise' - raise an exception

    **kwargs
        other keyword arguments to pass to `subprocess.Popen`

    Returns
    -------
    out : `str`
        the output stream of the command
    err : `str`
        the error stream from the command

    Raises
    ------
    OSError
        if `cmd` is a `str` (or `shell=True` is passed) and the executable
        is not found
    subprocess.CalledProcessError
        if the command fails otherwise
     shellFT)stdoutstderrignorewarnzutf-8)
isinstancelisttuplejoin
setdefaultstrr   communicate
returncoder   warningsr   decode)
cmdr   r   Zon_errorkwargsZcmdstrprocouterrer   r   r   call=   s    +
r&   )__doc__r   distutils.spawnr   
subprocessr   r   r   Z
decoratorsr   
__author__r   r&   r   r   r   r   <module>   s   