Skip to content

Commit 0bb2c8e

Browse files
committed
parse_requirements: simpler line cleanup
- no need to check for startswith('#') since the regex removed all comments - make sure the regex removes all spaces before the comment - remove all long prefixes the same way - only lstrip short options since the line has already been stripped
1 parent 79ea6b7 commit 0bb2c8e

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

pip/req/req_file.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
_scheme_re = re.compile(r'^(http|https|file):', re.I)
1313

1414

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+
1520
def parse_requirements(filename, finder=None, comes_from=None, options=None,
1621
session=None):
1722
if session is None:
@@ -33,18 +38,18 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None,
3338
for line_number, line in enumerate(content.splitlines(), 1):
3439
line = line.strip()
3540

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)
3843

39-
if not line or line.startswith('#'):
44+
if not line:
4045
continue
4146
if skip_match and skip_match.search(line):
4247
continue
4348
if line.startswith(('-r', '--requirement')):
4449
if line.startswith('-r'):
45-
req_url = line[2:].strip()
50+
req_url = line[2:].lstrip()
4651
else:
47-
req_url = line[len('--requirement'):].strip().strip('=')
52+
req_url = _remove_prefix(line, '--requirement')
4853
if _scheme_re.search(filename):
4954
# Relative to a URL
5055
req_url = urllib_parse.urljoin(filename, req_url)
@@ -62,9 +67,9 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None,
6267
pass
6368
elif line.startswith(('-f', '--find-links')):
6469
if line.startswith('-f'):
65-
line = line[2:].strip()
70+
line = line[2:].lstrip()
6671
else:
67-
line = line[len('--find-links'):].strip().lstrip('=')
72+
line = _remove_prefix(line, '--find-links')
6873
# FIXME: it would be nice to keep track of the source of
6974
# the find_links:
7075
# 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,
7580
finder.find_links.append(line)
7681
elif line.startswith(('-i', '--index-url')):
7782
if line.startswith('-i'):
78-
line = line[2:].strip()
83+
line = line[2:].lstrip()
7984
else:
80-
line = line[len('--index-url'):].strip().lstrip('=')
85+
line = _remove_prefix(line, '--index-url')
8186
if finder:
8287
finder.index_urls = [line]
8388
elif line.startswith('--extra-index-url'):
84-
line = line[len('--extra-index-url'):].strip().lstrip('=')
89+
line = _remove_prefix(line, '--extra-index-url')
8590
if finder:
8691
finder.index_urls.append(line)
8792
elif line.startswith('--use-wheel'):
@@ -94,7 +99,7 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None,
9499
if finder:
95100
finder.index_urls = []
96101
elif line.startswith("--allow-external"):
97-
line = line[len("--allow-external"):].strip().lstrip("=")
102+
line = _remove_prefix(line, '--allow-external')
98103
if finder:
99104
finder.allow_external |= set([normalize_name(line).lower()])
100105
elif line.startswith("--allow-all-external"):
@@ -108,20 +113,20 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None,
108113
pass
109114
# Remove after 7.0
110115
elif line.startswith("--allow-insecure"):
111-
line = line[len("--allow-insecure"):].strip().lstrip("=")
116+
line = _remove_prefix(line, '--allow-insecure')
112117
if finder:
113118
finder.allow_unverified |= set([normalize_name(line).lower()])
114119
elif line.startswith("--allow-unverified"):
115-
line = line[len("--allow-unverified"):].strip().lstrip("=")
120+
line = _remove_prefix(line, '--allow-unverified')
116121
if finder:
117122
finder.allow_unverified |= set([normalize_name(line).lower()])
118123
else:
119124
comes_from = '-r %s (line %s)' % (filename, line_number)
120125
if line.startswith(('-e', '--editable')):
121126
if line.startswith('-e'):
122-
line = line[2:].strip()
127+
line = line[2:].lstrip()
123128
else:
124-
line = line[len('--editable'):].strip().lstrip('=')
129+
line = _remove_prefix(line, '--editable')
125130
req = InstallRequirement.from_editable(
126131
line,
127132
comes_from=comes_from,

0 commit comments

Comments
 (0)