/
runHelloWorld.py
178 lines (143 loc) · 5.77 KB
/
runHelloWorld.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
examples/ScriptingService/runHelloWorld.py
-----------------------------------------------------------------------------
Copyright (C) 2006-2014 University of Dundee. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
------------------------------------------------------------------------------
This script demonstrates how to use the scripting service to call a script on
an OMERO server.
@author Donald MacDonald
<a href="mailto:donald@lifesci.dundee.ac.uk">donald@lifesci.dundee.ac.uk</a>
@author Will Moore
<a href="mailto:will@lifesci.dundee.ac.uk">will@lifesci.dundee.ac.uk</a>
@version 3.0
<small>
(<b>Internal version:</b> $Revision: $Date: $)
</small>
@since 3.0-Beta4.2
"""
import omero
import omero.scripts
import getopt
import sys
import os
import omero.util.script_utils as scriptUtil
def uploadScript(scriptService, scriptPath):
file = open(scriptPath)
script = file.read()
file.close()
# print script
# prints the script ID to the command line. This can be used to run the
# script. E.g. see runHelloWorld.py
print "Uploading script:", scriptPath
scriptId = scriptService.uploadScript(scriptPath, script)
print "Script uploaded with ID:", scriptId
def runScript(session, scriptService, scriptPath):
# Identify the script we want to run: Get all 'my' scripts and filter by
# path.
# An empty list implies that the server should return what it would by
# default trust.
acceptsList = []
# returns list of OriginalFiles
scripts = scriptService.getUserScripts(acceptsList)
for s in scripts:
print s.id.val, s.path.val + s.name.val
namedScripts = [s.id.val for s in scripts
if s.path.val + s.name.val == scriptPath]
if len(namedScripts) == 0:
print "Didn't find any scripts with specified path"
# use the most recent script (highest ID)
scriptId = max(namedScripts)
print "Running script: %s with ID: %s" % (scriptPath, scriptId)
# Don't attempt to run script without starting user processor!
# return
# make a map of all the parameters we want to pass to the script
# keys are strings. Values must be omero.rtypes such as rlong, rbool,
# rlist.
map = {
"Input_Message": omero.rtypes.rstring(
"Sending this message to the server!"),
}
# The last parameter is how long to wait as an RInt
proc = scriptService.runScript(scriptId, map, None)
try:
cb = omero.scripts.ProcessCallbackI(client, proc)
while not cb.block(1000): # ms.
pass
cb.close()
results = proc.getResults(0) # ms
finally:
proc.close(False)
# handle any results from the script
# print results.keys()
if 'Message' in results:
print results['Message'].getValue()
rawFileService = session.createRawFileStore()
queryService = session.getQueryService()
if 'stdout' in results:
origFile = results['stdout'].getValue()
fileId = origFile.getId().getValue()
print ("\n******** Script generated StdOut in file:%s *******"
% fileId)
print scriptUtil.readFromOriginalFile(
rawFileService, queryService, fileId)
if 'stderr' in results:
origFile = results['stderr'].getValue()
fileId = origFile.getId().getValue()
print ("\n******** Script generated StdErr in file:%s *******"
% fileId)
print scriptUtil.readFromOriginalFile(
rawFileService, queryService, fileId)
rawFileService.close()
def readCommandArgs():
"""
Read the arguments from the command line and put them in a map
@return A map of the command args, with keys: "host", "username",
"password", "scriptId"
"""
def usage():
print ("Usage: runHelloWorld.py --server server --username username"
" --password password --file file")
try:
opts, args = getopt.getopt(
sys.argv[1:], "s:u:p:f:",
["server=", "username=", "password=", "file="])
except getopt.GetoptError:
usage()
sys.exit(2)
returnMap = {}
for opt, arg in opts:
if opt in ("-s", "--server"):
returnMap["host"] = arg
elif opt in ("-u", "--username"):
returnMap["username"] = arg
elif opt in ("-p", "--password"):
returnMap["password"] = arg
elif opt in ("-f", "--file"):
returnMap["script"] = arg
return returnMap
if __name__ == "__main__":
commandArgs = readCommandArgs()
# log on to the server, create client and session and scripting service
client = omero.client(commandArgs["host"])
session = client.createSession(
commandArgs["username"], commandArgs["password"])
scriptPath = commandArgs["script"]
scriptService = session.getScriptService()
scriptPath = os.path.abspath(scriptPath)
# upload script. Could comment this out if you just want to run.
uploadScript(scriptService, scriptPath)
# run script
runScript(session, scriptService, scriptPath)