ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/JobDB.py
Revision: 1.7
Committed: Tue Aug 23 10:38:09 2005 UTC (19 years, 8 months ago) by slacapra
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_1_0_0_pre3, CRAB_1_0_0_pre2
Changes since 1.6: +1 -1 lines
Log Message:
too many changes to be listed here...

File Contents

# User Rev Content
1 nsmirnov 1.1 from WorkSpace import WorkSpace
2     from crab_exceptions import *
3     import common
4    
5 nsmirnov 1.2 import os, string
6 nsmirnov 1.1
7     class dbEntry:
8     def __init__(self):
9 slacapra 1.5 self.status = 'X' # job status
10     self.jid = '' # scheduler job id
11     self.firstEvent = 0 # first event for this job
12     self.maxEvents = 0 # last event for this job
13     self.collections = [] # EvCollection to be analyzed in this job
14     self.inputSandbox = [] # InputSandbox
15     self.outputSandbox = [] # OutputSandbox
16 nsmirnov 1.1 return
17    
18 nsmirnov 1.2 def __str__(self):
19     txt = 'Status <' + self.status + '>; '
20     txt += 'Job Id <' + self.jid + '>\n'
21 slacapra 1.4 if self.maxEvents!=0:
22     txt += 'FirstEvent <' + str(self.firstEvent) + '>\n'
23     txt += 'MaxEvents <' + str(self.maxEvents) + '>\n'
24     if len(self.collections)>0:
25 slacapra 1.5 txt += 'Collections <' + str(self.collections) + '>\n'
26 slacapra 1.4
27 nsmirnov 1.1 return txt
28    
29     class JobDB:
30     def __init__(self):
31     self._dir = common.work_space.shareDir() + 'db/'
32     self._db_fname = 'jobs'
33     self._jobs = [] # list of dbEntry's
34     return
35    
36 nsmirnov 1.2 def __str__(self):
37 nsmirnov 1.1 njobs = self.nJobs()
38     if njobs == 1: plural = ''
39     else: plural = 's'
40     txt = 'Total of %d job%s:\n' % (njobs, plural)
41     for i in range(njobs):
42 nsmirnov 1.3 txt += ('Job %03d' % (i+1)) + ': '
43 nsmirnov 1.1 txt += str(self._jobs[i])
44     pass
45     return txt
46    
47 slacapra 1.4 def dump(self, jobs):
48     njobs = len(jobs)
49     if njobs == 1: plural = ''
50     else: plural = 's'
51     print 'Listing %d job%s:\n' % (njobs, plural)
52     for job in jobs:
53     print string.strip(('Job %03d' % (job+1)) + ': ' + str(self._jobs[job]))
54     pass
55    
56 nsmirnov 1.1 def nJobs(self):
57     return len(self._jobs)
58    
59     def create(self, njobs):
60    
61     if os.path.exists(self._dir):
62     msg = 'Cannot create Job DB: already exists.'
63     raise CrabException(msg)
64    
65     os.mkdir(self._dir)
66    
67     for i in range(njobs):
68     self._jobs.append(dbEntry())
69     pass
70    
71 slacapra 1.4 common.logger.debug(5,'Created DB for '+str(njobs)+' jobs')
72    
73 nsmirnov 1.1 self.save()
74     return
75    
76     def save(self):
77     db_file = open(self._dir+self._db_fname, 'w')
78     for i in range(len(self._jobs)):
79     db_file.write(`(i+1)`+';')
80     db_file.write(self._jobs[i].status+';')
81     db_file.write(self._jobs[i].jid+';')
82 slacapra 1.4 db_file.write(str(self._jobs[i].firstEvent)+';')
83     db_file.write(str(self._jobs[i].maxEvents)+';')
84     db_file.write(str(self._jobs[i].collections)+';')
85 slacapra 1.5 db_file.write(str(self._jobs[i].inputSandbox)+';')
86     db_file.write(str(self._jobs[i].outputSandbox)+';')
87 nsmirnov 1.1 db_file.write('\n')
88     pass
89     db_file.close()
90     return
91    
92     def load(self):
93     self._jobs = []
94 slacapra 1.6 try:
95     db_file = open(self._dir+self._db_fname, 'r')
96     except IOError:
97 slacapra 1.7 raise DBException("Something really serious! no JobDB is present!!!")
98 slacapra 1.6
99 nsmirnov 1.1 for line in db_file:
100 nsmirnov 1.2 db_entry = dbEntry()
101 slacapra 1.5 (n, db_entry.status, db_entry.jid, db_entry.firstEvent, db_entry.maxEvents, collectionsTMP, inputSandboxTMP , outputSandboxTMP , rest) = string.split(line, ';')
102     db_entry.collections = self.strToList_(collectionsTMP)
103     db_entry.inputSandbox = self.strToList_(inputSandboxTMP)
104     db_entry.outputSandbox = self.strToList_(outputSandboxTMP)
105 nsmirnov 1.1 self._jobs.append(db_entry)
106     pass
107     db_file.close()
108     return
109    
110 slacapra 1.5 def strToList_(self, list):
111     return string.split(string.replace(list[1:-1],"'",""),',')
112    
113 nsmirnov 1.1 def setStatus(self, nj, status):
114     self._jobs[nj].status = status
115     return
116    
117     def status(self, nj):
118     return self._jobs[nj].status
119    
120     def setJobId(self, nj, jid):
121     self._jobs[nj].jid = jid
122     return
123    
124     def jobId(self, nj):
125     return self._jobs[nj].jid
126 slacapra 1.4
127     def setFirstEvent(self, nj, firstEvent):
128     self._jobs[nj].firstEvent = firstEvent
129     return
130    
131     def firstEvent(self, nj):
132     return self._jobs[nj].firstEvent
133    
134     def setMaxEvents(self, nj, MaxEvents):
135     self._jobs[nj].maxEvents = MaxEvents
136     return
137    
138     def maxEvents(self, nj):
139     return self._jobs[nj].maxEvents
140    
141     def setCollections(self, nj, Collections):
142     self._jobs[nj].Collections = Collections
143     return
144    
145     def collections(self, nj):
146     return self._jobs[nj].collections
147 slacapra 1.5
148     def setInputSandbox(self, nj, InputSandbox):
149     self._jobs[nj].inputSandbox = InputSandbox
150     return
151    
152     def inputSandbox(self, nj):
153     return self._jobs[nj].inputSandbox
154    
155     def setOutputSandbox(self, nj, OutputSandbox):
156     self._jobs[nj].outputSandbox = OutputSandbox
157     return
158    
159     def outputSandbox(self, nj):
160     return self._jobs[nj].outputSandbox