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