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