-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.py
181 lines (128 loc) · 4.82 KB
/
manage.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
import cwltool.factory
import requests
import os
import json
import io
import ast
from minio import Minio
from minio.error import (ResponseError, BucketAlreadyOwnedByYou,
BucketAlreadyExists)
def get_minio():
return Minio('minio-internal.odahub.io',
access_key='minio',
secret_key=open(os.environ.get('HOME')+"/.minio").read().strip(),
secure=False)
def run(inputs):
fac = cwltool.factory.Factory()
tool = fac.make("crab.cwl")
result = tool(
**inputs
)
result_json=json.dumps(result)
print(result_json)
return result_json, open("crab.cwl","r").read()
def load(bucket):
client = get_minio()
try:
result = client.get_object(bucket, 'result')
inputs = client.get_object(bucket, 'inputs')
meta = json.loads(client.get_object(bucket, 'meta').read())
except Exception as e:
raise
return result, meta['cwl']
def store(meta, inputs, result_json, bucket_name):
# Initialize client with an endpoint and access/secret keys.
client = get_minio()
try:
try:
get_name = lambda object: object.object_name
names = map(get_name, client.list_objects_v2(bucket_name, '', recursive=True))
for err in client.remove_objects(bucket_name, names):
print("Deletion Error: {}".format(err))
except ResponseError as err:
print(err)
client.remove_bucket(bucket_name)
except Exception as e:
print(e)
try:
client.make_bucket(bucket_name, location="us-east-1")
except BucketAlreadyOwnedByYou as err:
pass
except BucketAlreadyExists as err:
pass
except ResponseError as err:
raise
else:
# Put an object 'pumaserver_debug.log' with contents from 'pumaserver_debug.log'.
try:
print(client.put_object(bucket_name, 'result', io.BytesIO(result_json.encode()), len(result_json)))
print(client.put_object(bucket_name, 'inputs', io.BytesIO(json.dumps(inputs).encode()), len(json.dumps(inputs))))
print(client.put_object(bucket_name, 'meta', io.BytesIO(json.dumps(meta).encode()), len(json.dumps(meta))))
print("stored")
except ResponseError as err:
print(err)
def get_record():
#inputs, results, bucket_name):
r=requests.post('http://fuseki.internal.odahub.io/dataanalysis/query',
params=dict(query='''PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dda: <http://ddahub.io/ontology/analysis#>
SELECT * WHERE {
?sub ?x ?y .
?sub rdfs:subClassOf dda:CalibrationWorkflow .
}
LIMIT 10
'''),
auth=requests.auth.HTTPBasicAuth("admin", open(os.path.join(os.environ.get('HOME'), '.jena-password')).read().strip())
)
print(r)
print(r.text)
def create_record(inputs, results, bucket_name):
entries="dda:{bucket_name} rdfs:subClassOf dda:CalibrationWorkflow . ".format(bucket_name=bucket_name)
for k,v in inputs.items():
entries+="\ndda:{k}Input rdfs:subClassOf dda:hasInput . ".format(k=k)
entries+="\ndda:{bucket_name} dda:{k}Input \"{v}\" .".format(bucket_name=bucket_name, k=k, v=v)
entries+="\ndda:{bucket_name} dda:usesCalibrationCandle db:Crab_pulsar .".format(bucket_name=bucket_name, k=k, v=v)
print(entries)
r=requests.post('http://fuseki.internal.odahub.io/dataanalysis/update',
data='''PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dda: <http://ddahub.io/ontology/analysis#>
PREFIX dbprop: <http://dbpedia.org/property/>
PREFIX db: <http://dbpedia.org/resource/>
INSERT DATA
{
%s
}'''%entries,
auth=requests.auth.HTTPBasicAuth("admin", open(os.path.join(os.environ.get('HOME'), '.jena-password')).read().strip())
)
print(r)
print(r.text)
# find entity to run in rdf/jena
inputs=dict(
t1="2018-08-10T13:35:15",
t2="2018-09-19T13:35:15",
nscw=5,
chi2_limit=1.0,
systematic_fraction=0.02,
)
import hashlib
hashdigest = hashlib.md5(json.dumps(inputs).encode()).hexdigest()
get_record()
bucket_name = "workflow-crab-"+hashdigest
print("loading", bucket_name)
try:
r = load(bucket_name)
except:
r = None
if r is not None:
result_json, cwl_content = r
else:
result_json, cwl_content = run(
inputs
) # run with inputs in context
# in reana?
store(dict(name=bucket_name, notebook_url="https://github.com/volodymyrss/cc-crab/blob/master/crab.ipynb", cwl=cwl_content), inputs, result_json, bucket_name)
create_record(inputs, result_json, bucket_name)