1
2
3
4
5
6
7
8 """
9 Implements the arcgui user interface threads.
10 """
11
12 import os, sys, threading, types, wx, shutil
13
14 from datetime import datetime
15
16 try:
17 import arc
18 except:
19 print "ARC1 Python binding not found. Please check search paths."
20 sys.exit(-1)
21
22
23
24 EVT_PROGRESS_UPDATE_TYPE = wx.NewEventType()
25 EVT_PROGRESS_UPDATE = wx.PyEventBinder(EVT_PROGRESS_UPDATE_TYPE, 1)
26 EVT_LOG_UPDATE_TYPE = wx.NewEventType()
27 EVT_LOG_UPDATE = wx.PyEventBinder(EVT_LOG_UPDATE_TYPE, 1)
28 EVT_UPDATE_STATUS_DONE_TYPE = wx.NewEventType()
29 EVT_UPDATE_STATUS_DONE = wx.PyEventBinder(EVT_UPDATE_STATUS_DONE_TYPE, 1)
30 EVT_RETRIEVE_JOBS_DONE_TYPE = wx.NewEventType()
31 EVT_RETRIEVE_JOBS_DONE = wx.PyEventBinder(EVT_RETRIEVE_JOBS_DONE_TYPE, 1)
32 EVT_CLEAN_JOBS_DONE_TYPE = wx.NewEventType()
33 EVT_CLEAN_JOBS_DONE = wx.PyEventBinder(EVT_CLEAN_JOBS_DONE_TYPE, 1)
34 EVT_KILL_JOBS_DONE_TYPE = wx.NewEventType()
35 EVT_KILL_JOBS_DONE = wx.PyEventBinder(EVT_KILL_JOBS_DONE_TYPE, 1)
36 EVT_SUBMIT_JOB_DONE_TYPE = wx.NewEventType()
37 EVT_SUBMIT_JOB_DONE = wx.PyEventBinder(EVT_SUBMIT_JOB_DONE_TYPE, 1)
38
40 """
41 Event sent by worker threads to update progress.
42 """
43 - def __init__(self, etype, eid, info="update"):
44 wx.PyCommandEvent.__init__(self, etype, eid)
45 self.__info = info
46
49
50 info = property(getInfo)
51
53 """
54 Event sent by worker threads to update log.
55 """
56 - def __init__(self, etype, eid, output="update"):
57 wx.PyCommandEvent.__init__(self, etype, eid)
58 self.__output = output
59
62
63 output = property(getOutput)
64
66 """
67 Event sent when the worker thread UpdateStatusThread has finished
68 processing.
69 """
71 wx.PyCommandEvent.__init__(self, etype, eid)
72
74 """
75 Event sent when the worker thread RetrieveJobsThread has finished
76 processing.
77 """
79 wx.PyCommandEvent.__init__(self, etype, eid)
80
82 """
83 Event sent when the worker thread CleanJobsThread has finished
84 processing.
85 """
87 wx.PyCommandEvent.__init__(self, etype, eid)
88
90 """
91 Event sent when the worker thread KillJobsThread has finished
92 processing.
93 """
95 wx.PyCommandEvent.__init__(self, etype, eid)
96
98 """
99 Event sent when the worker thread SubmitJobThread has finished
100 processing.
101 """
102 - def __init__(self, etype, eid, resultList=[]):
105
107 return self.__resultList
108
109 resultList = property(getResultList)
110
113 """
114 Base class for all ArcGui threads.
115 """
116 threading.Thread.__init__(self)
117 self.__stopEvent = threading.Event()
118 self.__parent = parent
119 self.__logger = None
120 self.__arcClient = arcClient
121 self.__arcClient.updateProgress = self.doProgress
122 self.__debugLevel = self.__arcClient.debugLevel
123
125 """
126 Set the stop event. To signal that the thread should stop.
127 """
128 if not self.__stopEvent.isSet():
129 self.__stopEvent.set()
130
132 """
133 Return thread stopped status.
134 """
135 return self.__stopEvent.isSet()
136
137 - def logMsg(self, level, msg):
138 """
139 Log message to logger.
140 """
141 if self.__logger!=None:
142 self.__logger.msg(level, msg)
143
145 """
146 Log informational message to logger.
147 """
148 if self.__logger!=None:
149 self.logMsg(arc.INFO, msg)
150
152 """
153 Log debug message to logger.
154 """
155 if self.__logger!=None:
156 self.logMsg(arc.DEBUG, msg)
157
159 """
160 Log error message to logger.
161 """
162 if self.__logger!=None:
163 self.logMsg(arc.ERROR, msg)
164
165
166
167
168
170 """
171 Send message (EVT_PROGRESS_UPDATE_TYPE) to
172 parent window for updating progress messages.
173 """
174 evt = UpdateProgressEvent(EVT_PROGRESS_UPDATE_TYPE, -1, message)
175 wx.PostEvent(self.__parent, evt)
176
178 """
179 Return parent instance.
180 """
181 return self.__parent
182
184 """
185 Return ArcClient instance.
186 """
187 return self.__arcClient
188
190 """
191 Set debug level (deprecated).
192 """
193 self.__debugLevel = debugLevel
194
196 """
197 Return debug level (deprecated).
198 """
199 return self.__debugLevel
200
202 """
203 Set logger to be used by thread.
204 """
205 self.__logger = logger
206
208 """
209 Return assigned logger.
210 """
211 return self.__logger
212
213 parent = property(getParent)
214 arcClient = property(getArcClient)
215 debugLevel = property(getDebugLevel, setDebugLevel)
216 logger = property(getLogger, setLogger)
217
219 """
220 Worker thread for updating job status.
221 """
231
233 """
234 Worker thread for retrieving (downloading) jobs.
235 """
236 - def __init__(self, parent, arcClient, jobIds, downloadDir, downloadHistoryFile):
237 """
238 Class constructor
239 """
240 ArcGuiThread.__init__(self, parent, arcClient)
241 self.__jobIds = jobIds
242 self.__downloadDir = downloadDir
243 self.__removeJobDir = True
244 self.__downloadHistoryFile = downloadHistoryFile
245
247 """
248 Main thread method for retrieving jobs from the grid.
249 """
250 self.logInfoMsg("RetrieveJobsThread starting.")
251 if type(self.__downloadDir) is types.StringType:
252 self.logInfoMsg("Download to single dir.")
253 self.arcClient.downloadDir = self.__downloadDir
254 self.arcClient.get(self.__jobIds)
255 elif type(self.__downloadDir) is types.ListType:
256 self.logInfoMsg("Download to separate dirs.")
257 for jobId, downloadDir in zip(self.__jobIds, self.__downloadDir):
258 if self.isStopped():
259 break
260 self.logInfoMsg("downloading %s to %s." % (jobId, downloadDir))
261 self.arcClient.downloadDir = downloadDir
262 self.arcClient.get([jobId])
263
264 if self.__removeJobDir:
265 sessionId = jobId.split("/")[-1]
266 jobDir = os.path.join(self.arcClient.downloadDir, sessionId)
267 if os.path.isdir(jobDir):
268 print "Moving from ",jobDir,"to",self.arcClient.downloadDir
269 os.system("mv %s/* %s" % (jobDir, self.arcClient.downloadDir))
270 shutil.rmtree(jobDir)
271 if self.__downloadHistoryFile!="":
272 historyFile = open(self.__downloadHistoryFile, "a")
273 historyFile.write("%s;%s;%s\n" % (jobId, str(datetime.now()), self.arcClient.downloadDir))
274 historyFile.close()
275
276
277
278 evt = RetrieveJobsDoneEvent(EVT_RETRIEVE_JOBS_DONE_TYPE, -1)
279 wx.PostEvent(self.parent, evt)
280 self.logInfoMsg("RetrieveJobsThread stopping.")
281
283 """
284 Worker thread for cleaning jobs.
285 """
286 - def __init__(self, parent, arcClient, jobIds, force=False):
293
303
305 """
306 Worker thread for killing jobs.
307 """
308 - def __init__(self, parent, arcClient, jobIds, force=False):
315
325
327 """
328 Worker thread for submitting a job.
329 """
330 - def __init__(self, parent, arcClient, jobList):
336
348