1
2 """
3 Startup plugin for command-line deletes
4
5 Copyright 2009 Glencoe Software, Inc. All rights reserved.
6 Use is subject to license terms supplied in LICENSE.txt
7
8 """
9
10 import os
11 import sys
12 import array
13 import exceptions
14
15 from omero.cli import BaseControl, CLI
16
17 HELP = """Delete OMERO data.
18
19 Where available (currently: Image & Plate) special methods
20 are used for deleting the objects. Otherwise, IUpdate.deleteObject()
21 is used.
22
23
24 Examples:
25
26 bin/omero delete Image:50
27 bin/omero delete Plate:1
28
29 # New-style delete
30 bin/omero delete /Image:1 /Screen:5
31
32 """
33
35
43
45
46 import omero
47 import omero.callbacks
48
49 client = self.ctx.conn(args)
50 delete = client.sf.getDeleteService()
51 specs = delete.availableCommands()
52 keys = sorted([spec.type for spec in specs])
53
54 if args.list_details:
55 map = dict()
56 for spec in specs:
57 map[spec.type] = spec
58 for key in keys:
59 spec = map[key]
60 self.ctx.out("=== %s ===" % key)
61 for k, v in spec.options.items():
62 self.ctx.out("%s" % (k,))
63 elif args.list:
64 self.ctx.out("\n".join(keys))
65 return
66
67 images = []
68 plates = []
69 objects = []
70 commands = []
71 for arg in args.obj:
72 if 0 > arg.find(":"):
73 self.ctx.die(5, "Format: 'Image:<id>'")
74 klass, id = arg.split(":")
75 if klass == "Image":
76 images.append(long(id))
77 elif klass == "Plate":
78 plates.append(long(id))
79 elif klass in keys:
80 commands.append(omero.api.delete.DeleteCommand(klass, long(id), None))
81 else:
82 try:
83 ctor = getattr(omero.model, "%sI" % klass)
84 except AttributeError:
85 self.ctx.die(6, "Unknown delete command: %s" % klass)
86 if not ctor:
87 ctor = getattr(omero.model, klass)
88 try:
89 objects.append(ctor(long(id), False))
90 except exceptions.Exception, e:
91 self.ctx.dbg("Exception on ctor: %s" % e)
92 self.ctx.die(5, "Can't delete type: %s" % klass)
93
94 def action(klass, method, *args):
95 import omero
96 self.status(klass, args)
97 try:
98 method(*args)
99 self.ctx.out("ok.")
100 except omero.ApiUsageException, aue:
101 self.ctx.out(aue.message)
102 except exceptions.Exception, e:
103 self.ctx.out("failed (%s)" % e)
104
105 deleteSrv = client.getSession().getDeleteService()
106 updateSrv = client.getSession().getUpdateService()
107 for image in images: action("Image", deleteSrv.deleteImage, image, True)
108 for plate in plates: action("Plate", deleteSrv.deletePlate, plate)
109 for object in objects: action(object.__class__.__name__, updateSrv.deleteObject, object)
110
111 handle = deleteSrv.queueDelete(commands)
112 if args.wait == 0:
113 self.ctx.out("Not waiting for delete")
114 return
115 elif args.wait > 0:
116 self.ctx.die(321, "Unsupported wait: %s" % args.wait)
117
118 callback = omero.callbacks.DeleteCallbackI(client, handle)
119 try:
120 try:
121
122 rv = None
123 while True:
124 rv = callback.block(500)
125 if rv is not None:
126 break
127
128 reports = handle.report()
129 if args.report:
130 self.detailed_report(rv, reports)
131 else:
132 self.simple_report(rv, reports)
133
134 if rv:
135 self.ctx.die(rv, "Failed")
136
137
138 except KeyboardInterrupt:
139 self.ctx.out("Attempting cancel...")
140 if handle.cancel():
141 self.ctx.out("Cancelled")
142 else:
143 self.ctx.out("Failed to cancel")
144 finally:
145 callback.close()
146
147 - def status(self, klass, args):
148 self.ctx.out(("Deleting %s %s... " % (klass, args)), newline = False)
149
151 for i, report in enumerate(reports):
152 command = report.command
153 self.status(command.type, command.id)
154 if rv:
155 msg = "error"
156 else:
157 msg = "ok"
158 self.ctx.out(msg)
159
160 msg = ""
161 if report.error:
162 which = "error"
163 msg = report.error
164 elif report.warning:
165 which = "warning"
166 msg = report.warning
167
168 for line in msg.split("\n"):
169 line = line.strip()
170 if line:
171 self.ctx.out("\t%s: %s" % (which, line))
172
174 for i, report in enumerate(reports):
175 command = report.command
176 self.status(command.type, command.id)
177 if rv:
178 msg = "error"
179 else:
180 msg = "ok"
181 self.ctx.out(msg)
182
183 for k, v in report.undeletedFiles.items():
184 if v:
185 self.ctx.out("Undeleted %s objects" % k)
186 for i in v:
187 self.ctx.out("%s:%s" % (k, i))
188
189
190 self.ctx.out("Steps: %s" % report.steps)
191 self.ctx.out("Scheduled deletes: %s" % report.scheduledDeletes)
192 self.ctx.out("Actual deletes: %s" % report.actualDeletes)
193 if report.stop > 0 and report.start > 0:
194 elapse = report.stop - report.start
195 self.ctx.out("Elapsed time: %s secs." % (elapse/1000.0))
196 else:
197 self.ctx.out("Unfinished.")
198
199 if report.warning:
200 self.ctx.out("Warning message: %s" % report.warning)
201
202 if report.error:
203 self.ctx.out("Error message: %s" % report.error)
204
205 self.ctx.out(" ")
206
207 try:
208 register("delete", DeleteControl, HELP)
209 except NameError:
210 if __name__ == "__main__":
211 cli = CLI()
212 cli.register("delete", DeleteControl, HELP)
213 cli.invoke(sys.argv[1:])
214