requirements.py 4.6 KB

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