Skip to content

Instantly share code, notes, and snippets.

@smehringer
Last active December 17, 2021 06:18
Show Gist options
  • Save smehringer/13f7534ce893e05b5d16c18cfcda0297 to your computer and use it in GitHub Desktop.
Save smehringer/13f7534ce893e05b5d16c18cfcda0297 to your computer and use it in GitHub Desktop.
Sharg Script
import os
from mod_python import apache
# - make sure file in subfolder 'resources/versions.txt' exists
# - URI passed to script is formated like: check/RESOURCENAME
# - RESOURCENAME is formated as either:
# * 'SeqAn_Linux_64_Alf_2.0.0' (Argument Parser in Seqan 2)
# * 'SeqAn3_Linux_64_Raptor_2.0.0' (Argument Parser in Seqan 3)
# * 'SeqAn-Sharg_Linux_64_Alf_2.0.0' (Stand alone Sharg Parser)
# The toolname of a request is extracted and the returned by the server.
def handler( req ):
resource_database = {'SeqAn_REST_TEST': 'true'}
script_path = os.path.dirname(os.path.realpath(__file__))
# load resource file
fname = script_path + '/resources/seqan_versions.txt'
if not os.path.isfile(fname):
req.log_error("[VERSION CHECK ERROR] The resource file /check/resources/seqan_versions.txt cannot be found!")
raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
lines = [line.strip() for line in open(fname, 'r')]
for line in lines:
fields = line.split("\t")
if len(fields) == 2:
resource_database[fields[0]] = fields[1]
else:
req.log_error("[VERSION CHECK ERROR] Mal formed resource file!")
# Extract the resource name
uripath = [p for p in req.uri.split('/') if p]
if len(uripath) != 3 or uripath[0] != 'check' or uripath[1] != 'check.py':
req.log_error("[VERSION CHECK ERROR] malformed URI path!")
raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
resource_name = uripath[-1]
req.log_error(req.filename)
# return resource
if req.method == 'GET':
req.content_type = 'application/octet-stream'
# for debugging: if 'all' is provided as resource name print all entries
if resource_name == 'all':
content = resource_database
req.write(str(content))
return apache.OK
#now we have a proper resource string like: 'SeqAn3_Linux_64_Raptor_2.0.0'
fields = resource_name.split('_')
# sanity check for number of fields and prefix
if len(fields) == 1:
raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
if not 'SeqAn' in fields[0]:
req.log_error("[VERSION CHECK ERROR] SeqAn prefix missing in request string!")
raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
tool_name = "_".join(fields[3:-1]) # in case the app name had contained '_'
seqan = "seqan"
if 'SeqAn3' in fields[0]:
seqan = "seqan3"
elif 'Sharg' in fields[0]:
seqan = "sharg"
# retrieve previously stored resource
if tool_name in resource_database:
req.write(resource_database[tool_name] + os.linesep + resource_database[seqan])
else:
req.write("UNREGISTERED_APP" + os.linesep + resource_database[seqan])
# store full request string in apache log (also stores ip and date)
req.log_error(resource_name)
return apache.OK
else:
raise apache.SERVER_RETURN, apache.HTTP_METHOD_NOT_ALLOWED
return apache.OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment