providers.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. class AbstractProvider(object):
  2. """Delegate class to provide the required interface for the resolver."""
  3. def identify(self, requirement_or_candidate):
  4. """Given a requirement, return an identifier for it.
  5. This is used to identify a requirement, e.g. whether two requirements
  6. should have their specifier parts merged.
  7. """
  8. raise NotImplementedError
  9. def get_preference(
  10. self,
  11. identifier,
  12. resolutions,
  13. candidates,
  14. information,
  15. backtrack_causes,
  16. ):
  17. """Produce a sort key for given requirement based on preference.
  18. The preference is defined as "I think this requirement should be
  19. resolved first". The lower the return value is, the more preferred
  20. this group of arguments is.
  21. :param identifier: An identifier as returned by ``identify()``. This
  22. identifies the dependency matches which should be returned.
  23. :param resolutions: Mapping of candidates currently pinned by the
  24. resolver. Each key is an identifier, and the value is a candidate.
  25. The candidate may conflict with requirements from ``information``.
  26. :param candidates: Mapping of each dependency's possible candidates.
  27. Each value is an iterator of candidates.
  28. :param information: Mapping of requirement information of each package.
  29. Each value is an iterator of *requirement information*.
  30. :param backtrack_causes: Sequence of requirement information that were
  31. the requirements that caused the resolver to most recently backtrack.
  32. A *requirement information* instance is a named tuple with two members:
  33. * ``requirement`` specifies a requirement contributing to the current
  34. list of candidates.
  35. * ``parent`` specifies the candidate that provides (depended on) the
  36. requirement, or ``None`` to indicate a root requirement.
  37. The preference could depend on various issues, including (not
  38. necessarily in this order):
  39. * Is this package pinned in the current resolution result?
  40. * How relaxed is the requirement? Stricter ones should probably be
  41. worked on first? (I don't know, actually.)
  42. * How many possibilities are there to satisfy this requirement? Those
  43. with few left should likely be worked on first, I guess?
  44. * Are there any known conflicts for this requirement? We should
  45. probably work on those with the most known conflicts.
  46. A sortable value should be returned (this will be used as the ``key``
  47. parameter of the built-in sorting function). The smaller the value is,
  48. the more preferred this requirement is (i.e. the sorting function
  49. is called with ``reverse=False``).
  50. """
  51. raise NotImplementedError
  52. def find_matches(self, identifier, requirements, incompatibilities):
  53. """Find all possible candidates that satisfy the given constraints.
  54. :param identifier: An identifier as returned by ``identify()``. This
  55. identifies the dependency matches of which should be returned.
  56. :param requirements: A mapping of requirements that all returned
  57. candidates must satisfy. Each key is an identifier, and the value
  58. an iterator of requirements for that dependency.
  59. :param incompatibilities: A mapping of known incompatibilities of
  60. each dependency. Each key is an identifier, and the value an
  61. iterator of incompatibilities known to the resolver. All
  62. incompatibilities *must* be excluded from the return value.
  63. This should try to get candidates based on the requirements' types.
  64. For VCS, local, and archive requirements, the one-and-only match is
  65. returned, and for a "named" requirement, the index(es) should be
  66. consulted to find concrete candidates for this requirement.
  67. The return value should produce candidates ordered by preference; the
  68. most preferred candidate should come first. The return type may be one
  69. of the following:
  70. * A callable that returns an iterator that yields candidates.
  71. * An collection of candidates.
  72. * An iterable of candidates. This will be consumed immediately into a
  73. list of candidates.
  74. """
  75. raise NotImplementedError
  76. def is_satisfied_by(self, requirement, candidate):
  77. """Whether the given requirement can be satisfied by a candidate.
  78. The candidate is guaranteed to have been generated from the
  79. requirement.
  80. A boolean should be returned to indicate whether ``candidate`` is a
  81. viable solution to the requirement.
  82. """
  83. raise NotImplementedError
  84. def get_dependencies(self, candidate):
  85. """Get dependencies of a candidate.
  86. This should return a collection of requirements that `candidate`
  87. specifies as its dependencies.
  88. """
  89. raise NotImplementedError
  90. class AbstractResolver(object):
  91. """The thing that performs the actual resolution work."""
  92. base_exception = Exception
  93. def __init__(self, provider, reporter):
  94. self.provider = provider
  95. self.reporter = reporter
  96. def resolve(self, requirements, **kwargs):
  97. """Take a collection of constraints, spit out the resolution result.
  98. This returns a representation of the final resolution state, with one
  99. guarenteed attribute ``mapping`` that contains resolved candidates as
  100. values. The keys are their respective identifiers.
  101. :param requirements: A collection of constraints.
  102. :param kwargs: Additional keyword arguments that subclasses may accept.
  103. :raises: ``self.base_exception`` or its subclass.
  104. """
  105. raise NotImplementedError