diff --git a/pip/req/req_file.py b/pip/req/req_file.py index 12a7bc8cfe5..73ec0bd860d 100644 --- a/pip/req/req_file.py +++ b/pip/req/req_file.py @@ -12,6 +12,18 @@ _scheme_re = re.compile(r'^(http|https|file):', re.I) +def _remove_prefixes(line, short_prefix, long_prefix): + if line.startswith(short_prefix): + return line[len(short_prefix):].lstrip() + else: + return _remove_prefix(line, long_prefix) + + +def _remove_prefix(line, prefix): + """Remove the prefix and eventually one '=' or spaces""" + return re.sub(r'\s*=?\s*', '', line[len(prefix):]) + + def parse_requirements(filename, finder=None, comes_from=None, options=None, session=None): if session is None: @@ -33,18 +45,15 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None, for line_number, line in enumerate(content.splitlines(), 1): line = line.strip() - # Remove comments from file - line = re.sub(r"(^|\s)#.*$", "", line) + # Remove comments from file and all spaces before it + line = re.sub(r"(^|\s)+#.*$", "", line) - if not line or line.startswith('#'): + if not line: continue if skip_match and skip_match.search(line): continue - if line.startswith('-r') or line.startswith('--requirement'): - if line.startswith('-r'): - req_url = line[2:].strip() - else: - req_url = line[len('--requirement'):].strip().strip('=') + if line.startswith(('-r', '--requirement')): + req_url = _remove_prefixes(line, '-r', '--requirement') if _scheme_re.search(filename): # Relative to a URL req_url = urllib_parse.urljoin(filename, req_url) @@ -56,32 +65,26 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None, options=options, session=session): yield item - elif line.startswith('-Z') or line.startswith('--always-unzip'): + elif line.startswith(('-Z', '--always-unzip')): # No longer used, but previously these were used in # requirement files, so we'll ignore. pass - elif line.startswith('-f') or line.startswith('--find-links'): - if line.startswith('-f'): - line = line[2:].strip() - else: - line = line[len('--find-links'):].strip().lstrip('=') + elif line.startswith(('-f', '--find-links')): + find_links = _remove_prefixes(line, '-f', '--find-links') # FIXME: it would be nice to keep track of the source of # the find_links: # support a find-links local path relative to a requirements file - relative_to_reqs_file = os.path.join(reqs_file_dir, line) + relative_to_reqs_file = os.path.join(reqs_file_dir, find_links) if os.path.exists(relative_to_reqs_file): - line = relative_to_reqs_file + find_links = relative_to_reqs_file if finder: - finder.find_links.append(line) - elif line.startswith('-i') or line.startswith('--index-url'): - if line.startswith('-i'): - line = line[2:].strip() - else: - line = line[len('--index-url'):].strip().lstrip('=') + finder.find_links.append(find_links) + elif line.startswith(('-i', '--index-url')): + index_url = _remove_prefixes(line, '-i', '--index-url') if finder: - finder.index_urls = [line] + finder.index_urls = [index_url] elif line.startswith('--extra-index-url'): - line = line[len('--extra-index-url'):].strip().lstrip('=') + line = _remove_prefix(line, '--extra-index-url') if finder: finder.index_urls.append(line) elif line.startswith('--use-wheel'): @@ -94,7 +97,7 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None, if finder: finder.index_urls = [] elif line.startswith("--allow-external"): - line = line[len("--allow-external"):].strip().lstrip("=") + line = _remove_prefix(line, '--allow-external') if finder: finder.allow_external |= set([normalize_name(line).lower()]) elif line.startswith("--allow-all-external"): @@ -108,22 +111,19 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None, pass # Remove after 7.0 elif line.startswith("--allow-insecure"): - line = line[len("--allow-insecure"):].strip().lstrip("=") + line = _remove_prefix(line, '--allow-insecure') if finder: finder.allow_unverified |= set([normalize_name(line).lower()]) elif line.startswith("--allow-unverified"): - line = line[len("--allow-unverified"):].strip().lstrip("=") + line = _remove_prefix(line, '--allow-unverified') if finder: finder.allow_unverified |= set([normalize_name(line).lower()]) else: comes_from = '-r %s (line %s)' % (filename, line_number) - if line.startswith('-e') or line.startswith('--editable'): - if line.startswith('-e'): - line = line[2:].strip() - else: - line = line[len('--editable'):].strip().lstrip('=') + if line.startswith(('-e', '--editable')): + editable = _remove_prefixes(line, '-e', '--editable') req = InstallRequirement.from_editable( - line, + editable, comes_from=comes_from, default_vcs=options.default_vcs if options else None, isolated=options.isolated_mode if options else False,