Skip to content

Commit 332c57a

Browse files
fix linter errors
and restore lint_urls for lint_tool_dependencies_urls
1 parent 7aca51a commit 332c57a

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

planemo/linters/biocontainer_registered.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Ensure best-practice biocontainer registered for this tool."""
22

33
from typing import (
4+
List,
45
Optional,
56
TYPE_CHECKING,
67
)
@@ -42,7 +43,7 @@ def lint(cls, tool_source: "ToolSource", lint_ctx: "LintContext"):
4243
name = mulled_container_name("biocontainers", targets)
4344
if not name:
4445
requirements_node = xml_node_from_toolsource(tool_source, "requirements")
45-
lint_ctx.warn(MESSAGE_WARN_NO_CONTAINER, linter=cls.name(), node=requirements)
46+
lint_ctx.warn(MESSAGE_WARN_NO_CONTAINER, linter=cls.name(), node=requirements_node)
4647

4748

4849
def mulled_container_name(namespace: str, targets: List[CondaTarget]) -> Optional[str]:

planemo/linters/conda_requirements.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
)
1616

1717
if TYPE_CHECKING:
18+
from galaxy.tool_util.deps.conda_util import CondaTarget
1819
from galaxy.tool_util.lint import LintContext
1920
from galaxy.tool_util.parser.interface import ToolSource
2021

@@ -32,7 +33,7 @@ def lint(cls, tool_source: "ToolSource", lint_ctx: "LintContext"):
3233
if best_hit and exact:
3334
message = f"Requirement [{conda_target_str}] matches target in best practice Conda channel [{best_hit.get('channel')}]."
3435
requirements_node = xml_node_from_toolsource(tool_source, "requirements")
35-
lint_ctx.info(message, linter=cls.name(), node=requirements_nodes)
36+
lint_ctx.info(message, linter=cls.name(), node=requirements_node)
3637

3738

3839
class CondaRequirementInexact(Linter):
@@ -63,7 +64,7 @@ def lint(cls, tool_source: "ToolSource", lint_ctx: "LintContext"):
6364
lint_ctx.warn(message, linter=cls.name(), node=requirements_node)
6465

6566

66-
def _requirements_conda_targets(tool_source: "ToolSource") -> Generator[CondaTarget]:
67+
def _requirements_conda_targets(tool_source: "ToolSource") -> Generator["CondaTarget"]:
6768
requirements, *_ = tool_source.parse_requirements_and_containers()
6869
for requirement in requirements:
6970
conda_target = requirement_to_conda_targets(requirement)

planemo/shed_lint.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,55 @@ def lint_readme(realized_repository, lint_ctx):
188188

189189

190190
def lint_tool_dependencies_urls(realized_repository, lint_ctx):
191-
192-
191+
192+
def lint_urls(root, lint_ctx):
193+
"""Find referenced URLs and verify they are valid.
194+
195+
note this function was used previously for tools (URLs in help) and tool dependency files
196+
the former has been rewritten and therefore the function has been moved here
197+
"""
198+
urls, docs = find_urls_for_xml(root)
199+
200+
# This is from Google Chome on macOS, current at time of writing:
201+
BROWSER_USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36"
202+
203+
def validate_url(url, lint_ctx, user_agent=None):
204+
is_valid = True
205+
if url.startswith("http://") or url.startswith("https://"):
206+
if user_agent:
207+
headers = {"User-Agent": user_agent, "Accept": "*/*"}
208+
else:
209+
headers = None
210+
r = None
211+
try:
212+
r = requests.get(url, headers=headers, stream=True)
213+
r.raise_for_status()
214+
next(r.iter_content(1000))
215+
except Exception as e:
216+
if r is not None and r.status_code == 429:
217+
# too many requests
218+
pass
219+
if r is not None and r.status_code in [403, 503] and "cloudflare" in r.text:
220+
# CloudFlare protection block
221+
pass
222+
else:
223+
is_valid = False
224+
lint_ctx.error(f"Error '{e}' accessing {url}")
225+
else:
226+
try:
227+
with urlopen(url) as handle:
228+
handle.read(100)
229+
except Exception as e:
230+
is_valid = False
231+
lint_ctx.error(f"Error '{e}' accessing {url}")
232+
if is_valid:
233+
lint_ctx.info("URL OK %s" % url)
234+
235+
for url in urls:
236+
validate_url(url, lint_ctx)
237+
for url in docs:
238+
validate_url(url, lint_ctx, BROWSER_USER_AGENT)
239+
193240
path = realized_repository.real_path
194241
tool_dependencies = os.path.join(path, "tool_dependencies.xml")
195242
if not os.path.exists(tool_dependencies):

0 commit comments

Comments
 (0)