|
6 | 6 |
|
7 | 7 | from coalib.results.Diff import Diff
|
8 | 8 | from coalib.bears.LocalBear import LocalBear
|
| 9 | +from coalib.misc.Enum import enum |
9 | 10 | from dependency_management.requirements.PipRequirement import PipRequirement
|
10 | 11 | from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
|
11 | 12 | from coalib.results.Result import Result
|
@@ -50,8 +51,7 @@ def parse_pip_vcs_url(link):
|
50 | 51 | return splitted_schema
|
51 | 52 |
|
52 | 53 | @staticmethod
|
53 |
| - def find_links_in_file(file, network_timeout, link_ignore_regex, |
54 |
| - link_ignore_list): |
| 54 | + def extract_links_from_file(file, link_ignore_regex, link_ignore_list): |
55 | 55 | link_ignore_regex = re.compile(link_ignore_regex)
|
56 | 56 | regex = re.compile(
|
57 | 57 | r"""
|
@@ -80,22 +80,35 @@ def find_links_in_file(file, network_timeout, link_ignore_regex,
|
80 | 80 | # Unbalanced parenthesis
|
81 | 81 | (?<!\.)(?<!,) # Exclude trailing `.` or `,` from URL
|
82 | 82 | """, re.VERBOSE)
|
| 83 | + |
83 | 84 | for line_number, line in enumerate(file):
|
84 | 85 | for match in re.findall(regex, line):
|
85 | 86 | link = match[0]
|
| 87 | + context = enum( |
| 88 | + pip_vcs_url=False) |
| 89 | + if link.startswith(('hg+', 'bzr+', 'git+', 'svn+')): |
| 90 | + context.pip_vcs_url = True |
86 | 91 | if not (link_ignore_regex.search(link) or
|
87 | 92 | fnmatch(link, link_ignore_list)):
|
88 |
| - if link.startswith(('hg+', 'bzr+', 'git+', 'svn+')): |
89 |
| - link = InvalidLinkBear.parse_pip_vcs_url(link) |
90 |
| - host = urlparse(link).netloc |
91 |
| - code = InvalidLinkBear.get_status_code( |
92 |
| - link, |
93 |
| - network_timeout.get(host) |
94 |
| - if host in network_timeout |
95 |
| - else network_timeout.get('*') |
96 |
| - if '*' in network_timeout |
97 |
| - else InvalidLinkBear.DEFAULT_TIMEOUT) |
98 |
| - yield line_number + 1, link, code |
| 93 | + yield link, line_number, context |
| 94 | + |
| 95 | + def analyze_links_in_file(self, file, network_timeout, link_ignore_regex, |
| 96 | + link_ignore_list): |
| 97 | + for link, line_number, link_context in self.extract_links_from_file( |
| 98 | + file, link_ignore_regex, link_ignore_list): |
| 99 | + |
| 100 | + if link_context.pip_vcs_url: |
| 101 | + link = InvalidLinkBear.parse_pip_vcs_url(link) |
| 102 | + |
| 103 | + host = urlparse(link).netloc |
| 104 | + code = InvalidLinkBear.get_status_code( |
| 105 | + link, |
| 106 | + network_timeout.get(host) |
| 107 | + if host in network_timeout |
| 108 | + else network_timeout.get('*') |
| 109 | + if '*' in network_timeout |
| 110 | + else InvalidLinkBear.DEFAULT_TIMEOUT) |
| 111 | + yield line_number + 1, link, code, link_context |
99 | 112 |
|
100 | 113 | @deprecate_settings(link_ignore_regex='ignore_regex',
|
101 | 114 | network_timeout=('timeout', lambda t: {'*': t}))
|
@@ -134,7 +147,7 @@ def run(self, filename, file,
|
134 | 147 | if not url == '*' else '*': timeout
|
135 | 148 | for url, timeout in network_timeout.items()}
|
136 | 149 |
|
137 |
| - for line_number, link, code in InvalidLinkBear.find_links_in_file( |
| 150 | + for line_number, link, code, context in self.analyze_links_in_file( |
138 | 151 | file, network_timeout, link_ignore_regex, link_ignore_list):
|
139 | 152 | if code is None:
|
140 | 153 | yield Result.from_values(
|
|
0 commit comments