From f323af122fcfbf31061a04309adc21fbcdae25fa Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Tue, 7 Apr 2015 15:17:43 +0000 Subject: distbuild: Move SubprocessEventSource into its own module Previously it was only available in the distbuild-helper program. Moving it to its own module means we can test it and reuse it. This commit also adds a docstring to the class. Change-Id: Iaf7854048cf0ff463a87894f1f500cdcb6a34d8b --- distbuild-helper | 80 ++++---------------------------------------------------- 1 file changed, 5 insertions(+), 75 deletions(-) (limited to 'distbuild-helper') diff --git a/distbuild-helper b/distbuild-helper index d77fcaea..76a39d5f 100755 --- a/distbuild-helper +++ b/distbuild-helper @@ -32,78 +32,6 @@ import urlparse import distbuild -class FileReadable(object): - - def __init__(self, request_id, p, f): - self.request_id = request_id - self.process = p - self.file = f - - -class FileWriteable(object): - - def __init__(self, request_id, p, f): - self.request_id = request_id - self.process = p - self.file = f - - -class SubprocessEventSource(distbuild.EventSource): - - def __init__(self): - self.procs = [] - self.closed = False - - def get_select_params(self): - r = [] - w = [] - for requst_id, p in self.procs: - if p.stdin_contents is not None: - w.append(p.stdin) - if p.stdout is not None: - r.append(p.stdout) - if p.stderr is not None: - r.append(p.stderr) - return r, w, [], None - - def get_events(self, r, w, x): - events = [] - - for request_id, p in self.procs: - if p.stdin in w: - events.append(FileWriteable(request_id, p, p.stdin)) - if p.stdout in r: - events.append(FileReadable(request_id, p, p.stdout)) - if p.stderr in r: - events.append(FileReadable(request_id, p, p.stderr)) - - return events - - def add(self, request_id, process): - - self.procs.append((request_id, process)) - distbuild.set_nonblocking(process.stdin) - distbuild.set_nonblocking(process.stdout) - distbuild.set_nonblocking(process.stderr) - - def remove(self, process): - self.procs = [t for t in self.procs if t[1] != process] - - def kill_by_id(self, request_id): - logging.debug('SES: Killing all processes for %s', request_id) - for id, process in self.procs: - if id == request_id: - logging.debug('SES: killing %s', repr(process)) - process.kill() - - def close(self): - self.procs = [] - self.closed = True - - def is_finished(self): - return self.closed - - class HelperMachine(distbuild.StateMachine): def __init__(self, conn): @@ -117,7 +45,7 @@ class HelperMachine(distbuild.StateMachine): jm = self.jm = distbuild.JsonMachine(self.conn) self.mainloop.add_state_machine(jm) - p = self.procsrc = SubprocessEventSource() + p = self.procsrc = distbuild.SubprocessEventSource() self.mainloop.add_event_source(p) self.send_helper_ready(jm) @@ -125,8 +53,10 @@ class HelperMachine(distbuild.StateMachine): spec = [ ('waiting', jm, distbuild.JsonNewMessage, 'waiting', self.do), ('waiting', jm, distbuild.JsonEof, None, self._eofed), - ('waiting', p, FileReadable, 'waiting', self._relay_exec_output), - ('waiting', p, FileWriteable, 'waiting', self._feed_stdin), + ('waiting', p, distbuild.FileReadable, 'waiting', + self._relay_exec_output), + ('waiting', p, distbuild.FileWriteable, 'waiting', + self._feed_stdin), ] self.add_transitions(spec) -- cgit v1.2.1