U
    «ÃT_I  ã                   @   sR  d dl mZ d dlZ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 d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZmZ d dlmZmZmZ d dl m!Z! d dl"m#Z# e#�rd dl$Z$d dl$m%Z% d dl&m'Z'm(Z(m)Z)m*Z* d dl+m,Z, dZ-e .e/¡Z0ddd„Z1dd„ Z2G dd„ de3ƒZ4dd„ Z5dd„ Z6dS )é    )Úabsolute_importN)Úpkg_resources)Úversion)Úensure_binary)ÚLinkCollector)ÚPackageFinder)ÚSearchScope)ÚSelectionPreferences)Úadjacent_tmp_fileÚcheck_path_ownerÚreplace)Ú
ensure_dirÚget_installed_versionÚredact_auth_from_url)Úget_installer)ÚMYPY_CHECK_RUNNING)ÚValues)ÚAnyÚDictÚTextÚUnion)Ú
PipSessionz%Y-%m-%dT%H:%M:%SZFc                 C   s`   |j g|j }|jr8|s8t dd dd„ |D ƒ¡¡ g }|jp@g }tj||d�}t	| |d�}|S )z¶
    :param session: The Session to use to make requests.
    :param suppress_no_index: Whether to ignore the --no-index option
        when constructing the SearchScope object.
    zIgnoring indexes: %sú,c                 s   s   | ]}t |ƒV  qd S ©N)r   )Ú.0Úurl© r   úE/tmp/pip-unpacked-wheel-8u7dkzi1/pip/_internal/self_outdated_check.pyÚ	<genexpr>?   s     z&make_link_collector.<locals>.<genexpr>)Ú
find_linksÚ
index_urls)ÚsessionÚsearch_scope)
Ú	index_urlÚextra_index_urlsÚno_indexÚloggerÚdebugÚjoinr   r   Úcreater   )r!   ÚoptionsÚsuppress_no_indexr    r   r"   Úlink_collectorr   r   r   Úmake_link_collector0   s    
þ
 ÿr-   c                 C   s   t | ƒ}t |¡ ¡ }|S r   )r   ÚhashlibÚsha224Ú	hexdigest)ÚkeyÚ	key_bytesÚnamer   r   r   Ú_get_statefile_nameO   s    r4   c                   @   s(   e Zd Zdd„ Zedd„ ƒZdd„ ZdS )ÚSelfCheckStatec              
   C   sn   i | _ d | _|rjtj |dt| jƒ¡| _z&t| jƒ�}t 	|¡| _ W 5 Q R X W n t
ttfk
rh   Y nX d S )NÚ	selfcheck)ÚstateÚstatefile_pathÚosÚpathr(   r4   r1   ÚopenÚjsonÚloadÚIOErrorÚ
ValueErrorÚKeyError)ÚselfÚ	cache_dirÚ	statefiler   r   r   Ú__init__W   s      ÿzSelfCheckState.__init__c                 C   s   t jS r   )ÚsysÚprefix)rA   r   r   r   r1   i   s    zSelfCheckState.keyc              	   C   s¦   | j s
d S ttj | j ¡ƒs d S ttj | j ¡ƒ | j| t¡|dœ}t	j
|ddd�}t| j ƒ�}| t|ƒ¡ W 5 Q R X zt|j| j ƒ W n tk
r    Y nX d S )N)r1   Ú
last_checkÚpypi_versionT)r   ú:)Ú	sort_keysÚ
separators)r8   r   r9   r:   Údirnamer   r1   ÚstrftimeÚSELFCHECK_DATE_FMTr<   Údumpsr
   Úwriter   r   r3   ÚOSError)rA   rH   Úcurrent_timer7   ÚtextÚfr   r   r   Úsavem   s     ûzSelfCheckState.saveN)Ú__name__Ú
__module__Ú__qualname__rD   Úpropertyr1   rU   r   r   r   r   r5   V   s   
r5   c                 C   s6   zt  | ¡}dt|ƒkW S  t jk
r0   Y dS X dS )zµChecks whether pkg was installed by pip

    This is used not to display the upgrade message when pip is in fact
    installed by system package manager, such as dnf on Fedora.
    ÚpipFN)r   Úget_distributionr   ÚDistributionNotFound)ÚpkgÚdistr   r   r   Úwas_installed_by_pip‘   s
    
r_   c                 C   sT  t dƒ}|sdS t |¡}d}�z
t|jd�}tj ¡ }d|jkrzd|jkrztj |jd t	¡}||  
¡ dk rz|jd }|dkrÚt| |dd�}td	d	d
�}	tj||	d�}
|
 d¡j}|dkrÄW dS t|jƒ}| ||¡ t |¡}||k oþ|j|jkoþtdƒ}|�sW dS d tj¡}t d|||¡ W n$ tk
�rN   tjddd� Y nX dS )zÛCheck for an update for pip.

    Limit the frequency of checks to once per week. State is stored either in
    the active virtualenv or in the user's USER_CACHE_DIR keyed off the prefix
    of the pip script path.
    rZ   N)rB   rG   rH   i€:	 T)r*   r+   F)Úallow_yankedÚallow_all_prereleases)r,   Úselection_prefsz	{} -m pipz‰You are using pip version %s; however, version %s is available.
You should consider upgrading via the '%s install --upgrade pip' command.z5There was an error checking the latest version of pip)Úexc_info)r   Úpackaging_versionÚparser5   rB   ÚdatetimeÚutcnowr7   ÚstrptimerN   Útotal_secondsr-   r	   r   r)   Úfind_best_candidateÚbest_candidateÚstrr   rU   Úbase_versionr_   ÚformatrE   Ú
executabler&   ÚwarningÚ	Exceptionr'   )r!   r*   Úinstalled_versionÚpip_versionrH   r7   rR   rG   r,   rb   Úfinderrk   Úremote_versionÚlocal_version_is_olderÚpip_cmdr   r   r   Úpip_self_version_checkŸ   sl    

þ
ýþþ


ÿý  üþrx   )F)7Ú
__future__r   rf   r.   r<   ÚloggingÚos.pathr9   rE   Úpip._vendorr   Zpip._vendor.packagingr   rd   Zpip._vendor.sixr   Úpip._internal.index.collectorr   Ú"pip._internal.index.package_finderr   Ú!pip._internal.models.search_scoper   Ú$pip._internal.models.selection_prefsr	   Úpip._internal.utils.filesystemr
   r   r   Úpip._internal.utils.miscr   r   r   Úpip._internal.utils.packagingr   Úpip._internal.utils.typingr   Úoptparser   Útypingr   r   r   r   Úpip._internal.network.sessionr   rN   Ú	getLoggerrV   r&   r-   r4   Úobjectr5   r_   rx   r   r   r   r   Ú<module>   s<   
 ý
;