
    u*e9                        d 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
mZmZ ddlZddlmZ  ej        e          ZefZ G d de          Zdd	ZddZej                            ed          ej                            ej                            dd                    fZddZd Z	 ddZej         d             Z!d Z"ddZ#	 d dZ$d Z%	 d!dZ&d Z'dS )"zR
conda_build utils we need, without configuring logging as an import side effect.
    N)stat)isdirisfileislink)root_dirc                       e Zd ZdZdS )	LockErrorz(Raised when we failed to acquire a lock.N)__name__
__module____qualname____doc__     7lib/python3.11/site-packages/conda_index/utils_build.pyr	   r	       s        2222r   r	   Tc                 P    | g S t          | |          rt          |           S | gS )a  
    Ensure the object is a list. If not return it in a list.

    :param arg: Object to ensure is a list
    :type arg: any
    :param include_dict: Whether to treat `dict` as a `list`
    :type include_dict: bool, optional
    :return: `arg` as a `list`
    :rtype: list
    N)include_dict)islistlist)argr   s     r   ensure_listr   $   s6     {		,	/	/	/ Cyyur   Fc                    t          | t                    st          | d          sdS |st          | t                    rdS sdS du rCt	          |           } 	 t          t          |                     n# t          $ r Y dS w xY wfd	 t          fd| D                       S # t          t          f$ r Y dS w xY w)af  
    Check whether `arg` is a `list`. Optionally determine whether the list elements
    are all uniform.

    When checking for generic uniformity (`uniform=True`) we check to see if all
    elements are of the first element's type (`type(arg[0]) == type(arg[1])`). For
    any other kinds of uniformity checks are desired provide a uniformity function:

    .. code-block:: pycon
        # uniformity function checking if elements are str and not empty
        >>> truthy_str = lambda e: isinstance(e, str) and e
        >>> islist(["foo", "bar"], uniform=truthy_str)
        True
        >>> islist(["", "bar"], uniform=truthy_str)
        False
        >>> islist([0, "bar"], uniform=truthy_str)
        False

    .. note::
        Testing for uniformity will consume generators.

    :param arg: Object to ensure is a `list`
    :type arg: any
    :param uniform: Whether to check for uniform or uniformity function
    :type uniform: bool, function, optional
    :param include_dict: Whether to treat `dict` as a `list`
    :type include_dict: bool, optional
    :return: Whether `arg` is a `list`
    :rtype: bool
    __iter__FTc                 *    t          |           k    S N)type)eetypes    r   uniformzislist.<locals>.uniformj   s    77e##r   c              3   .   K   | ]} |          V  d S r   r   ).0r   r   s     r   	<genexpr>zislist.<locals>.<genexpr>n   s+      ++!771::++++++r   )
isinstancestrhasattrdictiterr   nextStopIterationall
ValueError	TypeError)r   r   r   r   s    ` @r   r   r   7   s   > #s 73
#;#; u jd33 u t $3ii	cOOEE 	 	 	44		$ 	$ 	$ 	$ 	$++++s++++++	"   uus$   A8 8
BBB* *B?>B?locks~z.conda_build_locks  c                    d }	 t           j                            t           j                            |                     }n# t          $ r | }Y nw xY w|}t          |d          r|                                }t          j        |          	                                }t          D ]}	 t           j                            |          st          j        |           t           j                            ||          }t          |d          5 }|                    d           d d d            n# 1 swxY w Y   t!          j        ||          } n,# t          $ r Y w xY wt%           dj        t                     |S )Nencodew z\Could not write locks folder to either system location ({})or user location ({}).  Aborting.)ospathabspathnormpathOSErrorr$   r0   hashlibsha256	hexdigest_lock_foldersr   makedirsjoinopenwritefilelockFileLockRuntimeErrorformat)	foldertimeoutfllocation
b_locationlock_filename	locks_dir	lock_filefs	            r   get_lockrM   }   s   	B7??27#3#3F#;#;<<   Jz8$$ )&&((
 N:..88::M" 
 
			7==++ 'I&&&Y>>Ii%%               "9g66BE 	 	 	H	 7 006G
 
 	
 IsH   <A AA)A#ED."E.D2	2E5D2	6E
EEc                 ^   || k    }t          |t                    rt          | t                    rt          j                            |           sZt          j                            t          j                            t          j                            ||                               } t          j                            |          sZt          j                            t          j                            t          j                            ||                              }|| |k    z  }|S r   )r"   string_typesr3   r4   isabsr5   r6   r=   )
base_valuevaluer4   
equivalents       r   _equivalentrT      s    *$J%&& *:j,+O+O *w}}Z(( 	  dJ!?!?@@ J w}}U## 	QGOOBG$4$4RW\\$5N5N$O$OPPEjE))
r   r2   c                    | |k    r| S |                                 D ]/\  }}|| v s|r"|                     ||          }t          |d          rt          |||||          }|| |<   Mt          |d          r|t	          |t
                    sg|r_||k    r0	 |                    |           n# t          t          f$ r |}Y nw xY w	 t          |          | |<   # t          $ r || |<   Y w xY w|| |<   |rD|rBt          |||          s1|r/t                              d                    |||                     |	|| v r| |= *|| |<   1| S )Nkeys)raise_on_clobberr   z3clobbering key {} (original value {}) with value {})itemsgetr$   merge_or_update_dictr"   r#   extendr+   AttributeErrorr   rT   logdebugrC   )	basenewr4   mergerW   add_missing_keyskeyrR   rQ   s	            r   rZ   rZ      s    s{{iikk $& $&
U$;;*;#u--Juf%% !&1tUEU  
 'S		
++ &Juc4J4J & &!U**/&--e4444 )>: / / /).JJJ//$($4$4S		$ / / /$.S			/ !&DII 

 (
E4@@
 )	
 IIMTTU   
 =SD[[S		 %DIKs$   B..CCCC-,C-c              #     K   t          j                     }t          j                     |z
  |k     rf	 | D ]}|                    d           	 nW# t          j        $ r | D ]}|                                 Y nw xY wt          j                     |z
  |k     ft          d          	 dV  | D ]}|                                 dS # | D ]}|                                 w xY w)a2  Try to acquire all locks.

    If any lock can't be immediately acquired, free all locks.
    If the timeout is reached withou acquiring all locks, free all locks and raise.

    http://stackoverflow.com/questions/9814008/multiple-mutex-locking-strategies-and-why-libraries-dont-use-address-comparison
    g?rE   zFailed to acquire all locksN)timeacquirer@   Timeoutreleaser	   )r,   rE   tlocks       r   try_acquire_locksrl      s+      		A
)++/G
#
#	 * *S))))*   	 	 	    	 )++/G
#
#0 5666 	 	DLLNNNN	 	E 	 	DLLNNNN	s   A (A98A9&C C c           	         d}t           j        t           j        t           j        fD ])}	  || |           d} n# t          t
          f$ r Y &w xY w|s	 t          j        d|  d| dt          j        t          j                   d S # t          j	        $ rB}t          j                            |          st	          d|  d| d|           Y d }~d S d }~ww xY wd S )	NFTzcp -a  )shellstderrstdoutzFailed to copy  to z.  Error was: )shutilcopy2copycopyfiler7   PermissionError
subprocess
check_callPIPECalledProcessErrorr3   r4   r   )srcdst	is_copiedfuncr   s        r   _copy_with_shell_fallbackr     sO   Iv{FO<  	DcNNNIE) 	 	 	H	 
Q		Q!$$$s$$!!	      , 	Q 	Q 	Q7>>#&& QOOOOOAOOPPPQ Q Q Q Q Q	Q
Q 
Qs'   9AA2B C7CCc                    t           j                                      s)t          j                   t	          j        |            t          j        |           }|r || |          fd|D             }d|v r|                    d           fd|D             }|sHt          |          D ]7\  }}t           j        	                    | |          }	||         }
|rt           j        
                    |	          rt           j                            |
          rt          j        |
           t          j        t          j        |	          |
           	 t          j        |	          }t          j        |j                  }t          j        |
|           #  Y xY wt           j                            |	          rt)          |	|
||           't+          |	|
           9|S )Nc                     g | ]}|v|	S r   r   )r    xexcls     r   
<listcomp>zcopytree.<locals>.<listcomp>$  s    ///Q$qr   z.conda_lockc                 P    g | ]"}t           j                            |          #S r   )r3   r4   r=   )r    itemr}   s     r   r   zcopytree.<locals>.<listcomp>*  s)    7774rw||C&&777r   )r3   r4   existsr<   rs   copystatlistdirremove	enumerater=   r   lexistssymlinkreadlinklstatr   S_IMODEst_modelchmodr   copytreer   )r|   r}   symlinksignoredry_runlstdst_lstidxr   sdstmoder   s    `           @r   r   r     s   7>># "
CS!!!
*S//C 0vc3////#/// 

=!!!77773777G 0"3 	0 	0ICS$''AA 0BGNN1-- 07??1%% !IaLLL
2;q>>1---!B<
33DIa&&&&Dq!! 0Ax0000)!Q////Ns   AF!!F%c                    t           j                            t           j                            |                    }t           j                            t           j                            |                     } |                    |           rJ d                    | |                      t          | ||d          }d |D             }|r,|s*t          d                    | ||d                             g }	|r|st          | |          }|g}	t          |	|          5  t          | ||           d	d	d	           d	S # 1 swxY w Y   d	S )
z
    Merge src into dst recursively by copying all files from src into dst.
    Return a list of all files copied.

    Like copytree(src, dst), but raises an error if merging the two trees
    would overwrite any files.
    zyCan't merge/copy source into subdirectory of itself.  Please create separate spaces for these things.
  src: {}
  dst: {}T)r   r   c                 0    g | ]}t          |          |S r   )r   )r    rL   s     r   r   zmerge_tree.<locals>.<listcomp>V  s#    222aq		2222r   z'Can't merge {} into {}: file exists: {}r   re   )r   N)
r3   r4   r6   normcase
startswithrC   r   r7   rM   rl   )
r|   r}   r   rE   rk   lockingclobber	new_filesexistingr,   s
             r   
merge_treer   B  s    '

27++C00
1
1C
'

27++C00
1
1C~~c""  	 F3$$	   cHdCCCI229222H 
 
8??S(ST+VV
 
 	
 E  	2C111D	5'	*	* . .cH----. . . . . . . . . . . . . . . . . .s   #EE
Ec                 :   |                      t          j        j                  }|                     t          j        j                  }|r,|d         |d         k    r|d= |d= |r|d         |d         k    t          j        j        | t          j        j        | fS )N)splitr3   r4   sepr=   )r|   r}   ssplitdsplits       r   get_prefix_replacement_pathsr   f  s    YYrw{##FYYrw{##F
 VBZ6":--2J2J  VBZ6":-- 7< "',"777r   c           	         |r%t          |           r	 t          j        t          j                            |                     n# t
          $ r Y nw xY wt          j                            |          rt          j        |           t          | |          \  }}t          j	        |           }	|	
                    ||          }
t          j        |
|           	 t          j        |           }t          j        |j                  }t          j        ||           dS #  Y dS xY wt#          |           rt%          | ||||||           dS t#          |          r>t          j                            |t          j                            |                     }n|}t          j                            |           r t          j                            |           }nyt          j        j        |v rSt          j                            |          }t          j                            |          st          j        |           nt          j                    }t          j                             |           rYt          j                            t          j                            |                     st4                              d|            dS |s|rt9          ||          }|r|gng }t;          ||          5  t          j                            |          }|rEt          j                            |          s&	 t          j        |           n# t
          $ r Y nw xY w	 t=          | |           nN# t>          j         $ r< t4          !                    dt          j                            |           |           Y nw xY wddd           dS # 1 swxY w Y   dS )z>Copy all the files and directories in src to the directory dst)rE   rk   r   r   z+path %s is a broken symlink - ignoring copyNre   z"skipping %s - already exists in %s)"r   r3   r<   r4   dirnamer7   r   r   r   r   replacer   r   r   r   r   r   r   r   r=   basenamerP   r   getcwdr   realpathr]   warnrM   rl   r   rs   Errorr^   )r|   r}   rE   r   rk   r   r   src_basedst_base
src_targetsrc_replacedr   r   dst_fn
src_folderr,   
dst_folders                    r   	copy_intor   o  s   
  @F3KK @	K,,---- 	 	 	D	7??3 	IcNNN9#sCC([%%
!))(H==

<%%%	#B<
++DIc4     	DD	s /	
 	
 	
 	
 	
 	
 :: 	W\\#rw'7'7'<'<==FFF7== 	)--JJw{f$$W__V44
w}}Z00 ,K
+++Y[[
7>># 	rw~~bg6F6Fs6K6K'L'L 	HHBCHHHF 	9 	9J888D!)rug.. 	 	--J "'.."<"< K
++++   D)#v6666<   		8"':J:J3:O:OQT    	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	sz   1A 
AAAD! !D&;AO=MO
MOMO#M43O4AN?<O>N??OOOc           	         	 t          j        | |           d S # t          $ r\ 	 t          | |           t	          j        |            Y d S # t          $ r& t                              d|  d| d           Y Y d S w xY ww xY w)Nz Failed to copy/remove path from rr   z due to permission error)rs   moverw   r   r3   unlinkr]   r^   )r|   r}   s     r   move_with_fallbackr     s    
C   	c3IcNNNNNN 	 	 	IIY3YYCYYY      		s&    
A?$A+A;6A?:A;;A?)T)FT)r.   )r2   TFT)FNF)Fr.   NTF)r.   FNTF)(r   
contextlibr8   loggingr3   rs   rx   rf   r   os.pathr   r   r   r@   conda.exportsr   	getLoggerr
   r]   r#   rO   	Exceptionr	   r   r   r4   r=   
expanduserr;   rM   rT   rZ   contextmanagerrl   r   r   r   r   r   r   r   r   r   <module>r      s:          				             ) ) ) ) ) ) ) ) ) )  " " " " " "g!! v3 3 3 3 3	 3 3 3   &: : : :B GLL7##Grw||C)=>>??   @
 
 
 NR+ + + +\ ' ' 'ZQ Q Q." " " "L MR!. !. !. !.H8 8 8 MRE E E EP    r   