
    3i\!                    Z   U d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlmZ ddlmZ ddlmZ ddlmZ ddl	mZmZ dd	lmZmZ d
dlmZ d
dlmZ erddlmZmZ d
dlm Z m!Z!  e       Z"de#d<    e       e" ee
jH                        <    ejJ                  e&      Z'de#d<   	 d&ddd	 	 	 	 	 	 	 	 	 	 	 	 	 d'dZ(dd	 	 	 	 	 	 	 	 	 	 	 d(dZ)	 	 	 	 	 	 	 	 	 	 	 	 d)dZ*	 	 	 	 	 	 	 	 d*dZ+dZ,de#d<   d+dZ-ed,d       Z.d-d Z/	 	 	 	 	 	 	 	 d.d!Z0 G d" d#      Z1d/d$Z2g d%Z3y)0zQAcquire Python information via subprocess interrogation with multi-level caching.    )annotationsN)OrderedDict)contextmanager)Path)quote)PopenTimeoutExpired)TYPE_CHECKINGFinal   )	NoOpCache)
PythonInfo)	GeneratorMapping)ContentStorePyInfoCachez)OrderedDict[Path, PythonInfo | Exception]_CACHEzFinal[logging.Logger]_LOGGERTF)raise_on_errorignore_cachec                   |t         j                  n|}t        | ||||      }t        |t              r|r|t
        j                  d|       d }|S )Nr   z%s)osenviron_get_from_cache
isinstance	Exceptionr   info)clscacheexeenvr   r   results          S/var/www/opsc/venv/lib/python3.12/site-packages/python_discovery/_cached_py_info.pyfrom_exer%   #   sO     "**CS%cMF&)$LT6"M    r   c                   t        |      }|s|t        v r
t        |   }nt        | ||||      }|x}t        |<   t        |t              r||_        |S N)r   r   _get_via_file_cacher   r   
executable)r   r    r!   r"   r   exe_pathr#   py_infos           r$   r   r   6   sY     CyHH.!%c5(CE$++!&*%Mr&   c                6   t        |      }	 |j                         j                  }t	        t	        t
              j                               j                  dz  }	 t        j                  |j                               j                         }||n	t               }	d }
|	j                  |      }|j                         5  |j                         r|j!                         x}|j#                  d      |j#                  d      }}|j#                  d      |j#                  d      }}||k(  r(||k(  r#||k(  rt%        |t&              rt)        | ||      }
n|j+                          |
lt-        | ||      \  }}
|'t.        j1                  d||       t-        | ||      \  }}
||cd d d        S |
$|j3                  |||
j5                         |d       d d d        |
| d	}t7        |      S |
S # t        $ r d}Y w xY w# t        $ r d }Y w xY w# 1 sw Y   AxY w)
N_py_info.pypathst_mtimecontenthashz5first subprocess attempt failed for %s (%s), retrying)r1   r0   r2   r3   z# failed to produce interpreter info)strstatr1   OSErrorr   __file__resolveparenthashlibsha256
read_bytes	hexdigestr   r,   lockedexistsreadgetr   dict_load_cached_py_inforemove_run_subprocessr   debugwriteto_dictRuntimeError)r   r    r0   r!   r"   	path_textpath_modifiedpy_info_scriptpy_info_hashresolved_cacher,   py_info_storedataof_pathof_st_mtime
of_contentof_hashfailuremsgs                      r$   r)   r)   I   s    D	I		,, $x.0023::]JN#*>>.2K2K2M#N#X#X#Z $/UY[N!%G"**40M				 !}/A/A/C'Ct&P#'88F#3TXXj5I[G"&((9"5txx7GJ9$=0|+z40.sM:N$$&?.sC=GW"UWZ\cd#23S#A "' ( "## -%&0(	% +6 89C  NS  
   s6   G+ 1G= =C"H)&H+G:9G:=HHHc                    	 | j                  |j                               }|j
                  x}*t        |      j                         s|j	                          y |S # t        t        f$ r |j	                          Y y w xY wr(   )	from_dictcopyKeyError	TypeErrorrD   system_executabler   r?   )r   rO   r2   r,   sys_exes        r$   rC   rC      sv    
--/ ,,,9$w-BVBVBXN i  s   A A=<A=    z
Final[int]COOKIE_LENGTHc                 :    t        j                  t        dz        S )N   )secrets	token_hexr_    r&   r$   
gen_cookiere      s    ]a/00r&   c               #    K   t        t        t              j                               j                  dz  } | j	                         r|  y t        j                  t        xs t        d      }|d}t        |      t        j                  d      \  }}	 t        j                  ||       t        j                  |       t        |       t        |      j                          y # t        |      j                          w xY ww)Nr/   z6cannot locate _py_info.py for subprocess interrogationz.py)suffix)r   r7   r8   r9   is_filepkgutilget_data__package____name__FileNotFoundErrortempfilemkstempr   rG   closeunlink)rL   rP   rV   fdtmps        r$   _resolve_py_info_scriptrt      s     $x.0023::]JN 7xG<JC#C((""%0C	HHRHHRLs)OIDIs   BD8C( D(DDc                J   | }| j                  |ddd         }|dkD  r2| d| x}rt        j                  j                  |       | |t        z   d } | j                  |ddd         }|dkD  r2| |t        z   d x}rt        j                  j                  |       | d| } | |||fS )z]Extract payload between reversed cookie markers, forwarding any surrounding output to stdout.Nr.   )findsysstdoutrG   r_   )outstart_cookie
end_cookieraw_out
out_starts
pre_cookieout_endspost_cookies           r$   _extract_between_cookiesr      s    G,tt,-JB[j)):)JJZ(*},./xx
4R4()H"}h6899;9JJ[))8nX--r&   c                   t               }t               }t        |j                  dd            }t               5 }|t	        |      ||g}t        |      }|j                  dd        d|d<   t        j                  dt        |             	 t        |dt        j                  t        j                  t        j                  |dd	
      }|j                  |      \  }	}
|j                  }d d d        dk7  r)| d| 	rd|	nd 
rd|
nd }t)        d|       d fS t+        	||      \  }	}}}	 | j-                  |	      }||_        d |fS # t        $ r) j!                          |j                          d\  }	}
}Y t"        $ r$}d|j$                  |j&                  }}
}	Y d }~d }~ww xY w# 1 sw Y   xY w# t0        j2                  $ rs}t        j5                  d|t7        |      |dkD  rdnd|dkD  rdndt7        |	      |	r|	d d nd       | d| d
rd|
nd }t)        |      }||_        |d fcY d }~S d }~ww xY w)NPY_DISCOVERY_TIMEOUT15__PYVENV_LAUNCHER__1
PYTHONUTF8z get interpreter info via cmd: %sTzutf-8backslashreplace)universal_newlinesstdinstderrrx   r"   encodingerrors)timeout) z	timed outr.   r   r   z with code z out: z err: zfailed to query ztsubprocess %s returned invalid JSON; raw stdout %d chars, start cookie %s, end cookie %s, parsed output %d chars: %rr.   foundmissing   z<empty>z" returned invalid JSON (exit code )z
, stderr: )re   floatrA   rt   r4   rB   popr   rF   LogCmdr   
subprocessPIPEcommunicate
returncoder	   killr6   strerrorerrnorI   r   	from_jsonr*   jsonJSONDecodeErrorwarninglen	__cause__)r   r!   r"   rz   r{   r   rL   cmdprocessry   errcodeos_errorrV   r|   r}   r   r#   excrU   s                       r$   rE   rE      sp   
 <LJCGG2D9:G	 	" CnC'zB3i%t,L8&+F	C#' oo!! )	G **7*;HC%%D%C2 qy[3w'7B&G\_&QTPWHXegGhi.se45t;;)A#|U_)`&C*hs#  <9  	1LLN!0NCd 	C!2!2HNNdC	C/C C@  )L!BGI"}G)HCI)		
 7vQWZC7?S`b>cds#}s\   AF/AE3F; /F,>F/ F,F'"F/'F,,F//F8;IA(H<6I<Ic                      e Zd ZdddZddZy)r   Nc                     || _         || _        y r(   )r   r"   )selfr   r"   s      r$   __init__zLogCmd.__init__   s    r&   c                    dj                  d | j                  D              }| j                  | d| j                  }|S )N c              3  D   K   | ]  }t        t        |              y wr(   )r   r4   ).0cs     r$   	<genexpr>z"LogCmd.__repr__.<locals>.<genexpr>   s     <aE#a&M<s    z env of )joinr   r"   )r   cmd_reprs     r$   __repr__zLogCmd.__repr__   s=    88<488<<88"8DHH<8Hr&   r(   )r   z	list[str]r"   Mapping[str, str] | NonereturnNoner   r4   )rl   
__module____qualname__r   r   rd   r&   r$   r   r      s    r&   r   c                L    | j                          t        j                          y r(   )py_info_clearr   clear)r    s    r$   r   r      s    	
LLNr&   )r   r   r%   r(   )r   type[PythonInfo]r    PyInfoCache | Noner!   r4   r"   r   r   boolr   r   r   PythonInfo | None)r   r   r    r   r!   r4   r"   Mapping[str, str]r   r   r   PythonInfo | Exception)r   r   r    r   r0   r   r!   r4   r"   r   r   r   )r   r   rO   r   r2   rB   r   r   r   )r   zGenerator[Path])ry   r4   rz   r4   r{   r4   r   ztuple[str, str, int, int])r   r   r!   r4   r"   r   r   z*tuple[Exception | None, PythonInfo | None])r    r   r   r   )4__doc__
__future__r   r:   r   loggingr   ri   rb   r   rw   rn   collectionsr   
contextlibr   pathlibr   shlexr   r   r	   typingr
   r   _cacher   _py_infor   collections.abcr   r   r   r   r   __annotations__r*   	getLoggerrl   r   r%   r   r)   rC   r_   re   rt   r   rE   r   r   __all__rd   r&   r$   <module>r      s   W "    	    
  # %   , '   21 5@M1 A)|tCNN !2!2!28!<	 < %)	  	 
 
"	   2 	 
 
	  &3	33 3 
	3
 
3 3l	  	  z 1  $. 7	7	7 
7 0	7t	 	
r&   