3 # Copyright (C) 2012 GRNET S.A.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 PROGNAME = os.path.basename(sys.argv[0])
31 MSG_TYPE = 'image-helper'
34 'TASK_START': ('task-start', 'task'),
35 'TASK_END': ('task-end', 'task'),
36 'WARNING': ('warning', 'messages'),
37 'STDERR': ('error', 'stderr'),
38 'ERROR': ('error', 'messages')}
42 sys.stderr.write("HELPER-MONITOR ERROR: %s\n" % msg)
46 def send(fd, msg_type, value):
47 subtype, value_name = PROTOCOL[msg_type]
50 msg['type'] = MSG_TYPE
51 msg['subtype'] = subtype
52 msg[value_name] = value
53 msg['timestamp'] = time.time()
54 os.write(fd, "%s\n" % json.dumps(msg))
57 if __name__ == "__main__":
58 usage = "Usage: %s <file-descriptor>\n" % PROGNAME
60 if len(sys.argv) != 2:
61 sys.stderr.write(usage)
67 error("File descriptor is not an integer")
72 error("File descriptor is not valid")
79 # Can't use sys.stdin.readline since I want unbuffered I/O
80 new_data = os.read(sys.stdin.fileno(), BUFSIZE)
89 split = new_data.split('\n', 1)
92 if len(line) > LINESIZE:
93 error("Line size exceeded the maximum allowed size")
99 if line_count >= MAXLINES + 1:
100 error("Exceeded maximum allowed number of lines: %d." %
104 stderr += "%s\n" % line
107 send(fd, "STDERR", stderr)
116 if line.startswith("STDERR:"):
117 m = re.match("STDERR:(\d+):(.*)", line)
119 error("Invalid syntax for STDERR line")
121 lines_left = int(m.group(1))
123 error("Second field in STDERR line must be an integer")
125 if lines_left > STDERR_MAXLINES:
126 error("Too many lines in the STDERR output")
128 error("Second field of STDERR: %d is invalid" % lines_left)
131 stderr = m.group(2) + "\n"
135 send(fd, "STDERR", stderr)
137 elif line.startswith("TASK_START:") \
138 or line.startswith("TASK_END:") \
139 or line.startswith("WARNING:") \
140 or line.startswith("ERROR:"):
141 (msg_type, _, value) = line.partition(':')
143 if line.startswith("WARNING:") or line.startswith("ERROR:"):
145 send(fd, msg_type, value)
147 error("Unknown command!")
149 # Remove the processed line
152 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :