12
12
import fcntl
13
13
import glob
14
14
from string import Template
15
+ import time
15
16
16
17
logger = None
17
18
18
- # Cheap colored terminal logging. Fixme.
19
- def color_logging ():
20
- if sys .stderr .isatty ():
21
- logging .addLevelName ( logging .WARNING , "\033 [1;31m%s\033 [1;0m" % logging .getLevelName (logging .WARNING ))
22
- logging .addLevelName ( logging .ERROR , "\033 [1;41m%s\033 [1;0m" % logging .getLevelName (logging .ERROR ))
19
+ class colors (object ):
20
+
21
+ RED = 31
22
+ GREEN = 32
23
+ YELLOW = 33
24
+ BLUE = 34
25
+ PURPLE = 35
26
+ CYAN = 36
27
+ REDB = 41
28
+ GREENB = 42
29
+ YELLOWB = 43
30
+ BLUEB = 44
31
+ PURPLEB = 45
32
+ CYANB = 46
33
+
34
+ @staticmethod
35
+ def color (string , color ):
36
+ if sys .stderr .isatty ():
37
+ return "\033 [1;%sm%s\033 [1;0m" % (color , string )
38
+ else :
39
+ return string
40
+
41
+ # Adds a method for each color to the colors class
42
+ for attr in dir (colors ):
43
+ def colormethod (attr ):
44
+ def f (klass , string ):
45
+ return colors .color (string , getattr (colors , attr ))
46
+ return classmethod (f )
23
47
48
+ if attr .isupper ():
49
+ setattr (colors , attr .lower (), colormethod (attr ))
50
+
51
+
52
+
53
+
54
+ def color_logging ():
55
+ logging .addLevelName ( logging .WARNING , colors .red (logging .getLevelName (logging .WARNING )))
56
+ logging .addLevelName ( logging .ERROR , colors .redb (logging .getLevelName (logging .ERROR )))
24
57
25
58
def init_logging (name , lvl = logging .DEBUG ):
26
59
global logger
@@ -31,6 +64,27 @@ def init_logging(name, lvl=logging.DEBUG):
31
64
return logger
32
65
33
66
67
+ class Profiler (object ):
68
+
69
+ ENABLED = True
70
+ LOGFILE = 'plog'
71
+
72
+ def __enter__ (self ):
73
+ self .start = time .time ()
74
+ return self
75
+
76
+ def __exit__ (self , * exc ):
77
+ self .end = time .time ()
78
+ self .duration = self .end - self .start
79
+
80
+ def log (self , operation , prefix = '' ):
81
+ msg = "[profiler] %s%s : %s seconds (%s minutes)" % (prefix , operation , self .duration , self .duration / 60.0 )
82
+ if self .ENABLED :
83
+ logger .info (colors .cyan (msg ))
84
+ if self .LOGFILE :
85
+ with open (self .LOGFILE , "a" ) as f :
86
+ f .write (msg + "\n " )
87
+
34
88
############################################################
35
89
#
36
90
# Log and execute system commands
@@ -62,13 +116,17 @@ def execute(args, sudo=False, chroot=None, ex=None):
62
116
63
117
logger .debug ("Executing:%s" , args )
64
118
65
- try :
66
- subprocess .check_call (args , shell = shell )
67
- return 0
68
- except subprocess .CalledProcessError , e :
69
- if ex :
70
- raise ex
71
- return e .returncode
119
+ rv = 0
120
+ with Profiler () as profiler :
121
+ try :
122
+ subprocess .check_call (args , shell = shell )
123
+ rv = 0
124
+ except subprocess .CalledProcessError , e :
125
+ if ex :
126
+ raise ex
127
+ rv = e .returncode
128
+ profiler .log (args )
129
+ return rv
72
130
73
131
74
132
# Flatten lists if string lists
0 commit comments