/usr/lib/python2.7/dist-packages/psshlib/task.py is in pssh 2.3.1-1.
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 277 278 279 280 281 282 283 284 285 286 287 | # Copyright (c) 2009-2012, Andrew McNabb
from errno import EINTR
from subprocess import Popen, PIPE
import os
import signal
import sys
import time
import traceback
from psshlib import askpass_client
from psshlib import color
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, opts, stdin=None):
self.exitstatus = None
self.host = host
self.pretty_host = host
self.port = port
self.cmd = cmd
if user != opts.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 = opts.verbose
try:
self.print_out = bool(opts.print_out)
except AttributeError:
self.print_out = False
try:
self.inline = bool(opts.inline)
except AttributeError:
self.inline = False
try:
self.inline_stdout = bool(opts.inline_stdout)
except AttributeError:
self.inline_stdout = False
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['PSSH_NODENUM'] = str(nodenum)
environ['PSSH_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['PSSH_ASKPASS_SOCKET'] = askpass_socket
if self.verbose:
environ['PSSH_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'] = 'pssh-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 or self.inline_stdout:
self.outputbuffer += buf
if self.outfile:
self.writer.write(self.outfile, buf)
if self.print_out:
sys.stdout.write('%s: %s' % (self.host, buf))
if buf[-1] != '\n':
sys.stdout.write('\n')
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)
def report(self, n):
"""Pretty prints a status report after the Task completes."""
error = ', '.join(self.failures)
tstamp = time.asctime().split()[3] # Current time
if color.has_colors(sys.stdout):
progress = color.c("[%s]" % color.B(n))
success = color.g("[%s]" % color.B("SUCCESS"))
failure = color.r("[%s]" % color.B("FAILURE"))
stderr = color.r("Stderr: ")
error = color.r(color.B(error))
else:
progress = "[%s]" % n
success = "[SUCCESS]"
failure = "[FAILURE]"
stderr = "Stderr: "
host = self.pretty_host
if self.failures:
print(' '.join((progress, tstamp, failure, host, error)))
else:
print(' '.join((progress, tstamp, success, host)))
# NOTE: The extra flushes are to ensure that the data is output in
# the correct order with the C implementation of io.
if self.outputbuffer:
sys.stdout.flush()
try:
sys.stdout.buffer.write(self.outputbuffer)
sys.stdout.flush()
except AttributeError:
sys.stdout.write(self.outputbuffer)
if self.errorbuffer:
sys.stdout.write(stderr)
# Flush the TextIOWrapper before writing to the binary buffer.
sys.stdout.flush()
try:
sys.stdout.buffer.write(self.errorbuffer)
except AttributeError:
sys.stdout.write(self.errorbuffer)
|