o
    6‘ðiµ_  ã                   @  sJ  d dl mZ d dl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mZ d dlmZ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mZmZ d dl m!Z!m"Z" d dl#m$Z$ ee%ƒZ&d/dd„Z'd0dd„Z(e(d1dd„ƒZ)d2d"d#„Z*d2d$d%„Z+d3d'd(„Z,G d)d*„ d*ƒZ-G d+d,„ d,ƒZ.G d-d.„ d.ƒZ/dS )4é    )ÚannotationsN)Ú	GeneratorÚIterable)ÚAnyÚCallable)ÚLegacyDistutilsInstallÚUninstallMissingRecord)Úget_bin_prefixÚget_bin_user)ÚBaseDistribution)ÚWINDOWS)Úegg_link_path_from_location)Ú	getLoggerÚ
indent_log)ÚaskÚnormalize_pathÚrenamesÚrmtree)ÚAdjacentTempDirectoryÚTempDirectory)Úrunning_under_virtualenvÚbin_dirÚstrÚscript_nameÚis_guiÚboolÚreturnúGenerator[str, None, None]c                 c  sZ   � t j | |¡}|V  tsdS |› d�V  |› d�V  |r%|› d�V  dS |› d�V  dS )z‘Create the fully qualified name of the files created by
    {console,gui}_scripts for the given ``dist``.
    Returns the list of file names
    Nz.exez.exe.manifestz-script.pywz
-script.py)ÚosÚpathÚjoinr   )r   r   r   Úexe_name© r"   ú^/root/parts/websockify/install/lib/python3.10/site-packages/pip/_internal/req/req_uninstall.pyÚ_script_names   s   €r$   Úfnú)Callable[..., Generator[Any, None, None]]c                   s   t  ˆ ¡d‡ fdd„ƒ}|S )	NÚargsr   Úkwr   úGenerator[Any, None, None]c                  ?  s8   � t ƒ }ˆ | i |¤ŽD ]}||vr| |¡ |V  qd S ©N)ÚsetÚadd)r'   r(   ÚseenÚitem©r%   r"   r#   Úunique-   s   €
€ýz_unique.<locals>.unique)r'   r   r(   r   r   r)   )Ú	functoolsÚwraps)r%   r0   r"   r/   r#   Ú_unique*   s   r3   Údistr   c                 c  sª   � | j }|dusJ dƒ‚|  ¡ }|du rt| d�‚|D ]7}tj ||¡}|V  | d¡rRtj |¡\}}|dd… }tj ||d ¡}|V  tj ||d ¡}|V  qdS )aÇ  
    Yield all the uninstallation paths for dist based on RECORD-without-.py[co]

    Yield paths to all the files in RECORD. For each .py file in RECORD, add
    the .pyc and .pyo in the same directory.

    UninstallPathSet.add() takes care of the __pycache__ .py[co].

    If RECORD is not found, raises an error,
    with possible information from the INSTALLER file.

    https://packaging.python.org/specifications/recording-installed-packages/
    Nznot installed©Údistributionú.pyéýÿÿÿú.pycú.pyo)ÚlocationÚiter_declared_entriesr   r   r   r    ÚendswithÚsplit)r4   r;   ÚentriesÚentryr   Údnr%   Úbaser"   r"   r#   Úuninstallation_paths8   s$   €

€÷rC   ÚpathsúIterable[str]úset[str]c                   sJ   t jj‰tƒ }t| td�D ]‰ t‡ ‡fdd„|D ƒƒ}|s"| ˆ ¡ q|S )zÈCompact a path set to contain the minimal number of paths
    necessary to contain all paths in the set. If /a/path/ and
    /a/path/to/a/file.txt are both in the set, leave only the
    shorter path.©Úkeyc                 3  s<   � | ]}ˆ   | d ¡¡oˆ t| d ¡ ˆ¡ƒ ˆkV  qdS )Ú*N)Ú
startswithÚrstripÚlen)Ú.0Ú	shortpath©r   Úsepr"   r#   Ú	<genexpr>c   s   € þ
þzcompact.<locals>.<genexpr>)r   r   rP   r+   ÚsortedrL   Úanyr,   )rD   Úshort_pathsÚshould_skipr"   rO   r#   ÚcompactZ   s   ý
€rV   c           	        sò   dd„ | D ƒ}t |ƒ}tdd„ | ¡ D ƒtd�}t ƒ }dd	d
„‰|D ]L‰t‡fdd„|D ƒƒr0q"t ƒ }t ƒ }t ˆ¡D ]!\‰ }}| ‡ ‡‡fdd„|D ƒ¡ | ‡ ‡‡fdd„|D ƒ¡ q;|| sn| |¡ | 	ˆtj
 ¡ q"t t|j|ƒƒ|B S )z«Returns a set containing the paths that need to be renamed.

    This set may include directories when the original sequence of paths
    included every file on disk.
    c                 S  s   i | ]	}t j |¡|“qS r"   )r   r   Únormcase©rM   Úpr"   r"   r#   Ú
<dictcomp>s   ó    z'compress_for_rename.<locals>.<dictcomp>c                 S  s   h | ]
}t j |¡d  ’qS )r   )r   r   r>   rX   r"   r"   r#   Ú	<setcomp>u   s    z&compress_for_rename.<locals>.<setcomp>rG   Úar   r   c                  W  s   t j t jj| Ž ¡S r*   )r   r   rW   r    )r]   r"   r"   r#   Ú	norm_joinx   s   z&compress_for_rename.<locals>.norm_joinc                 3  s"   � | ]}t j ˆ ¡ |¡V  qd S r*   )r   r   rW   rJ   )rM   Úw)Úrootr"   r#   rQ   |   s   €  z&compress_for_rename.<locals>.<genexpr>c                 3  ó   � | ]	}ˆˆˆ |ƒV  qd S r*   r"   )rM   Úd©Údirnamer^   r`   r"   r#   rQ   ƒ   ó   € c                 3  ra   r*   r"   )rM   Úfrc   r"   r#   rQ   „   re   N)r]   r   r   r   )r+   rR   ÚvaluesrL   rS   r   ÚwalkÚupdateÚdifference_updater,   rP   ÚmapÚ__getitem__)	rD   Úcase_mapÚ	remainingÚ	uncheckedÚ	wildcardsÚ	all_filesÚall_subdirsÚsubdirsÚfilesr"   rc   r#   Úcompress_for_renamem   s$   

€ru   útuple[set[str], set[str]]c                 C  sü   t | ƒ}t ƒ }t ƒ }t ƒ }|D ]}| d¡rq| d¡s d|v r)| tj |¡¡ | |¡ qt ttjj|ƒƒ}t|ƒ}|D ]2}t 	|¡D ]*\}}	}
|
D ]"}| d¡rTqLtj 
||¡}tj |¡rntj |¡|vrn| |¡ qLqEq>|dd„ |D ƒB }||fS )as  Returns a tuple of 2 sets of which paths to display to user

    The first set contains paths that would be deleted. Files of a package
    are not added and the top-level directory of the package has a '*' added
    at the end - to signify that all it's contents are removed.

    The second set contains files that would have been skipped in the above
    folders.
    r9   z__init__.pyz
.dist-infoc                 S  s   h | ]	}t j |d ¡’qS )rI   )r   r   r    )rM   Úfolderr"   r"   r#   r\   »   r[   z.compress_for_output_listing.<locals>.<setcomp>)r+   r=   r,   r   r   rd   rk   rW   rV   rh   r    Úisfile)rD   Úwill_removeÚ	will_skipÚfoldersrt   r   Ú_normcased_filesrw   ÚdirpathÚ_ÚdirfilesÚfnameÚfile_r"   r"   r#   Úcompress_for_output_listing�   s6   


ÿ
€öÿr‚   c                   @  sZ   e Zd ZdZddd„Zddd	„Zdd
d„Zddd„Zddd„Zddd„Z	e
ddd„ƒZdS )ÚStashedUninstallPathSetzWA set of file rename operations to stash files while
    tentatively uninstalling them.r   ÚNonec                 C  s   i | _ g | _d S r*   )Ú
_save_dirsÚ_moves©Úselfr"   r"   r#   Ú__init__Ä   s   
z StashedUninstallPathSet.__init__r   r   c                 C  sB   zt |ƒ}W n ty   tdd�}Y nw || jtj |¡< |jS )z�Stashes a directory.

        Directories are stashed adjacent to their original location if
        possible, or else moved/copied into the user's temp dir.Ú	uninstall©Úkind)r   ÚOSErrorr   r…   r   r   rW   )rˆ   r   Úsave_dirr"   r"   r#   Ú_get_directory_stashÌ   s   ÿz,StashedUninstallPathSet._get_directory_stashc                 C  sº   t j |¡}t j |¡d}}d}||kr3z| j| }W n& ty%   Y nw t j |¡|}}||kst j |¡}tdd�}|| j|< t j ||¡}|rZ|t jjkrZt j 	|j|¡S |jS )z‚Stashes a file.

        If no root has been provided, one will be created for the directory
        in the user's temp directory.NrŠ   r‹   )
r   r   rW   rd   r…   ÚKeyErrorr   ÚrelpathÚcurdirr    )rˆ   r   ÚheadÚold_headrŽ   r‘   r"   r"   r#   Ú_get_file_stashÚ   s&   
ÿú	

z'StashedUninstallPathSet._get_file_stashc                 C  sl   t j |¡ot j |¡ }|r|  |¡}n|  |¡}| j ||f¡ |r/t j |¡r/t  |¡ t	||ƒ |S )z�Stashes the directory or file and returns its new location.
        Handle symlinks as files to avoid modifying the symlink targets.
        )
r   r   ÚisdirÚislinkr�   r•   r†   ÚappendÚrmdirr   )rˆ   r   Úpath_is_dirÚnew_pathr"   r"   r#   Ústashõ   s   


zStashedUninstallPathSet.stashc                 C  s(   | j  ¡ D ]}| ¡  qg | _i | _ dS )z0Commits the uninstall by removing stashed files.N)r…   rg   Úcleanupr†   )rˆ   rŽ   r"   r"   r#   Úcommit
  s   

zStashedUninstallPathSet.commitc                 C  sÊ   | j D ]}tjdg|¢R Ž  q| j D ]L\}}z*t d||¡ tj |¡s*tj |¡r0t |¡ n
tj 	|¡r:t
|ƒ t||ƒ W q ty^ } zt d|¡ t d|¡ W Y d}~qd}~ww |  ¡  dS )z2Undoes the uninstall by moving stashed files back.zMoving to %s
 from %szReplacing %s from %szFailed to restore %szException: %sN)r†   ÚloggerÚinfoÚdebugr   r   rx   r—   Úunlinkr–   r   r   r�   Úerrorrž   )rˆ   rY   r›   r   Úexr"   r"   r#   Úrollback  s    
€þz StashedUninstallPathSet.rollbackr   c                 C  s
   t | jƒS r*   )r   r†   r‡   r"   r"   r#   Úcan_rollback$  s   
z$StashedUninstallPathSet.can_rollbackN©r   r„   )r   r   r   r   ©r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r‰   r�   r•   rœ   rž   r¥   Úpropertyr¦   r"   r"   r"   r#   rƒ   À   s    





rƒ   c                   @  sp   e Zd ZdZd!dd„Zd"dd„Zd#dd„Zd$dd„Zd%d&dd„Zd'dd„Z	d(dd„Z
d(dd„Zed)dd„ƒZd S )*ÚUninstallPathSetzMA set of file paths to be removed in the uninstallation of a
    requirement.r4   r   r   r„   c                 C  s4   t ƒ | _t ƒ | _i | _|| _tƒ | _t t	¡| _
d S r*   )r+   Ú_pathsÚ_refuseÚ_pthÚ_distrƒ   Ú_moved_pathsr1   Ú	lru_cacher   Ú_normalize_path_cached)rˆ   r4   r"   r"   r#   r‰   -  s   zUninstallPathSet.__init__r   r   r   c                 C  s   t ƒ sdS | |  tj¡¡S )zs
        Return True if the given path is one we are permitted to
        remove/modify, False otherwise.

        T)r   rJ   rµ   ÚsysÚprefix)rˆ   r   r"   r"   r#   Ú
_permitted8  s   zUninstallPathSet._permittedc                 C  sœ   t j |¡\}}t j |  |¡t j |¡¡}t j |¡sd S |  |¡r*| j 	|¡ n| j
 	|¡ t j |¡d dkrLt j t j |¡d¡}|  	|¡ d S d S )Né   r7   Ú__pycache__)r   r   r>   r    rµ   rW   Úexistsr¸   r¯   r,   r°   Úsplitextrd   )rˆ   r   r“   ÚtailÚpycacher"   r"   r#   r,   C  s   
	þzUninstallPathSet.addÚpth_filer@   c                 C  sP   |   |¡}|  |¡r || jvrt|ƒ| j|< | j|  |¡ d S | j |¡ d S r*   )rµ   r¸   r±   ÚUninstallPthEntriesr,   r°   )rˆ   r¿   r@   r"   r"   r#   Úadd_pth\  s   


zUninstallPathSet.add_pthFÚauto_confirmÚverbosec                 C  sâ   | j st d| jj¡ dS | jj› d| jj› �}t d|¡ tƒ �F |s)|  |¡r_| j}t	| j ƒ}t
t|ƒƒD ]}| |¡ t d|¡ q7| j ¡ D ]}| ¡  qJt d|¡ W d  ƒ dS W d  ƒ dS 1 sjw   Y  dS )z\Remove paths in ``self._paths`` with confirmation (unless
        ``auto_confirm`` is True).z7Can't uninstall '%s'. No files were found to uninstall.Nú-zUninstalling %s:zRemoving file or directory %szSuccessfully uninstalled %s)r¯   rŸ   r    r²   Úraw_nameÚraw_versionr   Ú_allowed_to_proceedr³   ru   rR   rV   rœ   rÃ   r±   rg   Úremove)rˆ   rÂ   rÃ   Údist_name_versionÚmovedÚ
for_renamer   Úpthr"   r"   r#   rÈ   e  s*   þ


ó"ÿzUninstallPathSet.removec                 C  sp   ddd„}|st | jƒ\}}nt| jƒ}tƒ }|d	|ƒ |d
|ƒ |d| jƒ |r1|dt| jƒƒ tddƒdkS )z@Display which files would be deleted and prompt for confirmationÚmsgr   rD   rE   r   r„   c                 S  sZ   |sd S t  | ¡ tƒ � tt|ƒƒD ]}t  |¡ qW d   ƒ d S 1 s&w   Y  d S r*   )rŸ   r    r   rR   rV   )rÍ   rD   r   r"   r"   r#   Ú_display…  s   
ÿ"ÿz6UninstallPathSet._allowed_to_proceed.<locals>._displayzWould remove:z+Would not remove (might be manually added):z%Would not remove (outside of prefix):zWill actually move:zProceed (Y/n)? )ÚyÚnÚ rÐ   N)rÍ   r   rD   rE   r   r„   )r‚   r¯   r+   r°   ru   r   )rˆ   rÃ   rÎ   ry   rz   r"   r"   r#   rÇ   ‚  s   
	


z$UninstallPathSet._allowed_to_proceedc                 C  sR   | j jst d| jj¡ dS t d| jj¡ | j  ¡  | j 	¡ D ]}| ¡  q dS )z1Rollback the changes previously made by remove().z'Can't roll back %s; was not uninstalledNzRolling back uninstall of %s)
r³   r¦   rŸ   r£   r²   rÅ   r    r¥   r±   rg   )rˆ   rÌ   r"   r"   r#   r¥   ž  s   þ

ÿzUninstallPathSet.rollbackc                 C  s   | j  ¡  dS )z?Remove temporary save dir: rollback will no longer be possible.N)r³   rž   r‡   r"   r"   r#   rž   «  s   zUninstallPathSet.commitc              
     sš  |j }|j}|d u rt d|j¡ | |ƒS t|ƒ}|js*t d|j|tj¡ | |ƒS |dd„ t	 
d¡t	 
d¡hD ƒv rGt d|j|¡ | |ƒS | |ƒ}t|jƒ}|jof|d uoftj |¡of| |j› d�¡ }|rÞ|d urr| |¡ | ¡ }|d urŠ|D ]}	| tj ||	¡¡ q|nò| d	¡rÝz| d
¡}
W n ty¡   g ‰ Y nw |
jdd�‰ ‡ fdd„| d	¡ ¡ D ƒD ]&}tj ||¡}| |¡ | |› d�¡ | |› d�¡ | |› d�¡ q¶nž|jræt|d�‚|j�r| |¡ tj |¡d }tj tj |¡d¡}| |d| ¡ nq|j �rt!|ƒD ]}| |¡ �qn_|�rut"|ƒ�}tj #| $¡  %¡ ¡}| &|¡}W d   ƒ n	1 �s?w   Y  tj '||¡�s^J d|› d|› d|j› d|› d�	ƒ‚| |¡ tj tj |¡d¡}| ||¡ nt (d||¡ |j)�r„t*ƒ }nt+ƒ }z$| ,¡ D ]}| tj ||¡¡ t-�r¨| tj ||› d�¡¡ �qŒW n tt.f�y·   Y nw d%d#d$„}|||ƒD ]}| |¡ �qÂ|S )&Nz-Not uninstalling %s since it is not installedz1Not uninstalling %s at %s, outside environment %sc                 S  s   h | ]}|r|’qS r"   r"   rX   r"   r"   r#   r\   Ä  s    ýÿz-UninstallPathSet.from_dist.<locals>.<setcomp>ÚstdlibÚ
platstdlibz<Not uninstalling %s at %s, as it is in the standard library.z	.egg-infoztop_level.txtznamespace_packages.txtF)Úkeependsc                   s   g | ]
}|r|ˆ vr|‘qS r"   r"   rX   ©Ú
namespacesr"   r#   Ú
<listcomp>ò  s    ýþz.UninstallPathSet.from_dist.<locals>.<listcomp>r7   r9   r:   r5   r¹   zeasy-install.pthz./z	Egg-link z (to z') does not match installed location of z (at ú)z)Not sure how to uninstall: %s - Check: %sz.batr4   r   r   r   r   r   c                 s  sR   � |   ¡ D ]!}|jdkrt||jdƒE d H  q|jdkr&t||jdƒE d H  qd S )NÚconsole_scriptsFÚgui_scriptsT)Úiter_entry_pointsÚgroupr$   Úname)r4   r   Úentry_pointr"   r"   r#   Úiter_scripts_to_remove=  s   €

€üz:UninstallPathSet.from_dist.<locals>.iter_scripts_to_remove)r4   r   r   r   r   r   )/r;   Úinfo_locationrŸ   r    Úcanonical_namer   Úlocalr¶   r·   Ú	sysconfigÚget_pathr   rÅ   Ú"installed_with_setuptools_egg_infor   r   r»   r=   Úsetuptools_filenamer,   r<   r    Úis_fileÚ	read_textÚFileNotFoundErrorÚ
splitlinesÚinstalled_by_distutilsr   Úinstalled_as_eggr>   rd   rÁ   Úinstalled_with_dist_inforC   ÚopenrW   ÚreadlineÚstriprµ   Úsamefiler¡   Úin_usersiter
   r	   Úiter_distutils_script_namesr   ÚNotADirectoryError)Úclsr4   Údist_locationrà   Únormalized_dist_locationÚpaths_to_removeÚdevelop_egg_linkÚsetuptools_flat_installationÚinstalled_filesÚinstalled_fileÚnamespace_packagesÚtop_level_pkgr   Úeasy_install_eggÚeasy_install_pthÚfhÚlink_pointerÚnormalized_link_pointerr   Úscriptrß   Úsr"   rÕ   r#   Ú	from_dist¯  sè   þüþý
ÿ
þú
ÿ
ÿ

þ
€


þÿ
ÿþÿÿÿý
ÿý€ýÿ

zUninstallPathSet.from_distN)r4   r   r   r„   )r   r   r   r   )r   r   r   r„   )r¿   r   r@   r   r   r„   )FF)rÂ   r   rÃ   r   r   r„   )rÃ   r   r   r   r§   )r4   r   r   r®   )r©   rª   r«   r¬   r‰   r¸   r,   rÁ   rÈ   rÇ   r¥   rž   Úclassmethodr  r"   r"   r"   r#   r®   )  s    



	


r®   c                   @  s4   e Zd Zddd„Zddd	„Zdd
d„Zddd„ZdS )rÀ   r¿   r   r   r„   c                 C  s   || _ tƒ | _d | _d S r*   )Úfiler+   r?   Ú_saved_lines)rˆ   r¿   r"   r"   r#   r‰   N  s   
zUninstallPthEntries.__init__r@   c                 C  s<   t j |¡}trt j |¡d s| dd¡}| j |¡ d S )Nr   ú\ú/)r   r   rW   r   Ú
splitdriveÚreplacer?   r,   )rˆ   r@   r"   r"   r#   r,   S  s   
zUninstallPthEntries.addc              	   C  s0  t  d| j¡ tj | j¡st  d| j¡ d S t| jdƒ�}| ¡ }|| _	W d   ƒ n1 s/w   Y  t
dd„ |D ƒƒr@d}nd}|rY|d  | d	¡¡sY|d | d	¡ |d< | jD ]}zt  d
|¡ | ||  d	¡¡ W q\ tyy   Y q\w t| jdƒ�}| |¡ W d   ƒ d S 1 s‘w   Y  d S )NzRemoving pth entries from %s:z.Cannot remove entries from nonexistent file %sÚrbc                 s  s   � | ]}d |v V  qdS )s   
Nr"   )rM   Úliner"   r"   r#   rQ   m  s   € z-UninstallPthEntries.remove.<locals>.<genexpr>z
Ú
éÿÿÿÿzutf-8zRemoving entry: %sÚwb)rŸ   rÃ   r  r   r   rx   Úwarningrî   Ú	readlinesr	  rS   r=   Úencoder?   rÈ   Ú
ValueErrorÚ
writelines)rˆ   r  ÚlinesÚendliner@   r"   r"   r#   rÈ   b  s.   ý
ÿ"ÿzUninstallPthEntries.remover   c                 C  sh   | j d u rt d| j¡ dS t d| j¡ t| jdƒ�}| | j ¡ W d   ƒ dS 1 s-w   Y  dS )Nz.Cannot roll back changes to %s, none were madeFz!Rolling %s back to previous stater  T)r	  rŸ   r£   r  r¡   rî   r  )rˆ   r  r"   r"   r#   r¥   }  s   

ÿþzUninstallPthEntries.rollbackN)r¿   r   r   r„   )r@   r   r   r„   r§   r¨   )r©   rª   r«   r‰   r,   rÈ   r¥   r"   r"   r"   r#   rÀ   M  s
    


rÀ   )r   r   r   r   r   r   r   r   )r%   r&   r   r&   )r4   r   r   r   )rD   rE   r   rF   )rD   rE   r   rv   )0Ú
__future__r   r1   r   r¶   rã   Úcollections.abcr   r   Útypingr   r   Úpip._internal.exceptionsr   r   Úpip._internal.locationsr	   r
   Úpip._internal.metadatar   Úpip._internal.utils.compatr   Úpip._internal.utils.egg_linkr   Úpip._internal.utils.loggingr   r   Úpip._internal.utils.miscr   r   r   r   Úpip._internal.utils.temp_dirr   r   Úpip._internal.utils.virtualenvr   r©   rŸ   r$   r3   rC   rV   ru   r‚   rƒ   r®   rÀ   r"   r"   r"   r#   Ú<module>   s:    


!

"1i  &