summaryrefslogtreecommitdiff
path: root/src/leap/util/coroutines.py
blob: 5e25eb63b4e2579f56ab02ca75ce45a9134dca68 (plain)
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
# the problem of watching a stdout pipe from
# openvpn binary: using subprocess and coroutines
# acting as event consumers

from __future__ import division, print_function

from subprocess import PIPE, Popen
import sys
from threading import Thread

ON_POSIX = 'posix' in sys.builtin_module_names


#
# Coroutines goodies
#

def coroutine(func):
    def start(*args, **kwargs):
        cr = func(*args, **kwargs)
        cr.next()
        return cr
    return start


@coroutine
def process_events(callback):
    """
    coroutine loop that receives
    events sent and dispatch the callback.
    :param callback: callback to be called\
for each event
    :type callback: callable
    """
    try:
        while True:
            m = (yield)
            if callable(callback):
                callback(m)
            else:
                #XXX log instead
                print('not a callable passed')
    except GeneratorExit:
        return

#
# Threads
#


def launch_thread(target, args):
    """
    launch and demonize thread.
    :param target: target function that will run in thread
    :type target: function
    :param args: args to be passed to thread
    :type args: list
    """
    t = Thread(target=target,
               args=args)
    t.daemon = True
    t.start()
    return t


def watch_output(out, observers):
    """
    initializes dict of observer coroutines
    and pushes lines to each of them as they are received
    from the watched output.
    :param out: stdout of a process.
    :type out: fd
    :param observers: tuple of coroutines to send data\
for each event
    :type ovservers: tuple
    """
    observer_dict = {observer: process_events(observer)
                     for observer in observers}
    for line in iter(out.readline, b''):
        for obs in observer_dict:
            observer_dict[obs].send(line)
    out.close()


def spawn_and_watch_process(command, args, observers=None):
    """
    spawns a subprocess with command, args, and launch
    a watcher thread.
    :param command: command to be executed in the subprocess
    :type command: str
    :param args: arguments
    :type args: list
    :param observers: tuple of observer functions to be called \
for each line in the subprocess output.
    :type observers: tuple
    :return: a tuple containing the child process instance, and watcher_thread,
    :rtype: (Subprocess, Thread)
    """
    subp = Popen([command] + args,
                 stdout=PIPE,
                 stderr=PIPE,
                 bufsize=1,
                 close_fds=ON_POSIX)
    watcher = launch_thread(
        watch_output,
        (subp.stdout, observers))
    return subp, watcher