a
    <Bìj#^  ã                   @  s~  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
 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ddœdd„Z)dddœdd„Z*e*dddœdd„ƒZ+dddœd d!„Z,dddœd"d#„Z-dd$dœd%d&„Z.G d'd(„ d(ƒZ/G d)d*„ d*ƒZ0G d+d,„ d,ƒZ1dS )-é    )ÚannotationsN)Ú	GeneratorÚIterable)Úcache_from_source)Ú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ÚstrÚboolúGenerator[str, None, None])Úbin_dirÚscript_nameÚis_guiÚreturnc                 c  sV   t j | |¡}|V  tsdS |› d�V  |› d�V  |rF|› d�V  n|› 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#   ú^/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pip/_internal/req/req_uninstall.pyÚ_script_names   s    r%   z)Callable[..., Generator[Any, None, None]])Úfnr   c                   s$   t  ˆ ¡ddddœ‡ fdd„ƒ}|S )Nr   zGenerator[Any, None, None])ÚargsÚkwr   c                  ?  s6   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Úwraps)r&   r/   r#   r.   r$   Ú_unique+   s    r2   r   ©Údistr   c                 c  s¨   | j }|dusJ dƒ‚|  ¡ }|du r0t| d�‚|D ]n}tj ||¡}|V  | d¡r4tj |¡\}}|dd… }tj ||d ¡}|V  tj ||d ¡}|V  q4d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_paths9   s    

rC   úIterable[str]zset[str])Úpathsr   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:   | ]2}ˆ   | d ¡¡o0ˆ t| d ¡ ˆ¡ƒ ˆkV  qdS )Ú*N)Ú
startswithÚrstripÚlen)Ú.0Ú	shortpath©r    Úsepr#   r$   Ú	<genexpr>d   s   þzcompact.<locals>.<genexpr>)r   r    rO   r*   ÚsortedrK   Úanyr+   )rE   Úshort_pathsÚshould_skipr#   rN   r$   Úcompact[   s    ýrU   c           	        sø   dd„ | D ƒ}t |ƒ}tdd„ | ¡ D ƒtd�}t ƒ }dddœdd	„‰|D ]˜‰t‡fd
d„|D ƒƒrfqJt ƒ }t ƒ }t ˆ¡D ]B\‰ }}| ‡ ‡‡fdd„|D ƒ¡ | ‡ ‡‡fdd„|D ƒ¡ q||| sJ| |¡ | 	ˆtj
 ¡ qJt 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©rL   Úpr#   r#   r$   Ú
<dictcomp>t   ó    z'compress_for_rename.<locals>.<dictcomp>c                 S  s   h | ]}t j |¡d  ’qS )r   )r   r    r>   rW   r#   r#   r$   Ú	<setcomp>v   rZ   z&compress_for_rename.<locals>.<setcomp>rF   r   )Úar   c                  W  s   t j t jj| Ž ¡S r)   )r   r    rV   r!   )r\   r#   r#   r$   Ú	norm_joiny   s    z&compress_for_rename.<locals>.norm_joinc                 3  s    | ]}t j ˆ ¡ |¡V  qd S r)   )r   r    rV   rI   )rL   Úw)Úrootr#   r$   rP   }   rZ   z&compress_for_rename.<locals>.<genexpr>c                 3  s   | ]}ˆˆˆ |ƒV  qd S r)   r#   )rL   Úd©Údirnamer]   r_   r#   r$   rP   „   rZ   c                 3  s   | ]}ˆˆˆ |ƒV  qd S r)   r#   )rL   Úfra   r#   r$   rP   …   rZ   )r*   rQ   ÚvaluesrK   rR   r   ÚwalkÚupdateÚdifference_updater+   rO   ÚmapÚ__getitem__)	rE   Úcase_mapÚ	remainingÚ	uncheckedÚ	wildcardsÚ	all_filesÚall_subdirsÚsubdirsÚfilesr#   ra   r$   Úcompress_for_renamen   s"    
rr   ztuple[set[str], set[str]]c                 C  sü   t | ƒ}t ƒ }t ƒ }t ƒ }|D ]>}| d¡r.q| d¡s@d|v rR| tj |¡¡ | |¡ qt ttjj|ƒƒ}t|ƒ}|D ]d}t 	|¡D ]T\}}	}
|
D ]D}| d¡r¨q˜tj 
||¡}tj |¡r˜tj |¡|vr˜| |¡ q˜qŠq||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 )rH   )r   r    r!   )rL   Úfolderr#   r#   r$   r[   ¼   rZ   z.compress_for_output_listing.<locals>.<setcomp>)r*   r=   r+   r   r    rb   rh   rV   rU   re   r!   Úisfile)rE   Úwill_removeÚ	will_skipÚfoldersrq   r    Ú_normcased_filesrs   ÚdirpathÚ_ÚdirfilesÚfnameÚfile_r#   r#   r$   Úcompress_for_output_listing�   s2    


ÿþr~   c                   @  s|   e Zd ZdZddœdd„Zdddœdd	„Zdddœd
d„Zdddœdd„Zddœdd„Zddœdd„Z	e
ddœdd„ƒZdS )ÚStashedUninstallPathSetzWA set of file rename operations to stash files while
    tentatively uninstalling them.ÚNone©r   c                 C  s   i | _ g | _d S r)   )Ú
_save_dirsÚ_moves©Úselfr#   r#   r$   Ú__init__Å   s    z StashedUninstallPathSet.__init__r   ©r    r   c                 C  sB   zt |ƒ}W n ty(   tdd�}Y n0 || 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    rV   )r…   r    Úsave_dirr#   r#   r$   Ú_get_directory_stashÍ   s    z,StashedUninstallPathSet._get_directory_stashc                 C  s¸   t j |¡}t j |¡d }}d}||krdz| j| }W q„W n tyN   Y n0 t j |¡| }}q"t j |¡}tdd�}|| j|< t j ||¡}|r²|t jjkr²t 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    rV   rb   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‚   rd   Úcleanuprƒ   )r…   rŒ   r#   r#   r$   Úcommit  s    
zStashedUninstallPathSet.commitc                 C  sÌ   | j D ]}tjdg|¢R Ž  q| j D ]š\}}zTt d||¡ tj |¡sTtj |¡r`t |¡ ntj 	|¡rtt
|ƒ t||ƒ W q$ ty¼ } z$t d|¡ t d|¡ W Y d}~q$d}~0 0 q$|  ¡  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    rt   r•   Úunlinkr”   r   r   r‹   Úerrorrœ   )r…   rX   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)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r†   r�   r“   rš   rœ   r£   Úpropertyr¤   r#   r#   r#   r$   r   Á   s   r   c                   @  s¦   e Zd ZdZdddœdd„Zddd	œd
d„Zddd	œdd„Zddddœdd„Zd ddddœdd„Zdddœdd„Z	ddœdd„Z
ddœdd„Zedd dœdd„ƒZdS )!ÚUninstallPathSetzMA set of file paths to be removed in the uninstallation of a
    requirement.r   r€   r3   c                 C  s4   t ƒ | _t ƒ | _i | _|| _tƒ | _t t	¡| _
d S r)   )r*   Ú_pathsÚ_refuseÚ_pthÚ_distr   Ú_moved_pathsr0   Ú	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   rI   r±   ÚsysÚprefix)r…   r    r#   r#   r$   Ú
_permitted9  s    zUninstallPathSet._permittedc                 C  s†   t j |¡\}}t j |  |¡t j |¡¡}t j |¡s<d S |  |¡rT| j 	|¡ n| j
 	|¡ t j |¡d dkr‚|  	t|ƒ¡ d S )Né   r7   )r   r    r>   r!   r±   rV   Úexistsr´   r«   r+   r¬   Úsplitextr   )r…   r    r‘   Útailr#   r#   r$   r+   D  s    
zUninstallPathSet.add)Úpth_filer@   r   c                 C  sN   |   |¡}|  |¡r>|| jvr,t|ƒ| j|< | j|  |¡ n| j |¡ d S r)   )r±   r´   r­   ÚUninstallPthEntriesr+   r¬   )r…   r¹   r@   r#   r#   r$   Úadd_pthW  s    


zUninstallPathSet.add_pthF)Úauto_confirmÚverboser   c                 C  sÐ   | j st d| jj¡ dS | jj› d| jj› �}t d|¡ tƒ �z |sR|  |¡r®| j}t	| j ƒ}t
t|ƒƒD ]}| |¡ t d|¡ qn| j ¡ D ]}| ¡  q”t d|¡ W d  ƒ n1 sÂ0    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_nameZraw_versionr   Ú_allowed_to_proceedr¯   rr   rQ   rU   rš   r½   r­   rd   Úremove)r…   r¼   r½   Údist_name_versionÚmovedÚ
for_renamer    Úpthr#   r#   r$   rÁ   `  s$    þ


zUninstallPathSet.remove)r½   r   c                 C  sx   ddddœdd„}|s&t | jƒ\}}nt| jƒ}tƒ }|d|ƒ |d|ƒ |d	| jƒ |rj|d
t| jƒƒ tddƒdkS )z@Display which files would be deleted and prompt for confirmationr   rD   r€   )ÚmsgrE   r   c                 S  sX   |sd S t  | ¡ tƒ �, tt|ƒƒD ]}t  |¡ q&W d   ƒ n1 sJ0    Y  d S r)   )r�   rž   r   rQ   rU   )rÆ   rE   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É   )r~   r«   r*   r¬   rr   r   )r…   r½   rÇ   ru   rv   r#   r#   r$   rÀ   }  s    	


z$UninstallPathSet._allowed_to_proceedr�   c                 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­   rd   )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sTt d|j|tj¡ | |ƒS |dd„ t	 
d¡t	 
d¡hD ƒv rŽt d|j|¡ | |ƒS | |ƒ}t|jƒ}|joÌ|d uoÌtj |¡oÌ| |j› d�¡ }|�rÈ|d uræ| |¡ | ¡ }|d u�r|D ]}	| tj ||	¡¡ qün¬| d	¡�r
z| d
¡}
W n t�yJ   g ‰ Y n0 |
jdd�‰ ‡ fdd„| d	¡ ¡ D ƒD ]N}tj ||¡}| |¡ | |› d�¡ | |› d�¡ | |› d�¡ �qt�nB|j�rÞt|d�‚�n,|j�r(| |¡ tj |¡d }tj tj |¡d¡}| |d| ¡ nâ|j �rLt!|ƒD ]}| |¡ �q8n¾|�rüt"|ƒ�.}tj #| $¡  %¡ ¡}| &|¡}W d   ƒ n1 �s�0    Y  tj '||¡�sÎJ d|› d|› d|j› d|› d�	ƒ‚| |¡ tj tj |¡d¡}| ||¡ nt (d||¡ |j)�rt*ƒ }nt+ƒ }zH| ,¡ D ]:}| tj ||¡¡ t-�r*| tj ||› d�¡¡ �q*W n tt.f�y€   Y n0 ddd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#   rW   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#   rW   ©Ú
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.batr   r   r   )r4   r   r   c                 s  sP   |   ¡ D ]B}|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_remove8  s
    

z:UninstallPathSet.from_dist.<locals>.iter_scripts_to_remove)/r;   Úinfo_locationr�   rž   Úcanonical_namer   Úlocalr²   r³   Ú	sysconfigÚget_pathr   r¿   Z"installed_with_setuptools_egg_infor   r    r¶   r=   Zsetuptools_filenamer+   r<   r!   Úis_fileÚ	read_textÚFileNotFoundErrorÚ
splitlinesZinstalled_by_distutilsr   Zinstalled_as_eggr>   rb   r»   Zinstalled_with_dist_inforC   ÚopenrV   ÚreadlineÚstripr±   ÚsamefilerŸ   Zin_usersiter   r
   Ziter_distutils_script_namesr   ÚNotADirectoryError)Úclsr4   Údist_locationrÙ   Znormalized_dist_locationÚpaths_to_removeÚdevelop_egg_linkZsetuptools_flat_installationZinstalled_filesÚinstalled_fileZnamespace_packagesÚtop_level_pkgr    Úeasy_install_eggÚeasy_install_pthÚfhÚlink_pointerZnormalized_link_pointerr   ÚscriptrØ   Úsr#   rÎ   r$   Ú	from_distª  sØ    þüþý
ÿ
þú



þ



þ
ÿ$ÿÿÿý
ÿý"
zUninstallPathSet.from_distN)FF)r¥   r¦   r§   r¨   r†   r´   r+   r»   rÁ   rÀ   r£   rœ   Úclassmethodró   r#   r#   r#   r$   rª   *  s   	rª   c                   @  sH   e Zd Zdddœdd„Zdddœdd„Zdd	œd
d„Zd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†   I  s    zUninstallPthEntries.__init__)r@   r   c                 C  s<   t j |¡}tr,t j |¡d s,| dd¡}| j |¡ d S )Nr   ú\ú/)r   r    rV   r   Ú
splitdriveÚreplacer?   r+   )r…   r@   r#   r#   r$   r+   N  s    
zUninstallPthEntries.addr�   c              	   C  s2  t  d| j¡ tj | j¡s.t  d| j¡ d S t| jdƒ�}| ¡ }|| _	W d   ƒ n1 s^0    Y  t
dd„ |D ƒƒr€d}nd}|r²|d  | d	¡¡s²|d | d	¡ |d< | jD ]<}z$t  d
|¡ | ||  d	¡¡ W q¸ tyò   Y q¸0 q¸t| jdƒ�}| |¡ W d   ƒ n1 �s$0    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#   )rL   Úliner#   r#   r$   rP   h  rZ   z-UninstallPthEntries.remove.<locals>.<genexpr>z
Ú
éÿÿÿÿzutf-8zRemoving entry: %sÚwb)r�   r½   rõ   r   r    rt   Úwarningrâ   Ú	readlinesrö   rR   r=   Úencoder?   rÁ   Ú
ValueErrorÚ
writelines)r…   rï   ÚlinesÚendliner@   r#   r#   r$   rÁ   ]  s(    $
zUninstallPthEntries.remover   c                 C  sf   | j d u rt d| j¡ dS t d| j¡ t| jdƒ�}| | j ¡ W d   ƒ n1 sX0    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£   x  s    
*zUninstallPthEntries.rollbackN)r¥   r¦   r§   r†   r+   rÁ   r£   r#   r#   r#   r$   rº   H  s   rº   )2Ú
__future__r   r0   r   r²   rÜ   Úcollections.abcr   r   Úimportlib.utilr   Útypingr   r   Úpip._internal.exceptionsr   r	   Úpip._internal.locationsr
   r   Zpip._internal.metadatar   Úpip._internal.utils.compatr   Z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%   r2   rC   rU   rr   r~   r   rª   rº   r#   r#   r#   r$   Ú<module>   s:   !"1i   