providers.py 5.5 KB

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