o
    Ëý°j]  ã                	   @   sÆ  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	m
Z
mZmZmZmZmZ d dl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"m#Z# d dl$m%Z%m&Z& ee'ƒZ(de)de)de*dee) fdd„Z+dedee f dedee f fdd„Z,e,dedee) fdd„ƒZ-de
e) dee) fdd„Z.de
e) dee) fdd„Z/de
e) deee) ee) f fdd„Z0G d d!„ d!ƒZ1G d"d#„ d#ƒZ2G d$d%„ d%ƒZ3dS )&é    N)Úcache_from_source)	ÚAnyÚCallableÚDictÚIterableÚIteratorÚListÚOptionalÚSetÚTuple)ÚUninstallationError)Úget_bin_prefixÚget_bin_user)ÚBaseDistribution)ÚWINDOWS)Úegg_link_path_from_location)Ú	getLoggerÚ
indent_log)ÚaskÚis_localÚnormalize_pathÚrenamesÚrmtree)ÚAdjacentTempDirectoryÚTempDirectoryÚbin_dirÚscript_nameÚis_guiÚreturnc                 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/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/pip/_internal/req/req_uninstall.pyÚ_script_names   s   €r%   Úfn.c                    s,   t  ˆ ¡dtdtdtt f‡ fdd„ƒ}|S )NÚargsÚkwr   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)Ú	functoolsÚwrapsr   r   )r&   r/   r#   r.   r$   Ú_unique%   s    r2   Údistc                 c   sú   � | j }|dusJ dƒ‚|  ¡ }|du rAdj| d�}| j}|r#|dkr6d | j| j¡}|d |¡7 }t|ƒ‚|d |¡7 }t|ƒ‚|D ]7}tj 	||¡}|V  | 
d	¡rztj |¡\}}	|	dd
… }
tj 	||
d ¡}|V  tj 	||
d ¡}|V  qCd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 UninstallationError,
    with possible information from the INSTALLER file.

    https://packaging.python.org/specifications/recording-installed-packages/
    Nznot installedz/Cannot uninstall {dist}, RECORD file not found.)r3   Úpipz{}=={}zZ You might be able to recover from this via: 'pip install --force-reinstall --no-deps {}'.z' Hint: The package was installed by {}.ú.pyéýÿÿÿú.pycú.pyo)ÚlocationÚiter_declared_entriesÚformatÚ	installerÚraw_nameÚversionr   r   r    r!   ÚendswithÚsplit)r3   r9   ÚentriesÚmsgr<   ÚdepÚentryr    Údnr&   Úbaser#   r#   r$   Úuninstallation_paths1   s8   €þÿ
€÷rG   Úpathsc                    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>f   s   € þ
þzcompact.<locals>.<genexpr>)r   r    rR   r*   ÚsortedrN   Úanyr+   )rH   Úshort_pathsÚshould_skipr#   rQ   r$   Úcompact]   s   ý
€rX   c           	         sú   dd„ | D ƒ}t |ƒ}tdd„ | ¡ D ƒtd�}t ƒ }dtdtfdd	„‰|D ]L‰t‡fd
d„|D ƒƒr4q&t ƒ }t ƒ }t ˆ¡D ]!\‰ }}| ‡ ‡‡fdd„|D ƒ¡ | ‡ ‡‡fdd„|D ƒ¡ q?|| sr| 	|¡ | 
ˆ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©rO   Úpr#   r#   r$   Ú
<dictcomp>v   ó    z'compress_for_rename.<locals>.<dictcomp>c                 S   s   h | ]
}t j |¡d  ’qS )r   )r   r    r@   rZ   r#   r#   r$   Ú	<setcomp>x   s    z&compress_for_rename.<locals>.<setcomp>rI   Úar   c                  W   s   t j t jj| Ž ¡S r)   )r   r    rY   r!   )r_   r#   r#   r$   Ú	norm_join{   s   z&compress_for_rename.<locals>.norm_joinc                 3   s"   � | ]}t j ˆ ¡ |¡V  qd S r)   )r   r    rY   rL   )rO   Úw)Úrootr#   r$   rS      s   €  z&compress_for_rename.<locals>.<genexpr>c                 3   ó   � | ]	}ˆˆˆ |ƒV  qd S r)   r#   )rO   Úd©Údirnamer`   rb   r#   r$   rS   †   ó   € c                 3   rc   r)   r#   )rO   Úfre   r#   r$   rS   ‡   rg   )r*   rT   ÚvaluesrN   ÚstrrU   r   ÚwalkÚupdateÚdifference_updater+   rR   ÚmapÚ__getitem__)	rH   Úcase_mapÚ	remainingÚ	uncheckedÚ	wildcardsÚ	all_filesÚall_subdirsÚsubdirsÚfilesr#   re   r$   Úcompress_for_renamep   s$   
€rx   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.
    r7   z__init__.pyz
.dist-infoc                 S   s   h | ]	}t j |d ¡’qS )rK   )r   r    r!   )rO   Úfolderr#   r#   r$   r^   ¿   r]   z.compress_for_output_listing.<locals>.<setcomp>)r*   r?   r+   r   r    rf   rn   rY   rX   rk   r!   Úisfile)rH   Úwill_removeÚ	will_skipÚfoldersrw   r    Ú_normcased_filesry   ÚdirpathÚ_ÚdirfilesÚfnameÚfile_r#   r#   r$   Úcompress_for_output_listing’   s6   


ÿ
€öÿr„   c                   @   sv   e Zd ZdZddd„Zdedefdd„Zdedefd	d
„Zdedefdd„Zddd„Z	ddd„Z
edefdd„ƒZdS )ÚStashedUninstallPathSetzWA set of file rename operations to stash files while
    tentatively uninstalling them.r   Nc                 C   s   i | _ g | _d S r)   )Ú
_save_dirsÚ_moves©Úselfr#   r#   r$   Ú__init__È   s   
z StashedUninstallPathSet.__init__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    rY   )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    rY   rf   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†   ÚitemsÚcleanupr‡   )r‰   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    rz   r˜   Úunlinkr—   r   r   rŽ   Úerrorr    )r‰   r[   rœ   r    Úexr#   r#   r$   Úrollback  s    
€þz StashedUninstallPathSet.rollbackc                 C   s
   t | jƒS r)   )Úboolr‡   rˆ   r#   r#   r$   Úcan_rollback(  s   
z$StashedUninstallPathSet.can_rollback©r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rŠ   rj   r�   r–   r�   r    r§   Úpropertyr¨   r©   r#   r#   r#   r$   r…   Ä   s    


r…   c                   @   s°   e Zd ZdZdeddfdd„Zdedefdd	„Zdeddfd
d„Z	dededdfdd„Z
ddededdfdd„Zdedefdd„Zddd„Zddd„Zededd fdd„ƒZdS )ÚUninstallPathSetzMA set of file paths to be removed in the uninstallation of a
    requirement.r3   r   Nc                 C   s(   t ƒ | _t ƒ | _i | _|| _tƒ | _d S r)   )r*   Ú_pathsÚ_refuseÚ_pthÚ_distr…   Ú_moved_paths)r‰   r3   r#   r#   r$   rŠ   1  s
   zUninstallPathSet.__init__r    c                 C   s   t |ƒS )zs
        Return True if the given path is one we are permitted to
        remove/modify, False otherwise.

        )r   )r‰   r    r#   r#   r$   Ú
_permitted8  s   zUninstallPathSet._permittedc                 C   sˆ   t j |¡\}}t j t|ƒt j |¡¡}t j |¡sd S |  |¡r)| j 	|¡ n| j
 	|¡ t j |¡d dkrB|  	t|ƒ¡ d S d S )Né   r5   )r   r    r@   r!   r   rY   Úexistsr¶   r±   r+   r²   Úsplitextr   )r‰   r    r”   Útailr#   r#   r$   r+   @  s   
ÿzUninstallPathSet.addÚpth_filerD   c                 C   sN   t |ƒ}|  |¡r|| jvrt|ƒ| j|< | j|  |¡ d S | j |¡ d S r)   )r   r¶   r³   ÚUninstallPthEntriesr+   r²   )r‰   r»   rD   r#   r#   r$   Úadd_pthS  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´   r=   r>   r   Ú_allowed_to_proceedrµ   rx   rT   rX   r�   r¿   r³   ri   Úremove)r‰   r¾   r¿   Údist_name_versionÚmovedÚ
for_renamer    Úpthr#   r#   r$   rÂ   \  s*   þ


ó"ÿzUninstallPathSet.removec                 C   s€   dt dtt  ddfdd„}|st| jƒ\}}nt| jƒ}tƒ }|d|ƒ |d|ƒ |d	| jƒ |r9|d
t| jƒƒ tddƒdkS )z@Display which files would be deleted and prompt for confirmationrB   rH   r   Nc                 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   rT   rX   )rB   rH   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É   )rj   r   r„   r±   r*   r²   rx   r   )r‰   r¿   rÇ   r{   r|   r#   r#   r$   rÁ   y  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³   ri   )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¡ƒ‚|j�r| |¡ tj |¡d }tj tj |¡d¡}|  |d| ¡ nf|j!�r t"|ƒD ]}| |¡ �qnT|�rmt#|ƒ�}tj $| %¡  &¡ ¡}W d   ƒ n	1 �s=w   Y  ||k�sVJ d|› d|j› d|› d�ƒ‚| |¡ tj tj |¡d¡}|  ||¡ nt 'd||¡ |j(�r|t)ƒ }nt*ƒ }z'| +d¡D ]}| tj ||j,¡¡ t-�r£| tj ||j,› d�¡¡ �q…W n tt.f�y²   Y nw dt/dt0dt1t0 f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#   rZ   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#   rZ   ©Ú
namespacesr#   r$   Ú
<listcomp>é  s    ýþz.UninstallPathSet.from_dist.<locals>.<listcomp>r5   r7   r8   z©Cannot uninstall {!r}. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.r·   zeasy-install.pthz./z	Egg-link z& does not match installed location of z (at ú)z)Not sure how to uninstall: %s - Check: %sÚscriptsz.batr3   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)r3   r   Úentry_pointr#   r#   r$   Úiter_scripts_to_remove2  s   €

€üz:UninstallPathSet.from_dist.<locals>.iter_scripts_to_remove)2r9   Úinfo_locationr¡   r¢   Úcanonical_namer   ÚlocalÚsysÚprefixÚ	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   r;   Úinstalled_as_eggr@   rf   r½   Úinstalled_with_dist_inforG   ÚopenrY   ÚreadlineÚstripr£   Úin_usersiter   r   Úiterdirr×   r   ÚNotADirectoryErrorr   rj   r   )Úclsr3   Ú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_pointerr   ÚscriptrÙ   Úsr#   rÎ   r$   Ú	from_dist¦  sô   þüþý
ÿ
þú
ÿ
ÿ

þ
€ÿý

þÿ
ÿÿÿÿ
ÿý€ýÿÿþ
ý
zUninstallPathSet.from_dist)FFrª   )r«   r¬   r­   r®   r   rŠ   rj   r¨   r¶   r+   r½   rÂ   rÁ   r§   r    Úclassmethodr   r#   r#   r#   r$   r°   -  s    	

r°   c                   @   sH   e Zd Zdeddfdd„Zdeddfdd„Zdd	d
„Zdefdd„ZdS )r¼   r»   r   Nc                 C   s   || _ tƒ | _d | _d S r)   )Úfiler*   rA   Ú_saved_lines)r‰   r»   r#   r#   r$   rŠ   C  s   
zUninstallPthEntries.__init__rD   c                 C   s<   t j |¡}trt j |¡d s| dd¡}| j |¡ d S )Nr   ú\ú/)r   r    rY   r   Ú
splitdriveÚreplacerA   r+   )r‰   rD   r#   r#   r$   r+   H  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#   )rO   Úliner#   r#   r$   rS   b  s   € z-UninstallPthEntries.remove.<locals>.<genexpr>z
Ú
éÿÿÿÿzutf-8zRemoving entry: %sÚwb)r¡   r¿   r  r   r    rz   Úwarningrê   Ú	readlinesr  rU   r?   ÚencoderA   rÂ   Ú
ValueErrorÚ
writelines)r‰   rü   ÚlinesÚendlinerD   r#   r#   r$   rÂ   W  s.   ý
ÿ"ÿzUninstallPthEntries.removec                 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§   r  s   

ÿþzUninstallPthEntries.rollbackrª   )	r«   r¬   r­   rj   rŠ   r+   rÂ   r¨   r§   r#   r#   r#   r$   r¼   B  s
    
r¼   )4r0   r   rÝ   rß   Úimportlib.utilr   Útypingr   r   r   r   r   r   r	   r
   r   Úpip._internal.exceptionsr   Ú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   r   Úpip._internal.utils.temp_dirr   r   r«   r¡   rj   r¨   r%   r2   rG   rX   rx   r„   r…   r°   r¼   r#   r#   r#   r$   Ú<module>   s6    ,*+&"2i  