12
12
_scheme_re = re .compile (r'^(http|https|file):' , re .I )
13
13
14
14
15
+ def _remove_prefix (line , prefix ):
16
+ """Remove the prefix and eventually one '=' or spaces"""
17
+ return re .sub (r'\s*=?\s*' , '' , line [len (prefix ):])
18
+
19
+
15
20
def parse_requirements (filename , finder = None , comes_from = None , options = None ,
16
21
session = None ):
17
22
if session is None :
@@ -33,18 +38,18 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None,
33
38
for line_number , line in enumerate (content .splitlines (), 1 ):
34
39
line = line .strip ()
35
40
36
- # Remove comments from file
37
- line = re .sub (r"(^|\s)#.*$" , "" , line )
41
+ # Remove comments from file and all spaces before it
42
+ line = re .sub (r"(^|\s)+ #.*$" , "" , line )
38
43
39
- if not line or line . startswith ( '#' ) :
44
+ if not line :
40
45
continue
41
46
if skip_match and skip_match .search (line ):
42
47
continue
43
48
if line .startswith (('-r' , '--requirement' )):
44
49
if line .startswith ('-r' ):
45
- req_url = line [2 :].strip ()
50
+ req_url = line [2 :].lstrip ()
46
51
else :
47
- req_url = line [ len ( '--requirement' ):]. strip (). strip ( '= ' )
52
+ req_url = _remove_prefix ( line , '--requirement' )
48
53
if _scheme_re .search (filename ):
49
54
# Relative to a URL
50
55
req_url = urllib_parse .urljoin (filename , req_url )
@@ -62,9 +67,9 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None,
62
67
pass
63
68
elif line .startswith (('-f' , '--find-links' )):
64
69
if line .startswith ('-f' ):
65
- line = line [2 :].strip ()
70
+ line = line [2 :].lstrip ()
66
71
else :
67
- line = line [ len ( '--find-links' ):]. strip (). lstrip ( '= ' )
72
+ line = _remove_prefix ( line , '--find-links' )
68
73
# FIXME: it would be nice to keep track of the source of
69
74
# the find_links:
70
75
# support a find-links local path relative to a requirements file
@@ -75,13 +80,13 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None,
75
80
finder .find_links .append (line )
76
81
elif line .startswith (('-i' , '--index-url' )):
77
82
if line .startswith ('-i' ):
78
- line = line [2 :].strip ()
83
+ line = line [2 :].lstrip ()
79
84
else :
80
- line = line [ len ( '--index-url' ):]. strip (). lstrip ( '= ' )
85
+ line = _remove_prefix ( line , '--index-url' )
81
86
if finder :
82
87
finder .index_urls = [line ]
83
88
elif line .startswith ('--extra-index-url' ):
84
- line = line [ len ( '--extra-index-url' ):]. strip (). lstrip ( '= ' )
89
+ line = _remove_prefix ( line , '--extra-index-url' )
85
90
if finder :
86
91
finder .index_urls .append (line )
87
92
elif line .startswith ('--use-wheel' ):
@@ -94,7 +99,7 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None,
94
99
if finder :
95
100
finder .index_urls = []
96
101
elif line .startswith ("--allow-external" ):
97
- line = line [ len ( " --allow-external" ):]. strip (). lstrip ( "=" )
102
+ line = _remove_prefix ( line , ' --allow-external' )
98
103
if finder :
99
104
finder .allow_external |= set ([normalize_name (line ).lower ()])
100
105
elif line .startswith ("--allow-all-external" ):
@@ -108,20 +113,20 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None,
108
113
pass
109
114
# Remove after 7.0
110
115
elif line .startswith ("--allow-insecure" ):
111
- line = line [ len ( " --allow-insecure" ):]. strip (). lstrip ( "=" )
116
+ line = _remove_prefix ( line , ' --allow-insecure' )
112
117
if finder :
113
118
finder .allow_unverified |= set ([normalize_name (line ).lower ()])
114
119
elif line .startswith ("--allow-unverified" ):
115
- line = line [ len ( " --allow-unverified" ):]. strip (). lstrip ( "=" )
120
+ line = _remove_prefix ( line , ' --allow-unverified' )
116
121
if finder :
117
122
finder .allow_unverified |= set ([normalize_name (line ).lower ()])
118
123
else :
119
124
comes_from = '-r %s (line %s)' % (filename , line_number )
120
125
if line .startswith (('-e' , '--editable' )):
121
126
if line .startswith ('-e' ):
122
- line = line [2 :].strip ()
127
+ line = line [2 :].lstrip ()
123
128
else :
124
- line = line [ len ( '--editable' ):]. strip (). lstrip ( '= ' )
129
+ line = _remove_prefix ( line , '--editable' )
125
130
req = InstallRequirement .from_editable (
126
131
line ,
127
132
comes_from = comes_from ,
0 commit comments