3
LY                 @   s   d dl mZ d dlZd dlZd dlZd dlmZmZmZ d dl	m
Z
 d dlmZmZmZmZmZ d dlmZ ejeZG dd deZG d	d
 d
eZdS )    )absolute_importN)uses_pycacheWINDOWScache_from_source)UninstallationError)rmtreeaskis_localrenamesnormalize_path)
indent_logc               @   sZ   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dddZ
dd Zdd ZdS )UninstallPathSetzMA set of file paths to be removed in the uninstallation of a
    requirement.c             C   s,   t  | _t  | _i | _|| _d | _g | _d S )N)setpaths_refusepthdistsave_dir_moved_paths)selfr    r   h/private/var/folders/7d/20zwc49s3kn54d3vgq8bd4640000gn/T/pip-build-an2lx5zf/pip/pip/req/req_uninstall.py__init__   s    zUninstallPathSet.__init__c             C   s   t |S )zs
        Return True if the given path is one we are permitted to
        remove/modify, False otherwise.

        )r	   )r   pathr   r   r   
_permitted   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rtr| j	t| d S )N   z.py)osr   splitjoinr   normcaseexistsr   r   addr   splitextr   r   )r   r   headtailr   r   r   r!   #   s    
zUninstallPathSet.addc             C   sL   t |}| j|r<|| jkr*t|| j|< | j| j| n| jj| d S )N)r   r   r   UninstallPthEntriesr!   r   )r   pth_fileentryr   r   r   add_pth6   s    

zUninstallPathSet.add_pthc                s@   t  }x4t|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                s4   g | ],} j |o. t|jtjj tjjkqS r   )
startswithlenrstripr   r   sep).0	shortpath)r   r   r   
<listcomp>G   s   z,UninstallPathSet.compact.<locals>.<listcomp>)r   sortedr+   anyr!   )r   r   short_pathsr   )r   r   compact?   s    

zUninstallPathSet.compactc             C   s&   t jj| jt jj|d jt jjS )Nr   )r   r   r   r   
splitdrivelstripr-   )r   r   r   r   r   _stashM   s    zUninstallPathSet._stashFc             C   s:  | j stjd| jj dS tjd| jj| jj t  t| j| j }|rRd}n"x|D ]}tj| qXW t	dd}| j
rtjd x| j| j
D ]}tj| qW |dkr,tjdd	d
| _x8|D ]0}| j|}tjd| | jj| t|| qW x| jj D ]}|j  qW tjd| jj| jj 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.NzUninstalling %s-%s:yzProceed (y/n)? nz.Not removing or modifying (outside of prefix):z
-uninstallzpip-)suffixprefixzRemoving file or directory %szSuccessfully uninstalled %s-%s)r8   r9   )r   loggerinfor   project_nameversionr   r1   r4   r   r   tempfilemkdtempr   r7   debugr   appendr
   r   valuesremove)r   auto_confirmr   responser   new_pathr   r   r   r   rE   Q   s@    







zUninstallPathSet.removec             C   s~   | j dkrtjd| jj dS tjd| jj x.| jD ]$}| j|}tjd| t	|| q6W x| j
j D ]}|j  qjW dS )z1Rollback the changes previously made by remove().Nz'Can't roll back %s; was not uninstalledFzRolling back uninstall of %szReplacing %s)r   r<   errorr   r>   r=   r   r7   rB   r
   r   rD   rollback)r   r   tmp_pathr   r   r   r   rJ   {   s    


zUninstallPathSet.rollbackc             C   s$   | j dk	r t| j  d| _ g | _dS )z?Remove temporary save dir: rollback will no longer be possible.N)r   r   r   )r   r   r   r   commit   s    

zUninstallPathSet.commitN)F)__name__
__module____qualname____doc__r   r   r!   r(   r4   r7   rE   rJ   rL   r   r   r   r   r      s   	
*r   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
r%   c             C   s0   t jj|std| || _t | _d | _d S )Nz.Cannot remove entries from nonexistent file %s)r   r   isfiler   filer   entries_saved_lines)r   r&   r   r   r   r      s    
zUninstallPthEntries.__init__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   r5   replacerS   r!   )r   r'   r   r   r   r!      s    zUninstallPthEntries.addc             C   s   t jd| j t| jd}|j }|| _W d Q R X tdd |D rLd}nd}xH| jD ]>}y$t jd| |j|| j	d W qX t
k
r   Y qXX qXW t| jd	}|j| W d Q R X d S )
NzRemoving pth entries from %s:rbc             s   s   | ]}d |kV  qdS )s   
Nr   )r.   liner   r   r   	<genexpr>   s    z-UninstallPthEntries.remove.<locals>.<genexpr>z

zRemoving entry: %szutf-8wb)r<   rB   rR   open	readlinesrT   r2   rS   rE   encode
ValueError
writelines)r   fhlinesendliner'   r   r   r   rE      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)rT   r<   rI   rR   rB   r]   ra   )r   rb   r   r   r   rJ      s    

zUninstallPthEntries.rollbackN)rM   rN   rO   r   r!   rE   rJ   r   r   r   r   r%      s   	
r%   )
__future__r   loggingr   r@   
pip.compatr   r   r   pip.exceptionsr   	pip.utilsr   r   r	   r
   r   pip.utils.loggingr   	getLoggerrM   r<   objectr   r%   r   r   r   r   <module>   s   
 