U
    cUh-]                     @   s  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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*e+e+e,e
e+ddf dddZ-ede
eddf f ede
eddf f dddZ.e.ee
e+ddf dddZ/ee+ ee+ dddZ0ee+ ee+ dddZ1ee+ eee+ ee+ f dddZ2G dd dZ3G d d! d!Z4G d"d# d#Z5dS )$    N)cache_from_source)	AnyCallableDict	GeneratorIterableListOptionalSetTuple)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)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 )zCreate 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   Zexe_name r#   _/opt/bot/vods_filmes/script/venv/lib/python3.8/site-packages/pip/_internal/req/req_uninstall.py_script_names   s    r%   .)fnr   c                    s.   t  ttttd d f d fdd}|S )N)argskwr   c                  ?   s2   t  } | |D ]}||kr|| |V  qd S N)setadd)r'   r(   seenitemr&   r#   r$   unique+   s
    
z_unique.<locals>.unique)	functoolswrapsr   r   )r&   r/   r#   r.   r$   _unique(   s    "r2   distr   c                 c   s   | j }|dk	std|  }|dk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AssertionErroriter_declared_entriesr   r    r!   r"   endswithsplit)r4   r;   entriesentryr!   dnr&   baser#   r#   r$   uninstallation_paths6   s    

rD   )pathsr   c                    sJ   t jjt }t| tdD ]* t fdd|D }|s|  q|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}  |d o0 t|d  kV  qdS )*N)
startswithrstriplen).0Z	shortpathr!   sepr#   r$   	<genexpr>a   s   zcompact.<locals>.<genexpr>)r    r!   rN   r*   sortedrK   anyr+   )rE   Zshort_pathsZshould_skipr#   rM   r$   compactX   s    rR   c           	         s   dd | D }t |}tdd | D td}t  }tt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 )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||qS r#   )r    r!   normcaserL   pr#   r#   r$   
<dictcomp>q   s     
 z'compress_for_rename.<locals>.<dictcomp>c                 S   s   h | ]}t j|d  qS )r   )r    r!   r?   rT   r#   r#   r$   	<setcomp>s   s     z&compress_for_rename.<locals>.<setcomp>rF   )ar   c                  W   s   t jt jj|  S r)   )r    r!   rS   r"   )rX   r#   r#   r$   	norm_joinv   s    z&compress_for_rename.<locals>.norm_joinc                 3   s    | ]}t j |V  qd S r)   )r    r!   rS   rI   )rL   w)rootr#   r$   rO   z   s     z&compress_for_rename.<locals>.<genexpr>c                 3   s   | ]} |V  qd S r)   r#   )rL   ddirnamerY   r[   r#   r$   rO      s     c                 3   s   | ]} |V  qd S r)   r#   )rL   fr]   r#   r$   rO      s     )r*   rP   valuesrK   strrQ   r    walkupdatedifference_updater+   rN   map__getitem__)	rE   Zcase_map	remainingZ	unchecked	wildcardsZ	all_filesZall_subdirssubdirsfilesr#   r]   r$   compress_for_renamek   s"    
rk   c                 C   s   t | }t  }t  }t  }|D ]>}|dr.q|ds@d|krR|tj| || qt ttjj|}t|}|D ]d}t	|D ]T\}}	}
|
D ]D}|drqtj
||}tj|rtj||kr|| qqq||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$   rW      s     z.compress_for_output_listing.<locals>.<setcomp>)r*   r>   r+   r    r!   r^   re   rS   rR   rb   r"   isfile)rE   will_remove	will_skipZfoldersrj   r!   Z_normcased_filesrl   dirpath_ZdirfilesfnameZfile_r#   r#   r$   compress_for_output_listing   s2    


rs   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 r)   )
_save_dirs_movesselfr#   r#   r$   __init__   s    z StashedUninstallPathSet.__init__r!   r   c                 C   sD   zt |}W n tk
r*   tdd}Y nX || jt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   OSErrorr   rv   r    r!   rS   )ry   r!   save_dirr#   r#   r$   _get_directory_stash   s    z,StashedUninstallPathSet._get_directory_stashc                 C   s   t j|}t j|d }}d}||krfz| j| }W qW n tk
rP   Y nX t j|| }}q"t j|}tdd}|| j|< t j||}|r|t jjkrt 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!   rS   r^   rv   KeyErrorr   relpathcurdirr"   )ry   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 )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   rw   appendrmdirr   )ry   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)rv   r`   cleanuprw   )ry   r   r#   r#   r$   commit  s    
zStashedUninstallPathSet.commitc                 C   s   | j D ]}tjd|  q| j D ]\}}zTtd|| tj|sPtj|r\t| ntj	|rpt
| t|| W q  tk
r } ztd| td| W 5 d}~X Y q X q |   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   )rw   loggerinfodebugr    r!   rm   r   unlinkr   r   r   r   errorr   )ry   rU   r   r!   exr#   r#   r$   rollback  s    
 z StashedUninstallPathSet.rollbackc                 C   s
   t | jS r)   )boolrw   rx   r#   r#   r$   can_rollback"  s    z$StashedUninstallPathSet.can_rollback)__name__
__module____qualname____doc__rz   ra   r   r   r   r   r   propertyr   r   r#   r#   r#   r$   rt      s   rt   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.Nr3   c                 C   s4   t  | _t  | _i | _|| _t | _tt	| _
d S r)   )r*   _paths_refuse_pth_distrt   _moved_pathsr0   	lru_cacher   _normalize_path_cached)ry   r4   r#   r#   r$   rz   +  s    zUninstallPathSet.__init__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)ry   r!   r#   r#   r$   
_permitted6  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   rS   existsr   r   r+   r   splitextr   )ry   r!   r   tailr#   r#   r$   r+   A  s    
zUninstallPathSet.add)pth_filerA   r   c                 C   sN   |  |}| |r>|| jkr,t|| j|< | j| | n| j| d S r)   )r   r   r   UninstallPthEntriesr+   r   )ry   r   rA   r#   r#   r$   add_pthT  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 p |sR| |r| j}t	| j }t
t|D ]}|| td| qn| j D ]}|  qtd| W 5 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)r   r   r   r   raw_nameraw_versionr   _allowed_to_proceedr   rk   rP   rR   r   r   r   r`   remove)ry   r   r   Zdist_name_versionmovedZ
for_renamer!   pthr#   r#   r$   r   ]  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)msgrE   r   c              	   S   sD   |sd S t |  t " tt|D ]}t | q&W 5 Q R X d S r)   )r   r   r   rP   rR   )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   )ra   r   rs   r   r*   r   rk   r   )ry   r   r   rn   ro   r#   r#   r$   r   z  s    	


z$UninstallPathSet._allowed_to_proceedru   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   r`   )ry   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   rx   r#   r#   r$   r     s    zUninstallPathSet.commitc              
      s  |j }|j}|d kr*td|j | |S t|}|jsTtd|j|tj | |S |dd t	
dt	
dhD krtd|j| | |S | |}t|j}|jo|d k	otj|o||j d }|r|d k	r|| | }|d k	r|D ]}	|tj||	 qn|d	rz|d
}
W n tk
rL   g  Y nX |
jdd  fdd|d	 D D ]N}tj||}|| || d || d || d qvn,|jrt|dn|jr*|| tj|d }tjtj|d}||d|  n|j rNt!|D ]}|| q:n|rt"|$}tj#|$ % }|&|}W 5 Q R X tj'||st(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k
rn   Y nX t0t1t2t1d d f 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#   rT   r#   r#   r$   rW     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| kr|qS r#   r#   rT   Z
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.bat)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_remove2  s
    

z:UninstallPathSet.from_dist.<locals>.iter_scripts_to_remove)3r;   info_locationr   r   canonical_namer   localr   r   	sysconfigget_pathr   r   "installed_with_setuptools_egg_infor    r!   r   r>   setuptools_filenamer+   r=   r"   is_file	read_textFileNotFoundError
splitlinesinstalled_by_distutilsr   installed_as_eggr?   r^   r   installed_with_dist_inforD   openrS   readlinestripr   samefiler<   r   in_usersiter   r   iter_distutils_script_namesr   NotADirectoryErrorr   ra   r   )clsr4   Zdist_locationr   Znormalized_dist_locationZpaths_to_removeZdevelop_egg_linkZsetuptools_flat_installationZinstalled_filesZinstalled_fileZnamespace_packagesZtop_level_pkgr!   Zeasy_install_eggZeasy_install_pthfhZlink_pointerZnormalized_link_pointerr   scriptr   sr#   r   r$   	from_dist  s    










 

 "
zUninstallPathSet.from_dist)FF)r   r   r   r   r   rz   ra   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 r)   )filer*   r@   _saved_lines)ry   r   r#   r#   r$   rz   C  s    zUninstallPthEntries.__init__)rA   r   c                 C   s<   t j|}tr,t j|d s,|dd}| j| d S )Nr   \/)r    r!   rS   r   
splitdrivereplacer@   r+   )ry   rA   r#   r#   r$   r+   H  s    
zUninstallPthEntries.addru   c              	   C   s
  t d| j tj| js.t d| j d S t| jd}| }|| _	W 5 Q R X t
dd |D rld}nd}|r|d |d	s|d |d	 |d< | jD ]>}z$t d
| ||| d	 W q tk
r   Y qX qt| jd}|| W 5 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#   )rL   liner#   r#   r$   rO   b  s     z-UninstallPthEntries.remove.<locals>.<genexpr>z

zutf-8zRemoving entry: %swb)r   r   r   r    r!   rm   warningr   	readlinesr   rQ   r>   encoder@   r   
ValueError
writelines)ry   r   linesZendlinerA   r#   r#   r$   r   W  s(    
zUninstallPthEntries.removec              	   C   sR   | j d krtd| j dS td| j t| jd}|| j  W 5 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   )ry   r   r#   r#   r$   r   r  s    
zUninstallPthEntries.rollback)	r   r   r   ra   rz   r+   r   r   r   r#   r#   r#   r$   r   B  s   r   )6r0   r    r   r   importlib.utilr   typingr   r   r   r   r   r   r	   r
   r   pip._internal.exceptionsr   r   pip._internal.locationsr   r   pip._internal.metadatar   pip._internal.utils.compatr   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   ra   r   r%   r2   rD   rR   rk   rs   rt   r   r   r#   r#   r#   r$   <module>   sB   ,  !"$1i  