forked from Tribler/gumby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·124 lines (107 loc) · 3.6 KB
/
run.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
#!/usr/bin/env python
# run.py ---
#
# Filename: run.py
# Description:
# Author: Elric Milon
# Maintainer:
# Created: Wed Jun 5 14:47:19 2013 (+0200)
# Commentary:
#
#
#
#
# Change Log:
#
#
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street, Fifth
# Floor, Boston, MA 02110-1301, USA.
#
#
# Code:
import sys
from os.path import dirname, exists
from twisted.python.log import msg, FileLogObserver, textFromEventDict, _safeFormat
from twisted.python import util
from twisted.internet import reactor
from gumby.runner import ExperimentRunner
class ColoredFileLogObserver(FileLogObserver):
CANCEL_COLOR = "\x1b[0m"
RED_NORMAL = "\x1b[31m"
RED_BOLD = "\x1b[31;1m"
GREEN_NORMAL = "\x1b[32m"
GREEN_BOLD = "\x1b[32;1m"
GREY_NORMAL = "\x1b[37m"
def __init__(self, f=None):
if f is None:
FileLogObserver.__init__(self, sys.stdout)
else:
FileLogObserver.__init__(self, f)
def _colorize(self, text, color=GREY_NORMAL):
return color + text + ColoredFileLogObserver.CANCEL_COLOR
def emit(self, eventDict):
text = textFromEventDict(eventDict)
if text is None:
return
timeStr = self._colorize(
self.formatTime(eventDict['time']),
ColoredFileLogObserver.GREY_NORMAL)
fmtDict = {
'system': eventDict['system'],
'text': text.replace("\n", "\n\t")}
systemStr = ""
systemStr = self._colorize(
_safeFormat("[%(system)s]", fmtDict),
ColoredFileLogObserver.GREY_NORMAL)
textStr = _safeFormat("%(text)s", fmtDict)
if textStr.startswith("SSH"):
t = textStr.find("STDERR:")
if t != -1:
textStr = self._colorize(
textStr[t + 8:],
ColoredFileLogObserver.RED_BOLD)
else:
textStr = self._colorize(
textStr[textStr.find("STDOUT:") + 8:],
ColoredFileLogObserver.GREEN_BOLD)
# only text for incoming data
msgStr = textStr + "\n"
else:
# add system to the local logs
# TODO: Make system more useful, not just "SSHChannel...".
msgStr = systemStr + " " + textStr + "\n"
util.untilConcludes(self.write, timeStr + " " + msgStr)
util.untilConcludes(self.flush) # Hoorj!
if __name__ == '__main__':
sys.path.append(dirname(__file__))
if len(sys.argv) == 2:
# startLogging(sys.stdout)
# startLogging(open("/tmp/cosa.log",'w'))
observer = ColoredFileLogObserver()
observer.start()
conf_path = sys.argv[1]
if not exists(conf_path):
print "Error: The specified configuration file doesn't exist."
exit(1)
exp_runner = ExperimentRunner(conf_path)
exp_runner.run()
reactor.run()
msg("Execution finished, have a nice day.")
else:
print "Usage:\n%s EXPERIMENT_CONFIG" % sys.argv[0]
#
# run.py ends here