ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/DBinterface.py
Revision: 1.10
Committed: Tue Mar 25 16:29:12 2008 UTC (17 years, 1 month ago) by spiga
Content type: text/x-python
Branch: MAIN
Changes since 1.9: +13 -7 lines
Log Message:
still another fix on the job index plus bosslite integration optimization.

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 if jobsList == 'all':
50 task = common.bossSession.load(1)[0]
51 else:
52 if len(jobsList)>1: str_jobs=string.join(map(str,jobsList),",")
53 else: str_jobs=str(jobsList)
54 task = common.bossSession.load(1,jobsList)[0]
55 return task
56
57 def getJob(self, n):
58 """
59 Return a task with a single job
60 """
61 task = common.bossSession.load(1,str(n))[0]
62 return task
63
64
65 def createTask_(self, optsToSave):
66 """
67 Task declaration
68 with the first coniguration stuff
69 """
70 opt={}
71 opt['serverName']=optsToSave['server_name']
72 opt[ 'name']=common.work_space.taskName()
73 task = Task( opt )
74
75 common.bossSession.saveTask( task )
76 return
77
78 def updateTask_(self,optsToSave):
79 """
80 Update task fields
81 """
82 task = common.bossSession.load(1)[0]
83
84 for key in optsToSave.keys():
85 task[key] = optsToSave[key]
86 common.bossSession.updateDB( task )
87 return
88
89 def createJobs_(self, jobsL):
90 """
91 Fill crab DB with the jobs filed
92 """
93 task = common.bossSession.loadTask(1)
94 jobs = []
95 for id in jobsL:
96 parameters = {}
97 parameters['jobId'] = str(id)
98 parameters['name'] = 'job' + str(id)
99 job = Job(parameters)
100 jobs.append(job)
101 task.addJobs(jobs)
102 common.bossSession.updateDB( task )
103 return
104
105 def updateJob_(self, jobsL, optsToSave):
106 """
107 Update Job fields
108 """
109 if len(jobsL)>1: str_jobs=string.join(map(str,jobsL),",")
110 else: str_jobs=str(jobsL)
111 task = common.bossSession.load(1,jobsL)[0]
112 id =0
113 for job in task.jobs:
114 for key in optsToSave[id].keys():
115 job[key] = optsToSave[id][key]
116 id+=1
117 common.bossSession.updateDB( task )
118 return
119
120 def updateRunJob_(self, jobsL, optsToSave):
121 """
122 Update Running Job fields
123 """
124 if len(jobsL)>1: str_jobs=string.join(map(str,jobsL),",")
125 else: str_jobs=str(jobsL)
126 task = common.bossSession.load(1,jobsL)[0]
127 for job in task.jobs:
128 common.bossSession.getRunningInstance(job)
129 for key in optsToSave.keys():
130 job.runningJob[key] = optsToSave[key]
131 common.bossSession.updateDB( task )
132 return
133
134 def nJobs(self,list=''):
135
136 task = common.bossSession.load(1)[0]
137 listId=[]
138 if list == 'list':
139 for job in task.jobs:listId.append(int(job['jobId']))
140 return listId
141 else:
142 return len(task.jobs)
143
144 def dump(self,jobs):
145 """
146 List a complete set of infos for a job/range of jobs
147 """
148 task = common.bossSession.load(1)[0]
149
150 njobs = len(jobs)
151 lines=[]
152 header=''
153 # ##query the DB asking the right infos for runningJobs TODO DS
154 # for job in jobs:
155 # ## here the query over runngJobs
156 # pass
157
158
159 # ##Define Header to show and Pass the query results,
160 # ## header and format to displayReport() TODO DS
161 # if njobs == 1: plural = ''
162 # else: plural = 's'
163 # header += 'Listing %d job%s:\n' % (njobs, plural)
164 # header += ' :\n' % (---) ## TODO DS
165
166 # displayReport(header, lines):
167 return
168
169 def serializeTask(self, tmp_task = None):
170 if tmp_task is None:
171 #tmp_task = common.bossSession.loadTaskByID(1)
172 tmp_task = common.bossSession.load(1)[0]
173 return common.bossSession.serialize(tmp_task)
174
175 def queryID(self,server_mode=0):
176 '''
177 Return the taskId if serevr_mode =1
178 Return the joblistId if serevr_mode =0
179 '''
180 header=''
181 lines=[]
182 task = common.bossSession.load(1)[0]
183 if server_mode == 1:
184 header= "Task Id = %-40s " %(task['name'])
185 else:
186 for i in range(len(task.job)):
187 common.bossSession.getRunningInstance(task.jobs[i])
188 lines.append(task.jobs[i].runningJob['schedulerId'])
189
190 header+= "Job: %-5s Id = %-40s: \n"
191 displayReport(header,lines)
192 return
193
194 def queryTask(self,attr):
195 '''
196 Perform a query over a generic task attribute
197 '''
198 task = common.bossSession.loadTask(1)
199 return task[attr]
200
201 def queryJob(self, attr, jobs):
202 '''
203 Perform a query for a range/all/single job
204 over a generic job attribute
205 '''
206 lines=[]
207 str_jobs=string.join(map(str,jobs),",")
208 task = common.bossSession.load(1,str_jobs)[0]
209 for job in task.jobs:
210 lines.append(eval(job[attr]))
211 return lines
212
213 def queryRunJob(self, attr, jobs):
214 '''
215 Perform a query for a range/all/single job
216 over a generic job attribute
217 '''
218 lines=[]
219 str_jobs=string.join(map(str,jobs),",")
220 task = common.bossSession.load(1,str_jobs)[0]
221 for job in task.jobs:
222 common.bossSession.getRunningInstance(job)
223 lines.append(job.runningJob[attr])
224 return lines
225
226 def queryDistJob(self, attr):
227 '''
228 Returns the list of distinct value for a given job attributes
229 '''
230 distAttr=[]
231 task = common.bossSession.loadJobDist( 1, attr )
232 for i in task: distAttr.append(eval(i[attr]))
233 return distAttr
234
235 def queryDistJob_Attr(self, attr_1, attr_2, list):
236 '''
237 Returns the list of distinct value for a given job attribute
238 '''
239 distAttr=[]
240 task = common.bossSession.loadJobDistAttr( 1, attr_1, attr_2, list )
241 for i in task: distAttr.append(eval(i[attr_1]))
242 return distAttr
243
244 def queryAttrJob(self, attr, field):
245 '''
246 Returns the list of jobs matching the given attribute
247 '''
248 matched=[]
249 task = common.bossSession.loadJobsByAttr(attr )
250 for i in task:
251 matched.append(i[field])
252 return matched
253
254
255 def queryAttrRunJob(self, attr,field):
256 '''
257 Returns the list of jobs matching the given attribute
258 '''
259 matched=[]
260 task = common.bossSession.loadJobsByRunningAttr(attr)
261 for i in task:
262 matched.append(i[field])
263 return matched