-
Notifications
You must be signed in to change notification settings - Fork 1
/
irodsmanager.py
264 lines (198 loc) · 7.66 KB
/
irodsmanager.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
#! /usr/bin/env python
"""
#
#
#
"""
import os
import json
import textwrap
import irods
from irods.session import iRODSSession
import irods.exception as ex
from irods.column import Criterion
from irods.data_object import chunks
import irods.test.helpers as helpers
import irods.keywords as kw
from irods.meta import iRODSMeta
from irods.models import (DataObject, Collection, Resource, User, DataObjectMeta,CollectionMeta, ResourceMeta, UserMeta)
from irods.results import ResultSet
from irods.rule import Rule
from irods.meta import iRODSMetaCollection
from irods.exception import CollectionDoesNotExist
#
# data access object class for irods
#
class irodsDAO():
def __init__(self, config, log):
print("irods ")
self.session = None
self.log = log
self.config = config
#
# irods Connection
#
def _irodsConnect(self):
if self.session:
return
self.log.info("try connection to irods")
# Make iRODS connection
self.session = iRODSSession(host=str(self.config['IRODS']['HOST']), port=str(self.config['IRODS']['PORT']), user=str(self.config['IRODS']['USER']), password=str(self.config['IRODS']['PWD']), zone=str(self.config['IRODS']['ZONE']))
self.log.info("done")
#
# irods ingestion iREG
#
def doRegister(self, dirname, collname, filename):
self._irodsConnect()
obj_file = os.path.join(dirname, filename)
obj_path = '{collname}/{filename}'.format(**locals())
self._checkCollExsist(collname)
# register file in test collection
self.log.info("check obj_file : "+obj_file)
self.log.info("check obj_path : "+obj_path)
self.log.info("check or create a collection recursively : "+collname)
try:
self.session.data_objects.register(obj_file, obj_path)
self.log.info("file registered! : "+obj_path)
except Exception as ex:
self.log.error("Could not register a file_obj ")
self.log.error(ex)
pass
# confirm object presence
#obj = self.session.data_objects.get(obj_path)
#print("registred!")
#print (obj)
#
# Execute a paramless rule
#
def _ruleExec(self, rule_file_path):
self._irodsConnect()
#print("path rule: "+rule_file_path)
# run rule
myrule = Rule(self.session, rule_file_path)
ruleout = myrule.execute()
return ruleout
#
# Check if a collection exsist, if not create recursively
#
def _checkCollExsist(self, collname):
self.log.info("check or create a collection recursively : "+collname)
try:
self.session.collections.create(collname, recurse=True)
self.log.info("collection created! : "+collname)
except Exception as ex:
self.log.error("Could not create a collection recursively ")
self.log.error(ex)
pass
#
# get Digital Object
#
def getObject(self, obj_path):
self._irodsConnect()
return self.session.data_objects.get(obj_path)
#
# irods Rule Execution: PID creation (PID)
#
def rulePIDsingle(self, object_path, rule_path):
# check connection
self._irodsConnect()
self.log.info("exec PID SINGLE rule inside irods ")
# load rule from file
rule_total = self.load_rule(rule_path, path='"{object_path}"'.format(**locals()) )
# prep rule
myrule = Rule(self.session, body=rule_total['body'], params=rule_total['params'], output=rule_total['output'] )
# exec rule
try:
myrule.execute()
# check that metadata is there
#returnedMeta = self.session.metadata.get(DataObject, object_path)
self.log.info(" PID for digitalObject: "+object_path+" is: OK")
except Exception as ex:
self.log.info("Could not execute a rule for PID ")
self.log.info(ex)
pass
return 1 #returnedMeta
#
# irods Rule Execution: REPLICATION (REP)
#
def ruleReplication(self, object_path, target_path, rule_path):
# check connection
self._irodsConnect()
self.log.info("exec Replication rule inside irods ")
returnedMeta = {}
# load rule from file
rule_total = self.load_rule(rule_path, source='"{object_path}"'.format(**locals()), destination='"{target_path}"'.format(**locals()) )
# prep rule
myrule = Rule(self.session, body=rule_total['body'], params=rule_total['params'], output=rule_total['output'] )
# exec rule
try:
myrule.execute()
# check that metadata is there
#returnedMeta = self.session.metadata.get(DataObject, object_path)
self.log.info(" REPLICA for digitalObject: "+object_path+" is: OK")
except Exception as ex:
self.log.info("Could not execute a rule for REPLICATION ")
self.log.info(ex)
pass
return 1 #returnedMeta
#
# irods Rule Execution: REMOTE REPLICA REGISTRATION (RRR)
#
def ruleRegistration(self, object_path, target_path, rule_path):
# check connection
self._irodsConnect()
self.log.info("exec Registration Remote replicated object inside irods ")
# load rule from file
rule_total = self.load_rule(rule_path, source='"{object_path}"'.format(**locals()), destination='"{target_path}"'.format(**locals()) )
# prep rule
myrule = Rule(self.session, body=rule_total['body'], params=rule_total['params'], output=rule_total['output'] )
# exec rule
try:
myrule.execute()
# check that metadata is there
#returnedMeta = self.session.metadata.get(DataObject, object_path)
self.log.info(" REGISTRATION for digitalObject: "+object_path+" is: OK")
except Exception as ex:
self.log.info("Could not execute a rule for REGISTRATION ")
self.log.info(ex)
pass
return 1 #returnedMeta
#
# load irods rule from rule_file.r
#
def load_rule(self, rule_file, **parameters):
results = {}
params = {}
output = ''
body = '@external\n'
# parse rule file
with open(rule_file) as f:
for line in f:
# parse input line
if line.strip().lower().startswith('input'):
input_header, input_line = line.split(None, 1)
# sanity check
if input_header.lower() != 'input':
raise ValueError
# parse *param0="value0",*param1="value1",...
for pair in input_line.split(','):
label, value = pair.split('=')
params[label.strip()] = value.strip()
# parse output line
elif line.strip().lower().startswith('output'):
output_header, output_line = line.split(None, 1)
# sanity check
if output_header.lower() != 'output':
raise ValueError
# use line as is
output = output_line.strip()
# parse rule
else:
body += line
# put passed parameters in params
for key, value in parameters.items():
params['*'+key] = value
results['params']=params
results['body']=body
results['output']=output
return results