Skip to content

Commit 444ee95

Browse files
committed
Merge pull request #2497 from xavfernandez/parse_req_cleanup
Parse req cleanup
2 parents 18d92c4 + 88e9f34 commit 444ee95

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

pip/req/req_file.py

+33-33
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
_scheme_re = re.compile(r'^(http|https|file):', re.I)
1313

1414

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+
1527
def parse_requirements(filename, finder=None, comes_from=None, options=None,
1628
session=None):
1729
if session is None:
@@ -33,18 +45,15 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None,
3345
for line_number, line in enumerate(content.splitlines(), 1):
3446
line = line.strip()
3547

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

39-
if not line or line.startswith('#'):
51+
if not line:
4052
continue
4153
if skip_match and skip_match.search(line):
4254
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')
4857
if _scheme_re.search(filename):
4958
# Relative to a URL
5059
req_url = urllib_parse.urljoin(filename, req_url)
@@ -56,32 +65,26 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None,
5665
options=options,
5766
session=session):
5867
yield item
59-
elif line.startswith('-Z') or line.startswith('--always-unzip'):
68+
elif line.startswith(('-Z', '--always-unzip')):
6069
# No longer used, but previously these were used in
6170
# requirement files, so we'll ignore.
6271
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')
6874
# FIXME: it would be nice to keep track of the source of
6975
# the find_links:
7076
# 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)
7278
if os.path.exists(relative_to_reqs_file):
73-
line = relative_to_reqs_file
79+
find_links = relative_to_reqs_file
7480
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')
8184
if finder:
82-
finder.index_urls = [line]
85+
finder.index_urls = [index_url]
8386
elif line.startswith('--extra-index-url'):
84-
line = line[len('--extra-index-url'):].strip().lstrip('=')
87+
line = _remove_prefix(line, '--extra-index-url')
8588
if finder:
8689
finder.index_urls.append(line)
8790
elif line.startswith('--use-wheel'):
@@ -94,7 +97,7 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None,
9497
if finder:
9598
finder.index_urls = []
9699
elif line.startswith("--allow-external"):
97-
line = line[len("--allow-external"):].strip().lstrip("=")
100+
line = _remove_prefix(line, '--allow-external')
98101
if finder:
99102
finder.allow_external |= set([normalize_name(line).lower()])
100103
elif line.startswith("--allow-all-external"):
@@ -108,22 +111,19 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None,
108111
pass
109112
# Remove after 7.0
110113
elif line.startswith("--allow-insecure"):
111-
line = line[len("--allow-insecure"):].strip().lstrip("=")
114+
line = _remove_prefix(line, '--allow-insecure')
112115
if finder:
113116
finder.allow_unverified |= set([normalize_name(line).lower()])
114117
elif line.startswith("--allow-unverified"):
115-
line = line[len("--allow-unverified"):].strip().lstrip("=")
118+
line = _remove_prefix(line, '--allow-unverified')
116119
if finder:
117120
finder.allow_unverified |= set([normalize_name(line).lower()])
118121
else:
119122
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')
125125
req = InstallRequirement.from_editable(
126-
line,
126+
editable,
127127
comes_from=comes_from,
128128
default_vcs=options.default_vcs if options else None,
129129
isolated=options.isolated_mode if options else False,

0 commit comments

Comments
 (0)