
    TAil-                       S r SSKJr  SSKrSSKrSSKrSSKrSSKJrJ	r	J
r
  SSKJr  SSKJr  SSKJrJrJrJrJr  SSKJr  SS	KJr  SS
KJr  \R6                  " \5      r\
\\-     r\R@                  r!SSSS.         S#S jjrSSS.         S$S jjr"      S%S jr#SSSS.       S&S jjr$Sr%Sr&Sr'Sr(Sr)Sr*Sr+Sr,S'S jr-S(S jr.          S)S jr/            S*S  jr0SS\S!.               S+S" jjr1g),z$Wrappers to manage subprocess calls.    )annotationsN)CallableMappingSequence)suppress)Path)PIPESTDOUTCalledProcessErrorCompletedProcessPopen)run)Version)MissingDependencyErrorF)envlogs_errors_to_stdoutcheckc               "   [        XU5      u  ppVSnU(       d  SOSn [        U 4XS.UD6n	[        XS5      n UR	                  [
        R                  5      (       ac  U(       a\  [        [        [        5         UR                  SS5      nSSS5        U(       a  UR                  SU5        U	$ UR                  SU5        U	$ ! [         a  n
[        XS5      ne Sn
A
ff = f! , (       d  f       N\= f! UR	                  [
        R                  5      (       av  U(       an  [        [        [        5         UR                  SS5      nSSS5        O! , (       d  f       O= fU(       a  UR                  SU5        f UR                  SU5        f f f = f)	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              X/var/www/html/land-ocr/venv/lib/python3.13/site-packages/ocrmypdf/subprocess/__init__.pyr   r      sJ   8 %6d$H!D{F"7(XK9dCCFC
 D1##GMM22v.*<=w	: >$!!"6? K !!-8K  . >= ##GMM22v.*<=w	: >==$!!"6?!!-8 8>2sM   C C2 8C!
CCCC2 !
C/2A F2E	F
E6F)r   r   c                  [        XU5      u  ppVU(       d   S5       e[        U 4SU0UD6 n/ nUR                  5       c  UR                  c  M   [	        UR                  R
                  S5       H_  n	UR                  [        R                  5      (       a  UR                  U	R                  5       5        U" U	5        UR                  U	5        Ma     UR                  5       c  M  SR                  U5      n
U(       a&  UR                  S:w  a  [        UR                  U SU
S9e[        XR                  SU
S9sSSS5        $ ! , (       d  f       g= f)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_stderrr8   O   s	   & $5T#G D{%%%4	t	'	'	'4iik!{{"DKK00"5++GMM::%%ciik2S!	 6 iik! T__)$T__d4PVWWootFK 
(	'	's   B7E%AE
Ec                `   U(       d  [         R                  n[        U S   5      n[        R                  S:X  a  SSKJn  U" X0U5      n [        R                  SU 5        [        R                  [         R                  R                  U5      5      n[        UR                  SS5      5      nXXV4$ )Nr   win32)fix_windows_argszRunning: %sr5   F)osenvironstrsysplatformocrmypdf.subprocess._windowsr;   logr!   getChildpathbasenameboolget)r"   r   r#   programr;   r$   r5   s          r)   r   r   v   s     jj $q'lG
||wAs3IImT",,rww//89K

65)*Dk''    z	--versionz(\d+(\.\d+)*))version_argregexr   c          
        X/n [        USS[        [        SUS9nUR                  n[        R                  " X&R                  5       5      nU(       d  [        S	U  S
U 35      eUR                  S5      n	U	$ ! [         a  n[        SU  S35      UeSnAf[         aV  nUR                  S:w  a1  [        R                  U5        [        SU  SUR                   35      Ue[        SU  S35      UeSnAff = f)aH  Get the version of the specified program.

Arguments:
    program: The program to version check.
    version_arg: The argument needed to ask for its version, e.g. ``--version``.
    regex: A regular expression to parse the program's output and obtain the
        version.
    env: Custom ``os.environ`` in which to run program.
T)	close_fdsr5   r   r   r   r   zCould not find program 'z' on the PATHNr   zRan program 'z' but it exited with an error:
zThe program 'z+' did not report its version. Message was:
   )r   r	   r
   r   FileNotFoundErrorr   r   r3   rB   	exceptionr,   rematchr0   group)
rH   rJ   rK   r   	args_progr'   r,   r(   rR   versions
             r)   get_versionrV      s     &I
 kk HHULLN+E$G9 %#H&
 	
 kk!nGN-  $&wi}=
	  <<1MM!(y(H
S %&wi}=
	s$   #A7 7
C4BC4AC//C4zU
The program '{program}' could not be executed or was not found on your
system PATH.
z
The program '{program}' could not be executed or was not found on your
system PATH.  This program is required when you use the
{required_for} arguments.  You could try omitting these arguments, or install
the package.
z
The program '{program}' could not be executed or was not found on your
system PATH.  This program is recommended when using the {required_for} arguments,
but not required, so we will proceed.  For best results, install the program.
z
OCRmyPDF requires '{program}' {need_version} or higher.  Your system appears
to have {found_version}.  Please update this program.
z
OCRmyPDF requires '{program}' {need_version} or higher when run with the
{required_for} arguments.  {program} {found_version} is installed.

If you omit these arguments, OCRmyPDF may be able to
proceed.  For best results, update the program.
zn
If you have homebrew installed, try these command to install the missing
package:
    brew install {package}
z
On systems with the aptitude package manager (Debian, Ubuntu), try these
commands:
    sudo apt update
    sudo apt install {package}

On RPM-based systems (Red Hat, Fedora), try this command:
    sudo dnf install {package}
z
If not already installed, install the Chocolatey package manager. Then use
a command prompt to install the missing package:
    choco install {package}
c                     [         R                  R                  S5      (       a  g[         R                  R                  S5      (       a  g[         R                  R                  S5      (       a  g[         R                  $ )Nfreebsdlinuxwinwindows)r?   r@   
startswith rI   r)   _get_platformr^      sT    
||y))		 	 	)	)		 	 	'	'<<rI   c                   A[        U[        5      (       a  UR                  [        5       U 5      n[        5       S:X  a2  [        R                  [        R                  " S0 [        5       D65        g [        5       S:X  a2  [        R                  [        R                  " S0 [        5       D65        g [        5       S:X  a2  [        R                  [        R                  " S0 [        5       D65        g g )NdarwinrY   r[   r]   )
isinstancer   rG   r^   rB   infoOSX_INSTALL_ADVICEformatlocalsLINUX_INSTALL_ADVICEWINDOWS_INSTALL_ADVICE)rH   packager#   s      r)   _error_trailerri     s    '7##++mow7("#**6VX67	G	#%,,8vx89	I	%'..::; 
&rI   c                p   U(       a2  [         R                  [        R                  " S0 [	        5       D65        OjU(       a2  [         R                  [        R                  " S0 [	        5       D65        O1[         R                  [        R                  " S0 [	        5       D65        [        S0 [	        5       D6  g Nr]   )	rB   warningMISSING_RECOMMEND_PROGRAMrd   re   errorMISSING_OPTIONAL_PROGRAMMISSING_PROGRAMri   )rH   rh   required_forrecommendeds       r)   _error_missing_programrs     sm     -44@vx@A			*11=FH=>		/((46845VXrI   c                    U(       a2  [         R                  [        R                  " S0 [	        5       D65        O1[         R                  [
        R                  " S0 [	        5       D65        [        S0 [	        5       D6  g rk   )rB   rn   OLD_VERSION_REQUIRED_FORrd   re   OLD_VERSIONri   )rH   rh   need_versionfound_versionrq   s        r)   _error_old_versionry     sM     		*11=FH=>		+$$0vx01VXrI   )rq   rr   version_parserc                   [        U[        5      (       d  U" U5      n U" 5       nU(       a7  Xs:  a2  [        X[        U5      [        U5      U5        U(       d  [        U 5      e[        R                  SX5        g! [        [        4 a)  n[	        XXE5        U(       d  [        U 5      Ue SnAgSnAf[
         a    [	        XXE5        U(       d  e  gf = f)a  Check for required version of external program and raise exception if not.

Args:
    program: The name of the program to test.
    package: The name of a software package that typically supplies this program.
        Usually the same as program.
    version_checker: A callable without arguments that retrieves the installed
        version of program.
    need_version: The minimum required version.
    required_for: The name of an argument of feature that requires this program.
    recommended: If this external program is recommended, instead of raising
        an exception, log a warning and allow execution to continue.
    version_parser: A class that should be used to parse and compare version
        numbers. Used when version numbers do not follow standard conventions.
NzFound %s %s)
ra   r   r   rO   rs   r   ry   r>   rB   r!   )	rH   rh   version_checkerrw   rq   rr   rz   rx   r(   s	            r)   check_external_programr}   *  s    2 lG,,%l3') 5c,/]1C\	
 (11IImW4%  12 wK(1q8! wK	s   A; ;CB//!CC)
r"   Argsr   OsEnviron | Noner   rF   r   rF   returnr   )
r"   r~   r4   zCallable[[str], None]r   rF   r   r   r   r   )r"   r~   r   r   r   z,tuple[Args, OsEnviron, logging.Logger, bool])rH   r>   rJ   r>   r   r   r   r>   )r   r>   )rH   r>   rh   zstr | Mapping[str, str]r   None)
rH   r>   rh   r>   rq   
str | Nonerr   rF   r   r   )rH   r>   rh   r>   rw   r>   rx   r>   rq   r   r   r   )rH   r>   rh   r>   r|   zCallable[[], Version]rw   zstr | Versionrq   r   rr   rF   rz   ztype[Version]r   r   )2__doc__
__future__r   r   r<   rQ   r?   collections.abcr   r   r   
contextlibr   pathlibr   
subprocessr	   r
   r   r   r   r   r   packaging.versionr   ocrmypdf.exceptionsr   	getLogger__name__rB   r>   r~   _Environ	OsEnvironr8   r   rV   rp   ro   rm   rv   ru   rc   rf   rg   r^   ri   rs   ry   r}   r]   rI   r)   <module>r      sF   + "  	 	 
 7 7   P P , % 6 !s
KK	 !"'/
/ 
/  	/
 / /l  $L
$L $$L 	$L
 
$L $LN(
(%(1(2 #
 22 2
 
2 	2j
  
    
<


.8
GK
	
  	
  
*  $$+/5/5 /5 +	/5
  /5 /5 /5 "/5 
/5rI   