-
Notifications
You must be signed in to change notification settings - Fork 1
/
prefixping.py
339 lines (279 loc) · 11.3 KB
/
prefixping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import requests
import yaml
import os
import stat
import re
import json
import logging
from flask import Flask, url_for # , abort
from datetime import datetime
import email.utils as emut
# Tue, 01 Aug 2017 14:14:26 GMT
# note see http://strftime.org/
# fmt = r'%a, %d %b %Y %H:%M:%S GMT'
# using email.utils instead
app = Flask(__name__)
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
'''
Note:
My first of this sort of framework based web app / micro service thingys.
I am sure to be "doing it wrong"(tm).
This should sit on the web somewhere and you send it a string
and it lets you know if it has found it registered somewhere.
That is: yes/no.
As such you do not even need to transmit pages
('cept humans will want them)
http head requests and the returned status codes is enough.
so that is where we will are. next we start pileing on the request forms
and beautiful search results ...
hopefully never aproaching the 1Meg page just to say "nope" a source does.
'''
def fetch_regurl():
'''
Reads in the local list of urls to ping
'''
# if not hasattr(session, 'regurl'):
if os.path.isfile('registry_url.yaml'):
with open('registry_url.yaml', 'r') as fh:
regurl = yaml.load(fh)
else:
log.warrning("halp. halp. I've fallen and can't get up")
return regurl
# maybe put in a different package and import?
regurl = fetch_regurl()
def remote_metadata(head_url):
remote_date = None
remote_size = -1
try:
response = requests.head(head_url, allow_redirects=True)
except requests.exceptions.RequestException:
response.raise_for_status()
if response.status_code == requests.codes.ok:
if 'last-modified' in response.headers:
date_string = response.headers['last-modified']
remote_date = datetime(*emut.parsedate(date_string)[:6])
else:
log.warning(
'No "Last-Modified:" header for ' + response.url)
if 'Content-Length' in response.headers:
remote_size = response.headers['Content-Length']
else:
log.warning(
'No "Content-Length:" header for ' + response.url)
else:
if response.status_code >= 500:
log.error('Server Error: ' + str(response.status_code))
else:
log.error(
response.url + ' returned ' + str(response.status_code))
return [remote_date, remote_size]
def local_metadata(pth):
local_date = None
local_size = -1
if os.path.isfile(pth):
local_date = datetime.fromtimestamp(
os.stat(pth)[stat.ST_MTIME])
local_size = os.stat(pth)[stat.ST_SIZE]
return [local_date, local_size]
def read_yaml_array(fname, arr):
if os.path.isfile(fname):
with open(fname, 'r') as fh:
arr = yaml.load(fh)
local_datetime = arr[0]
log.info('Found local cache from: ', local_datetime)
else:
log.error("Cannot open " + fname)
return arr
def fetch_prefix(lcl_file, rmt_url, process_raw):
'''
Fetches yaml
if the local cache is more than day old
check if the remote is newer
if so use and refresh cache
otherwise use local cache
'''
result_array = []
# check local cache
(local_date, local_size) = local_metadata(lcl_file)
# is cache more than a day old?
if local_date is None or (datetime.now() - local_date).days > 0:
(remote_date, remote_size) = remote_metadata(rmt_url)
# has the remote been updated ?
if local_date is None or remote_date is not None and remote_date > local_date:
log.info("Remote newer")
log.info('Fetching: ' + rmt_url)
try:
response = requests.get(rmt_url)
except requests.exceptions.RequestException:
response.raise_for_status()
# the happy path
if response.status_code == requests.codes.ok:
rawyaml = yaml.load(response.text)
rmt_pth = rmt_url.split('/')
rmt_file = rmt_pth[len(rmt_pth)-1] # last on path
log.info('renewing local copy of %s', rmt_file)
with open(rmt_file, 'w') as fh:
yaml.dump(result_array, fh)
# remote_datetime = datetime.strptime(remote_date, fmt)
result_array.append('# ' + str(remote_date))
# source specific
result_array = process_raw(rawyaml, result_array)
log.info('renewing local ptrfix cache %s', lcl_file)
with open(lcl_file, 'w') as fh:
yaml.dump(result_array, fh)
else: # fetch remote failed
log.error(
'ERROR ' + response.url + ' returned ' +
str(response.status_code))
log.info('Trying local cache')
result_array = read_yaml_array(lcl_file, result_array)
else:
log.info('Remote is not newer than ' + lcl_file)
result_array = read_yaml_array(lcl_file, result_array)
else: # cache is fresh
log.info('Cache ' + lcl_file + ' is less than a day old')
result_array = read_yaml_array(lcl_file, result_array)
# result_array is a list for go but a dict for cdl ??
return [word.lower() for word in result_array]
# get the GO prefixes refreshing local cache as needed
# specific GO yaml helper f(x)
def go_proc_raw(rawyaml, rstarr):
for db in rawyaml:
rstarr.append(db['database'])
return rstarr
# till geneontology file is fixed
# gocurl = 'http://current.geneontology.org/metadata/db-xrefs.yaml'
gocurl = 'https://raw.githubusercontent.com/geneontology/go-site/master/metadata/db-xrefs.yaml'
gocprefixfile = 'gocprefix.yaml'
gocprefix = fetch_prefix(gocprefixfile, gocurl, go_proc_raw)
# get the CDL prefixes refreshing local cache as needed
# specific CDL yaml helper f(x)
def cdl_proc_raw(rawyaml, rstarr):
for reg in rawyaml:
rstarr.append(reg['namespace'])
if 'alias' in reg: # TODO should I be including these?
rstarr.append(reg['alias'])
return rstarr
cdlebi_url = 'https://n2t.net/e/cdl_ebi_prefixes.yaml'
cdlebi_file = 'cdl_ebi_prefixes.yaml'
cdlebiprefix = fetch_prefix(cdlebi_file, cdlebi_url, cdl_proc_raw)
# blurb to return when the string does not parse
howto = 'letter followed by one or more letters, digits, or dash(dot)'
dot_whinge = '''
prefix accepted with prejudice.
dot is best left delimiting trailing version numbers.
please consider replacing dot with dash.
Thanks, The Management
'''
def sanitize(tainted):
'''
a letter followed a handful of alphanumerics or hyphen
not including underscore or colon
as they are expected to delimit the local id
dots should be for trailing versions on the local id (but are not)
spaces should get you shot.
need at least two chars (would prefer more)
limiting to 32 chars (would prefer less)
:return: safer string or None
'''
penultimat = 32
match = re.match(
r'[a-zA-Z][0-9a-zA-Z.-]{1,' + str(penultimat) + '}', tainted)
if not match:
pfx = None
else: # limit to first acceptable part
pfx = match.group(0)
return pfx.strip()
@app.route('/ping/')
def hello_world():
return '<h1>PPOOONNNGGGG!</h1><br>' + str(datetime.now()) +'<br>'
@app.route('/ping/help/')
def help():
route_path = url_for('ping', qrystr='XYZ')
return '<h2>Prefix Ping!</h2><br>Usage: http://[host]%s<br>' % route_path
@app.route('/ping/prefix/<string:qrystr>', methods=['GET'])
def ping(qrystr):
qry = sanitize(qrystr)
pfx = qry.lower()
result = {'user_query': qrystr, 'hits': 0, 'miss': 0}
if pfx is not None and pfx is not '':
result['accepted_prefix'] = pfx
result['sources'] = {}
if len(pfx) == len(qrystr):
if re.search(r'\.', pfx) is not None:
result['status'] = dot_whinge
else:
result['status'] = 'prefix accepted'
result['sources']['GO'] = {
'uri': 'http://current.geneontology.org/metadata/db-xrefs.yaml',
'url': 'http://amigo2.berkeleybop.org/xrefs#'}
if pfx in gocprefix:
result['sources']['GO']['registered'] = True
result['hits'] += 1
result['sources']['GO']['link'] = \
'http://amigo2.berkeleybop.org/xrefs#' + qry
else:
result['sources']['GO']['registered'] = False
result['miss'] += 1
result['sources']['N2T'] = {
'uri': 'https://n2t.net/e/cdl_ebi_prefixes.yaml',
'url': 'http://identifiers.org/'}
if pfx in cdlebiprefix:
result['sources']['N2T']['registered'] = True
result['hits'] += 1
result['sources']['N2T']['link'] = \
'http://identifiers.org/' + qry
else:
result['sources']['N2T']['registered'] = False
result['miss'] += 1
# hit the remote sites
for reg in regurl:
response = None
try:
response = requests.head(
regurl[reg] + pfx, allow_redirects=True)
except requests.exceptions.RequestException:
response.raise_for_status()
result['sources'][reg] = {
'url': regurl[reg]}
if response.status_code == requests.codes.ok:
result['sources'][reg]['registered'] = True
result['hits'] += 1
result['sources'][reg]['link'] = str(regurl[reg]) + qry
elif response.status_code > 499 or response.status_code < 400:
# non negative response
result['sources'][reg]['registered'] = \
'ERROR:' + str(response.status_code)
result['miss'] += 1
else: # non positive response
result['sources'][reg]['registered'] = False
result['miss'] += 1
else:
# only accepting part of the beginning of the prefix to check
result['status'] = 'prefix only partialy accepted'
result['comment'] = howto
else:
# something very wrong with the input
result['status'] = 'NOPE! query prefix NOT accepted'
result['comment'] = howto
return json.dumps(result)
# TODO this keepalive may not be needed with uwsgi parameter tweaks
@app.route('/ping/pong/', methods=['GET'])
def pong():
(go_date, go_size) = local_metadata(gocprefixfile)
(cdl_date, cdl_size) = local_metadata(cdlebi_file)
# return nonzero if the app's local cache needs refreshing
age = (datetime.now() - go_date).days | (datetime.now() - cdl_date).days
return json.dumps({'cachedays': age})
@app.route('/ping/refresh/', methods=['GET'])
def refresh(go=gocprefix, cdl=cdlebiprefix):
'''
was an alternative method of refreshing caches but
fetch_prefix() now tries to refresh automatically
and have not rewritten this with new methods
'''
return "{'implemented': 'False'}"
# for local testing
if __name__ == "__main__":
app.run(host='0.0.0.0')