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