2023-01-12 01:04:47 +00:00
|
|
|
# This file is dual licensed under the terms of the Apache License, Version
|
|
|
|
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
|
|
|
|
# for complete details.
|
|
|
|
|
|
|
|
import re
|
2023-01-14 02:55:00 +00:00
|
|
|
import string
|
|
|
|
import urllib.parse
|
|
|
|
from typing import List, Optional as TOptional, Set
|
|
|
|
|
|
|
|
from pkg_resources.extern.pyparsing import ( # noqa
|
|
|
|
Combine,
|
|
|
|
Literal as L,
|
|
|
|
Optional,
|
|
|
|
ParseException,
|
|
|
|
Regex,
|
|
|
|
Word,
|
|
|
|
ZeroOrMore,
|
|
|
|
originalTextFor,
|
|
|
|
stringEnd,
|
|
|
|
stringStart,
|
|
|
|
)
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
from .markers import MARKER_EXPR, Marker
|
|
|
|
from .specifiers import LegacySpecifier, Specifier, SpecifierSet
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidRequirement(ValueError):
|
|
|
|
"""
|
|
|
|
An invalid requirement was found, users should refer to PEP 508.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
ALPHANUM = Word(string.ascii_letters + string.digits)
|
|
|
|
|
|
|
|
LBRACKET = L("[").suppress()
|
|
|
|
RBRACKET = L("]").suppress()
|
|
|
|
LPAREN = L("(").suppress()
|
|
|
|
RPAREN = L(")").suppress()
|
|
|
|
COMMA = L(",").suppress()
|
|
|
|
SEMICOLON = L(";").suppress()
|
|
|
|
AT = L("@").suppress()
|
|
|
|
|
|
|
|
PUNCTUATION = Word("-_.")
|
|
|
|
IDENTIFIER_END = ALPHANUM | (ZeroOrMore(PUNCTUATION) + ALPHANUM)
|
|
|
|
IDENTIFIER = Combine(ALPHANUM + ZeroOrMore(IDENTIFIER_END))
|
|
|
|
|
|
|
|
NAME = IDENTIFIER("name")
|
|
|
|
EXTRA = IDENTIFIER
|
|
|
|
|
|
|
|
URI = Regex(r"[^ ]+")("url")
|
|
|
|
URL = AT + URI
|
|
|
|
|
|
|
|
EXTRAS_LIST = EXTRA + ZeroOrMore(COMMA + EXTRA)
|
|
|
|
EXTRAS = (LBRACKET + Optional(EXTRAS_LIST) + RBRACKET)("extras")
|
|
|
|
|
|
|
|
VERSION_PEP440 = Regex(Specifier._regex_str, re.VERBOSE | re.IGNORECASE)
|
|
|
|
VERSION_LEGACY = Regex(LegacySpecifier._regex_str, re.VERBOSE | re.IGNORECASE)
|
|
|
|
|
|
|
|
VERSION_ONE = VERSION_PEP440 ^ VERSION_LEGACY
|
|
|
|
VERSION_MANY = Combine(
|
|
|
|
VERSION_ONE + ZeroOrMore(COMMA + VERSION_ONE), joinString=",", adjacent=False
|
|
|
|
)("_raw_spec")
|
2023-01-14 02:55:00 +00:00
|
|
|
_VERSION_SPEC = Optional((LPAREN + VERSION_MANY + RPAREN) | VERSION_MANY)
|
2023-01-12 01:04:47 +00:00
|
|
|
_VERSION_SPEC.setParseAction(lambda s, l, t: t._raw_spec or "")
|
|
|
|
|
|
|
|
VERSION_SPEC = originalTextFor(_VERSION_SPEC)("specifier")
|
|
|
|
VERSION_SPEC.setParseAction(lambda s, l, t: t[1])
|
|
|
|
|
|
|
|
MARKER_EXPR = originalTextFor(MARKER_EXPR())("marker")
|
|
|
|
MARKER_EXPR.setParseAction(
|
|
|
|
lambda s, l, t: Marker(s[t._original_start : t._original_end])
|
|
|
|
)
|
|
|
|
MARKER_SEPARATOR = SEMICOLON
|
|
|
|
MARKER = MARKER_SEPARATOR + MARKER_EXPR
|
|
|
|
|
|
|
|
VERSION_AND_MARKER = VERSION_SPEC + Optional(MARKER)
|
|
|
|
URL_AND_MARKER = URL + Optional(MARKER)
|
|
|
|
|
|
|
|
NAMED_REQUIREMENT = NAME + Optional(EXTRAS) + (URL_AND_MARKER | VERSION_AND_MARKER)
|
|
|
|
|
|
|
|
REQUIREMENT = stringStart + NAMED_REQUIREMENT + stringEnd
|
|
|
|
# pkg_resources.extern.pyparsing isn't thread safe during initialization, so we do it eagerly, see
|
|
|
|
# issue #104
|
|
|
|
REQUIREMENT.parseString("x[]")
|
|
|
|
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
class Requirement:
|
2023-01-12 01:04:47 +00:00
|
|
|
"""Parse a requirement.
|
|
|
|
|
|
|
|
Parse a given requirement string into its parts, such as name, specifier,
|
|
|
|
URL, and extras. Raises InvalidRequirement on a badly-formed requirement
|
|
|
|
string.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# TODO: Can we test whether something is contained within a requirement?
|
|
|
|
# If so how do we do that? Do we need to test against the _name_ of
|
|
|
|
# the thing as well as the version? What about the markers?
|
|
|
|
# TODO: Can we normalize the name and extra name?
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
def __init__(self, requirement_string: str) -> None:
|
2023-01-12 01:04:47 +00:00
|
|
|
try:
|
|
|
|
req = REQUIREMENT.parseString(requirement_string)
|
|
|
|
except ParseException as e:
|
|
|
|
raise InvalidRequirement(
|
2023-01-14 02:55:00 +00:00
|
|
|
f'Parse error at "{ requirement_string[e.loc : e.loc + 8]!r}": {e.msg}'
|
2023-01-12 01:04:47 +00:00
|
|
|
)
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
self.name: str = req.name
|
2023-01-12 01:04:47 +00:00
|
|
|
if req.url:
|
2023-01-14 02:55:00 +00:00
|
|
|
parsed_url = urllib.parse.urlparse(req.url)
|
2023-01-12 01:04:47 +00:00
|
|
|
if parsed_url.scheme == "file":
|
2023-01-14 02:55:00 +00:00
|
|
|
if urllib.parse.urlunparse(parsed_url) != req.url:
|
2023-01-12 01:04:47 +00:00
|
|
|
raise InvalidRequirement("Invalid URL given")
|
|
|
|
elif not (parsed_url.scheme and parsed_url.netloc) or (
|
|
|
|
not parsed_url.scheme and not parsed_url.netloc
|
|
|
|
):
|
2023-01-14 02:55:00 +00:00
|
|
|
raise InvalidRequirement(f"Invalid URL: {req.url}")
|
|
|
|
self.url: TOptional[str] = req.url
|
2023-01-12 01:04:47 +00:00
|
|
|
else:
|
|
|
|
self.url = None
|
2023-01-14 02:55:00 +00:00
|
|
|
self.extras: Set[str] = set(req.extras.asList() if req.extras else [])
|
|
|
|
self.specifier: SpecifierSet = SpecifierSet(req.specifier)
|
|
|
|
self.marker: TOptional[Marker] = req.marker if req.marker else None
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
def __str__(self) -> str:
|
|
|
|
parts: List[str] = [self.name]
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
if self.extras:
|
2023-01-14 02:55:00 +00:00
|
|
|
formatted_extras = ",".join(sorted(self.extras))
|
|
|
|
parts.append(f"[{formatted_extras}]")
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
if self.specifier:
|
|
|
|
parts.append(str(self.specifier))
|
|
|
|
|
|
|
|
if self.url:
|
2023-01-14 02:55:00 +00:00
|
|
|
parts.append(f"@ {self.url}")
|
2023-01-12 01:04:47 +00:00
|
|
|
if self.marker:
|
|
|
|
parts.append(" ")
|
|
|
|
|
|
|
|
if self.marker:
|
2023-01-14 02:55:00 +00:00
|
|
|
parts.append(f"; {self.marker}")
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
return "".join(parts)
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"<Requirement('{self}')>"
|