
    wBf/                       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 ddl	m
Z
mZ ddlmZ ddlmZmZmZmZmZmZmZmZ ddlmZ dd	lmZ d
dlmZ ddlmZ erddlmZm Z   ee!      Z"dZ#dZ$d Z%d Z&d Z' ed      d        Z(d Z)d/dZ*d0dZ+d1dZ,d Z-d Z.d Z/d2dZ0d Z1 ejd                  d      Z3d3d Z4d! Z5d" Z6d# Z7d$ Z8d4d%Z9d& Z:d' Z;d( Z<d) Z=d* Z>d5d+Z?d, Z@d6d-ZAd. ZBy)7zCommon path utilities.    )annotationsN)	lru_cachereduce)
accumulatechain)	getLogger)abspathbasename
expanduser
expandvarsjoinnormcasesplitsplitext)TYPE_CHECKING)urlsplit   )
CondaError   on_win)IterableSequencez#\./|\.\.|~|/|[a-zA-Z]:[/\\]|\\\\|//).conda.tar.bz2z.jsonz.jlapz	.json.zstc                @    d| v ryt        j                  t        |       S )N://F)rematchPATH_MATCH_REGEX)values    1lib/python3.12/site-packages/conda/common/path.pyis_pathr#   1   s    ~88$e,,    c                <    t        t        t        |                   S N)r	   r   r   paths    r"   expandr)   7   s    :j./00r$   c                    t         r)t        t        |             t        t        |            k(  S t        |       t        |      k(  S )zR
    Examples:
        >>> paths_equal('/a/b/c', '/a/b/c/d/..')
        True

    )r   r   r	   )path1path2s     r"   paths_equalr-   ;   s7     '8GEN+CCCu~//r$   )maxsizec                J   t        |       r| S | j                  d      st        d| z        t        |       \  }}}}}ddlm}  ||      }|dvr|j                  d      sd|z   }||z   S d}t        j                  d	|t        j                        r|d   d
z   |dd z   }||z   S )zlConvert a file:// URL to a path.

    Relative file URLs (i.e. `file:relative/path`) are not supported.
    zfile://z9You can only turn absolute file: urls into paths (not %s)r   )percent_decode) 	localhostz	127.0.0.1z::1\\//r1   z^/([a-z])[:|]:   N)	r#   
startswithr   r   urlr0   r   r   I)r8   _netlocr(   r0   s        r"   url_to_pathr<   H   s     s|
>>)$G#M
 	
 %SMAvtQ#$D::  (F]F D=	 88OT24407S=48+DD=r$   c                :    t        d t        | |      D              S )Nc              3  ,   K   | ]  \  }}||k(    y wr&    ).0tsws      r"   	<genexpr>z'tokenized_startswith.<locals>.<genexpr>e   s     L51bqBwLs   )allzip)test_iterablestartswith_iterables     r"   tokenized_startswithrH   d   s    LC7J$KLLLr$   c           
     |    t        | D ch c]  }t        |j                  d      d d       ! c}dhz
        S c c}w )N/r?   )sortedtupler   )filesfs     r"   get_all_directoriesrP   h   s6    U;5cr*+;rdBCC;s   $9c                    t        |       }|syg fd}t        ||      }sj                  |d          n t        |d         sj                  |       t	        d D              S )Nr?   c                B    t        ||       sj                  |        |S r&   )rH   append)xyleavess     r"   _processz&get_leaf_directories.<locals>._processu   s    #Aq)MM!r$   rK   c              3  >   K   | ]  }d j                  |        yw)rJ   N)r   )r@   leafs     r"   rC   z'get_leaf_directories.<locals>.<genexpr>   s     3D$3s   )rP   r   rS   rH   rM   )rN   directoriesrW   lastrV   s       @r"   get_leaf_directoriesr\   l   sk     &e,KF
 (K(Dk"o&!$r
3d3F333r$   c                L    t        t        j                  d | D                    S )Nc              3  B   K   | ]  }|st        |t                y wr&   )r   r   )r@   	directorys     r"   rC   z&explode_directories.<locals>.<genexpr>   s      
,5iJy$'
s   )setr   from_iterable)child_directoriess    r"   explode_directoriesrc      s,      
9J
 	
 r$   c                    |j                  dd      }|j                  d      r| dz   S t        |       \  }}t        |      \  }}d| d| | dz   }|rdj	                  |d|      S |S )	a	  
    This must not return backslashes on Windows as that will break
    tests and leads to an eventual need to make url_to_path return
    backslashes too and that may end up changing files on disc or
    to the result of comparisons with the contents of them.
    .r1   2cz__pycache__/z	.cpython-z{}{}{}rJ   )replacer7   r   r   format)py_pathpython_major_minor_versionpyver_stringr_   py_filebasename_root	extensionpyc_files           r"   pyc_pathrq      s     .55c2>Ls#}"7^	7#+G#4 y]O9\N9+UV"WW 	 =Fxy#x8S8Sr$   c                Z     d D        } fd|D        }t        fd|D              }|S )Nc              3  D   K   | ]  }|j                  d       s|  yw)z.pyN)endswith)r@   rO   s     r"   rC   z$missing_pyc_files.<locals>.<genexpr>   s     6aAJJu$56s     c              3  :   K   | ]  }|t        |      f  y wr&   )rq   )r@   rm   rk   s     r"   rC   z$missing_pyc_files.<locals>.<genexpr>   s#      EL(7$>?@s   c              3  2   K   | ]  }|d    vs|  yw)r   Nr?   )r@   r   rN   s     r"   rC   z$missing_pyc_files.<locals>.<genexpr>   s     KUU1XU5J5Ks   )rM   )rk   rN   py_filespyc_matchesresults   ``   r"   missing_pyc_filesrz      s3    656HPXK KkKKFMr$   c                    | j                  dd      \  }}|j                  dd      \  }}|j                         |j                         |j                         }}}|||fS )Nr5   r   =)rsplitstrip)ep_definitioncmd_modfunccommandmodules        r"   parse_entry_point_defr      sX    !((a0MGTnnS!,OGV#MMOV\\^TZZ\TVGFD  r$   c                d    t         ry| rd| vrdj                  |       } t        dd| xs dz        S )Nz
python.exere   binzpython%sr1   )r   r   )python_versions    r"   get_python_short_pathr      s7    #^3.1z^%9r:;;r$   c                6    | y t         ryt        |       }d|z  S )NzLib/site-packageszlib/python%s/site-packages)r   get_major_minor_version)r   py_vers     r"   #get_python_site_packages_short_pathr      s&    	"(8+f44r$   z[0-9]+\.[0-9]+c                   t        | t              sJ | j                  d      r?| j                  d      d   }t	        d      }t	        |      |dz   k  ry ||   ||dz   d  f}n| j                  d      rL| j                  d      d   }t	        d      }t	        |      |dz   k  ry ||dz      dk(  sJ ||   ||dz   d  f}nt
        j                  |       }|r+|j                  d	      j                  d      }|d	   |d   f}nId
j                  | D cg c]  }|j                         s| c}      }t	        |      dk  ry |d	   |dd  f}|rdj                  |      S d
j                  |      S c c}w )Nz
lib/pythonrJ   r   pythonr   z
bin/pythonr6   re   r   r1   )

isinstancestrr7   r   len_VERSION_REGEXr   groupr   isdigit)	stringwith_dot	pythonstrstartmaj_minr   versionrg   digitss	            r"   r   r      s~    fc"""&LL%a(	Hy>EAI%E"Ieaik$::			<	(LL%a(	Hy>EAI%#s***E"Ieaik$::$$V,kk!n**3/Gaj'!*,GWW?A199;a?@F6{QQi+G (388G>bggg.>> @s   E/'E/c                     t         rdS dS )NScriptsr   r   r?   r$   r"   get_bin_directory_short_pathr      s    9)E)r$   c                6    t         r| j                  dd      S | S )NrJ   \r   rh   r'   s    r"   win_path_okr      s    &,4<<T"6$6r$   c                6    t         r| j                  dd      S | S )Nr   r3   r   r'   s    r"   win_path_double_escaper      s    )/4<<f%9T9r$   c                P    t        j                  dd|       j                  dd      S )Nz	(\\(?! ))rJ   z:////r   )r   subrh   r'   s    r"   win_path_backoutr      s$     66,d+33GUCCr$   c                D    | r| d   | d   cxk(  r|k(  r| S  n| S | |  | S )z

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

    r   rK   r?   )namepads     r"   
ensure_padr      s=     47d2h-#- .tfSE""r$   c                6    | xr | d   | d   cxk(  xr dk(  S c S )z

    Examples:
        >>> is_private_env_name("_conda")
        False
        >>> is_private_env_name("_conda_")
        True

    r   rK   r:   r?   )env_names    r"   is_private_env_namer     s'     :x|:s::::r$   c                X    | (t        |       \  }}t        |      dk7  ryt        |      S y)z

    Examples:
        >>> is_private_env_path('/some/path/to/envs/_conda_')
        True
        >>> is_private_env_path('/not/an/envs_dir/_conda_')
        False

    envsF)r   r
   r   )env_pathenvs_directoryr   s      r"   is_private_env_pathr     s6     #(? N#v-"8,,r$   c                j    | j                  t        j                        r| S | t        j                  z   S r&   )rt   ossepr'   s    r"   right_pad_os_sepr   /  s$    ==(4;dRVVm;r$   c                >    t        |       \  }}d|v r|xs d |fS | d fS )Nre   )r   )path_or_urldnfns      r"   split_filenamer   3  s/    ;FB"byBJ$A{D.AAr$   c                    | j                  d      r|}| j                  d|d      S | j                  d      rt               }| j                  d|d      S | S )Nzsite-packages/zsite-packagesr   zpython-scripts/zpython-scripts)r7   rh   r   )source_short_pathtarget_site_packages_short_pathsp_dirbin_dirs       r"   get_python_noarch_target_pathr   8  s^    ##$450 ((&!DD		%	%&7	8.0 (()97AFF  r$   c                X   | syddl m}  |d      }|r\t        j                  j	                  dt        j
                  j                  t        j
                  j                  |      d            }n t        j                  j	                  dd      }	 t        j                  |d| g      j                  d      j                  d	      d   } | S # t        $ rS}t        j                  d
|z  d       fd}d}t        j                   |||       j#                  dd      } Y d }~| S d }~ww xY w)Nr1   r   whichbashCYGPATHzcygpath.exez-upascii
z%rT)exc_infoc                    | j                  d      j                  dd      j                  dd      j                  dd      }dz   |z   S )Nr   r   rJ   r5   r1   r4   )r   rh   )
found_pathfoundroot_prefixs     r"   _translationz&win_path_to_unix.<locals>._translationa  sL      #s#b!s#	  $u,,r$   zT(?<![:/^a-zA-Z])([a-zA-Z]:[/\\]+(?:[^:*?"<>|]+[/\\]+)*[^:*?"<>|;/\\]+?(?![a-zA-Z]:))z;/z:/)shutilr   r   environgetr(   r   dirname
subprocesscheck_outputdecoder   	Exceptionlogdebugr   r   rh   )r(   r   r   r   cygpather   path_res    `      r"   win_path_to_unixr   C  s      =D**..rww||BGGOOD$9=I
 **..M:G##WeT$:;VG_U4[ 	* K!  G		$(T	*	- ovvg|T2::4FK!Gs   9C 	D)AD$$D)c                    ddl m}  ||       S )zIBackwards-compatibility wrapper. Use `shutil.which` directly if possible.r   r   )r   r   )
executabler   s     r"   r   r   o  s    r$   c                h    t         D ]&  }| j                  |      s| dt        |        |fc S  | dfS )af  
    Examples:
        >>> strip_pkg_extension("/path/_license-1.1-py27_1.tar.bz2")
        ('/path/_license-1.1-py27_1', '.tar.bz2')
        >>> strip_pkg_extension("/path/_license-1.1-py27_1.conda")
        ('/path/_license-1.1-py27_1', '.conda')
        >>> strip_pkg_extension("/path/_license-1.1-py27_1")
        ('/path/_license-1.1-py27_1', None)
    N)KNOWN_EXTENSIONSrt   r   )r(   ro   s     r"   strip_pkg_extensionr   v  sD     & 6	==#)3y>/*I556 :r$   c                &    | dd dk(  xs | dd dk(  S )z
    Examples:
        >>> is_package_file("/path/_license-1.1-py27_1.tar.bz2")
        True
        >>> is_package_file("/path/_license-1.1-py27_1.conda")
        True
        >>> is_package_file("/path/_license-1.1-py27_1")
        False
    iNr   ir   r?   r'   s    r"   is_package_filer     s%     9 ;DI$;;r$   )rN   Iterable[str]returnzlist[tuple[str]])rN   r   r   zSequence[str])rb   zIterable[tuple[str, ...]]r   zset[str]r&   )T)r:   )r1   )r(   r   )C__doc__
__future__r   r   r   r   	functoolsr   r   	itertoolsr   r   loggingr   os.pathr	   r
   r   r   r   r   r   r   typingr   urllib.parser   r1   r   compatr   r   r   __name__r   r    r   r#   r)   r-   r<   rH   rP   r\   rc   rq   rz   r   r   r   compiler   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r?   r$   r"   <module>r      s    " 	 	  ' ' 	 	 	 ! !  )  I -1
0 4 6MD40T&!<5 -.!?H*7:D#$
;$<B
!)X$<r$   