<s> from __future__ import absolute_import <EOL> from collections import defaultdict <EOL> import functools <EOL> import itertools <EOL> import logging <EOL> import os <EOL> from pip . _vendor import pkg_resources <EOL> from pip . _vendor import requests <EOL> from pip . download import ( url_to_path , unpack_url ) <EOL> from pip . exceptions import ( InstallationError , BestVersionAlreadyInstalled , <EOL> DistributionNotFound , PreviousBuildDirError ) <EOL> from pip . req . req_install import InstallRequirement <EOL> from pip . utils import ( <EOL> display_path , dist_in_usersite , ensure_dir , normalize_path ) <EOL> from pip . utils . logging import indent_log <EOL> from pip . vcs import vcs <EOL> logger = logging . getLogger ( __name__ ) <EOL> class Requirements ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _keys = [ ] <EOL> self . _dict = { } <EOL> def keys ( self ) : <EOL> return self . _keys <EOL> def values ( self ) : <EOL> return [ self . _dict [ key ] for key in self . _keys ] <EOL> def __contains__ ( self , item ) : <EOL> return item in self . _keys <EOL> def __setitem__ ( self , key , value ) : <EOL> if key not in self . _keys : <EOL> self . _keys . append ( key ) <EOL> self . _dict [ key ] = value <EOL> def __getitem__ ( self , key ) : <EOL> return self . _dict [ key ] <EOL> def __repr__ ( self ) : <EOL> values = [ '<STR_LIT>' % ( repr ( k ) , repr ( self [ k ] ) ) for k in self . keys ( ) ] <EOL> return '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( values ) <EOL> class DistAbstraction ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , req_to_install ) : <EOL> self . req_to_install = req_to_install <EOL> def dist ( self , finder ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( self . dist ) <EOL> def prep_for_dist ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( self . dist ) <EOL> def make_abstract_dist ( req_to_install ) : <EOL> """<STR_LIT>""" <EOL> if req_to_install . editable : <EOL> return IsSDist ( req_to_install ) <EOL> elif req_to_install . link and req_to_install . link . is_wheel : <EOL> return IsWheel ( req_to_install ) <EOL> else : <EOL> return IsSDist ( req_to_install ) <EOL> class IsWheel ( DistAbstraction ) : <EOL> def dist ( self , finder ) : <EOL> return list ( pkg_resources . find_distributions ( <EOL> self . req_to_install . source_dir ) ) [ <NUM_LIT:0> ] <EOL> def prep_for_dist ( self ) : <EOL> pass <EOL> class IsSDist ( DistAbstraction ) : <EOL> def dist ( self , finder ) : <EOL> dist = self . req_to_install . get_dist ( ) <EOL> if dist . has_metadata ( '<STR_LIT>' ) : <EOL> finder . add_dependency_links ( <EOL> dist . get_metadata_lines ( '<STR_LIT>' ) <EOL> ) <EOL> return dist <EOL> def prep_for_dist ( self ) : <EOL> self . req_to_install . run_egg_info ( ) <EOL> self . req_to_install . assert_source_matches_version ( ) <EOL> class Installed ( DistAbstraction ) : <EOL> def dist ( self , finder ) : <EOL> return self . req_to_install . satisfied_by <EOL> def prep_for_dist ( self ) : <EOL> pass <EOL> class RequirementSet ( object ) : <EOL> def __init__ ( self , build_dir , src_dir , download_dir , upgrade = False , <EOL> ignore_installed = False , as_egg = False , target_dir = None , <EOL> ignore_dependencies = False , force_reinstall = False , <EOL> use_user_site = False , session = None , pycompile = True , <EOL> isolated = False , wheel_download_dir = None , <EOL> wheel_cache = None ) : <EOL> """<STR_LIT>""" <EOL> if session is None : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> self . build_dir = build_dir <EOL> self . src_dir = src_dir <EOL> self . download_dir = download_dir <EOL> self . upgrade = upgrade <EOL> self . ignore_installed = ignore_installed <EOL> self . force_reinstall = force_reinstall <EOL> self . requirements = Requirements ( ) <EOL> self . requirement_aliases = { } <EOL> self . unnamed_requirements = [ ] <EOL> self . ignore_dependencies = ignore_dependencies <EOL> self . successfully_downloaded = [ ] <EOL> self . successfully_installed = [ ] <EOL> self . reqs_to_cleanup = [ ] <EOL> self . as_egg = as_egg <EOL> self . use_user_site = use_user_site <EOL> self . target_dir = target_dir <EOL> self . session = session <EOL> self . pycompile = pycompile <EOL> self . isolated = isolated <EOL> if wheel_download_dir : <EOL> wheel_download_dir = normalize_path ( wheel_download_dir ) <EOL> self . wheel_download_dir = wheel_download_dir <EOL> self . _wheel_cache = wheel_cache <EOL> self . _dependencies = defaultdict ( list ) <EOL> def __str__ ( self ) : <EOL> reqs = [ req for req in self . requirements . values ( ) <EOL> if not req . comes_from ] <EOL> reqs . sort ( key = lambda req : req . name . lower ( ) ) <EOL> return '<STR_LIT:U+0020>' . join ( [ str ( req . req ) for req in reqs ] ) <EOL> def __repr__ ( self ) : <EOL> reqs = [ req for req in self . requirements . values ( ) ] <EOL> reqs . sort ( key = lambda req : req . name . lower ( ) ) <EOL> reqs_str = '<STR_LIT:U+002CU+0020>' . join ( [ str ( req . req ) for req in reqs ] ) <EOL> return ( '<STR_LIT>' <EOL> % ( self . __class__ . __name__ , len ( reqs ) , reqs_str ) ) <EOL> def add_requirement ( self , install_req , parent_req_name = None ) : <EOL> """<STR_LIT>""" <EOL> name = install_req . name <EOL> if not install_req . match_markers ( ) : <EOL> logger . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" , install_req . name , <EOL> install_req . markers ) <EOL> return [ ] <EOL> install_req . as_egg = self . as_egg <EOL> install_req . use_user_site = self . use_user_site <EOL> install_req . target_dir = self . target_dir <EOL> install_req . pycompile = self . pycompile <EOL> if not name : <EOL> self . unnamed_requirements . append ( install_req ) <EOL> return [ install_req ] <EOL> else : <EOL> try : <EOL> existing_req = self . get_requirement ( name ) <EOL> except KeyError : <EOL> existing_req = None <EOL> if ( parent_req_name is None and existing_req and not <EOL> existing_req . constraint ) : <EOL> raise InstallationError ( <EOL> '<STR_LIT>' <EOL> % ( install_req , existing_req , name ) ) <EOL> if not existing_req : <EOL> self . requirements [ name ] = install_req <EOL> if name . lower ( ) != name : <EOL> self . requirement_aliases [ name . lower ( ) ] = name <EOL> result = [ install_req ] <EOL> else : <EOL> if not existing_req . constraint : <EOL> result = [ ] <EOL> elif not install_req . constraint : <EOL> existing_req . constraint = False <EOL> result = [ existing_req ] <EOL> install_req = existing_req <EOL> if parent_req_name : <EOL> parent_req = self . get_requirement ( parent_req_name ) <EOL> self . _dependencies [ parent_req ] . append ( install_req ) <EOL> return result <EOL> def has_requirement ( self , project_name ) : <EOL> for name in project_name , project_name . lower ( ) : <EOL> if name in self . requirements or name in self . requirement_aliases : <EOL> return True <EOL> return False <EOL> @ property <EOL> def has_requirements ( self ) : <EOL> return list ( req for req in self . requirements . values ( ) if not <EOL> req . constraint ) or self . unnamed_requirements <EOL> @ property <EOL> def is_download ( self ) : <EOL> if self . download_dir : <EOL> self . download_dir = os . path . expanduser ( self . download_dir ) <EOL> if os . path . exists ( self . download_dir ) : <EOL> return True <EOL> else : <EOL> logger . critical ( '<STR_LIT>' ) <EOL> raise InstallationError ( <EOL> "<STR_LIT>" <EOL> % display_path ( self . download_dir ) ) <EOL> return False <EOL> def get_requirement ( self , project_name ) : <EOL> for name in project_name , project_name . lower ( ) : <EOL> if name in self . requirements : <EOL> return self . requirements [ name ] <EOL> if name in self . requirement_aliases : <EOL> return self . requirements [ self . requirement_aliases [ name ] ] <EOL> raise KeyError ( "<STR_LIT>" % project_name ) <EOL> def uninstall ( self , auto_confirm = False ) : <EOL> for req in self . requirements . values ( ) : <EOL> if req . constraint : <EOL> continue <EOL> req . uninstall ( auto_confirm = auto_confirm ) <EOL> req . commit_uninstall ( ) <EOL> def _walk_req_to_install ( self , handler ) : <EOL> """<STR_LIT>""" <EOL> discovered_reqs = [ ] <EOL> reqs = itertools . chain ( <EOL> list ( self . unnamed_requirements ) , list ( self . requirements . values ( ) ) , <EOL> discovered_reqs ) <EOL> for req_to_install in reqs : <EOL> more_reqs = handler ( req_to_install ) <EOL> if more_reqs : <EOL> discovered_reqs . extend ( more_reqs ) <EOL> def prepare_files ( self , finder ) : <EOL> """<STR_LIT>""" <EOL> if self . wheel_download_dir : <EOL> ensure_dir ( self . wheel_download_dir ) <EOL> self . _walk_req_to_install ( <EOL> functools . partial ( self . _prepare_file , finder ) ) <EOL> def _check_skip_installed ( self , req_to_install , finder ) : <EOL> """<STR_LIT>""" <EOL> req_to_install . check_if_exists ( ) <EOL> if req_to_install . satisfied_by : <EOL> skip_reason = '<STR_LIT>' <EOL> if self . upgrade : <EOL> best_installed = False <EOL> if not ( self . force_reinstall or req_to_install . link ) : <EOL> try : <EOL> finder . find_requirement ( req_to_install , self . upgrade ) <EOL> except BestVersionAlreadyInstalled : <EOL> skip_reason = '<STR_LIT>' <EOL> best_installed = True <EOL> except DistributionNotFound : <EOL> pass <EOL> if not best_installed : <EOL> if not ( self . use_user_site and not <EOL> dist_in_usersite ( req_to_install . satisfied_by ) ) : <EOL> req_to_install . conflicts_with = req_to_install . satisfied_by <EOL> req_to_install . satisfied_by = None <EOL> return skip_reason <EOL> else : <EOL> return None <EOL> def _prepare_file ( self , finder , req_to_install ) : <EOL> """<STR_LIT>""" <EOL> if req_to_install . constraint or req_to_install . prepared : <EOL> return [ ] <EOL> req_to_install . prepared = True <EOL> if req_to_install . editable : <EOL> logger . info ( '<STR_LIT>' , req_to_install ) <EOL> else : <EOL> assert req_to_install . satisfied_by is None <EOL> if not self . ignore_installed : <EOL> skip_reason = self . _check_skip_installed ( <EOL> req_to_install , finder ) <EOL> if req_to_install . satisfied_by : <EOL> assert skip_reason is not None , ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( req_to_install . satisfied_by , ) ) <EOL> logger . info ( <EOL> '<STR_LIT>' , skip_reason , <EOL> req_to_install ) <EOL> else : <EOL> if ( req_to_install . link and <EOL> req_to_install . link . scheme == '<STR_LIT:file>' ) : <EOL> path = url_to_path ( req_to_install . link . url ) <EOL> logger . info ( '<STR_LIT>' , display_path ( path ) ) <EOL> else : <EOL> logger . info ( '<STR_LIT>' , req_to_install ) <EOL> with indent_log ( ) : <EOL> if req_to_install . editable : <EOL> req_to_install . ensure_has_source_dir ( self . src_dir ) <EOL> req_to_install . update_editable ( not self . is_download ) <EOL> abstract_dist = make_abstract_dist ( req_to_install ) <EOL> abstract_dist . prep_for_dist ( ) <EOL> if self . is_download : <EOL> req_to_install . archive ( self . download_dir ) <EOL> elif req_to_install . satisfied_by : <EOL> abstract_dist = Installed ( req_to_install ) <EOL> else : <EOL> req_to_install . ensure_has_source_dir ( self . build_dir ) <EOL> if os . path . exists ( <EOL> os . path . join ( req_to_install . source_dir , '<STR_LIT>' ) ) : <EOL> raise PreviousBuildDirError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( req_to_install , req_to_install . source_dir ) <EOL> ) <EOL> req_to_install . populate_link ( finder , self . upgrade ) <EOL> assert req_to_install . link <EOL> try : <EOL> download_dir = self . download_dir <EOL> autodelete_unpacked = True <EOL> if req_to_install . link . is_wheel and self . wheel_download_dir : <EOL> download_dir = self . wheel_download_dir <EOL> if req_to_install . link . is_wheel : <EOL> if download_dir : <EOL> autodelete_unpacked = True <EOL> else : <EOL> autodelete_unpacked = False <EOL> unpack_url ( <EOL> req_to_install . link , req_to_install . source_dir , <EOL> download_dir , autodelete_unpacked , <EOL> session = self . session ) <EOL> except requests . HTTPError as exc : <EOL> logger . critical ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> req_to_install , <EOL> exc , <EOL> ) <EOL> raise InstallationError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( req_to_install , exc , req_to_install . link ) <EOL> ) <EOL> abstract_dist = make_abstract_dist ( req_to_install ) <EOL> abstract_dist . prep_for_dist ( ) <EOL> if self . is_download : <EOL> if req_to_install . link . scheme in vcs . all_schemes : <EOL> req_to_install . archive ( self . download_dir ) <EOL> if not self . ignore_installed : <EOL> req_to_install . check_if_exists ( ) <EOL> if req_to_install . satisfied_by : <EOL> if self . upgrade or self . ignore_installed : <EOL> if not ( self . use_user_site and not <EOL> dist_in_usersite ( <EOL> req_to_install . satisfied_by ) ) : <EOL> req_to_install . conflicts_with = req_to_install . satisfied_by <EOL> req_to_install . satisfied_by = None <EOL> else : <EOL> logger . info ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> req_to_install , <EOL> ) <EOL> dist = abstract_dist . dist ( finder ) <EOL> more_reqs = [ ] <EOL> def add_req ( subreq ) : <EOL> sub_install_req = InstallRequirement ( <EOL> str ( subreq ) , <EOL> req_to_install , <EOL> isolated = self . isolated , <EOL> wheel_cache = self . _wheel_cache , <EOL> ) <EOL> more_reqs . extend ( self . add_requirement ( <EOL> sub_install_req , req_to_install . name ) ) <EOL> if not self . has_requirement ( req_to_install . name ) : <EOL> self . add_requirement ( req_to_install , None ) <EOL> if not self . ignore_dependencies : <EOL> if ( req_to_install . extras ) : <EOL> logger . debug ( <EOL> "<STR_LIT>" , <EOL> '<STR_LIT:U+002C>' . join ( req_to_install . extras ) , <EOL> ) <EOL> missing_requested = sorted ( <EOL> set ( req_to_install . extras ) - set ( dist . extras ) <EOL> ) <EOL> for missing in missing_requested : <EOL> logger . warning ( <EOL> '<STR_LIT>' , <EOL> dist , missing <EOL> ) <EOL> available_requested = sorted ( <EOL> set ( dist . extras ) & set ( req_to_install . extras ) <EOL> ) <EOL> for subreq in dist . requires ( available_requested ) : <EOL> add_req ( subreq ) <EOL> self . reqs_to_cleanup . append ( req_to_install ) <EOL> if not req_to_install . editable and not req_to_install . satisfied_by : <EOL> self . successfully_downloaded . append ( req_to_install ) <EOL> return more_reqs <EOL> def cleanup_files ( self ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( '<STR_LIT>' ) <EOL> with indent_log ( ) : <EOL> for req in self . reqs_to_cleanup : <EOL> req . remove_temporary_source ( ) <EOL> def _to_install ( self ) : <EOL> """<STR_LIT>""" <EOL> order = [ ] <EOL> ordered_reqs = set ( ) <EOL> def schedule ( req ) : <EOL> if req . satisfied_by or req in ordered_reqs : <EOL> return <EOL> if req . constraint : <EOL> return <EOL> ordered_reqs . add ( req ) <EOL> for dep in self . _dependencies [ req ] : <EOL> schedule ( dep ) <EOL> order . append ( req ) <EOL> for install_req in self . requirements . values ( ) : <EOL> schedule ( install_req ) <EOL> return order <EOL> def install ( self , install_options , global_options = ( ) , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> to_install = self . _to_install ( ) <EOL> if to_install : <EOL> logger . info ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:U+002CU+0020>' . join ( [ req . name for req in to_install ] ) , <EOL> ) <EOL> with indent_log ( ) : <EOL> for requirement in to_install : <EOL> if requirement . conflicts_with : <EOL> logger . info ( <EOL> '<STR_LIT>' , <EOL> requirement . conflicts_with , <EOL> ) <EOL> with indent_log ( ) : <EOL> requirement . uninstall ( auto_confirm = True ) <EOL> try : <EOL> requirement . install ( <EOL> install_options , <EOL> global_options , <EOL> * args , <EOL> ** kwargs <EOL> ) <EOL> except : <EOL> if ( requirement . conflicts_with and not <EOL> requirement . install_succeeded ) : <EOL> requirement . rollback_uninstall ( ) <EOL> raise <EOL> else : <EOL> if ( requirement . conflicts_with and <EOL> requirement . install_succeeded ) : <EOL> requirement . commit_uninstall ( ) <EOL> requirement . remove_temporary_source ( ) <EOL> self . successfully_installed = to_install </s>
