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