req_requirement.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from pip._vendor.packaging.version import parse as parse_version
  2. class InstallationCandidate(object):
  3. def __init__(self, project, version, location):
  4. self.project = project
  5. self.version = parse_version(version)
  6. self.location = location
  7. self._key = (self.project, self.version, self.location)
  8. def __repr__(self):
  9. return "<InstallationCandidate({0!r}, {1!r}, {2!r})>".format(
  10. self.project, self.version, self.location,
  11. )
  12. def __hash__(self):
  13. return hash(self._key)
  14. def __lt__(self, other):
  15. return self._compare(other, lambda s, o: s < o)
  16. def __le__(self, other):
  17. return self._compare(other, lambda s, o: s <= o)
  18. def __eq__(self, other):
  19. return self._compare(other, lambda s, o: s == o)
  20. def __ge__(self, other):
  21. return self._compare(other, lambda s, o: s >= o)
  22. def __gt__(self, other):
  23. return self._compare(other, lambda s, o: s > o)
  24. def __ne__(self, other):
  25. return self._compare(other, lambda s, o: s != o)
  26. def _compare(self, other, method):
  27. if not isinstance(other, InstallationCandidate):
  28. return NotImplemented
  29. return method(self._key, other._key)