<s> import collections <EOL> from . bundled . traitlets import HasTraits , Dict , Instance , List , Long , Unicode <EOL> from . errors import DepSolverError , MissingPackageInfoInPool <EOL> from . package import PackageInfo <EOL> from . repository import Repository <EOL> from . requirement import Requirement <EOL> from . utils import CachedScheduler <EOL> MATCH_NONE = <NUM_LIT:0> <EOL> MATCH_NAME = <NUM_LIT:1> <EOL> MATCH = <NUM_LIT:2> <EOL> MATCH_PROVIDE = <NUM_LIT:3> <EOL> MATCH_REPLACE = <NUM_LIT:4> <EOL> class Pool ( HasTraits ) : <EOL> """<STR_LIT>""" <EOL> repositories = List ( Instance ( Repository ) ) <EOL> _packages_by_id = Dict ( ) <EOL> _packages_by_name = Dict ( ) <EOL> _id = Long ( <NUM_LIT:1> ) <EOL> _repository_by_name = Instance ( collections . defaultdict ) <EOL> _scheduler = Instance ( CachedScheduler ) <EOL> def __init__ ( self , repositories = None , ** kw ) : <EOL> scheduler = CachedScheduler ( ) <EOL> repository_by_name = collections . defaultdict ( list ) <EOL> super ( Pool , self ) . __init__ ( self , _scheduler = scheduler , <EOL> _repository_by_name = repository_by_name , ** kw ) <EOL> if repositories is None : <EOL> repositories = [ ] <EOL> self . _packages_by_name = collections . defaultdict ( list ) <EOL> if len ( repositories ) > <NUM_LIT:0> : <EOL> for repository in repositories : <EOL> self . add_repository ( repository ) <EOL> def has_package ( self , package ) : <EOL> package_id = package . id <EOL> return package_id in self . _packages_by_id <EOL> def add_repository ( self , repository ) : <EOL> """<STR_LIT>""" <EOL> self . repositories . append ( repository ) <EOL> self . _repository_by_name [ repository . name ] . append ( repository ) <EOL> for package in repository . iter_packages ( ) : <EOL> package . id = self . _id <EOL> self . _id += <NUM_LIT:1> <EOL> self . _packages_by_id [ package . id ] = package <EOL> self . _packages_by_name [ package . name ] . append ( package ) <EOL> for provide in package . provides : <EOL> self . _packages_by_name [ provide . name ] . append ( package ) <EOL> for replace in package . replaces : <EOL> self . _packages_by_name [ replace . name ] . append ( package ) <EOL> def package_by_id ( self , package_id ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _packages_by_id [ package_id ] <EOL> except KeyError : <EOL> raise MissingPackageInfoInPool ( package_id ) <EOL> def what_provides ( self , requirement , mode = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not mode in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise ValueError ( "<STR_LIT>" % mode ) <EOL> strict_matches = [ ] <EOL> provided_match = [ ] <EOL> name_match = False <EOL> for package in self . _packages_by_name [ requirement . name ] : <EOL> match = self . matches ( package , requirement ) <EOL> if match == MATCH_NONE : <EOL> pass <EOL> elif match == MATCH_NAME : <EOL> name_match = True <EOL> elif match == MATCH : <EOL> name_match = True <EOL> strict_matches . append ( package ) <EOL> elif match == MATCH_PROVIDE : <EOL> provided_match . append ( package ) <EOL> elif match == MATCH_REPLACE : <EOL> strict_matches . append ( package ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" . format ( match ) ) <EOL> if mode == '<STR_LIT>' : <EOL> if name_match : <EOL> return strict_matches <EOL> else : <EOL> return strict_matches + provided_match <EOL> elif mode == '<STR_LIT>' : <EOL> return strict_matches <EOL> elif mode == '<STR_LIT>' : <EOL> return strict_matches + provided_match <EOL> def matches ( self , candidate , requirement ) : <EOL> """<STR_LIT>""" <EOL> if requirement . name == candidate . name : <EOL> candidate_requirement = Requirement . from_package_string ( candidate . unique_name , candidate . version_factory ) <EOL> if requirement . is_universal or candidate_requirement . matches ( requirement ) : <EOL> return MATCH <EOL> else : <EOL> return MATCH_NAME <EOL> else : <EOL> for provide in candidate . provides : <EOL> if requirement . matches ( provide ) : <EOL> return MATCH_PROVIDE <EOL> for replace in candidate . replaces : <EOL> if requirement . matches ( replace ) : <EOL> return MATCH_REPLACE <EOL> return MATCH_NONE <EOL> def id_to_string ( self , package_id ) : <EOL> """<STR_LIT>""" <EOL> package = self . package_by_id ( abs ( package_id ) ) <EOL> if package_id > <NUM_LIT:0> : <EOL> return "<STR_LIT:+>" + str ( package ) <EOL> else : <EOL> return "<STR_LIT:->" + str ( package ) <EOL> def set_repository_order ( self , repository_name , after = None , before = None ) : <EOL> candidates = self . _repository_by_name [ repository_name ] <EOL> if len ( candidates ) < <NUM_LIT:1> : <EOL> raise DepSolverError ( "<STR_LIT>" % ( repository_name , ) ) <EOL> else : <EOL> self . _scheduler . set_constraints ( repository_name , after , before ) <EOL> def repository_priority ( self , repository ) : <EOL> """<STR_LIT>""" <EOL> if repository . name in self . _repository_by_name : <EOL> priorities = self . _scheduler . compute_priority ( ) <EOL> return priorities . get ( repository . name , <NUM_LIT:0> ) - ( len ( priorities ) - <NUM_LIT:1> ) <EOL> else : <EOL> raise DepSolverError ( "<STR_LIT>" % ( repository . name , ) ) </s>
