|
3 | 3 | import os
|
4 | 4 | import xml.etree.ElementTree as ET
|
5 | 5 | from typing import TYPE_CHECKING
|
| 6 | +from urllib.request import urlopen |
6 | 7 |
|
| 8 | +import requests |
7 | 9 | import yaml
|
8 | 10 | from galaxy.tool_util.lint import lint_tool_source_with
|
9 | 11 | from galaxy.tool_util.linters.help import rst_invalid
|
|
17 | 19 | )
|
18 | 20 | from planemo.shed import (
|
19 | 21 | CURRENT_CATEGORIES,
|
| 22 | + find_urls_for_xml, |
20 | 23 | REPO_TYPE_SUITE,
|
21 | 24 | REPO_TYPE_TOOL_DEP,
|
22 | 25 | REPO_TYPE_UNRESTRICTED,
|
@@ -188,8 +191,43 @@ def lint_readme(realized_repository, lint_ctx):
|
188 | 191 |
|
189 | 192 |
|
190 | 193 | def lint_tool_dependencies_urls(realized_repository, lint_ctx):
|
191 |
| - |
192 |
| - |
| 194 | + |
| 195 | + def lint_urls(root, lint_ctx): |
| 196 | + """Find referenced URLs and verify they are valid. |
| 197 | +
|
| 198 | + note this function was used previously for tools (URLs in help) and tool dependency files |
| 199 | + the former has been rewritten and therefore the function has been moved here |
| 200 | + """ |
| 201 | + urls, _ = find_urls_for_xml(root) |
| 202 | + for url in urls: |
| 203 | + is_valid = True |
| 204 | + if url.startswith("http://") or url.startswith("https://"): |
| 205 | + headers = None |
| 206 | + r = None |
| 207 | + try: |
| 208 | + r = requests.get(url, headers=headers, stream=True) |
| 209 | + r.raise_for_status() |
| 210 | + next(r.iter_content(1000)) |
| 211 | + except Exception as e: |
| 212 | + if r is not None and r.status_code == 429: |
| 213 | + # too many requests |
| 214 | + pass |
| 215 | + if r is not None and r.status_code in [403, 503] and "cloudflare" in r.text: |
| 216 | + # CloudFlare protection block |
| 217 | + pass |
| 218 | + else: |
| 219 | + is_valid = False |
| 220 | + lint_ctx.error(f"Error '{e}' accessing {url}") |
| 221 | + else: |
| 222 | + try: |
| 223 | + with urlopen(url) as handle: |
| 224 | + handle.read(100) |
| 225 | + except Exception as e: |
| 226 | + is_valid = False |
| 227 | + lint_ctx.error(f"Error '{e}' accessing {url}") |
| 228 | + if is_valid: |
| 229 | + lint_ctx.info("URL OK %s" % url) |
| 230 | + |
193 | 231 | path = realized_repository.real_path
|
194 | 232 | tool_dependencies = os.path.join(path, "tool_dependencies.xml")
|
195 | 233 | if not os.path.exists(tool_dependencies):
|
|
0 commit comments