
    j5j                    .   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 ddlmZ ddlmZmZmZ ddlmZ  ej(                  d	      Ze	eez     Zeeef   ej0                  z  Zdd
d
d	 	 	 	 	 	 	 	 	 ddZd
dd	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZy)af  Low-level wrappers around :py:mod:`subprocess`.

These functions exist to give OCRmyPDF child processes uniform logging
behavior and to route through any platform-specific PATH fix-ups before
invocation. They are intended as drop-in replacements for
:py:func:`subprocess.run` in contexts where that routing is desirable
(for example, plugin-provided tools).
    )annotationsN)CallableMappingSequence)suppress)Path)CalledProcessErrorCompletedProcessPopen)runzocrmypdf.subprocessF)envlogs_errors_to_stdoutcheckc                  t        | ||      \  } }}}d}|sdnd}	 t        | f||d|}	t        |	|d      }	 |j	                  t
        j                        rY|rWt        t        t              5  |j                  dd      }ddd       |r|j                  d|       |	S |j                  d|       |	S # t        $ r}
t        |
|d      } d}
~
ww xY w# 1 sw Y   RxY w# |j	                  t
        j                        rg|rdt        t        t              5  |j                  dd      }ddd       n# 1 sw Y   nxY w|r|j                  d|       w |j                  d|       w w w xY w)	a\  Wrapper around :py:func:`subprocess.run`.

    The main purpose of this wrapper is to log subprocess output in an orderly
    fashion that identifies the responsible subprocess. An additional
    task is that this function goes to greater lengths to find possible Windows
    locations of our dependencies when they are not on the system PATH.

    Arguments should be identical to ``subprocess.run``, except for following:

    Args:
        args: Positional arguments to pass to ``subprocess.run``.
        env: A set of environment variables. If None, the OS environment is used.
        logs_errors_to_stdout: If True, indicates that the process writes its error
            messages to stdout rather than stderr, so stdout should be logged
            if there is an error. If False, stderr is logged. Could be used with
            stderr=STDOUT, stdout=PIPE for example.
        check: If True, raise an exception if the process exits with a non-zero
            status code. If False, the return value will indicate success or failure.
        kwargs: Additional arguments to pass to ``subprocess.run``.
    Nstderrstdout)r   r   zutf-8replacezstdout/stderr = %szstderr = %s)_fix_process_argssubprocess_rungetattrr	   isEnabledForloggingDEBUGr   AttributeErrorUnicodeDecodeErrordecodedebug)argsr   r   r   kwargsprocess_log_textr   stderr_nameproces              N/var/www/html/qr/venv/lib/python3.12/site-packages/ocrmypdf/subprocess/_run.pyr   r      s`   8 %6dC$H!D#{EF"7(XK9dC5CFC
 {D1##GMM2v.*<= ;w	:;$!!"6? K !!-8K  K.; ; ##GMM2v.*<= ;w	:; ; ;$!!"6?!!-8 8>2sL   B3 C .C3	C<C

CC C6E&D0'	E&0D951E&)r   r   c                  t        | ||      \  } }}}|sJ d       t        | fd|i|5 }g }|j                         |j                  t	        |j                  j
                  d      D ]Y  }	|j                  t        j                        r|j                  |	j                                 ||	       |j                  |	       [ |j                         dj                  |      }
|r(|j                  dk7  rt        |j                  | d|
      t        | |j                  d|
      cddd       S # 1 sw Y   yxY w)a#  Run a process like ``ocrmypdf.subprocess.run``, and poll stderr.

    Every line of produced by stderr will be forwarded to the callback function.
    The intended use is monitoring progress of subprocesses that output their
    own progress indicators. In addition, each line will be logged if debug
    logging is enabled.

    Requires stderr to be opened in text mode for ease of handling errors. In
    addition the expected encoding= and errors= arguments should be set. Note
    that if stdout is already set up, it need not be binary.
    zMust use text=Truer   N r   )outputr   )r   )r   r   pollr   iterreadliner   r   r   r   stripappendjoin
returncoder	   r
   )r   callbackr   r   r   r    textr#   linesmsgr   s              r%   run_polling_stderrr4   O   s    & $5T3#G D#{D%%%4	t	'	'	' L4iik!{{"DKK00"5 "++GMM:%%ciik2S!	" iik! T__)$T__d4PVWWdootFKL L Ls   B,D4AD44D=c                Z   |st         j                  }t        | d         }t        j                  dk(  rddlm}  ||| |      } t        j                  d|        t        j                  t         j                  j                  |            }t        |j                  dd            }| |||fS )Nr   win32)fix_windows_argszRunning: %sr1   F)osenvironstrsysplatformocrmypdf.subprocess._windowsr7   logr   getChildpathbasenameboolget)r   r   r   programr7   r    r1   s          r%   r   r   v   s     jj $q'lG
||wAs3IImT",,rww//89K

65)*Dk4''    )
r   Argsr   Environ | Noner   rB   r   rB   returnr
   )
r   rF   r0   zCallable[[str], None]r   rB   r   rG   rH   r
   )r   rF   r   rG   rH   z*tuple[Args, Environ, logging.Logger, bool])__doc__
__future__r   r   r8   r;   collections.abcr   r   r   
contextlibr   pathlibr   
subprocessr	   r
   r   r   r   	getLoggerr>   r:   rF   _EnvironEnvironr4   r    rE   r%   <module>rS      s   #  	 
 7 7   B B ,g-.s

#s(
bkk
) "'/
/ 
/  	/
 / /l $L
$L $$L 	$L
 
$L $LN(
(#(/(rE   