Skip to content

Commit 21d0007

Browse files
committed
Rudimentary profiling and color enhancements.
1 parent 8d41791 commit 21d0007

File tree

1 file changed

+70
-12
lines changed

1 file changed

+70
-12
lines changed

tools/onlu.py

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,48 @@
1212
import fcntl
1313
import glob
1414
from string import Template
15+
import time
1516

1617
logger = None
1718

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)
2347

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)))
2457

2558
def init_logging(name, lvl=logging.DEBUG):
2659
global logger
@@ -31,6 +64,27 @@ def init_logging(name, lvl=logging.DEBUG):
3164
return logger
3265

3366

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+
3488
############################################################
3589
#
3690
# Log and execute system commands
@@ -62,13 +116,17 @@ def execute(args, sudo=False, chroot=None, ex=None):
62116

63117
logger.debug("Executing:%s", args)
64118

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
72130

73131

74132
# Flatten lists if string lists

0 commit comments

Comments
 (0)