o
    g}”eÄ\  ã                	   @   sâ  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
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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&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„Z0de	dee f de	dee f fdd„Z1e1dedee. fdd„ƒZ2dee. dee. fdd„Z3dee. dee. fdd„Z4dee. deee. ee. f fdd„Z5G d d!„ d!ƒZ6G d"d#„ d#ƒZ7G d$d%„ d%ƒZ8dS )&é    N)Úcache_from_source)	ÚAnyÚCallableÚDictÚIterableÚIteratorÚListÚOptionalÚSetÚTuple)Úpkg_resources)ÚDistribution)ÚUninstallationError)Úget_bin_prefixÚget_bin_user)ÚWINDOWS)Úegg_link_path_from_location)Ú	getLoggerÚ
indent_log)ÚaskÚdist_in_usersiteÚdist_is_localÚis_localÚnormalize_pathÚrenamesÚrmtree)ÚAdjacentTempDirectoryÚTempDirectoryÚdistÚscript_nameÚis_guiÚreturnc                 C   sr   t | ƒrtƒ }ntƒ }tj ||¡}|g}tr7| |d ¡ | |d ¡ |r0| |d ¡ |S | |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
    z.exez.exe.manifestz-script.pywz
-script.py)r   r   r   ÚosÚpathÚjoinr   Úappend)r   r   r    Úbin_dirZexe_nameÚpaths_to_remove© r(   úh/Users/abhinavbhinder/Documents/map/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)   Úunique5   s   €
€ýz_unique.<locals>.unique)Ú	functoolsÚwrapsr   r   )r+   r4   r(   r3   r)   Ú_unique4   s    r7   c                 c   s.  � z
t  |  d¡¡}W nL tyW } z@dj| d�}zt|  d¡ƒ}|r'|dkr*tƒ ‚W n tttfyF   d | j	| j
¡}|d |¡7 }Y nw |d |¡7 }t|ƒ|‚d	}~ww |D ]:}tj | j|d
 ¡}|V  | d¡r”tj |¡\}}	|	d	d… }
tj ||
d ¡}|V  tj ||
d ¡}|V  qZd	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/
    ÚRECORDz/Cannot uninstall {dist}, RECORD file not found.)r   Ú	INSTALLERÚpipz{}=={}zZ You might be able to recover from this via: 'pip install --force-reinstall --no-deps {}'.z' Hint: The package was installed by {}.Nr   ú.pyéýÿÿÿú.pycú.pyo)ÚcsvÚreaderÚget_metadata_linesÚFileNotFoundErrorÚformatÚnextÚ
ValueErrorÚOSErrorÚStopIterationÚproject_nameÚversionr   r"   r#   r$   ÚlocationÚendswithÚsplit)r   ÚrZmissing_record_exceptionÚmsgÚ	installerÚdepÚrowr#   Údnr+   Úbaser(   r(   r)   Úuninstallation_paths@   sB   €ÿþþ
€ò
€÷rT   Ú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)Ú.0Z	shortpath©r#   Úsepr(   r)   Ú	<genexpr>u   s   € þ
þzcompact.<locals>.<genexpr>)r"   r#   r^   r/   Úsortedr[   Úanyr0   )rU   Zshort_pathsZshould_skipr(   r]   r)   Úcompactl   s   ý
€rb   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©r\   Úpr(   r(   r)   Ú
<dictcomp>…   ó    z'compress_for_rename.<locals>.<dictcomp>c                 S   s   h | ]
}t j |¡d  ’qS )r   )r"   r#   rL   rd   r(   r(   r)   Ú	<setcomp>‡   s    z&compress_for_rename.<locals>.<setcomp>rV   Úar!   c                  W   s   t j t jj| Ž ¡S r.   )r"   r#   rc   r$   )ri   r(   r(   r)   Ú	norm_joinŠ   s   z&compress_for_rename.<locals>.norm_joinc                 3   s"   � | ]}t j ˆ ¡ |¡V  qd S r.   )r"   r#   rc   rY   )r\   Úw)Úrootr(   r)   r_   Ž   s   €  z&compress_for_rename.<locals>.<genexpr>c                 3   ó   � | ]	}ˆˆˆ |ƒV  qd S r.   r(   )r\   Úd©Údirnamerj   rl   r(   r)   r_   •   ó   € c                 3   rm   r.   r(   )r\   Úfro   r(   r)   r_   –   rq   )r/   r`   Úvaluesr[   Ústrra   r"   ÚwalkÚupdateÚdifference_updater0   r^   ÚmapÚ__getitem__)	rU   Zcase_mapÚ	remainingZ	uncheckedÚ	wildcardsZ	all_filesZall_subdirsÚsubdirsÚfilesr(   ro   r)   Úcompress_for_rename   s$   
€r~   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.
    r=   z__init__.pyú
.dist-infoc                 S   s   h | ]	}t j |d ¡’qS )rX   )r"   r#   r$   )r\   Úfolderr(   r(   r)   rh   Î   rg   z.compress_for_output_listing.<locals>.<setcomp>)r/   rK   r0   r"   r#   rp   rx   rc   rb   ru   r$   Úisfile)rU   Úwill_removeÚ	will_skipZfoldersr}   r#   Z_normcased_filesr€   ÚdirpathÚ_ZdirfilesÚfnameZfile_r(   r(   r)   Úcompress_for_output_listing¡   s8   


ÿþ
€öÿ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   rF   r   r‰   r"   r#   rc   )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#   rc   rp   r‰   ÚKeyErrorr   ÚrelpathÚcurdirr$   )rŒ   r#   ÚheadZ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Š   r%   Úrmdirr   )rŒ   r#   Z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#   r�   r™   Úunlinkr˜   r   r   rF   ÚerrorrŸ   )rŒ   re   r›   r#   Úexr(   r(   r)   Úrollback$  s    
€þz StashedUninstallPathSet.rollbackc                 C   s
   t | jƒS r.   )ÚboolrŠ   r‹   r(   r(   r)   Úcan_rollback7  s   
z$StashedUninstallPathSet.can_rollback©r!   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r�   rt   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.r   r!   Nc                 C   s(   t ƒ | _t ƒ | _i | _|| _tƒ | _d S r.   )r/   rU   Ú_refuseÚpthr   rˆ   Ú_moved_paths)rŒ   r   r(   r(   r)   r�   @  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)   Ú
_permittedG  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é   r;   )r"   r#   rL   r$   r   rc   Úexistsr³   rU   r0   r°   Úsplitextr   )rŒ   r#   r–   Útailr(   r(   r)   r0   O  s   
ÿzUninstallPathSet.addÚpth_fileÚentryc                 C   sN   t |ƒ}|  |¡r|| jvrt|ƒ| j|< | j|  |¡ d S | j |¡ d S r.   )r   r³   r±   ÚUninstallPthEntriesr0   r°   )rŒ   r¸   r¹   r(   r(   r)   Úadd_pthb  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|¡ q6| j ¡ D ]}| ¡  qIt d|¡ W d  ƒ dS W d  ƒ dS 1 siw   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)rU   r    r¡   r   rH   rI   r   Ú_allowed_to_proceedr²   r~   r`   rb   rœ   r½   r±   rs   Úremove)rŒ   r¼   r½   Zdist_name_versionÚmovedZ
for_renamer#   r±   r(   r(   r)   rÀ   k  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 confirmationrN   rU   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   r`   rb   )rN   rU   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Ä   )rt   r   r‡   rU   r/   r°   r~   r   )rŒ   r½   rÂ   r‚   rƒ   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   rH   r¡   r¦   r±   rs   )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Ò  t |jƒ}t|ƒst d|j|tj¡ | |ƒS |dd„ t 	d¡t 	d¡hD ƒv r4t d|j|¡ | |ƒS | |ƒ}t
|jƒ}d t |j¡¡}|joOtj |j¡}t|jdd ƒ}|rÒ|j d	¡rÒ|j |¡sÒ| |j¡ | d
¡rŒ| d
¡ ¡ D ]}tj tj |j|¡¡}	| |	¡ qwnæ| d¡rÑ| d¡rœ| d¡‰ ng ‰ ‡ fdd„| d¡ ¡ D ƒD ]$}
tj |j|
¡}	| |	¡ | |	d ¡ | |	d ¡ | |	d ¡ q¬n |rÜtd |j¡ƒ‚|j d¡�r| |j¡ tj |j¡d }tj tj |j¡d¡}| |d| ¡ nk|�r|j d¡�rt |ƒD ]}	| |	¡ �qnS|�rjt!|ƒ�}tj "| #¡  $¡ ¡}W d   ƒ n	1 �s<w   Y  ||jk�sRJ d ||j|j¡ƒ‚| |¡ tj tj |¡d¡}| ||j¡ nt %d||j¡ | d¡�r¬| &d¡�r¬| 'd¡D ](}t(|ƒ�rŽt)ƒ }nt*ƒ }| tj ||¡¡ t+�rª| tj ||¡d ¡ �qƒg }|j,dd�}| -¡ D ]}| .t/||dƒ¡ �q¸|j,dd�}| -¡ D ]}| .t/||d ƒ¡ �qÏ|D ]}| |¡ �qÞ|S )!Nz1Not uninstalling %s at %s, outside environment %sc                 S   s   h | ]}|r|’qS r(   r(   rd   r(   r(   r)   rh   Á  s    ýÿz-UninstallPathSet.from_dist.<locals>.<setcomp>ÚstdlibÚ
platstdlibz<Not uninstalling %s at %s, as it is in the standard library.z{}.egg-infor#   z	.egg-infozinstalled-files.txtztop_level.txtznamespace_packages.txtc                    s   g | ]
}|r|ˆ vr|‘qS r(   r(   rd   ©Ú
namespacesr(   r)   Ú
<listcomp>î  s    ýýÿz.UninstallPathSet.from_dist.<locals>.<listcomp>r;   r=   r>   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.z.eggr´   zeasy-install.pthz./r   z;Egg-link {} does not match installed location of {} (at {})z)Not sure how to uninstall: %s - Check: %sÚscriptsz.batÚconsole_scripts)ÚgroupFÚgui_scriptsT)0r   rJ   r   r    r¡   rW   ÚsysÚprefixÚ	sysconfigÚget_pathr   rH   rC   r   Úto_filenameÚegg_infor"   r#   rµ   ÚgetattrÚ	_providerrK   r0   Úhas_metadataÚget_metadataÚ
splitlinesÚnormpathr$   r   rL   rp   r»   rT   Úopenrc   ÚreadlineÚstripr¢   Úmetadata_isdirÚmetadata_listdirr   r   r   r   Úget_entry_mapÚkeysÚextendr*   )Úclsr   Z	dist_pathr'   Zdevelop_egg_linkZdevelop_egg_link_egg_infoZegg_info_existsZdistutils_egg_infoZinstalled_filer#   Ztop_level_pkgZeasy_install_eggZeasy_install_pthÚfhZlink_pointerÚscriptr&   Z_scripts_to_removerÌ   ÚnamerÎ   Úsr(   rÈ   r)   Ú	from_distµ  sÞ   
üþý

ÿÿ
þ
ý
ÿþü



þ
€ÿýÿÿ
ÿÿ
ÿþ
ÿý
€zUninstallPathSet.from_dist)FFr©   )rª   r«   r¬   r­   r   r�   rt   r§   r³   r0   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/   ÚentriesÚ_saved_lines)rŒ   r¸   r(   r(   r)   r�   C  s   
zUninstallPthEntries.__init__r¹   c                 C   s<   t j |¡}trt j |¡d s| dd¡}| j |¡ d S )Nr   ú\ú/)r"   r#   rc   r   Ú
splitdriveÚreplacerë   r0   )rŒ   r¹   r(   r(   r)   r0   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(   )r\   Úliner(   r(   r)   r_   b  s   € z-UninstallPthEntries.remove.<locals>.<genexpr>z
Ú
éÿÿÿÿzutf-8zRemoving entry: %sÚwb)r    r½   rê   r"   r#   r�   ÚwarningrÛ   Ú	readlinesrì   ra   rK   Úencoderë   rÀ   rE   Ú
writelines)rŒ   rä   ÚlinesZendliner¹   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¬   rt   r�   r0   rÀ   r§   r¦   r(   r(   r(   r)   rº   B  s
    
rº   )9r?   r5   r"   rÏ   rÑ   Úimportlib.utilr   Útypingr   r   r   r   r   r   r	   r
   r   Úpip._vendorr   Úpip._vendor.pkg_resourcesr   Úpip._internal.exceptionsr   Úpip._internal.locationsr   r   Úpip._internal.utils.compatr   Úpip._internal.utils.egg_linkr   Úpip._internal.utils.loggingr   r   Úpip._internal.utils.miscr   r   r   r   r   r   r   Úpip._internal.utils.temp_dirr   r   rª   r    rt   r§   r*   r7   rT   rb   r~   r‡   rˆ   r¯   rº   r(   r(   r(   r)   Ú<module>   s:    ,$	*+&"2i  