<s> from __future__ import absolute_import <EOL> import datetime <EOL> import json <EOL> import logging <EOL> import os . path <EOL> import sys <EOL> from pip . _vendor import lockfile <EOL> from pip . _vendor . packaging import version as packaging_version <EOL> from pip . compat import total_seconds , WINDOWS <EOL> from pip . models import PyPI <EOL> from pip . locations import USER_CACHE_DIR , running_under_virtualenv <EOL> from pip . utils import ensure_dir , get_installed_version <EOL> from pip . utils . filesystem import check_path_owner <EOL> SELFCHECK_DATE_FMT = "<STR_LIT>" <EOL> logger = logging . getLogger ( __name__ ) <EOL> class VirtualenvSelfCheckState ( object ) : <EOL> def __init__ ( self ) : <EOL> self . statefile_path = os . path . join ( sys . prefix , "<STR_LIT>" ) <EOL> try : <EOL> with open ( self . statefile_path ) as statefile : <EOL> self . state = json . load ( statefile ) <EOL> except ( IOError , ValueError ) : <EOL> self . state = { } <EOL> def save ( self , pypi_version , current_time ) : <EOL> with open ( self . statefile_path , "<STR_LIT:w>" ) as statefile : <EOL> json . dump ( <EOL> { <EOL> "<STR_LIT>" : current_time . strftime ( SELFCHECK_DATE_FMT ) , <EOL> "<STR_LIT>" : pypi_version , <EOL> } , <EOL> statefile , <EOL> sort_keys = True , <EOL> separators = ( "<STR_LIT:U+002C>" , "<STR_LIT::>" ) <EOL> ) <EOL> class GlobalSelfCheckState ( object ) : <EOL> def __init__ ( self ) : <EOL> self . statefile_path = os . path . join ( USER_CACHE_DIR , "<STR_LIT>" ) <EOL> try : <EOL> with open ( self . statefile_path ) as statefile : <EOL> self . state = json . load ( statefile ) [ sys . prefix ] <EOL> except ( IOError , ValueError , KeyError ) : <EOL> self . state = { } <EOL> def save ( self , pypi_version , current_time ) : <EOL> if not check_path_owner ( os . path . dirname ( self . statefile_path ) ) : <EOL> return <EOL> ensure_dir ( os . path . dirname ( self . statefile_path ) ) <EOL> with lockfile . LockFile ( self . statefile_path ) : <EOL> if os . path . exists ( self . statefile_path ) : <EOL> with open ( self . statefile_path ) as statefile : <EOL> state = json . load ( statefile ) <EOL> else : <EOL> state = { } <EOL> state [ sys . prefix ] = { <EOL> "<STR_LIT>" : current_time . strftime ( SELFCHECK_DATE_FMT ) , <EOL> "<STR_LIT>" : pypi_version , <EOL> } <EOL> with open ( self . statefile_path , "<STR_LIT:w>" ) as statefile : <EOL> json . dump ( state , statefile , sort_keys = True , <EOL> separators = ( "<STR_LIT:U+002C>" , "<STR_LIT::>" ) ) <EOL> def load_selfcheck_statefile ( ) : <EOL> if running_under_virtualenv ( ) : <EOL> return VirtualenvSelfCheckState ( ) <EOL> else : <EOL> return GlobalSelfCheckState ( ) <EOL> def pip_version_check ( session ) : <EOL> """<STR_LIT>""" <EOL> installed_version = get_installed_version ( "<STR_LIT>" ) <EOL> if installed_version is None : <EOL> return <EOL> pip_version = packaging_version . parse ( installed_version ) <EOL> pypi_version = None <EOL> try : <EOL> state = load_selfcheck_statefile ( ) <EOL> current_time = datetime . datetime . utcnow ( ) <EOL> if "<STR_LIT>" in state . state and "<STR_LIT>" in state . state : <EOL> last_check = datetime . datetime . strptime ( <EOL> state . state [ "<STR_LIT>" ] , <EOL> SELFCHECK_DATE_FMT <EOL> ) <EOL> if total_seconds ( current_time - last_check ) < <NUM_LIT:7> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> : <EOL> pypi_version = state . state [ "<STR_LIT>" ] <EOL> if pypi_version is None : <EOL> resp = session . get ( <EOL> PyPI . pip_json_url , <EOL> headers = { "<STR_LIT>" : "<STR_LIT:application/json>" } , <EOL> ) <EOL> resp . raise_for_status ( ) <EOL> pypi_version = [ <EOL> v for v in sorted ( <EOL> list ( resp . json ( ) [ "<STR_LIT>" ] ) , <EOL> key = packaging_version . parse , <EOL> ) <EOL> if not packaging_version . parse ( v ) . is_prerelease <EOL> ] [ - <NUM_LIT:1> ] <EOL> state . save ( pypi_version , current_time ) <EOL> remote_version = packaging_version . parse ( pypi_version ) <EOL> if ( pip_version < remote_version and <EOL> pip_version . base_version != remote_version . base_version ) : <EOL> if WINDOWS : <EOL> pip_cmd = "<STR_LIT>" <EOL> else : <EOL> pip_cmd = "<STR_LIT>" <EOL> logger . warning ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> pip_version , pypi_version , pip_cmd <EOL> ) <EOL> except Exception : <EOL> logger . debug ( <EOL> "<STR_LIT>" , <EOL> exc_info = True , <EOL> ) </s>
