providers.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 or candidate, return an identifier for it.
  5. This is used in many places to identify a requirement or candidate,
  6. e.g. whether two requirements should have their specifier parts merged,
  7. whether two candidates would conflict with each other (because they
  8. have same name but different versions).
  9. """
  10. raise NotImplementedError
  11. def get_preference(self, resolution, candidates, information):
  12. """Produce a sort key for given requirement based on preference.
  13. The preference is defined as "I think this requirement should be
  14. resolved first". The lower the return value is, the more preferred
  15. this group of arguments is.
  16. :param resolution: Currently pinned candidate, or `None`.
  17. :param candidates: An iterable of possible candidates.
  18. :param information: A list of requirement information.
  19. The `candidates` iterable's exact type depends on the return type of
  20. `find_matches()`. A sequence is passed-in as-is if possible. If it
  21. returns a callble, the iterator returned by that callable is passed
  22. in here.
  23. Each element in `information` is a named tuple with two entries:
  24. * `requirement` specifies a requirement contributing to the current
  25. candidate list.
  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, requirements):
  44. """Find all possible candidates that satisfy the given requirements.
  45. This should try to get candidates based on the requirements' types.
  46. For VCS, local, and archive requirements, the one-and-only match is
  47. returned, and for a "named" requirement, the index(es) should be
  48. consulted to find concrete candidates for this requirement.
  49. The return value should produce candidates ordered by preference; the
  50. most preferred candidate should come first. The return type may be one
  51. of the following:
  52. * A callable that returns an iterator that yields candidates.
  53. * An collection of candidates.
  54. * An iterable of candidates. This will be consumed immediately into a
  55. list of candidates.
  56. :param requirements: A collection of requirements which all of the
  57. returned candidates must match. All requirements are guaranteed to
  58. have the same identifier. The collection is never empty.
  59. """
  60. raise NotImplementedError
  61. def is_satisfied_by(self, requirement, candidate):
  62. """Whether the given requirement can be satisfied by a candidate.
  63. The candidate is guarenteed to have been generated from the
  64. requirement.
  65. A boolean should be returned to indicate whether `candidate` is a
  66. viable solution to the requirement.
  67. """
  68. raise NotImplementedError
  69. def get_dependencies(self, candidate):
  70. """Get dependencies of a candidate.
  71. This should return a collection of requirements that `candidate`
  72. specifies as its dependencies.
  73. """
  74. raise NotImplementedError
  75. class AbstractResolver(object):
  76. """The thing that performs the actual resolution work."""
  77. base_exception = Exception
  78. def __init__(self, provider, reporter):
  79. self.provider = provider
  80. self.reporter = reporter
  81. def resolve(self, requirements, **kwargs):
  82. """Take a collection of constraints, spit out the resolution result.
  83. This returns a representation of the final resolution state, with one
  84. guarenteed attribute ``mapping`` that contains resolved candidates as
  85. values. The keys are their respective identifiers.
  86. :param requirements: A collection of constraints.
  87. :param kwargs: Additional keyword arguments that subclasses may accept.
  88. :raises: ``self.base_exception`` or its subclass.
  89. """
  90. raise NotImplementedError