/usr/lib/python2.7/dist-packages/parallax/task.py is in python-parallax 1.0.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | # Copyright (c) 2009-2012, Andrew McNabb
# Copyright (c) 2013, Kristoffer Gronlund
from errno import EINTR
from subprocess import Popen, PIPE
import os
import signal
import sys
import time
import traceback
from parallax import askpass_client
BUFFER_SIZE = 1 << 16
try:
bytes
except NameError:
bytes = str
class Task(object):
"""Starts a process and manages its input and output.
Upon completion, the `exitstatus` attribute is set to the exit status
of the process.
"""
def __init__(self,
host,
port,
user,
cmd,
verbose=False,
quiet=False,
stdin=None,
print_out=False,
inline=False,
inline_stdout=False,
default_user=None):
# Backwards compatibility:
if not isinstance(verbose, bool):
opts = verbose
verbose = opts.verbose
quiet = opts.quiet
try:
print_out = bool(opts.print_out)
except AttributeError:
print_out = False
try:
inline = bool(opts.inline)
except AttributeError:
inline = False
try:
inline_stdout = bool(opts.inline_stdout)
except AttributeError:
inline_stdout = False
default_user = opts.user
self.exitstatus = None
self.host = host
self.pretty_host = host
self.port = port
self.cmd = cmd
if user and user != default_user:
self.pretty_host = '@'.join((user, self.pretty_host))
if port:
self.pretty_host = ':'.join((self.pretty_host, port))
self.proc = None
self.writer = None
self.timestamp = None
self.failures = []
self.killed = False
self.inputbuffer = stdin
self.byteswritten = 0
self.outputbuffer = bytes()
self.errorbuffer = bytes()
self.stdin = None
self.stdout = None
self.stderr = None
self.outfile = None
self.errfile = None
# Set options.
self.verbose = verbose
self.quiet = quiet
self.print_out = print_out
self.inline = inline
self.inline_stdout = inline or inline_stdout
def start(self, nodenum, iomap, writer, askpass_socket=None):
"""Starts the process and registers files with the IOMap."""
self.writer = writer
if writer:
self.outfile, self.errfile = writer.open_files(self.pretty_host)
# Set up the environment.
environ = dict(os.environ)
environ['PARALLAX_NODENUM'] = str(nodenum)
environ['PARALLAX_HOST'] = self.host
# Disable the GNOME pop-up password dialog and allow ssh to use
# askpass.py to get a provided password. If the module file is
# askpass.pyc, we replace the extension.
environ['SSH_ASKPASS'] = askpass_client.executable_path()
if askpass_socket:
environ['PARALLAX_ASKPASS_SOCKET'] = askpass_socket
if self.verbose:
environ['PARALLAX_ASKPASS_VERBOSE'] = '1'
# Work around a mis-feature in ssh where it won't call SSH_ASKPASS
# if DISPLAY is unset.
if 'DISPLAY' not in environ:
environ['DISPLAY'] = 'parallax-gibberish'
# Create the subprocess. Since we carefully call set_cloexec() on
# all open files, we specify close_fds=False.
self.proc = Popen(self.cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE,
close_fds=False, preexec_fn=os.setsid, env=environ)
self.timestamp = time.time()
if self.inputbuffer:
self.stdin = self.proc.stdin
iomap.register_write(self.stdin.fileno(), self.handle_stdin)
else:
self.proc.stdin.close()
self.stdout = self.proc.stdout
iomap.register_read(self.stdout.fileno(), self.handle_stdout)
self.stderr = self.proc.stderr
iomap.register_read(self.stderr.fileno(), self.handle_stderr)
def _kill(self):
"""Signals the process to terminate."""
if self.proc:
try:
os.kill(-self.proc.pid, signal.SIGKILL)
except OSError:
# If the kill fails, then just assume the process is dead.
pass
self.killed = True
def timedout(self):
"""Kills the process and registers a timeout error."""
if not self.killed:
self._kill()
self.failures.append('Timed out')
def interrupted(self):
"""Kills the process and registers an keyboard interrupt error."""
if not self.killed:
self._kill()
self.failures.append('Interrupted')
def cancel(self):
"""Stops a task that has not started."""
self.failures.append('Cancelled')
def elapsed(self):
"""Finds the time in seconds since the process was started."""
return time.time() - self.timestamp
def running(self):
"""Finds if the process has terminated and saves the return code."""
if self.stdin or self.stdout or self.stderr:
return True
if self.proc:
self.exitstatus = self.proc.poll()
if self.exitstatus is None:
if self.killed:
# Set the exitstatus to what it would be if we waited.
self.exitstatus = -signal.SIGKILL
return False
else:
return True
else:
if self.exitstatus < 0:
message = 'Killed by signal %s' % (-self.exitstatus)
self.failures.append(message)
elif self.exitstatus > 0:
message = 'Exited with error code %s' % self.exitstatus
self.failures.append(message)
self.proc = None
return False
def handle_stdin(self, fd, iomap):
"""Called when the process's standard input is ready for writing."""
try:
start = self.byteswritten
if start < len(self.inputbuffer):
chunk = self.inputbuffer[start:start+BUFFER_SIZE]
self.byteswritten = start + os.write(fd, chunk)
else:
self.close_stdin(iomap)
except (OSError, IOError):
_, e, _ = sys.exc_info()
if e.errno != EINTR:
self.close_stdin(iomap)
self.log_exception(e)
def close_stdin(self, iomap):
if self.stdin:
iomap.unregister(self.stdin.fileno())
self.stdin.close()
self.stdin = None
def handle_stdout(self, fd, iomap):
"""Called when the process's standard output is ready for reading."""
try:
buf = os.read(fd, BUFFER_SIZE)
if buf:
if self.inline_stdout:
if self.quiet:
self.outputbuffer += "%s: %s" % (self.host, buf)
else:
self.outputbuffer += buf
if self.outfile:
self.writer.write(self.outfile, buf)
if self.print_out:
for l in buf.split('\n'):
sys.stdout.write('%s: %s\n' % (self.host, l))
else:
self.close_stdout(iomap)
except (OSError, IOError):
_, e, _ = sys.exc_info()
if e.errno != EINTR:
self.close_stdout(iomap)
self.log_exception(e)
def close_stdout(self, iomap):
if self.stdout:
iomap.unregister(self.stdout.fileno())
self.stdout.close()
self.stdout = None
if self.outfile:
self.writer.close(self.outfile)
self.outfile = None
def handle_stderr(self, fd, iomap):
"""Called when the process's standard error is ready for reading."""
try:
buf = os.read(fd, BUFFER_SIZE)
if buf:
if self.inline:
self.errorbuffer += buf
if self.errfile:
self.writer.write(self.errfile, buf)
else:
self.close_stderr(iomap)
except (OSError, IOError):
_, e, _ = sys.exc_info()
if e.errno != EINTR:
self.close_stderr(iomap)
self.log_exception(e)
def close_stderr(self, iomap):
if self.stderr:
iomap.unregister(self.stderr.fileno())
self.stderr.close()
self.stderr = None
if self.errfile:
self.writer.close(self.errfile)
self.errfile = None
def log_exception(self, e):
"""Saves a record of the most recent exception for error reporting."""
if self.verbose:
exc_type, exc_value, exc_traceback = sys.exc_info()
exc = ("Exception: %s, %s, %s" %
(exc_type, exc_value, traceback.format_tb(exc_traceback)))
else:
exc = str(e)
self.failures.append(exc)
# vim:ts=4:sw=4:et:
|