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
|
from six.moves import StringIO
import argparse
import os
import sys
def get_home_dir(file):
abspath = os.path.abspath(file)
parent, dir = None, None
for dir in ( 'internal', 'internal2', 'external' ):
if abspath.find(dir) != -1:
return os.path.split(os.path.dirname(os.path.dirname(os.path.abspath(file))))[0]
return os.path.split(os.path.dirname(os.path.abspath(file)))[0]
def get_bin_dir(file):
return os.path.join(get_home_dir(file), 'bin')
def append_home_to_path(file):
sys.path.insert(0, get_home_dir(file))
append_home_to_path(__file__)
from tomahawk.expect import CommandWithExpect
from tomahawk.constants import (
DEFAULT_TIMEOUT,
DEFAULT_EXPECT_DELAY,
DEFAULT_RSYNC_OPTIONS,
)
def create_command_namespace(**kwargs):
defaults = {
'command': [ '' ], 'conf': None, 'continue_on_error': None,
'debug': False, 'deep_debug': False,
'delay': 0, 'expect_delay': 0.1,
'hosts': 'localhost', 'profile': False,
'ssh_user': 'tomahawk', 'timeout': DEFAULT_TIMEOUT
}
for k, v in defaults.items():
kwargs.setdefault(k, v)
return argparse.Namespace(**kwargs)
def create_rsync_namespace(**kwargs):
defaults = {
'source': None, 'destination': None,
'conf': None, 'continue_on_error': None,
'debug': False, 'deep_debug': False,
'delay': 0, 'expect_delay': 0.1,
'hosts': 'localhost', 'profile': False,
'rsync_user': 'tomahawk', 'rsync_options': DEFAULT_RSYNC_OPTIONS,
'timeout': DEFAULT_TIMEOUT,
}
for k, v in defaults.items():
kwargs.setdefault(k, v)
return argparse.Namespace(**kwargs)
def capture_stdout_stderr():
o = StdoutCapture()
e = StderrCapture()
o.start(), e.start()
return o, e
class StdoutCapture(object):
def __init__(self):
self.captured = StringIO()
def start(self):
sys.stdout = self.captured
return self
def stop(self):
sys.stdout = sys.__stdout__
return self
def value(self):
self.captured.flush()
return self.captured.getvalue()
def close(self):
self.captured.close()
# def __enter__(self):
# self.start()
# with StdoutCapture() as c:
# c.value()
# c = StdoutCapture()
# c.__enter__() -> c.start()
# c.value()
# c.__exit__() -> c.stop()
class StderrCapture(StdoutCapture):
def __init__(self):
super(StderrCapture, self).__init__()
def start(self):
sys.stderr = self.captured
return self
def stop(self):
sys.stderr = sys.__stderr__
return self
class MockPexpect(object):
def __init__(
self, command, args = [], timeout = 30, maxread = 2000,
searchwindowsize = None, logfile = None, cwd = None, env = None
):
self.command = command
self.args = args
self.timeout = timeout
self.maxread = maxread
self.searchwindowsize = searchwindowsize
self.logfile = logfile
self.cwd = cwd
self.env = env
self._exitstatus = 0
def expect(self, pattern, timeout = -1, searchwindowsize = -1):
#sys.stdout.write("password: ")
return 0
def sendline(self, s = ''):
if self.logfile:
self.logfile.write(s.encode('utf-8'))
self.logfile.write('\n'.encode('utf-8'))
pass
def send(self, s):
pass
def close(self):
pass
def get_exitstatus(self):
return self._exitstatus
def set_exitstatus(self, exitstatus):
self._exitstatus = exitstatus
exitstatus = property(get_exitstatus, set_exitstatus)
class MockCommandWithExpect(CommandWithExpect):
def __init__(
self, command, command_args, login_password, sudo_password,
timeout = DEFAULT_TIMEOUT, expect_delay = DEFAULT_EXPECT_DELAY,
debug_enabled = False, expect = None,
expect_out = StringIO()
):
if expect is None:
expect = MockPexpect(command, command_args, timeout)
super(MockCommandWithExpect, self).__init__(
command, command_args, login_password, sudo_password,
timeout, expect_delay, debug_enabled, expect, expect_out
)
def execute(self):
return 0, ''
|