Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mimic web browser to validate user help URLs #591

Merged
merged 1 commit into from
Oct 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions planemo/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
URLError,
)
from six.moves.urllib.request import (
Request,
urlopen,
)

Expand Down Expand Up @@ -126,12 +127,19 @@ def lint_xsd(lint_ctx, schema_path, path):

def lint_urls(root, lint_ctx):
"""Find referenced URLs and verify they are valid."""
urls = find_urls_for_xml(root)
urls, docs = find_urls_for_xml(root)

def validate_url(url, lint_ctx):
# This is from Google Chome 53.0.2785.143, current at time of writing:
BROWSER_USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"

def validate_url(url, lint_ctx, user_agent=None):
is_valid = True
if user_agent:
req = Request(url, headers={"User-Agent": user_agent})
else:
req = url
try:
handle = urlopen(url)
handle = urlopen(req)
handle.read(100)
except HTTPError as e:
if e.code == 429:
Expand All @@ -148,6 +156,8 @@ def validate_url(url, lint_ctx):

for url in urls:
validate_url(url, lint_ctx)
for url in docs:
validate_url(url, lint_ctx, BROWSER_USER_AGENT)


__all__ = [
Expand Down
10 changes: 8 additions & 2 deletions planemo/shed/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ def process_repo(realized_repository):


def find_urls_for_xml(root):
"""Returns two lists: explicit package URLs, and help text URLs.

For validating the user-facing URLs is it sensible to mimic
a web browser user agent.
"""
urls = []
for packages in root.findall("package"):
install_els = packages.findall("install")
Expand All @@ -240,11 +245,12 @@ def find_urls_for_xml(root):
if hasattr(subaction, 'packages'):
urls.extend(subaction.packages)

docs = []
for help_text in root.findall("help"):
for url in _find_urls_in_text(help_text.text):
urls.append(url[0])
docs.append(url[0])

return urls
return urls, docs


def handle_force_create(realized_repository, ctx, shed_context, **kwds):
Expand Down