Debian Codegraph @

lib/ClusterShell/Worker/Popen.py

raw symbols SCIP
1 #
2 # Copyright (C) 2008-2015 CEA/DAM
3 # Copyright (C) 2015 Stephane Thiell <[email protected]>
4 #
5 # This file is part of ClusterShell.
6 #
7 # ClusterShell is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # ClusterShell is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with ClusterShell; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
21 """
22 WorkerPopen
23
24 ClusterShell worker for executing local commands.
25
26 Usage example:
27 >>> worker = WorkerPopen("/bin/uname", key="mykernel")
28 >>> task.schedule(worker) # schedule worker
29 >>> task.resume() # run task
30 >>> worker.retcode() # get return code
31 0
32 >>> worker.read() # read command output
33 'Linux'
34
35 """
36
37 from ClusterShell.Worker.Worker import WorkerSimple, StreamClient
38 from ClusterShell.Worker.Worker import _eh_sigspec_invoke_compat
39
40
41 class PopenClient(StreamClient):
42
43 def __init__(self, worker, key, stderr, timeout, autoclose):
44 """PopenClient initializer"""
45 StreamClient.__init__(self, worker, key, stderr, timeout, autoclose)
46 self.popen = None
47 self.rc = None
48 # Declare writer stream to allow early buffering
49 self.streams.set_writer(worker.SNAME_STDIN, None, retain=False)
50
51 def _start(self):
52 """Worker is starting."""
53 assert not self.worker.started
54 assert self.popen is None
55
56 self.popen = self._exec_nonblock(self.worker.command, shell=True)
57
58 task = self.worker.task
59 if task.info("debug", False):
60 task.info("print_debug")(task, "POPEN: %s" % self.worker.command)
61
62 self.worker._on_start(self.key)
63 return self
64
65 def _close(self, abort, timeout):
66 """
67 Close client. See EngineClient._close().
68 """
69 if abort:
70 # it's safer to call poll() first for long time completed processes
71 prc = self.popen.poll()
72 # if prc is None, process is still running
73 if prc is None:
74 try: # try to kill it
75 self.popen.kill()
76 except OSError:
77 pass
78 prc = self.popen.wait()
79
80 self.streams.clear()
81 self.invalidate()
82
83 if prc >= 0: # filter valid rc
84 self.rc = prc
85 self.worker._on_close(self.key, prc)
86 elif timeout:
87 assert abort, "abort flag not set on timeout"
88 self.worker._on_timeout(self.key)
89 elif not abort:
90 # if process was signaled, return 128 + signum (bash-like)
91 self.rc = 128 + -prc
92 self.worker._on_close(self.key, self.rc)
93
94 if self.worker.eh is not None:
95 _eh_sigspec_invoke_compat(self.worker.eh.ev_close, 2, self.worker,
96 timeout)
97
98
99 class WorkerPopen(WorkerSimple):
100 """
101 Implements the Popen Worker.
102 """
103 def __init__(self, command, key=None, handler=None,
104 stderr=False, timeout=-1, autoclose=False):
105 """Initialize Popen worker."""
106 WorkerSimple.__init__(self, None, None, None, key, handler, stderr,
107 timeout, autoclose, client_class=PopenClient)
108 self.command = command
109 if not self.command:
110 raise ValueError("missing command parameter in WorkerPopen "
111 "constructor")
112 self.key = key
113
114 def retcode(self):
115 """Return return code or None if command is still in progress."""
116 return self.clients[0].rc
117
118 WORKER_CLASS = WorkerPopen