o
    |ŸŽi‚_  ã                	   @   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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)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„Z0e0dedee- fdd„ƒZ1dee- dee- fdd„Z2dee- dee- fdd„Z3dee- deee- ee- f fdd„Z4G dd „ d ƒZ5G d!d"„ d"ƒZ6G d#d$„ d$ƒZ7d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)Ú	getLoggerÚ
indent_log)ÚaskÚdist_in_usersiteÚdist_is_localÚegg_link_pathÚ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.pyN)r   r   r   ÚosÚpathÚjoinr   Úappend)r   r   r    Úbin_dirÚexe_nameÚpaths_to_remove© r)   úiC:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\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,   r5   r)   r4   r*   Ú_unique4   s    r8   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   ÚrÚmissing_record_exceptionÚmsgÚ	installerÚdepÚrowr#   Údnr,   Úbaser)   r)   r*   Úuninstallation_paths?   s@   €ÿÿþ
€ô
€÷rV   Ú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>r   s   € þ
þzcompact.<locals>.<genexpr>N)r"   r#   ra   r0   Úsortedr]   Úanyr1   )rW   Úshort_pathsÚshould_skipr)   r`   r*   Úcompacti   s   ý
€rg   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>‚   s    z'compress_for_rename.<locals>.<dictcomp>c                 S   s   h | ]
}t j |¡d  ’qS )r   )r"   r#   rM   ri   r)   r)   r*   Ú	<setcomp>„   s    z&compress_for_rename.<locals>.<setcomp>rX   Úar!   c                  W   s   t j t jj| Ž ¡S r/   )r"   r#   rh   r$   )rm   r)   r)   r*   Ú	norm_join‡   s   z&compress_for_rename.<locals>.norm_joinc                 3   s"   � | ]}t j ˆ ¡ |¡V  qd S r/   )r"   r#   rh   r[   )r^   Úw)Úrootr)   r*   rb   ‹   s   € ÿz&compress_for_rename.<locals>.<genexpr>c                 3   ó   � | ]	}ˆˆˆ |ƒV  qd S r/   r)   )r^   Úd©Údirnamern   rp   r)   r*   rb   “   ó   € ÿc                 3   rq   r/   r)   )r^   Úfrs   r)   r*   rb   •   ru   N)r0   rc   Úvaluesr]   Ústrrd   r"   ÚwalkÚupdateÚdifference_updater1   ra   ÚmapÚ__getitem__)	rW   Úcase_mapÚ	remainingÚ	uncheckedÚ	wildcardsÚ	all_filesÚall_subdirsÚsubdirsÚfilesr)   rs   r*   Úcompress_for_rename|   s0   ÿÿ
ÿ
€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 )rZ   )r"   r#   r$   )r^   Úfolderr)   r)   r*   rl   Ì   s    ÿz.compress_for_output_listing.<locals>.<setcomp>N)r0   rL   r1   r"   r#   rt   r|   rh   rg   ry   r$   Úisfile)rW   Úwill_removeÚ	will_skipÚfoldersr…   r#   Ú_normcased_filesrˆ   ÚdirpathÚ_ÚdirfilesÚfnameÚfile_r)   r)   r*   Úcompress_for_output_listing¡   s:   

ÿ
€øÿÿ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©ÚkindN)r   rG   r   r•   r"   r#   rh   )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 qC 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#   rh   rt   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.
        N)
r"   r#   ÚisdirÚislinkrž   r¤   r–   r%   Ú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#   r‰   r¦   Úunlinkr¥   r   r   rG   Úerrorr­   )r˜   rj   r©   r#   Úexr)   r)   r*   Úrollback#  s    
€þz StashedUninstallPathSet.rollbackc                 C   s
   t | jƒS r/   )Úboolr–   r—   r)   r)   r*   Úcan_rollback6  s   
z$StashedUninstallPathSet.can_rollback©r!   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r™   rx   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/   )r0   rW   Ú_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.

        N)r   )r˜   r#   r)   r)   r*   Ú
_permittedE  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#   rM   r$   r   rh   ÚexistsrÁ   rW   r1   r¾   Úsplitextr   )r˜   r#   r¢   Útailr)   r)   r*   r1   M  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¿   ÚUninstallPthEntriesr1   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|¡ 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)rW   r®   r¯   r   rI   rJ   r   Ú_allowed_to_proceedrÀ   r†   rc   rg   rª   rË   r¿   rw   Úremove)r˜   rÊ   rË   Údist_name_versionÚmovedÚ
for_renamer#   r¿   r)   r)   r*   rÎ   i  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 )zIDisplay which files would be deleted and prompt for confirmation
        rP   rW   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   rc   rg   )rP   rW   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Ô   )rx   r   r“   rW   r0   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   rI   r¯   r´   r¿   rw   )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
|ƒ}d t |j¡¡}|joNtj |j¡}t|jdd ƒ}|rÐ|j d	¡rÐ|j |¡sÐ| |j¡ | d
¡rŠ| d
¡ ¡ D ]ø}tj tj |j|¡¡}	| |	¡ qv| 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| ¡ nj|�r|j d¡�rt |ƒD ][}	| |	¡ �q|�rgt!|ƒ�}tj "| #¡  $¡ ¡}W d   ƒ n	1 �s9w   Y  ||jk�sOJ 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)   ri   r)   r)   r*   rl   Â  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)   ri   ©Ú
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   rK   r   r®   r¯   rY   ÚsysÚprefixÚ	sysconfigÚget_pathr   rD   r   Úto_filenamerI   Úegg_infor"   r#   rÃ   ÚgetattrÚ	_providerrL   r1   Úhas_metadataÚget_metadataÚ
splitlinesÚnormpathr$   r   rM   rt   rÉ   rV   Úopenrh   ÚreadlineÚstripr°   Úmetadata_isdirÚmetadata_listdirr   r   r   r   Úget_entry_mapÚkeysÚextendr+   )Úclsr   Ú	dist_pathr(   Údevelop_egg_linkÚdevelop_egg_link_egg_infoÚegg_info_existsÚdistutils_egg_infoÚinstalled_filer#   Útop_level_pkgÚeasy_install_eggÚeasy_install_pthÚfhÚlink_pointerÚscriptr&   Ú_scripts_to_removerÜ   ÚnamerÞ   Úsr)   rØ   r*   Ú	from_dist¶  sÖ   
ü
ÿý
ÿ
ÿ
ÿÿÿ



þ
óÿýÿ
ÿ
ÿþ
ÿþ
€zUninstallPathSet.from_dist)FFr·   )r¸   r¹   rº   r»   r   r™   rx   rµ   rÁ   r1   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/   )Úfiler0   ÚentriesÚ_saved_lines)r˜   rÆ   r)   r)   r*   r™   ;  s   
zUninstallPthEntries.__init__rÇ   c                 C   s<   t j |¡}trt j |¡d s| dd¡}| j |¡ d S )Nr   ú\ú/)r"   r#   rh   r   Ú
splitdriveÚreplacer  r1   )r˜   rÇ   r)   r)   r*   r1   @  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*   rb   \  s   € z-UninstallPthEntries.remove.<locals>.<genexpr>z
Ú
éÿÿÿÿzutf-8zRemoving entry: %sÚwb)r®   rË   r  r"   r#   r‰   Úwarningrë   Ú	readlinesr  rd   rL   Úencoder  rÎ   rF   Ú
writelines)r˜   rý   ÚlinesÚendlinerÇ   r)   r)   r*   rÎ   O  s2   ÿý
ÿ"ÿ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´   l  s   
ÿ
ÿþzUninstallPthEntries.rollbackr·   )	r¸   r¹   rº   rx   r™   r1   rÎ   rµ   r´   r)   r)   r)   r*   rÈ   :  s
    
rÈ   )8r@   r6   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.loggingr   r   Úpip._internal.utils.miscr   r   r   r   r   r   r   r   Úpip._internal.utils.temp_dirr   r   r¸   r®   rx   rµ   r+   r8   rV   rg   r†   r“   r”   r½   rÈ   r)   r)   r)   r*   Ú<module>   s8    ,(
*)&%2h  