6
6
Dict ,
7
7
TYPE_CHECKING ,
8
8
)
9
- from urllib .request import urlopen
10
9
11
- import requests
12
10
from galaxy .tool_util .lint import (
13
11
LintContext ,
14
12
Linter ,
15
13
)
16
14
17
15
from planemo .io import error
18
- from planemo .shed import find_urls_for_xml
19
16
from planemo .xml import validation
20
17
21
18
if TYPE_CHECKING :
@@ -71,46 +68,6 @@ def handle_lint_complete(lint_ctx, lint_args, failed=False):
71
68
return 1 if failed else 0
72
69
73
70
74
- def lint_dois (tool_xml , lint_ctx ):
75
- """Find referenced DOIs and check they have valid with https://doi.org."""
76
- dois = find_dois_for_xml (tool_xml )
77
- for publication in dois :
78
- is_doi (publication , lint_ctx )
79
-
80
-
81
- def find_dois_for_xml (tool_xml ):
82
- dois = []
83
- for element in tool_xml .getroot ().findall ("citations" ):
84
- for citation in list (element ):
85
- if citation .tag == "citation" and citation .attrib .get ("type" , "" ) == "doi" :
86
- dois .append (citation .text )
87
- return dois
88
-
89
-
90
- def is_doi (publication_id , lint_ctx ):
91
- """Check if dx.doi knows about the ``publication_id``."""
92
- base_url = "https://doi.org"
93
- if publication_id is None :
94
- lint_ctx .error ("Empty DOI citation" )
95
- return
96
- publication_id = publication_id .strip ()
97
- doiless_publication_id = publication_id .split ("doi:" , 1 )[- 1 ]
98
- if not doiless_publication_id :
99
- lint_ctx .error ("Empty DOI citation" )
100
- return
101
- url = f"{ base_url } /{ doiless_publication_id } "
102
- r = requests .get (url )
103
- if r .status_code == 200 :
104
- if publication_id != doiless_publication_id :
105
- lint_ctx .error ("%s is valid, but Galaxy expects DOI without 'doi:' prefix" % publication_id )
106
- else :
107
- lint_ctx .info ("%s is a valid DOI" % publication_id )
108
- elif r .status_code == 404 :
109
- lint_ctx .error ("%s is not a valid DOI" % publication_id )
110
- else :
111
- lint_ctx .warn ("dx.doi returned unexpected status code %d" % r .status_code )
112
-
113
-
114
71
def lint_xsd (lint_ctx , schema_path , path ):
115
72
"""Lint XML at specified path with supplied schema."""
116
73
name = lint_ctx .object_name or os .path .basename (path )
@@ -124,55 +81,8 @@ def lint_xsd(lint_ctx, schema_path, path):
124
81
lint_ctx .info ("File validates against XML schema." )
125
82
126
83
127
- def lint_urls (root , lint_ctx ):
128
- """Find referenced URLs and verify they are valid."""
129
- urls , docs = find_urls_for_xml (root )
130
-
131
- # This is from Google Chome on macOS, current at time of writing:
132
- 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"
133
-
134
- def validate_url (url , lint_ctx , user_agent = None ):
135
- is_valid = True
136
- if url .startswith ("http://" ) or url .startswith ("https://" ):
137
- if user_agent :
138
- headers = {"User-Agent" : user_agent , "Accept" : "*/*" }
139
- else :
140
- headers = None
141
- r = None
142
- try :
143
- r = requests .get (url , headers = headers , stream = True )
144
- r .raise_for_status ()
145
- next (r .iter_content (1000 ))
146
- except Exception as e :
147
- if r is not None and r .status_code == 429 :
148
- # too many requests
149
- pass
150
- if r is not None and r .status_code in [403 , 503 ] and "cloudflare" in r .text :
151
- # CloudFlare protection block
152
- pass
153
- else :
154
- is_valid = False
155
- lint_ctx .error (f"Error '{ e } ' accessing { url } " )
156
- else :
157
- try :
158
- with urlopen (url ) as handle :
159
- handle .read (100 )
160
- except Exception as e :
161
- is_valid = False
162
- lint_ctx .error (f"Error '{ e } ' accessing { url } " )
163
- if is_valid :
164
- lint_ctx .info ("URL OK %s" % url )
165
-
166
- for url in urls :
167
- validate_url (url , lint_ctx )
168
- for url in docs :
169
- validate_url (url , lint_ctx , BROWSER_USER_AGENT )
170
-
171
-
172
84
__all__ = (
173
85
"build_lint_args" ,
174
86
"handle_lint_complete" ,
175
- "lint_dois" ,
176
- "lint_urls" ,
177
87
"lint_xsd" ,
178
88
)
0 commit comments