ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/DBinterface.py
Revision: 1.12
Committed: Mon Mar 31 09:23:55 2008 UTC (17 years ago) by spiga
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_2_0_pre4, CRAB_2_2_0_pre2
Changes since 1.11: +9 -19 lines
Log Message:
improvement on updateRunJobs method

File Contents

# Content
1 from crab_logger import Logger
2 from crab_exceptions import *
3 from crab_util import *
4 import common
5 import os, time, shutil
6
7 from ProdCommon.BossLite.API.BossLiteAPI import BossLiteAPI
8
9
10 from ProdCommon.BossLite.DbObjects.Job import Job
11 from ProdCommon.BossLite.DbObjects.Task import Task
12 from ProdCommon.BossLite.DbObjects.RunningJob import RunningJob
13
14
15 class DBinterface:
16 def __init__(self, cfg_params):
17
18 self.cfg_params = cfg_params
19
20 self.db_type = cfg_params.get("USER.use_db",'SQLite')
21 return
22
23
24 def configureDB(self):
25
26 dbname = common.work_space.shareDir()+'crabDB'
27 dbConfig = {'dbName':dbname
28 }
29
30 common.bossSession = BossLiteAPI( self.db_type, dbConfig)
31 common.bossSession.installDB('$CRABPRODCOMMONPYTHON/ProdCommon/BossLite/DbObjects/setupDatabase-sqlite.sql')
32
33 return
34
35 def loadDB(self):
36
37 dbname = common.work_space.shareDir()+'crabDB'
38 dbConfig = {'dbName':dbname
39 }
40 common.bossSession = BossLiteAPI( self.db_type, dbConfig)
41 self.task = common.bossSession.load(1)[0]
42 return
43
44 def getTask(self, jobsList='all'): #, cfg_params):
45 """
46 Return task with all/list of jobs
47 """
48
49 task = common.bossSession.load(1,jobsList)[0]
50 return task
51
52 def getJob(self, n):
53 """
54 Return a task with a single job
55 """
56 task = common.bossSession.load(1,str(n))[0]
57 return task
58
59
60 def createTask_(self, optsToSave):
61 """
62 Task declaration
63 with the first coniguration stuff
64 """
65 opt={}
66 opt['serverName']=optsToSave['server_name']
67 opt[ 'name']=common.work_space.taskName()
68 task = Task( opt )
69
70 common.bossSession.saveTask( task )
71 return
72
73 def updateTask_(self,optsToSave):
74 """
75 Update task fields
76 """
77 task = common.bossSession.load(1)[0]
78
79 for key in optsToSave.keys():
80 task[key] = optsToSave[key]
81 common.bossSession.updateDB( task )
82 return
83
84 def createJobs_(self, jobsL):
85 """
86 Fill crab DB with the jobs filed
87 """
88 task = common.bossSession.loadTask(1)
89 jobs = []
90 for id in jobsL:
91 parameters = {}
92 parameters['jobId'] = str(id)
93 parameters['name'] = 'job' + str(id)
94 job = Job(parameters)
95 jobs.append(job)
96 task.addJobs(jobs)
97 common.bossSession.updateDB( task )
98 return
99
100 def updateJob_(self, jobsL, optsToSave):
101 """
102 Update Job fields
103 """
104 task = common.bossSession.load(1,jobsL)[0]
105 id =0
106 for job in task.jobs:
107 for key in optsToSave[id].keys():
108 job[key] = optsToSave[id][key]
109 id+=1
110 common.bossSession.updateDB( task )
111 return
112
113 def updateRunJob_(self, jobsL, optsToSave):
114 """
115 Update Running Job fields
116 """
117 task = common.bossSession.load(1,jobsL)[0]
118 id=0
119 for job in task.jobs:
120 common.bossSession.getRunningInstance(job)
121 for key in optsToSave[id].keys():
122 job.runningJob[key] = optsToSave[id][key]
123 id+=1
124 common.bossSession.updateDB( task )
125 return
126
127 def nJobs(self,list=''):
128
129 task = common.bossSession.load(1)[0]
130 listId=[]
131 if list == 'list':
132 for job in task.jobs:listId.append(int(job['jobId']))
133 return listId
134 else:
135 return len(task.jobs)
136
137 def dump(self,jobs):
138 """
139 List a complete set of infos for a job/range of jobs
140 """
141 task = common.bossSession.load(1)[0]
142
143 njobs = len(jobs)
144 lines=[]
145 header=''
146 # ##query the DB asking the right infos for runningJobs TODO DS
147 # for job in jobs:
148 # ## here the query over runngJobs
149 # pass
150
151
152 # ##Define Header to show and Pass the query results,
153 # ## header and format to displayReport() TODO DS
154 # if njobs == 1: plural = ''
155 # else: plural = 's'
156 # header += 'Listing %d job%s:\n' % (njobs, plural)
157 # header += ' :\n' % (---) ## TODO DS
158
159 # displayReport(header, lines):
160 return
161
162 def serializeTask(self, tmp_task = None):
163 if tmp_task is None:
164 tmp_task = common.bossSession.load(1)[0]
165 return common.bossSession.serialize(tmp_task)
166
167 def queryID(self,server_mode=0):
168 '''
169 Return the taskId if serevr_mode =1
170 Return the joblistId if serevr_mode =0
171 '''
172 header=''
173 lines=[]
174 task = common.bossSession.load(1)[0]
175 if server_mode == 1:
176 header= "Task Id = %-40s " %(task['name'])
177 else:
178 for job in task.jobs:
179 toPrint=''
180 common.bossSession.getRunningInstance(job)
181 toPrint = "%-5s %-50s " % (job['id'],job.runningJob['schedulerId'])
182 lines.append(toPrint)
183 header+= "%-5s %-50s " % ('Job:','ID' )
184 displayReport(self,header,lines)
185 return
186
187 def queryTask(self,attr):
188 '''
189 Perform a query over a generic task attribute
190 '''
191 task = common.bossSession.loadTask(1)
192 return task[attr]
193
194 def queryJob(self, attr, jobsL):
195 '''
196 Perform a query for a range/all/single job
197 over a generic job attribute
198 '''
199 lines=[]
200 task = common.bossSession.load(1,jobsL)[0]
201 for job in task.jobs:
202 lines.append(eval(job[attr]))
203 return lines
204
205 def queryRunJob(self, attr, jobsL):
206 '''
207 Perform a query for a range/all/single job
208 over a generic job attribute
209 '''
210 lines=[]
211 task = common.bossSession.load(1,jobsL)[0]
212 for job in task.jobs:
213 common.bossSession.getRunningInstance(job)
214 lines.append(job.runningJob[attr])
215 return lines
216
217 def queryDistJob(self, attr):
218 '''
219 Returns the list of distinct value for a given job attributes
220 '''
221 distAttr=[]
222 task = common.bossSession.loadJobDist( 1, attr )
223 for i in task: distAttr.append(eval(i[attr]))
224 return distAttr
225
226 def queryDistJob_Attr(self, attr_1, attr_2, list):
227 '''
228 Returns the list of distinct value for a given job attribute
229 '''
230 distAttr=[]
231 task = common.bossSession.loadJobDistAttr( 1, attr_1, attr_2, list )
232 for i in task: distAttr.append(eval(i[attr_1]))
233 return distAttr
234
235 def queryAttrJob(self, attr, field):
236 '''
237 Returns the list of jobs matching the given attribute
238 '''
239 matched=[]
240 task = common.bossSession.loadJobsByAttr(attr )
241 for i in task:
242 matched.append(i[field])
243 return matched
244
245
246 def queryAttrRunJob(self, attr,field):
247 '''
248 Returns the list of jobs matching the given attribute
249 '''
250 matched=[]
251 task = common.bossSession.loadJobsByRunningAttr(attr)
252 for i in task:
253 matched.append(i[field])
254 return matched