requirements.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import absolute_import, division, print_function
  5. import re
  6. import string
  7. import sys
  8. from pip._vendor.pyparsing import ( # noqa: N817
  9. Combine,
  10. Literal as L,
  11. Optional,
  12. ParseException,
  13. Regex,
  14. Word,
  15. ZeroOrMore,
  16. originalTextFor,
  17. stringEnd,
  18. stringStart,
  19. )
  20. from ._typing import TYPE_CHECKING
  21. from .markers import MARKER_EXPR, Marker
  22. from .specifiers import LegacySpecifier, Specifier, SpecifierSet
  23. if sys.version_info[0] >= 3:
  24. from urllib import parse as urlparse # pragma: no cover
  25. else: # pragma: no cover
  26. import urlparse
  27. if TYPE_CHECKING: # pragma: no cover
  28. from typing import List, Optional as TOptional, Set
  29. class InvalidRequirement(ValueError):
  30. """
  31. An invalid requirement was found, users should refer to PEP 508.
  32. """
  33. ALPHANUM = Word(string.ascii_letters + string.digits)
  34. LBRACKET = L("[").suppress()
  35. RBRACKET = L("]").suppress()
  36. LPAREN = L("(").suppress()
  37. RPAREN = L(")").suppress()
  38. COMMA = L(",").suppress()
  39. SEMICOLON = L(";").suppress()
  40. AT = L("@").suppress()
  41. PUNCTUATION = Word("-_.")
  42. IDENTIFIER_END = ALPHANUM | (ZeroOrMore(PUNCTUATION) + ALPHANUM)
  43. IDENTIFIER = Combine(ALPHANUM + ZeroOrMore(IDENTIFIER_END))
  44. NAME = IDENTIFIER("name")
  45. EXTRA = IDENTIFIER
  46. URI = Regex(r"[^ ]+")("url")
  47. URL = AT + URI
  48. EXTRAS_LIST = EXTRA + ZeroOrMore(COMMA + EXTRA)
  49. EXTRAS = (LBRACKET + Optional(EXTRAS_LIST) + RBRACKET)("extras")
  50. VERSION_PEP440 = Regex(Specifier._regex_str, re.VERBOSE | re.IGNORECASE)
  51. VERSION_LEGACY = Regex(LegacySpecifier._regex_str, re.VERBOSE | re.IGNORECASE)
  52. VERSION_ONE = VERSION_PEP440 ^ VERSION_LEGACY
  53. VERSION_MANY = Combine(
  54. VERSION_ONE + ZeroOrMore(COMMA + VERSION_ONE), joinString=",", adjacent=False
  55. )("_raw_spec")
  56. _VERSION_SPEC = Optional(((LPAREN + VERSION_MANY + RPAREN) | VERSION_MANY))
  57. _VERSION_SPEC.setParseAction(lambda s, l, t: t._raw_spec or "")
  58. VERSION_SPEC = originalTextFor(_VERSION_SPEC)("specifier")
  59. VERSION_SPEC.setParseAction(lambda s, l, t: t[1])
  60. MARKER_EXPR = originalTextFor(MARKER_EXPR())("marker")
  61. MARKER_EXPR.setParseAction(
  62. lambda s, l, t: Marker(s[t._original_start : t._original_end])
  63. )
  64. MARKER_SEPARATOR = SEMICOLON
  65. MARKER = MARKER_SEPARATOR + MARKER_EXPR
  66. VERSION_AND_MARKER = VERSION_SPEC + Optional(MARKER)
  67. URL_AND_MARKER = URL + Optional(MARKER)
  68. NAMED_REQUIREMENT = NAME + Optional(EXTRAS) + (URL_AND_MARKER | VERSION_AND_MARKER)
  69. REQUIREMENT = stringStart + NAMED_REQUIREMENT + stringEnd
  70. # pyparsing isn't thread safe during initialization, so we do it eagerly, see
  71. # issue #104
  72. REQUIREMENT.parseString("x[]")
  73. class Requirement(object):
  74. """Parse a requirement.
  75. Parse a given requirement string into its parts, such as name, specifier,
  76. URL, and extras. Raises InvalidRequirement on a badly-formed requirement
  77. string.
  78. """
  79. # TODO: Can we test whether something is contained within a requirement?
  80. # If so how do we do that? Do we need to test against the _name_ of
  81. # the thing as well as the version? What about the markers?
  82. # TODO: Can we normalize the name and extra name?
  83. def __init__(self, requirement_string):
  84. # type: (str) -> None
  85. try:
  86. req = REQUIREMENT.parseString(requirement_string)
  87. except ParseException as e:
  88. raise InvalidRequirement(
  89. 'Parse error at "{0!r}": {1}'.format(
  90. requirement_string[e.loc : e.loc + 8], e.msg
  91. )
  92. )
  93. self.name = req.name # type: str
  94. if req.url:
  95. parsed_url = urlparse.urlparse(req.url)
  96. if parsed_url.scheme == "file":
  97. if urlparse.urlunparse(parsed_url) != req.url:
  98. raise InvalidRequirement("Invalid URL given")
  99. elif not (parsed_url.scheme and parsed_url.netloc) or (
  100. not parsed_url.scheme and not parsed_url.netloc
  101. ):
  102. raise InvalidRequirement("Invalid URL: {0}".format(req.url))
  103. self.url = req.url # type: TOptional[str]
  104. else:
  105. self.url = None
  106. self.extras = set(req.extras.asList() if req.extras else []) # type: Set[str]
  107. self.specifier = SpecifierSet(req.specifier) # type: SpecifierSet
  108. self.marker = req.marker if req.marker else None # type: TOptional[Marker]
  109. def __str__(self):
  110. # type: () -> str
  111. parts = [self.name] # type: List[str]
  112. if self.extras:
  113. parts.append("[{0}]".format(",".join(sorted(self.extras))))
  114. if self.specifier:
  115. parts.append(str(self.specifier))
  116. if self.url:
  117. parts.append("@ {0}".format(self.url))
  118. if self.marker:
  119. parts.append(" ")
  120. if self.marker:
  121. parts.append("; {0}".format(self.marker))
  122. return "".join(parts)
  123. def __repr__(self):
  124. # type: () -> str
  125. return "<Requirement({0!r})>".format(str(self))