CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
mps_update.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import subprocess
3 import re
4 import os
5 import Alignment.MillePedeAlignmentAlgorithm.mpslib.Mpslibclass as mpslib
6 
7 lib = mpslib.jobdatabase()
8 lib.read_db()
9 
10 #IDEAS
11 #change Flag-list to binary True/False -> rename CARE?
12 #rework string-printing with references
13 
14 #create a FLAG-list of which entries are to worry about
15 submittedjobs = 0
16 FLAG = [] #FLAG[i] = 1 -> don't care
17  #FLAG[i] = -1 -> care
18 #asking 'in' to provide for composits, e.g. DISABLEDFETCH
19 for i in xrange(len(lib.JOBID)):
20  if 'SETUP' in lib.JOBSTATUS[i] or \
21  'DONE' in lib.JOBSTATUS[i] or \
22  'FETCH' in lib.JOBSTATUS[i] or \
23  'OK' in lib.JOBSTATUS[i] or \
24  'ABEND' in lib.JOBSTATUS[i] or \
25  'FAIL' in lib.JOBSTATUS[i]:
26  FLAG.append(1)
27  else:
28  FLAG.append(-1)
29  submittedjobs += 1
30 print "submitted jobs: ", submittedjobs
31 
32 
33 
34 #deal with submitted jobs by looking into output of shell('bjobs -l')
35 if submittedjobs > 0:
36  #execute shell command 'bjobs -l' and store output. Include error Messages.
37 # with open ("bjobs_test.txt", "r") as testfile:
38 # bjobs = testfile.read().replace('\n', '')
39  bjobs = subprocess.check_output('bjobs -l', stderr=subprocess.STDOUT, shell=True)
40  bjobs = bjobs.replace('\n','')
41 
42  if bjobs != 'No unfinished job found':
43  bjobs = bjobs.replace(' ','')
44  results = bjobs.split('-----------------------')
45  #print('\n\n'.join(results))
46  #print results
47  for line in results:
48  line.strip() #might be unnecessary
49  print line
50  #extract jobID
51  match = re.search('Job<(\d+?)>,', line)
52  if match:
53  jobid = int(match.group(1)) # FIXME match.group(0)???????????????????
54  #extract job status
55  match = re.search('Status<([A-Z]+?)>', line)
56  if match:
57  status = match.group(1)
58  #extract CPU time
59  match = re.search('TheCPUtimeusedis(\d+?)seconds', line)
60  cputime = 0
61  if match:
62  cputime = int(match.group(1))
63  print 'out ', jobid, ' ', status, ' ', cputime #this might fail
64 
65  #check for disabled Jobs
66  theIndex = -1
67  disabled = ''
68  for k in xrange(len(lib.JOBID)):
69  if jobid == lib.JOBID[k]:
70  theIndex = k
71  if 'DISABLED' in lib.JOBSTATUS[theIndex]:
72  disabled = 'DISABLED'
73 
74  #continue with next batch job if not found or not interesting
75  if theIndex == -1:
76  print 'mps_update.py - the job ', jobid,' was not found in the JOBID array'
77  continue
78  if FLAG[theIndex] == 1:
79  continue
80 
81  #if deemed interesting (FLAG = -1) update Joblists for mps.db
82  lib.JOBSTATUS[theIndex] = disabled+status
83  if status == 'RUN' or status == 'DONE':
84  if cputime > 0:
85  diff = cputime - lib.JOBRUNTIME[theIndex]
86  lib.JOBRUNTIME[theIndex] = cputime
87  lib.JOBHOST[theIndex] = '+'+str(diff)
88  lib.JOBINCR[theIndex] = diff
89  else:
90  lib.JOBRUNTIME[theIndex] = 0
91  lib.JOBINCR[theIndex] = 0
92  FLAG[theIndex] = 1;
93  print 'set flag of job', theIndex, 'with id', lib.JOBID[theIndex], 'to 1'
94 
95 
96 
97 #loop over remaining jobs to see whether they are done
98 for i in xrange(len(lib.JOBID)):
99 
100  #check if current job is disabled. Print stuff. Continue if flagged unimportant.
101  disabled = ''
102  if 'DISABLED' in lib.JOBSTATUS[i]:
103  disabled = 'DISABLED'
104  print ' DB job ', lib.JOBID[i], 'flag ', FLAG[i]
105  if FLAG[i] == 1:
106  continue
107 
108  #check if job may be done by looking if a folder exists in the project directory.
109  #if True -> jobstatus is set to DONE
110  theBatchDirectory = 'LSFJOB_'+str(lib.JOBID[i])
111  if os.path.isdir(theBatchDirectory):
112  print 'Directory ', theBatchDirectory, 'exists'
113  lib.JOBSTATUS[i] = disabled + 'DONE'
114  else:
115  if 'RUN' in lib.JOBSTATUS[i]:
116  print 'WARNING: Job ',i,' in state RUN, neither found by bjobs nor find LSFJOB directory!'
117 
118 #from Perl-script (dunno): FIXME: check if job not anymore in batch system
119 #from Perl-script (dunno): might set to FAIL -but probably theBatchDirectory is just somewhere else...
120 
121 
122 
123 #check for orphaned jobs
124 for i in xrange(len(lib.JOBID)):
125  if FLAG[i] != 1:
126  if 'SETUP' in lib.JOBSTATUS[i] or \
127  'DONE' in lib.JOBSTATUS[i] or \
128  'FETCH' in lib.JOBSTATUS[i] or \
129  'TIMEL' in lib.JOBSTATUS[i] or \
130  'SUBTD' in lib.JOBSTATUS[i]:
131  print 'Funny entry index ',i,' job ',lib.JOBID[i],' status ',lib.JOBSTATUS[i]
132 
133 
134 lib.write_db()