Exemple #1
0
def loop_and_callback(command, callback):
    """
    run the given command (a sequence of arguments, ordinarily
    from sys.argv), and call the given callback with each line
    of stdout or stderr encountered. after the command is finished,
    callback is called once more with None instead of a string.
    """
    proc = subprocess.Popen(
        command,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
    )

    def handle_sigterm(signum, frame):
        proc.send_signal(signum)

    # register a handler to delegate SIGTERM
    # to the child process
    orig_handler = signal.signal(signal.SIGTERM, handle_sigterm)

    while proc.poll() is None:
        try:
            line = proc.stdout.readline().strip('\r\n')
            line = utils.unicode_dammit(line)
            callback(line)
        except IOError:
            # if the signal handler is called while
            # we're waiting for readline() to return,
            # don't show a traceback
            break

    # restore the original signal handler, if any
    signal.signal(signal.SIGTERM, orig_handler)
    return proc.returncode
Exemple #2
0
def loop_and_callback(command, callback):
    """
    run the given command (a sequence of arguments, ordinarily
    from sys.argv), and call the given callback with each line
    of stdout or stderr encountered. after the command is finished,
    callback is called once more with None instead of a string.
    """
    proc = subprocess.Popen(
        command,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
    )

    # We write the pid of the spawned process as the first line of buildlogger.py's stdout because
    # smoke.py expects to use it to terminate processes individually if already running inside a job
    # object.
    sys.stdout.write("[buildlogger.py] pid: %d\n" % (proc.pid))
    sys.stdout.flush()

    def handle_sigterm(signum, frame):
        try:
            proc.send_signal(signum)
        except AttributeError:
            os.kill(proc.pid, signum)

    # register a handler to delegate SIGTERM
    # to the child process
    orig_handler = signal.signal(signal.SIGTERM, handle_sigterm)

    while proc.poll() is None:
        try:
            line = proc.stdout.readline().strip('\r\n')
            line = utils.unicode_dammit(line)
            callback(line)
        except IOError:
            # if the signal handler is called while
            # we're waiting for readline() to return,
            # don't show a traceback
            break

    # There may be additional buffered output
    for line in proc.stdout.readlines():
        callback(line.strip('\r\n'))

    # restore the original signal handler, if any
    signal.signal(signal.SIGTERM, orig_handler)
    return proc.returncode
Exemple #3
0
def loop_and_callback(command, callback):
    """
    run the given command (a sequence of arguments, ordinarily
    from sys.argv), and call the given callback with each line
    of stdout or stderr encountered. after the command is finished,
    callback is called once more with None instead of a string.
    """
    proc = subprocess.Popen(
        command,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
    )

    # We write the pid of the spawned process as the first line of buildlogger.py's stdout because
    # smoke.py expects to use it to terminate processes individually if already running inside a job
    # object.
    sys.stdout.write("[buildlogger.py] pid: %d\n" % (proc.pid))
    sys.stdout.flush()

    def handle_sigterm(signum, frame):
        try:
            proc.send_signal(signum)
        except AttributeError:
            os.kill(proc.pid, signum)

    # register a handler to delegate SIGTERM
    # to the child process
    orig_handler = signal.signal(signal.SIGTERM, handle_sigterm)

    while proc.poll() is None:
        try:
            line = proc.stdout.readline().strip('\r\n')
            line = utils.unicode_dammit(line)
            callback(line)
        except IOError:
            # if the signal handler is called while
            # we're waiting for readline() to return,
            # don't show a traceback
            break

    # There may be additional buffered output
    for line in proc.stdout.readlines():
        callback(line.strip('\r\n'))

    # restore the original signal handler, if any
    signal.signal(signal.SIGTERM, orig_handler)
    return proc.returncode
Exemple #4
0
def loop_and_callback(command, callback):
    """
    run the given command (a sequence of arguments, ordinarily
    from sys.argv), wait for it to exit, and call the given callback
    with each line of stdout or stderr encountered.
    """

    stdoutfp = tempfile.TemporaryFile(prefix='buildlogger-')
    proc = subprocess.Popen(
        command,
        stdout=stdoutfp,
        stderr=subprocess.STDOUT,
    )

    def handle_sigterm(signum, frame):
        try:
            proc.send_signal(signum)
        except AttributeError:
            os.kill(proc.pid, signum)

    # register a handler to delegate SIGTERM
    # to the child process
    orig_handler = signal.signal(signal.SIGTERM, handle_sigterm)

    # wait for the test to run. Its output will be stored in
    # the stdout temporary file
    while proc.returncode is None:
        try:
            proc.wait()
        # Catch interrupts (EINTR) and retry.
        except OSError as e:
            if e.errno == 4:
                continue
            raise e


    # restore the original signal handler, if any
    signal.signal(signal.SIGTERM, orig_handler)

    # rewind the temp file and read through all its lines
    stdoutfp.seek(0)
    for line in stdoutfp:
        callback(utils.unicode_dammit(line.strip('\r\n')))
    stdoutfp.close()

    return proc.returncode
Exemple #5
0
def loop_and_callback(command, callback):
    """
    run the given command (a sequence of arguments, ordinarily
    from sys.argv), wait for it to exit, and call the given callback
    with each line of stdout or stderr encountered.
    """

    stdoutfp = tempfile.TemporaryFile(prefix='buildlogger-')
    proc = subprocess.Popen(
        command,
        stdout=stdoutfp,
        stderr=subprocess.STDOUT,
    )

    def handle_sigterm(signum, frame):
        try:
            proc.send_signal(signum)
        except AttributeError:
            os.kill(proc.pid, signum)

    # register a handler to delegate SIGTERM
    # to the child process
    orig_handler = signal.signal(signal.SIGTERM, handle_sigterm)

    # wait for the test to run. Its output will be stored in
    # the stdout temporary file
    while proc.returncode is None:
        try:
            proc.wait()
        # Catch interrupts (EINTR) and retry.
        except OSError as e:
            if e.errno == 4:
                continue
            raise e

    # restore the original signal handler, if any
    signal.signal(signal.SIGTERM, orig_handler)

    # rewind the temp file and read through all its lines
    stdoutfp.seek(0)
    for line in stdoutfp:
        callback(utils.unicode_dammit(line.strip('\r\n')))
    stdoutfp.close()

    return proc.returncode
Exemple #6
0
def loop_and_callback(command, callback):
    """
    run the given command (a sequence of arguments, ordinarily
    from sys.argv), and call the given callback with each line
    of stdout or stderr encountered. after the command is finished,
    callback is called once more with None instead of a string.
    """
    proc = subprocess.Popen(
        command,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
    )

    def handle_sigterm(signum, frame):
        try:
            proc.send_signal(signum)
        except AttributeError:
            os.kill(proc.pid, signum)

    # register a handler to delegate SIGTERM
    # to the child process
    orig_handler = signal.signal(signal.SIGTERM, handle_sigterm)

    while proc.poll() is None:
        try:
            line = proc.stdout.readline().strip('\r\n')
            line = utils.unicode_dammit(line)
            callback(line)
        except IOError:
            # if the signal handler is called while
            # we're waiting for readline() to return,
            # don't show a traceback
            break

    # There may be additional buffered output
    for line in proc.stdout.readlines():
        callback(line.strip('\r\n'))

    # restore the original signal handler, if any
    signal.signal(signal.SIGTERM, orig_handler)
    return proc.returncode