
    9icF                       S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKJs  J	r
  S SKJr  S SK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JrJrJr  S SKJr  \" \5      r \\S	   \S
   4   r!\\S   \S   4   r"S!S"S jjr#\#" S	5      r$\#" S
5      r%S#S jr&S$S jr'S%S&S jjr( " S S5      r) " S S5      r*S'S(S jjr+S)S jr,      S*S jr-S+S jr.\" SS9S,S j5       r/S-S jr0S%S.S jjr1S/S jr2   S0     S1S jjr3S2S3S  jjr4g)4    )annotationsN)suppress)	lru_cachewraps)	getLogger)Path)AnyCallableIterableLiteralMappingOptionalSequenceUnion)	normalizetargetbaseusersystemc                H   U S:H  nSnS[         R                  ;   a  [        [        5         SSKJn  SSS5        U(       ai  [        R                  R                  S5      nU(       a  U$ U(       a  UR                  $ [        R                  R                  S[         R                  5      $ [        R                  R                  S5      nU(       a  U$ U(       a  UR                  $ [        R                  R                  S	[         R                  5      $ ! , (       d  f       N= f)
a  
The prefixes in menuinst need to be handled with care.

Conda installations that require superuser permissions need elevation for
the creation of shortcuts. Constructor will leave a sentinel file to signal
this. If a file `.nonadmin` is present in the 'base' environment (or root of
the installation directory if conda is not present), superuser access is not
needed.

In order to check for this file, menuinst needs to track 'base_prefix'. For
a regular 'conda' process, this should be `conda.base.context.context.root_prefix'.
However, constructor also relies on a pyinstaller-frozen conda installation,
'conda-standalone'. In these cases, 'sys.prefix' is set to temporary location
of the extracted contents of the executable -- that's NOT the base installation!

For those reasons, we handle the default prefix with 'sys.prefix' (or 'sys.base_prefix')
as a last resort. The logic is:

- If 'MENUINST_PREFIX' (or 'MENUINST_BASE_PREFIX') is an env var with a set value, use that.
- If are already using conda, we get the context object and use those values.
- If CONDA_PREFIX (or 'CONDA_ROOT_PREFIX') are available, use those
- Last resort: use sys.prefix and sys.base_prefix

This helps us pass a lot of CLI arguments back and forth.
r   Ncondar   )contextMENUINST_BASE_PREFIXCONDA_ROOT_PREFIXMENUINST_PREFIXCONDA_PREFIX)sysmodulesr   ImportErrorconda.base.contextr   osenvirongetroot_prefixbase_prefixtarget_prefixprefix)whichr   r   r'   s       .lib/python3.13/site-packages/menuinst/utils.py_default_prefixr*      s    4 F?DG#++k"2 #  67M&&&zz~~13??CCZZ^^-.F$$$::>>.#**55! #"s   D
D!c                    [        SU 5      R                  SS5      R                  S5      n [        R                  " SSU 5      R                  5       R                  5       n [        R                  " SSU 5      $ )z
Convert to ASCII, and spaces and underscores to hyphens.
Remove characters that aren't alphanumerics, or hyphens.
Convert to lowercase. Also strip leading and trailing whitespace.
NFKDasciiignorez[^\w\s-] z[_\-\s]+-)r   encodedecoderesubstriplower)texts    r)   slugifyr8   I   s_    : VT"))'8<CCGLD66+r4(..0668D66+sD))    c                :   SnSX-  -   n[        U 5      (       a  U R                  (       a  U R                  R                  5       (       d	  X2-   U l        U  HL  n[        XAS-   5        UR                  (       a!  UR                  R                  5       (       a  MC  X2-   Ul        MN     WR                  (       a  UR                  R                  5       (       d  X4l        ggU(       a8  U R                  (       a  U R                  R                  5       (       d  X0l        ggg)zJ
adds whitespace to the tree, so that it results in a pretty printed tree
z    
   N)lenr7   r5   indent_xml_treetail)elemlevelindentationbase_indentationes        r)   r>   r>   k   s     Ke11
4yyyy		 1 1(6DIAAqy)66)7  vvQVV\\^^%F , $))499??+<+<(I ,=5r9   c                D    [         R                  " X5      nUb  X#l        U$ )zY
Add a child element of specified tag type to parent.
The new child element is returned.
)XMLTree
SubElementr7   )parenttagr7   r@   s       r)   add_xml_childrJ      s$    
 f*D	Kr9   c                  T    \ rS rSr\SS j5       r\SS j5       r\S	S
S jj5       rSrg)WinLex   c           	       ^  [        U5      S:  a  SUS   R                  5       ;   d  SUS   R                  5       ;   a  US   R                  5       S:X  d  US   R                  5       S:X  aV  [        S USS   5       5      (       a<  T R                  US   S	5      US   S
SR	                  U 4S jUSS   5       5      -  /nU$ U Vs/ s H  nT R                  U5      PM     nnU$ s  snf )N   zCMD.EXEr   z	%COMSPEC%r<   z/Kz/Cc              3  ,   #    U  H
  nS U;   v   M     g7f) N ).0args     r)   	<genexpr>$WinLex.quote_args.<locals>.<genexpr>   s     3(3C3J(s   ""%s"rQ   c              3  H   >#    U  H  nTR                  US 5      v   M     g7f)rW   N)
ensure_pad)rS   rT   clss     r)   rU   rV      s     "Px3>>#s#;#;xs   ")r=   upperanyrZ   joinquote_string)r[   argsrT   s   `  r)   
quote_argsWinLex.quote_args   s     IMd1gmmo-Q1OaD(DGMMOt,C3$qr(333 tAw,Q#(("PtABx"PPQD  6::TcC$$S)TD: ;s   C&c                `    UR                  S5      nUS   S;   a  U$ SU;   d  SU;   a  SU-  $ U$ )z
quotes a string if necessary.
rW   r   )r0   rQ   rQ   /rX   )r5   )r[   ss     r)   r_   WinLex.quote_string   s>     GGCLQ4:H!8saxA:r9   c                Z    U(       a  US   US   s=:X  a  U:X  a   U$   OU$ U< U< U< 3$ )z7

Examples:
    >>> ensure_pad('conda')
    '_conda_'

r   rR   )r[   namepads      r)   rZ   WinLex.ensure_pad   s7     tAw$r(1c1K 2K"D#..r9   rR   N)r`   Sequence[str])re   rl   )_)ri   strrj   rn   )	__name__
__module____qualname____firstlineno__classmethodra   r_   rZ   __static_attributes__rR   r9   r)   rL   rL      s<     &   / /r9   rL   c                  <    \ rS rSr\SS j5       r\SS j5       rSrg)UnixLex   c                N    U Vs/ s H  o R                  U5      PM     sn$ s  snf N)r_   )r[   r`   as      r)   ra   UnixLex.quote_args   s#    -12T  #T222s   "c                |    [         R                  " U5      nUR                  S5      (       a  SU;  a	  SUSS  S3nU$ )N'rW   r<   rh   )shlexquote
startswith)r[   re   quoteds      r)   r_   UnixLex.quote_string   s@    QS!!c&7"a(Fr9   rR   N)r`   rl   returnrl   )re   rn   r   rn   )ro   rp   rq   rr   rs   ra   r_   rt   rR   r9   r)   rv   rv      s(    3 3  r9   rv   c                r     [         R                  " U 5        g ! [         a  nU(       d  Ue S nAg S nAff = fry   )r!   unlinkFileNotFoundError)path
missing_okexcs      r)   r   r      s/    
		$ I s    
6	16c                D    [        [        5      R                  nUS-  U -  $ )Ndata)r   __file__rH   )r   heres     r)   	data_pathr      s     >  D&=4r9   c                    U R                  5       nU Hc  nUR                  5        HL  u  pEXB;   a>  [        X$   [        5      (       a'  [        U[        5      (       a  [	        X$   U5      X$'   MH  XRU'   MN     Me     U$ ry   )copyitems
isinstancedictdeep_update)mappingupdating_mappingsupdated_mappingupdating_mappingkvs         r)   r   r      st    0 llnO-$**,DA$1488q$''%01CQ%G"%&" - . r9   c                T   [        5       (       a  g[        U S5      R                  5       (       a  g [        U S5      R                  5         g! [         a  n[
        R                  SXS9   SnAOSnAff = fX:X  a  g[        US5      R                  5       (       a  g[        R                  S:X  a  g[        R                  S:X  aG   [        US5      R                  5         g! [         a  n[
        R                  SXS9   SnAgSnAff = f[        S	[        R                   35      e)
z=
Checks if the current installation needs admin permissions.
Fz	.nonadminz%Attempt to write %s/.nonadmin failed.exc_infoNTntposixUnsupported operating system: )
user_is_adminr   existstouch	Exceptionloggerdebugr!   ri   RuntimeError)r&   r%   r   s      r)   needs_adminr      s    M;'..00 []K(..0 [<mZ[ #
 K%,,..	ww$	G		k*002
 	  	LL@-L^	 ;BGG9EFFs/   A 
A5A00A5C# #
D-DDr<   )maxsizec                     [         R                  S:X  a  SSKJn   [	        U " 5       5      $ [         R                  S:X  a  [         R
                  " 5       S:H  $ [        S[         R                   35      e)Nr   r<   isUserAdminr   r   r   )r!   ri   platforms.win_utils.win_elevater   boolgetuidr   r   s    r)   r   r   *  sQ    	ww$@KM""	G	yy{a;BGG9EFFr9   c                    [         R                  S:X  a  SSKJn  U" U 5      $ [         R                  S:X  a  [        R
                  " S/U Q5      $ [        S[         R                   35      e)z=
Rerun this command in a new process with admin permissions.
r   r<   )
runAsAdminr   sudor   )r!   ri   r   r   
subprocesscallr   )argvr   s     r)   run_as_adminr   7  sU     
ww$?$	G	//;BGG9EFFr9   c                *   [        U =(       d    [        5      n [        [        SS5      (       aU  [        R
                  S:X  a  U S-  nOU S-  S-  nUR                  5       (       a  [        U5      4$ [        R                  S4$ [        R                  4$ )NfrozenFr   z
python.exebinpython)	r   DEFAULT_BASE_PREFIXgetattrr   r!   ri   is_filern   
executable)r%   base_prefix_pythons     r)   python_executabler   E  s    {9&9:K sHe$$77d?!,|!;!,u!4x!? %%''*+-- ))NNr9   c                D   ^  [        T 5      SSS.   SU 4S jjj5       nU$ )a  
Multiplatform decorator to run a function as a superuser, if needed.

This depends on the presence of a `.nonadmin` file in the installation root.
This is usually planted by the `constructor` installer if the installation
process didn't need superuser permissions.

In the absence of this file, we assume that we will need superuser
permissions, so we try to run the decorated function as a superuser.
If that fails (the user rejects the request or doesn't have permissions
to accept it), we'll try to run it as a normal user.

NOTE: Only functions that return None should be decorated. The function
will run in a separate process, so we won't be able to capture the return
value anyway.
N)r&   r%   c                  > UR                  SS 5        U =(       d    [        n U=(       d    [        n[        X5      (       GaT  [        R                  R                  S5      S:w  Ga0   TR                  S:X  a%  STR                   S[         STR                   S3nOS	TR                   S
TR                   S3nSR                  [        R                  R                  5        VVs/ s H&  u  pVUR                  S5      (       d  M  SU SU S3PM(     snn5      n/ [        5       QSPSU SU TR                   SU< SU < SU< SU< S3Pn[        R                  SU5        [        U5      n	[        R                  R                  SS 5        U	S:X  a  g O[%        5       (       a  T" UU USS.UD6$ T" UU USS.UD6$ s  snnf ! [          a  n
[        R#                  SU
S9   S n
A
N5S n
A
ff = f)N_mode_MENUINST_RECURSING1__main__zimport runpy;z = runpy.run_path('z')['z'];zfrom z import ;)CONDA_CONSTRUCTOR_	MENUINST_zos.environ.setdefault('z', 'z')z-cz<import os;os.environ.setdefault('_MENUINST_RECURSING', '1');z(*z,target_prefix=z,base_prefix=z,_mode='system',**)zElevating command: %sr   z,Elevation failed! Falling back to user mode.r   r   )r&   r%   r   r   )popr   r   r!   r"   r#   rp   ro   r   r^   r   r   r   r   r   r   r   warningr   )r&   r%   r`   kwargsimport_funcr   r   env_varscmdreturn_coder   funcs              r)   wrapper_elevate*elevate_as_needed.<locals>.wrapper_elevaten  s0    	

7D!%<)<!8%833

45<&??j0'==/)<XJ G!]]O30   %*$//):(4==/QR"SK88 ')jj&6&6&8&8FQ<<(OP ?1!D2>&8&(Ij"m}}o &x  %%2$5 6##./ 2

 4c:*3/ 

4d;!# $__  ,'
     (#

 
 	
O.  ]MX[\]s2   (A>G &F>
F>
AG >G 
G,G''G,)r&   os.PathLike | Noner%   r   )r   )r   r   s   ` r)   elevate_as_neededr   \  sB    $ 4[ -1*.F
)F
 (F
 F
P r9   c           
     n   [         R                  S:X  a7  U(       a!  [         R                  R                  US5      nOSn[	        US5      nO[
        R                  n[        S[        5       S[         R                  R                  SS5      SUUS	9  [         R                  S:X  a  UR                  5         g g )
Nr   z_test_output.txtrz   zuser_is_admin():zenv_var:MENUINST_TESTzN/Az_mode:)file)r!   ri   r   r^   openr   stdoutprintr   r"   r#   close)r&   r%   r   outputouts        r)   _test_elevationr     s    
 
ww$WW\\+/ABF'F63jj	


. 
ww$		 r9   c                L   [         R                  " U 4SSS.UD6nU(       ak  [        R                  SUR                  UR
                  5        [        R                  SUR                  5        [        R                  SUR                  5        U(       a  UR                  5         U$ )NT)capture_outputr7   z%s returned %dz
stdout:
%sz
stderr:
%s)	r   runr   r   r`   
returncoder   stderrcheck_returncode)r`   checklogr   processs        r)   
logged_runr     sr    nnTL$TLVLG
%w||W5G5GH]GNN3]GNN3  "Nr9   )r   )r(   _TargetOrBaser   rn   )r7   rn   )r   ry   )rH   zXMLTree.ElementrI   rn   r7   zOptional[str])F)r   str | os.PathLiker   r   )r   r   r   r   )r   Mapping[str, Any]r   zIterable[Mapping]r   r   )r&   os.PathLiker%   r   r   r   )r   r   )r   rl   r   int)r%   Optional[os.PathLike]r   rl   )r   r
   r   r
   )NNr   )r&   r   r%   r   r   _UserOrSystem)FT)r   zsubprocess.CompletedProcess)5
__future__r   r!   r3   r~   r   r   xml.etree.ElementTreeetreeElementTreerF   
contextlibr   	functoolsr   r   loggingr   pathlibr   typingr	   r
   r   r   r   r   r   r   unicodedatar   ro   r   r   r   r*   DEFAULT_PREFIXr   r8   r>   rJ   rL   rv   r   r   r   r   r   r   r   r   r   r   rR   r9   r)   <module>r      s<   " 	 	   
 ' '  &   W W W !	8	gh'89gfowx'889-6` !*%f- *D)(// //d
 
 
##4E##L+G\ 1	G 	GG.[~ ,0)-!(& 4r9   