3
g\                 @   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-ee.e/ee. dddZ0e	dee f e	dee f dddZ1e1eee. dddZ2ee. ee. dddZ3ee. ee. dddZ4ee. eee. ee. f d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   sp   t | rt }nt }tjj||}|g}trl|j|d  |j|d  |r^|j|d  n|j|d  |S )zCreate 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(   ]/var/www/tester-filtro-web/env/lib/python3.6/site-packages/pip/_internal/req/req_uninstall.py_script_names   s    r*   .)fnr!   c                s(   t j tttt d fdd}|S )N)argskwr!   c              ?   s6   t  }x* | |D ]}||kr|j| |V  qW d S )N)setadd)r,   r-   seenitem)r+   r(   r)   unique5   s
    
z_unique.<locals>.unique)	functoolswrapsr   r   )r+   r2   r(   )r+   r)   _unique4   s    r5   )r   r!   c             c   s>  yt j| jd}W n tk
r } zdj| d}y&t| jd}| sP|dkrVt W n8 tttfk
r   dj| j	| j
}|dj|7 }Y nX |dj|7 }t||W Y d	d	}~X nX x||D ]t}tjj| j|d
 }|V  |jdrtjj|\}}	|	d	d }
tjj||
d }|V  tjj||
d }|V  qW 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 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   z.py   z.pycz.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@   s2    


rP   )pathsr!   c                sN   t jjt }x:t| tdD ]* t fdd|D }|s|j  qW |S )zCompact 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} j |jd o0 t|jd j kV  qdS )*N)
startswithrstriplen).0Z	shortpath)r#   sepr(   r)   	<genexpr>v   s   zcompact.<locals>.<genexpr>)r"   r#   rX   r.   sortedrV   anyr/   )rQ   Zshort_pathsZshould_skipr(   )r#   rX   r)   compactl   s    
r\   c       	         s   dd | D }t |}tdd |j D td}t  }ttdddx|D ]tfd	d
|D rhqLt  }t  }xPtjD ]B\ }}|j fdd
|D  |j fdd
|D  qW || sL|j	| |j
tj  qLW t t|j||B S )zReturns 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j|qS r(   )r"   r#   normcase)rW   pr(   r(   r)   
<dictcomp>   s    z'compress_for_rename.<locals>.<dictcomp>c             S   s   h | ]}t jj|d  qS )r   )r"   r#   rH   )rW   r^   r(   r(   r)   	<setcomp>   s    z&compress_for_rename.<locals>.<setcomp>)rR   )ar!   c              W   s   t jjt jj|  S )N)r"   r#   r]   r$   )ra   r(   r(   r)   	norm_join   s    z&compress_for_rename.<locals>.norm_joinc             3   s    | ]}t jj j|V  qd S )N)r"   r#   r]   rT   )rW   w)rootr(   r)   rY      s    z&compress_for_rename.<locals>.<genexpr>c             3   s   | ]} |V  qd S )Nr(   )rW   d)dirnamerb   rd   r(   r)   rY      s    c             3   s   | ]} |V  qd S )Nr(   )rW   f)rf   rb   rd   r(   r)   rY      s    )r.   rZ   valuesrV   strr[   r"   walkupdatedifference_updater/   rX   map__getitem__)	rQ   Zcase_map	remainingZ	unchecked	wildcardsZ	all_filesZall_subdirssubdirsfilesr(   )rf   rb   rd   r)   compress_for_rename   s"    
 
rs   c             C   s  t | }t  }t  }t  }xF|D ]>}|jdr0q |jdsBd|krT|jtjj| |j| q W t ttjj|}t|}xt|D ]l}xftj	|D ]X\}}	}
xL|
D ]D}|jdrqtjj
||}tjj|rtjj||kr|j| qW qW qW |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.
    z.pycz__init__.pyz
.dist-infoc             S   s   h | ]}t jj|d qS )rS   )r"   r#   r$   )rW   folderr(   r(   r)   r`      s    z.compress_for_output_listing.<locals>.<setcomp>)r.   rG   r/   r"   r#   rf   rm   r]   r\   rj   r$   isfile)rQ   will_remove	will_skipZfoldersrr   r#   Z_normcased_filesrt   dirpath_ZdirfilesfnameZfile_r(   r(   r)   compress_for_output_listing   s.    




r{   c               @   s|   e Zd ZdZddddZeedddZeedd	d
ZeedddZddddZ	ddddZ
eedddZdS )StashedUninstallPathSetzWA set of file rename operations to stash files while
    tentatively uninstalling them.N)r!   c             C   s   i | _ g | _d S )N)
_save_dirs_moves)selfr(   r(   r)   __init__   s    z StashedUninstallPathSet.__init__)r#   r!   c             C   sD   yt |}W n tk
r*   tdd}Y nX || jtjj|< |jS )zStashes 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   rB   r   r}   r"   r#   r]   )r   r#   save_dirr(   r(   r)   _get_directory_stash   s    z,StashedUninstallPathSet._get_directory_stashc             C   s   t jj|}t jj|d }}d}xd||krfy| j| }P W n tk
rP   Y nX t jj|| }}q$W t jj|}tdd}|| j|< t jj||}|r|t jjkrt jj	|j|S |jS )zStashes 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#   r]   rf   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j|ot jj| }|r*| j|}n
| j|}| jj||f |r^t jj|r^t j| t	|| |S )zStashes 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   s0   x| j j D ]\}}|j  qW g | _i | _ dS )z0Commits the uninstall by removing stashed files.N)r}   itemscleanupr~   )r   ry   r   r(   r(   r)   commit  s    zStashedUninstallPathSet.commitc             C   s   x| j D ]}tjd|  qW x| j D ]\}}yTtjd|| tjj|sVtjj|rbtj| ntjj	|rvt
| t|| W q& tk
r } ztjd| tjd| W Y dd}~X q&X q&W | j  dS )z2Undoes the uninstall by moving stashed files back.Moving to %s
 from %szReplacing %s from %szFailed to restore %szException: %sN)r   )r~   loggerinfodebugr"   r#   ru   r   unlinkr   r   r   rB   errorr   )r   r^   r   r#   exr(   r(   r)   rollback$  s    "z StashedUninstallPathSet.rollbackc             C   s
   t | jS )N)boolr~   )r   r(   r(   r)   can_rollback7  s    z$StashedUninstallPathSet.can_rollback)__name__
__module____qualname____doc__r   ri   r   r   r   r   r   propertyr   r   r(   r(   r(   r)   r|      s   r|   c               @   s   e Zd ZdZeddddZeedddZeddd	d
Z	eeddddZ
deeddddZeedddZddddZddddZeed dddZdS )UninstallPathSetzMA set of file paths to be removed in the uninstallation of a
    requirement.N)r   r!   c             C   s(   t  | _t  | _i | _|| _t | _d S )N)r.   rQ   _refusepthr   r|   _moved_paths)r   r   r(   r(   r)   r   @  s
    zUninstallPathSet.__init__)r#   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j|\}}t jjt|t jj|}t jj|s:d S | j|rR| jj	| n| j
j	| t jj|d dkr| j	t| d S )N   z.py)r"   r#   rH   r$   r   r]   existsr   rQ   r/   r   splitextr   )r   r#   r   tailr(   r(   r)   r/   O  s    
zUninstallPathSet.add)pth_fileentryr!   c             C   sL   t |}| j|r<|| jkr*t|| j|< | j| j| n| jj| d S )N)r   r   r   UninstallPthEntriesr/   r   )r   r   r   r(   r(   r)   add_pthb  s    

zUninstallPathSet.add_pthF)auto_confirmverboser!   c             C   s   | j stjd| jj dS | jjd | jj }tjd| t x |sP| j|r| j}t	| j }x*t
t|D ]}|j| tjd| qnW x| jj D ]}|j  qW tjd| W dQ R X 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)rQ   r   r   r   rD   rE   r   _allowed_to_proceedr   rs   rZ   r\   r   r   r   rh   remove)r   r   r   Zdist_name_versionmovedZ
for_renamer#   r   r(   r(   r)   r   k  s"    


zUninstallPathSet.remove)r   r!   c             C   s|   t tt  dddd}|s*t| j\}}nt| j}t }|d| |d| |d| j |rn|dt| j td	ddkS )z@Display which files would be deleted and prompt for confirmationN)rJ   rQ   r!   c          
   S   sH   |sd S t j|  t & xtt|D ]}t j| q(W W d Q R X d S )N)r   r   r   rZ   r\   )rJ   rQ   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   )ri   r   r{   rQ   r.   r   rs   r   )r   r   r   rv   rw   r(   r(   r)   r     s    	


z$UninstallPathSet._allowed_to_proceed)r!   c             C   sV   | j jstjd| jj dS tjd| jj | j j  x| jj	 D ]}|j  qBW 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   rD   r   r   r   rh   )r   r   r(   r(   r)   r     s    

zUninstallPathSet.rollbackc             C   s   | j 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jd|j|tj | |S |dd tj	dtj	dhD krhtjd|j| | |S | |}t
|j}djtj|j}|jotjj|j}t|jdd }|o|jjd	o|jj| r|j|j |jd
r"x|jd
j D ]&}tjjtjj|j|}	|j|	 qW n|jdr|jdrF|jd ng  xj fdd|jdj D D ]J}
tjj|j|
}	|j|	 |j|	d  |j|	d  |j|	d  qhW n4|rtdj|jn|jjdr*|j|j tjj|jd }tjjtjj|jd}|j|d|  n|r^|jjdr^xt |D ]}	|j|	 qHW n|rt!|}tjj"|j# j$ }W d Q R X ||jkst%dj||j|j|j| tjjtjj|d}|j||j ntj&d||j |jdrf|j'drfx^|j(dD ]P}t)|r(t* }nt+ }|jtjj|| t,r|jtjj||d  qW g }|j-dd}x$|j. D ]}|j/t0||d qW |j-dd}x$|j. D ]}|j/t0||d  qW x|D ]}|j| qW |S )!Nz1Not uninstalling %s at %s, outside environment %sc             S   s   h | ]}|r|qS r(   r(   )rW   r^   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-infor#   z	.egg-infozinstalled-files.txtztop_level.txtznamespace_packages.txtc                s   g | ]}|r| kr|qS r(   r(   )rW   r^   )
namespacesr(   r)   
<listcomp>  s   z.UninstallPathSet.from_dist.<locals>.<listcomp>z.pyz.pycz.pyozCannot 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./z
.dist-infoz;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)1r   rF   r   r   r   rR   sysprefix	sysconfigget_pathr   rD   r?   r   to_filenameegg_infor"   r#   r   getattr	_providerrG   r/   has_metadataget_metadata
splitlinesnormpathr$   r   rH   rf   r   rP   openr]   readlinestripAssertionErrorr   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)FF)r   r   r   r   r   r   ri   r   r   r/   r   r   r   r   r   classmethodr   r(   r(   r(   r)   r   <  s   	r   c               @   sH   e Zd ZeddddZeddddZddd	d
ZedddZdS )r   N)r   r!   c             C   s   || _ t | _d | _d S )N)filer.   entries_saved_lines)r   r   r(   r(   r)   r   C  s    zUninstallPthEntries.__init__)r   r!   c             C   s>   t jj|}tr.t jj|d  r.|jdd}| jj| d S )Nr   \/)r"   r#   r]   r   
splitdrivereplacer   r/   )r   r   r(   r(   r)   r/   H  s    
zUninstallPthEntries.add)r!   c             C   s  t jd| j tjj| js.t jd| j d S t| jd}|j }|| _	W d Q R X t
dd |D rld}nd}|r|d j|jd	 r|d |jd	 |d< xH| jD ]>}y$t jd
| |j|| jd	 W q tk
r   Y qX qW t| jd}|j| W d Q R X d S )NzRemoving pth entries from %s:z.Cannot remove entries from nonexistent file %srbc             s   s   | ]}d |kV  qdS )s   
Nr(   )rW   liner(   r(   r)   rY   b  s    z-UninstallPthEntries.remove.<locals>.<genexpr>z

r   zutf-8zRemoving entry: %swbr   r   )r   r   r   r"   r#   ru   warningr   	readlinesr   r[   rG   encoder   r   rA   
writelines)r   r   linesZendliner   r(   r(   r)   r   W  s(    
zUninstallPthEntries.removec          	   C   sR   | j d krtjd| j dS tjd| j t| jd}|j| j  W d Q R X 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.rollback)	r   r   r   ri   r   r/   r   r   r   r(   r(   r(   r)   r   B  s   r   )9r;   r3   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   ri   r   r*   r5   rP   r\   rs   r{   r|   r   r   r(   r(   r(   r)   <module>   s8   ,$	(+"$2i  