• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2017 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Event subprocess module.
6
7Event subprocesses are subprocesses that print events to stdout.
8
9Each event is one line of ASCII text with a terminating newline
10character.  The event is identified with one of the preset strings in
11Event.  The event string may be followed with a single space and a
12message, on the same line.  The interpretation of the message is up to
13the event handler.
14
15run_event_command() starts such a process with a synchronous event
16handler.
17"""
18
19from __future__ import absolute_import
20from __future__ import division
21from __future__ import print_function
22
23import logging
24
25import enum
26import subprocess32
27from subprocess32 import PIPE
28
29logger = logging.getLogger(__name__)
30
31
32class Event(enum.Enum):
33    """Status change event enum
34
35    Members of this enum represent all possible status change events
36    that can be emitted by an event command and that need to be handled
37    by the caller.
38
39    The value of enum members must be a string, which is printed by
40    itself on a line to signal the event.
41
42    This should be backward compatible with all versions of
43    lucifer_run_job, which lives in the infra/lucifer repository.
44
45    TODO(crbug.com/748234): Events starting with X are temporary to
46    support gradual lucifer rollout.
47
48    https://chromium.googlesource.com/chromiumos/infra/lucifer
49    """
50    # Job status
51    STARTING = 'starting'
52    GATHERING = 'gathering'
53    X_TESTS_DONE = 'x_tests_done'
54    PARSING = 'parsing'
55    COMPLETED = 'completed'
56
57    # Host status
58    HOST_READY = 'host_ready'
59    HOST_NEEDS_CLEANUP = 'host_needs_cleanup'
60    HOST_NEEDS_RESET = 'host_needs_reset'
61
62
63def run_event_command(event_handler, args):
64    """Run a command that emits events.
65
66    Events printed by the command to stdout will be handled by
67    event_handler synchronously.  Exceptions raised by event_handler
68    will not be caught.  If an exception escapes, the child process's
69    standard file descriptors are closed and the process is waited for.
70    The event command should terminate if this happens.
71
72    event_handler is called to handle each event.  Malformed events
73    emitted by the command will be logged and discarded.  The
74    event_handler should take two positional arguments: an Event
75    instance and a message string.
76
77    @param event_handler: event handler.
78    @param args: passed to subprocess.Popen.
79    @param returns: exit status of command.
80    """
81    logger.debug('Starting event command with %r', args)
82    with subprocess32.Popen(args, stdout=PIPE) as proc:
83        logger.debug('Event command child pid is %d', proc.pid)
84        _handle_subprocess_events(event_handler, proc)
85    logger.debug('Event command child with pid %d exited with %d',
86                 proc.pid, proc.returncode)
87    return proc.returncode
88
89
90def _handle_subprocess_events(event_handler, proc):
91    """Handle a subprocess that emits events.
92
93    Events printed by the subprocess will be handled by event_handler.
94
95    @param event_handler: callable that takes an Event instance.
96    @param proc: Popen instance.
97    """
98    while True:
99        logger.debug('Reading subprocess stdout')
100        line = proc.stdout.readline()
101        if not line:
102            break
103        _handle_output_line(event_handler, line)
104
105
106def _handle_output_line(event_handler, line):
107    """Handle a line of output from an event subprocess.
108
109    @param event_handler: callable that takes a StatusChangeEvent.
110    @param line: line of output.
111    """
112    event_str, _, message = line.rstrip().partition(' ')
113    try:
114        event = Event(event_str)
115    except ValueError:
116        logger.warning('Invalid output %r received', line)
117        return
118    event_handler(event, message)
119