providers.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. class AbstractProvider(object):
  2. """Delegate class to provide requirement interface for the resolver.
  3. """
  4. def identify(self, dependency):
  5. """Given a dependency, return an identifier for it.
  6. This is used in many places to identify the dependency, e.g. whether
  7. two requirements should have their specifier parts merged, whether
  8. two specifications would conflict with each other (because they the
  9. same name but different versions).
  10. """
  11. raise NotImplementedError
  12. def get_preference(self, resolution, candidates, information):
  13. """Produce a sort key for given specification based on preference.
  14. The preference is defined as "I think this requirement should be
  15. resolved first". The lower the return value is, the more preferred
  16. this group of arguments is.
  17. :param resolution: Currently pinned candidate, or `None`.
  18. :param candidates: A list of possible candidates.
  19. :param information: A list of requirement information.
  20. Each information instance is a named tuple with two entries:
  21. * `requirement` specifies a requirement contributing to the current
  22. candidate list
  23. * `parent` specifies the candidate that provids (dependend on) the
  24. requirement, or `None` to indicate a root requirement.
  25. The preference could depend on a various of issues, including (not
  26. necessarily in this order):
  27. * Is this package pinned in the current resolution result?
  28. * How relaxed is the requirement? Stricter ones should probably be
  29. worked on first? (I don't know, actually.)
  30. * How many possibilities are there to satisfy this requirement? Those
  31. with few left should likely be worked on first, I guess?
  32. * Are there any known conflicts for this requirement? We should
  33. probably work on those with the most known conflicts.
  34. A sortable value should be returned (this will be used as the `key`
  35. parameter of the built-in sorting function). The smaller the value is,
  36. the more preferred this specification is (i.e. the sorting function
  37. is called with `reverse=False`).
  38. """
  39. raise NotImplementedError
  40. def find_matches(self, requirement):
  41. """Find all possible candidates that satisfy a requirement.
  42. This should try to get candidates based on the requirement's type.
  43. For VCS, local, and archive requirements, the one-and-only match is
  44. returned, and for a "named" requirement, the index(es) should be
  45. consulted to find concrete candidates for this requirement.
  46. The returned candidates should be sorted by reversed preference, e.g.
  47. the most preferred should be LAST. This is done so list-popping can be
  48. as efficient as possible.
  49. """
  50. raise NotImplementedError
  51. def is_satisfied_by(self, requirement, candidate):
  52. """Whether the given requirement can be satisfied by a candidate.
  53. A boolean should be returned to indicate whether `candidate` is a
  54. viable solution to the requirement.
  55. """
  56. raise NotImplementedError
  57. def get_dependencies(self, candidate):
  58. """Get dependencies of a candidate.
  59. This should return a collection of requirements that `candidate`
  60. specifies as its dependencies.
  61. """
  62. raise NotImplementedError
  63. class AbstractResolver(object):
  64. """The thing that performs the actual resolution work.
  65. """
  66. base_exception = Exception
  67. def __init__(self, provider, reporter):
  68. self.provider = provider
  69. self.reporter = reporter
  70. def resolve(self, requirements, **kwargs):
  71. """Take a collection of constraints, spit out the resolution result.
  72. Parameters
  73. ----------
  74. requirements : Collection
  75. A collection of constraints
  76. kwargs : optional
  77. Additional keyword arguments that subclasses may accept.
  78. Raises
  79. ------
  80. self.base_exception
  81. Any raised exception is guaranteed to be a subclass of
  82. self.base_exception. The string representation of an exception
  83. should be human readable and provide context for why it occurred.
  84. Returns
  85. -------
  86. retval : object
  87. A representation of the final resolution state. It can be any object
  88. with a `mapping` attribute that is a Mapping. Other attributes can
  89. be used to provide resolver-specific information.
  90. The `mapping` attribute MUST be key-value pair is an identifier of a
  91. requirement (as returned by the provider's `identify` method) mapped
  92. to the resolved candidate (chosen from the return value of the
  93. provider's `find_matches` method).
  94. """
  95. raise NotImplementedError