-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBonnLogger.py
executable file
·593 lines (388 loc) · 13.5 KB
/
BonnLogger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#!/usr/bin/env python
##########################
# Interfaces to Databse for Logging Purposes
##########################
import sys, os, datetime
from sqlobject import *
from sqlobject.inheritance import InheritableSQLObject
__cvs_id__ = "$Id: BonnLogger.py,v 1.9 2010-08-10 21:29:42 anja Exp $"
#####################################
usage = '''
BonnLogger.py
Interface to the Bonn Pipeline Logging System
Commands:
log cmd [args] Record that cmd was called with args.
Will fail if previous logged command failed.
Prints to stdout the id number for the entry.
forceLog cmd [args] Record that cmd was called with args.
Forces the logging of cmd.
update id status comments Records exit status of cmd in entry id.
Comments are appended to the record.
clear Resets the fail status of a target
comment Insert a comment into the log
config KEY=VAR [...] Records key value pairs
state Prints to stdout the state of the logging:
Production/Testing : 1/0
last Prints record of last logged cmd
overview Prints summary of checkpoints and subsequent coms
checkpoint Registers a checkpoint for a target
help Prints this message
Env Vars:
BONN_TARGET (Req) Name of current working target
BONN_FILTER (Req) Name of current working filter
BONN_TEST If defined, or set to non-zero,
puts logging into test mode
BONN_LOG If defined to 0, then logging is turned off
'''
#####################################
class State(object):
def __init__(self, name, connection):
self.name = name
self.connection = connection
def __str__(self):
return self.name
def connect(self):
sqlhub.processConnection = connectionForURI(self.connection)
class NoneState(object):
def __str__(self):
return "nostate"
def connect(self):
pass
nostate = NoneState()
production = State('production', 'mysql://weaklensing:darkmatter@ki-sr01/subaru')
testing = State('testing', 'mysql://weaklensing:darkmatter@ki-sr01/test')
#testing = State('testing', 'mysql://weaklensing:darkmatter@ki-sr01/test?debug=t')
def useTestMode():
return ('BONN_TEST' in os.environ) and (os.environ['BONN_TEST'] != "0")
LogState = nostate
if useTestMode():
LogState = testing
else:
LogState = production
LogState.connect()
#########################
class Command(SQLObject):
class sqlmeta:
cacheValues = False
entry = ForeignKey('Entry')
command = StringCol()
args = StringCol(default = '')
status = IntCol(default = None)
def __str__(self):
report = 'Command: %s' % self.command
if self.args != '':
report += " %s" % ' '.join(self.args)
if self.status is not None:
report += ' Status: %d' % self.status
return report
def _set_args(self, args):
val = ' '.join(args)
self._SO_set_args(val)
def _get_args(self):
val = self._SO_get_args()
return val.split()
class Comment(SQLObject):
class sqlmeta:
cacheValues = False
entry = ForeignKey('Entry')
comment = StringCol()
def __str__(self):
return "Comment: %s" % self.comment
class Variable(SQLObject):
class sqlmeta:
cacheValues = False
entry = ForeignKey('Entry')
var = StringCol()
val = StringCol()
def __str__(self):
return "Var: %s = %s" % (self.var, self.val)
class Checkpoint(SQLObject):
class sqlmeta:
cacheValues = False
entry = ForeignKey('Entry')
checkpoint = StringCol()
def __str__(self):
return "Checkpoint: %s" % self.checkpoint
class Entry(SQLObject):
class sqlmeta:
cacheValues = False
user = StringCol()
target = StringCol()
filter = StringCol()
timestamp = DateTimeCol()
command = SingleJoin('Command')
comments = MultipleJoin('Comment')
variables = MultipleJoin('Variable')
checkpoint = SingleJoin('Checkpoint')
def __str__(self):
report = '%d\t%s\t%s\n' % (self.id, str(self.timestamp), self.user)
if self.command is not None:
report += '\t\t%s\n' % str(self.command)
if self.checkpoint is not None:
report += '\t\t%s\n' % str(self.checkpoint)
for comment in self.comments:
report += '\t\t%s\n' % str(comment)
for var in self.variables:
report += '\t\t%s\n' % str(var)
return report
Entry.createTable(ifNotExists = True)
Command.createTable(ifNotExists = True)
Comment.createTable(ifNotExists = True)
Variable.createTable(ifNotExists = True)
Checkpoint.createTable(ifNotExists = True)
########################
class NoTargetError(Exception): pass
class NoFilterError(Exception): pass
#########################
def loggingIsOff():
return 'BONN_LOG' in os.environ and \
os.environ['BONN_LOG'] == '0'
#########################
def state():
return LogState
#########################
def reset():
Entry.dropTable(ifExists = True)
Command.dropTable(ifExists = True)
Comment.dropTable(ifExists = True)
Variable.dropTable(ifExists = True)
Checkpoint.dropTable(ifExists = True)
Entry.createTable()
Command.createTable()
Comment.createTable()
Variable.createTable()
Checkpoint.createTable()
#########################
def getTarget():
try:
return os.environ['BONN_TARGET']
except KeyError:
raise NoTargetError('BONN_TARGET not defined')
########################
def getFilter():
try:
return os.environ['BONN_FILTER']
except KeyError:
raise NoFilterError('BONN_FILTER not defined')
########################
def targets():
targets = []
for entry in Entry.select():
if entry.target not in targets:
targets.append(entry.target)
return targets
#######################
def filters(target):
filters = []
for entry in Entry.selectBy(target = target):
if entry.filter not in filters:
filters.append(entry.filter)
return filters
########################
def getUser():
return os.environ['USER']
#########################
def lastEntry(target = None, filter = None):
entry = getLastEntries(num = 1,
target = target,
filter = filter)
if len(entry) == 0:
return None
else:
return entry[0]
############################
def getLastStatus():
isCurTarget = (Command.q.entryID == Entry.q.id) & \
(Entry.q.target == getTarget()) & \
(Entry.q.filter == getFilter())
curStatusId = Command.select(isCurTarget & \
(Command.q.status != None )).max(Command.q.id)
if curStatusId is None:
return None
return Command.get(curStatusId).status
############################
def lastCommand(target = None, filter = None):
if target is None and filter is None:
maxId = Command.select().max(Command.q.id)
else:
isCurTarget = (Command.q.entryID == Entry.q.id) & \
(Entry.q.target == getTarget()) & \
(Entry.q.filter == getFilter())
maxId = Command.select(isCurTarget).max(Command.q.id)
if maxId is None:
return None
return Command.get(maxId)
############################
def getLastEntries(num = 10, target = None, filter = None):
if target is None and filter is None:
entries = Entry.select()[-num:]
else:
entries = Entry.selectBy(target=target, filter=filter)[-num:]
return list(entries)
############################
def getEntriesByDate(range = None):
if range is None:
return list(Entry.select())
return list(Entry.select(AND(Entry.q.timestamp >= range[0],
Entry.q.timestamp <= range[1])))
############################
def addEntry():
return Entry(user = getUser(),
target = getTarget(),
filter = getFilter(),
timestamp = datetime.datetime.now())
############################
def addCommand(cmd, args = [], status = None, comment = None):
if loggingIsOff():
return -1
entry = addEntry()
c = Command(entry = entry,
command = cmd,
args = args,
status = status)
if comment is not None:
Comment(entry = entry, comment = comment)
return entry.id
############################
def addCheckpoint(target, filter, check, comment = None):
if loggingIsOff():
return -1;
confirm = raw_input("Confirm %s for %s : %s [y/n]?" % (check, target, filter))
if confirm == 'y' or confirm == 'Y':
entry = addEntry()
Checkpoint(entry = entry, checkpoint = check)
if comment is not None:
Comment(entry = entry, comment = comment)
############################
def getCheckpoints(target, filter):
return list(Entry.select(AND(Checkpoint.q.entryID == Entry.q.id,
AND(Entry.q.target == target,
Entry.q.filter == filter))))
############################
def updateStatus(id, code, comment = None):
if loggingIsOff():
return
entry = Entry.get(id)
if entry is None or entry.command is None:
return
entry.command.status = code
if comment is not None:
Comment(entry = entry, comment = comment)
############################
def clearStatus():
addCommand('clearStatus', status = 0)
############################
def report(entries, target, filter):
report = 'Log for %s : %s\n' % (target, filter)
for entry in entries:
report += str(entry)
return report
############################
############################
def parseLog(args):
if loggingIsOff():
return -1
curStatus = getLastStatus()
if curStatus is not None and curStatus > 0:
sys.stderr.write("BonnLogger: Last Job Failed - Exiting\n")
sys.exit(1)
return parseForceLog(args)
############################
def parseForceLog(args):
command = os.path.basename(args[0])
id = addCommand(command, args[1:])
print id
return id
###########################
def parseUpdate(args):
if loggingIsOff():
sys.exit(0)
commandId = args[0]
exitStatus = int(args[1])
comment = None
if len(args) > 2:
comment = ' '.join(args[2:])
updateStatus(commandId, exitStatus, comment)
sys.exit(exitStatus)
############################
def parseComment(args):
entry = addEntry()
Comment(entry = entry, comment = args[0])
############################
def parseConfig(args):
entry = addEntry()
for arg in args:
(var,val) = arg.split('=')
Variable(entry = entry, var = var, val = val)
############################
def parseState(args):
print "%s : %d" % (state(), int(not loggingIsOff()))
############################
def parseLast(args):
if len(args) > 0:
entries = getLastEntries(int(args[0]), getTarget(), getFilter())
else:
entries = getLastEntries(1, getTarget(), getFilter())
print report(entries, getTarget(), getFilter())
############################
def parseOverview(args):
for target in targets():
for filter in filters(target):
entries = getCheckpoints(target, filter)
lastEntry = getLastEntries(1, target, filter)[0]
if lastEntry not in entries:
entries.append(lastEntry)
print report(entries, target, filter)
print
#############################
def parseCheckpoint(args):
if loggingIsOff():
return -1;
check = args[0];
if len(args) > 1:
comment = args[1];
addCheckpoint(getTarget(), getFilter(), check, comment)
else:
addCheckpoint(getTarget(), getFilter(), check)
############################
def parseCommandLine():
command = sys.argv[1]
args = sys.argv[2:]
try:
if command == 'log':
parseLog(args)
elif command == 'forceLog':
parseForceLog(args)
elif command == 'update':
parseUpdate(args)
elif command == 'clear':
clearStatus()
elif command == 'config':
parseConfig(args)
elif command == 'comment':
parseComment(args)
elif command == 'state':
parseState(args)
elif command == 'last':
parseLast(args)
elif command == 'overview':
parseOverview(args)
elif command == 'checkpoint':
parseCheckpoint(args)
elif command == 'help':
print usage
else:
print "Error: %s Unrecognized"
print usage
sys.exit(1)
except NoTargetError:
sys.stderr.write("\nError: BONN_TARGET Not Defined\n")
if not loggingIsOff():
sys.exit(1)
except NoFilterError:
sys.stderr.write("\nError: BONN_FILTER Not Defined\n")
if not loggingIsOff():
sys.exit(1)
############################
if __name__ == '__main__':
parseCommandLine()