-
Notifications
You must be signed in to change notification settings - Fork 6
/
build_configuration.py
194 lines (167 loc) · 5.92 KB
/
build_configuration.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
#!/usr/bin/env python
#=======================================================================
# The build_configuration.py tool is used to build the configuration
# details used by the KB to interact with the Graph engine.
# The tool requires to connect to a properly configured OMERO server
# to retrieve all the information needed to build the proper
# configuration.
# If the tool is launched without the OMERO server configuration
# parameters the produced configuration file will use the neo4j
# driver but no URI will be recorded and the KB will raise a
# GraphEngineConfigurationError as soon as a Proxy object will be
# created. To fix this, generate a proper configuration file.
#=======================================================================
import argparse, sys, os
import omero
import omero_ServerErrors_ice
PATH = os.path.abspath(__file__)
BIOBANK_ROOT = os.path.dirname(PATH)
PYTHON_OUT_FN = os.path.join(BIOBANK_ROOT, "bl/vl/kb/config.py")
GENERATED_BY_MSG = "GENERATED BY %s" % os.sep.join(PATH.rsplit(os.sep, 2)[1:])
class BoolStr(object):
def __new__(cls, s):
return False if s == '0' or s.lower() == 'false' else True
CONFIG_VALUES = [
{
'ome_config_value': 'omero.biobank.graph.engine',
'kb_config_value': 'GRAPH_ENGINE_DRIVER',
'type': str,
'default': "neo4j",
},
{
'ome_config_value': 'omero.biobank.graph.uri',
'kb_config_value': 'GRAPH_ENGINE_URI',
'type': str,
'default': None,
},
{
'ome_config_value': 'omero.biobank.graph.user',
'kb_config_value': 'GRAPH_ENGINE_USERNAME',
'type': str,
'default': None,
},
{
'ome_config_value': 'omero.biobank.graph.password',
'kb_config_value': 'GRAPH_ENGINE_PASSWORD',
'type': str,
'default': None,
},
{
'ome_config_value': 'omero.biobank.messages_queue.enabled',
'kb_config_value': 'MESSAGES_QUEUE_ENGINE_ENABLED',
'type': BoolStr,
'default': False,
},
{
'ome_config_value': 'omero.biobank.messages_queue.host',
'kb_config_value': 'MESSAGES_QUEUE_ENGINE_HOST',
'type': str,
'default': None,
},
{
'ome_config_value': 'omero.biobank.messages_queue.port',
'kb_config_value': 'MESSAGES_QUEUE_ENGINE_PORT',
'type': int,
'default': None,
},
{
'ome_config_value': 'omero.biobank.messages_queue.queue',
'kb_config_value': 'MESSAGES_QUEUE_ENGINE_QUEUE',
'type': str,
'default': None,
},
{
'ome_config_value': 'omero.biobank.messages_queue.user',
'kb_config_value': 'MESSAGES_QUEUE_ENGINE_USERNAME',
'type': str,
'default': None,
},
{
'ome_config_value': 'omero.biobank.messages_queue.password',
'kb_config_value': 'MESSAGES_QUEUE_ENGINE_PASSWORD',
'type': str,
'default': None,
}
]
class ArgumentConstraintError(Exception):
pass
class BuildConfigurationError(Exception):
pass
def make_parser():
parser = argparse.ArgumentParser('Build the biobank config file')
parser.add_argument('-H', '--host', type=str, help='omero hostname')
parser.add_argument('-U', '--user', type=str, help='omero user')
parser.add_argument('-P', '--passwd', type=str, help='omero password')
parser.add_argument('--python', action='store_true',
help='build bl/vl/kb/config.py')
parser.add_argument('--profile', action='store_true',
help='build a .sh file in the current dir')
return parser
def get_ome_var(ome_config, conf_details):
k = conf_details['ome_config_value']
v = ome_config.getConfigValue(k)
return conf_details['default'] if not v else conf_details['type'](v)
def get_config(ome_session):
config = {}
ome_config = ome_session.getConfigService()
for d in CONFIG_VALUES:
config[d['kb_config_value']] = get_ome_var(ome_config, d)
if config.get('GRAPH_ENGINE_DRIVER') == "neo4j":
if not config.get('GRAPH_ENGINE_URI'):
raise ValueError("graph URI required for neo4j driver")
return config
def dump_config(output_fn, config, fmt="python"):
order = [d['kb_config_value'] for d in CONFIG_VALUES]
with open(output_fn, "w") as f:
f.write('# %s\n' % GENERATED_BY_MSG)
for k in order:
v = config.get(k)
if fmt == "profile":
k = k.upper().replace('.', '_')
entry = 'export %s="%s"' % (k, v or "NONE")
else:
entry = '%s = %r' % (k, v)
f.write("%s\n" % entry)
print "wrote %s" % output_fn
def dump_defaults():
config = dict((d['kb_config_value'], d['default']) for d in CONFIG_VALUES)
dump_config(PYTHON_OUT_FN, config)
def main(argv):
parser = make_parser()
args = parser.parse_args(argv)
if args.host:
if not args.user or not args.passwd:
raise ArgumentConstraintError(
"no username and password specified for %s" % args.host
)
else:
dump_defaults()
return 0
#--
c = omero.client(args.host)
s = c.createSession(args.user, args.passwd)
try:
config = get_config(s)
except ValueError as e:
raise BuildConfigurationError(str(e))
except omero.SecurityViolation:
raise BuildConfigurationError(
'%s is not an OMERO.biobank system user' % args.user
)
finally:
c.closeSession()
#--
if args.profile:
out_fmt = "profile"
out_fn = "%s.biobank.profile" % args.host
else:
out_fmt = "python"
out_fn = PYTHON_OUT_FN
dump_config(out_fn, config, fmt=out_fmt)
return 0
if __name__ == '__main__':
try:
retval = main(sys.argv[1:])
except (ArgumentConstraintError, BuildConfigurationError) as e:
retval = "ERROR: %s" % e
sys.exit(retval)