
    %g                         d 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j                  dddee   d	ee   d
ee   deeegdf      ddf
dZdedefdZy)z%
Run commands and handle returncodes
    N)CallableListOptionalTextIO   )pushd)
get_logger)fd_outpbarcmdcwdr
   r   returnc          
      *   t               j                  ddj                  |       |       	 t        ||nd      5  t	        j
                  | dt        j                  t        j                  t        j                  t        j                  d      }|j                         b|j                  E|j                  j                         }||j                  |       | ||j                                |j                         b|j!                         \  }}|r9t#        |      dkD  r+||j                  |       | ||j                                |j$                  dk7  rSd	}	 t        j&                  |j$                        }d
j-                  | t/        |j$                        |      }	t1        |	      	 ddd       y# t(        t*        f$ r Y Lw xY w# 1 sw Y   yxY w# t2        $ r,}
dj-                  | t5        |
            }	t1        |	      |
d}
~
ww xY w)a  
    Run command as subprocess, polls process output pipes and
    either streams outputs to supplied output stream or sends
    each line (stripped) to the supplied progress bar callback hook.

    Raises ``RuntimeError`` on non-zero return code or execption ``OSError``.

    :param cmd: command and args.
    :param cwd: directory in which to run command, if unspecified,
        run command in the current working directory.
    :param fd_out: when supplied, streams to this output stream,
        else writes to sys.stdout.
    :param pbar: optional callback hook to tqdm, which takes
       single ``str`` arguent, see:
       https://github.com/tqdm/tqdm#hooks-and-callbacks.

    zcmd: %s
cwd: %s N.r   T)bufsizestdinstdoutstderrenvuniversal_newlinesr    zCommand {}
	{} {}z!Command: {}
failed with error {}
)r	   debugjoinr   
subprocessPopenDEVNULLPIPESTDOUTosenvironpollr   readlinewritestripcommunicatelen
returncodestrerrorArithmeticError
ValueErrorformatreturncode_msgRuntimeErrorOSErrorstr)r   r   r
   r   procliner   _serrormsges              T/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/cmdstanpy/utils/command.py
do_commandr8      s   0 L)388C=#>)'#/3s3 $	(## ((!!((JJ#'D ))+%;;*;;//1D)T*'TZZ\* ))+% ((*IFAv;?)V,'V\\^,!#[[9F ,228& #3'' $7$	( $	(> (4 ?$	( $	(J  '3::3AG3Q&'s`   G CGAG"F<1G3G <GGGGGG G 	H&'HHretcodec                 X    | dk  r
d| z  }d| S | dk  ry| dk(  ry| dk(  ry	| d
z
  }d| S )zinterpret retcoder   zterminated by signal }   zerror during processing~   r      zprogram not found    )r9   sigs     r7   r-   r-   R   sS    {7l&se,,#~(#~#~"
C-C"3%((    )N)__doc__r    r   systypingr   r   r   r   
filesystemr   loggingr	   r   r0   r8   intr-   r@   rB   r7   <module>rI      s    
  
 3 3  
 B'  #zz,0B'	cB'	#B' V	B'
 8SE4K(
)B' 
B'J)C )C )rB   