def run_playbook(playbook, inventory, *args, **kwargs):
env = ansible_env(os.environ.copy())
cmd = ['ansible-playbook', '-i', inventory, playbook] + list(args)
if verbosity():
cmd += ['-' + ('v' * verbosity())]
show_timestamp = False
if 'timestamp' in kwargs:
show_timestamp = kwargs['timestamp']
del kwargs['timestamp']
output = print
if show_timestamp:
output = timestamp
logger.info('running %s', ' '.join(cmd))
logger.debug('env: %r', env)
process = Popen(cmd, env=env, stdout=PIPE,
bufsize=1, **kwargs)
for line in iter(process.stdout.readline, b''):
output(line[:-1])
# empty output buffers
process.poll()
return process.returncode
python类PIPE的实例源码
def checkOthers(self, test_step):
if os.access(test_step.path, X_OK):
return
logger.debug("add +x for %s", test_step.path)
command = "chmod +x " + test_step.path
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
outs, errs = proc.communicate()
rc = proc.returncode
if rc:
raise Exception("command %s failed: rc is %s, output is %s, errs is %s ", command, rc, outs, errs)
def check_call_realtime(args):
"""Run command with arguments and yield the output as they come.
Stderr is piped into stdout.
:raises subprocess.CalledProcessError: if exit code is non-zero
"""
p = subprocess.Popen(args, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
while p.poll() is None:
yield p.stdout.read()
yield p.stdout.read()
if p.returncode != 0:
raise subprocess.CalledProcessError(p.returncode, args)
def handle_eval(self, record):
self.process = Popen(['./sphere_ext', array2str(record.params[0])],
stdout=PIPE)
out = self.process.communicate()[0]
try:
val = float(out) # This raises ValueError if out is not a float
self.finish_success(record, val)
except ValueError:
logging.warning("Function evaluation crashed/failed")
self.finish_failure(record)
def handle_eval(self, record):
self.process = Popen(['./sumfun_ext', array2str(record.params[0])],
stdout=PIPE)
val = np.nan
# Continuously check for new outputs from the subprocess
while True:
output = self.process.stdout.readline()
if output == '' and self.process.poll() is not None: # No new output
break
if output: # New intermediate output
try:
val = float(output.strip()) # Try to parse output
if val > 350: # Terminate if too large
self.process.terminate()
self.finish_success(record, 350)
return
except ValueError: # If the output is nonsense we terminate
logging.warning("Incorrect output")
self.process.terminate()
self.finish_failure(record)
return
rc = self.process.poll() # Check the return code
if rc < 0 or np.isnan(val):
logging.warning("Incorrect output or crashed evaluation")
self.finish_failure(record)
else:
self.finish_success(record, val)
def spawn(self):
"""Spawn the fake executable using subprocess.Popen."""
self._process = subprocess.Popen(
[self.path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
self.addCleanup(self._process_kill)
def raw_cmd(self, *args, **kwargs):
'''adb command. return the subprocess.Popen object.'''
cmds = [self.adb_path()] + self._host_port_args + list(args)
kwargs['stdout'] = kwargs.get('stdout', subprocess.PIPE)
kwargs['stderr'] = kwargs.get('stderr', subprocess.PIPE)
# if os.name != "nt":
# cmd_line = [" ".join(cmd_line)]
cmds = [strutils.decode(v) for v in cmds]
return subprocess.Popen(cmds, **kwargs)
def create_journals(self, max_size, delta):
'''create usn journals to track changes for chosen drives if such don't
already exist'''
for drive in self.ntfs_drives:
#check if a journal exists, else:
Popen(('fsutil', 'usn', 'createjournal', max_size, delta, drive), stdout=PIPE).communicate()[0]
#experiment
def get_next_max_usn(self, drive):
'''On windows/ntfs this is 'next usn' - the usn index to latest change made.
Also returns max_usn since enumdata requires an upper boundary. Not needed
when using readjournal.
fsutil usn queryjournal result:
Usn Journal ID : 0x01d2a26e17dbc5e8
First Usn : 0x0000000000000000
Next Usn : 0x0000000000acddf0 <--- index #2
Lowest Valid Usn : 0x0000000000000000
Max Usn : 0x7fffffffffff0000 <--- index #4
.
. '''
if 'win' in sys.platform and drive == '/':
''' using '/' on windows works for scandir but not for fsutil'''
drive='c:'
else:
# Removing trailing slashes
drive=drive.split(':')[0]+':'
journal_specs=Popen(('fsutil', 'usn', 'queryjournal', drive), stdout=PIPE).communicate()[0].split('\r\n')
next_usn=journal_specs[2].split(': ')[1]
max_usn=journal_specs[4].split(': ')[1]
return next_usn, max_usn #int(next_usn, 16), int(max_usn, 16)
def run_cmd(self, params):
def convert_line(line):
line = to_str(line)
return line.strip() + '\r\n'
try:
self.process = run(self.cmd, async=True, stdin=self.stdin, outfile=self.outfile,
env_vars=self.env_vars, inherit_cwd=self.inherit_cwd)
if self.outfile:
if self.outfile == subprocess.PIPE:
# get stdout/stderr from child process and write to parent output
for line in iter(self.process.stdout.readline, ''):
if not (line and line.strip()) and self.is_killed():
break
line = convert_line(line)
sys.stdout.write(line)
sys.stdout.flush()
for line in iter(self.process.stderr.readline, ''):
if not (line and line.strip()) and self.is_killed():
break
line = convert_line(line)
sys.stderr.write(line)
sys.stderr.flush()
self.process.wait()
else:
self.process.communicate()
except Exception as e:
if self.process and not self.quiet:
LOGGER.warning('Shell command error "%s": %s' % (e, self.cmd))
if self.process and not self.quiet and self.process.returncode != 0:
LOGGER.warning('Shell command exit code "%s": %s' % (self.process.returncode, self.cmd))
def setUpClass(cls):
# In Python 3 Click makes a call to locale to determine how the
# terminal wants to handle unicode. Because we mock Popen to avoid
# calling the real verifier, we need to get the actual result of
# locale to provide it to Click during the test run.
if os.name == 'nt':
cls.locale = '' # pragma: no cover
else:
cls.locale = Popen(
['locale', '-a'], stdout=PIPE, stderr=PIPE).communicate()[0]
def run_simple (self, cmds, x, family) :
'''
Run a single command in a subprocess. Line buffer output.
cmds -> A list of commands to be run for this family
x -> The sequence of the command to run
family -> The family that goes with these commands
pee -> The process
fifofh -> File handle to fifo
'''
pee = None
try :
cmd = cmds[x]
except IndexError :
return pee, None
#fifo = os.path.join ("/tmp", "fifo{0}".format (family))
#if not os.path.exists (fifo) :
#os.mkfifo (fifo)
#fifofh = open (fifo, mode='rw+')
pee = subprocess.Popen (cmd,
shell=True,
bufsize=1,
cwd=os.path.join (self.home, family),
stdout=subprocess.PIPE,
universal_newlines=True,
close_fds=ON_POSIX)
fifofh = pee.stdout
return pee, fifofh
def run_cmds (self, cmds, x = 0, ems = None) :
'''
Run conversion commands in a subprocess
cmds -> A dictionary of families that point to a list of commands.
cmds['B']['125a2ph5 -n master.ph5 ...', '1302ph5 -n master.ph5 ...']
x -> The sequence of the current command executing in the list in cmds.
ems -> The list of families ['A', 'B', 'C' etc]
'''
pees = {}
if ems == None :
ems = self.nmini
else :
ems = [ems]
#for inst in ('texan', 'rt-130', 'nodal') :
for m in ems :
if len (cmds[m]) > x :
insts = cmds[m][x]
pees[m] = subprocess.Popen (insts,
shell=True,
bufsize=-1,
cwd=os.path.join (self.home, m),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
else :
pees[m] = None
if len (ems) > 1 :
return pees, x
else :
return pees[m], x
#
### Should this be implemented as a closure?
#
def run_cmd(self, params):
def convert_line(line):
line = to_str(line)
return line.strip() + '\r\n'
try:
self.process = run(self.cmd, async=True, stdin=self.stdin, outfile=self.outfile,
env_vars=self.env_vars, inherit_cwd=self.inherit_cwd)
if self.outfile:
if self.outfile == subprocess.PIPE:
# get stdout/stderr from child process and write to parent output
for line in iter(self.process.stdout.readline, ''):
if not (line and line.strip()) and self.is_killed():
break
line = convert_line(line)
sys.stdout.write(line)
sys.stdout.flush()
for line in iter(self.process.stderr.readline, ''):
if not (line and line.strip()) and self.is_killed():
break
line = convert_line(line)
sys.stderr.write(line)
sys.stderr.flush()
self.process.wait()
else:
self.process.communicate()
except Exception as e:
if self.process and not self.quiet:
LOGGER.warning('Shell command error "%s": %s' % (e, self.cmd))
if self.process and not self.quiet and self.process.returncode != 0:
LOGGER.warning('Shell command exit code "%s": %s' % (self.process.returncode, self.cmd))
def spawn(self, engine):
self.engine = engine
self.process = subprocess.Popen(self.command, stdout=subprocess.PIPE, stdin=subprocess.PIPE, bufsize=1, universal_newlines=True)
self._receiving_thread.start()
test_subscription_transport.py 文件源码
项目:graphql-python-subscriptions
作者: hballard
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def test_send_subscription_fail_message_to_client_with_invalid_query(server):
node_script = '''
module.paths.push('{0}')
WebSocket = require('ws')
const SubscriptionClient =
require('subscriptions-transport-ws').SubscriptionClient
const client = new SubscriptionClient('ws://localhost:{1}/socket')
setTimeout(function () {{
client.subscribe({{
query: `subscription useInfo($id: String) {{
user(id: $id) {{
id
birthday
}}
}}`,
operationName: 'useInfo',
variables: {{
id: 3,
}},
}}, function (error, result) {{
}}
);
}}, 100);
client.client.onmessage = (message) => {{
let msg = JSON.parse(message.data)
console.log(JSON.stringify({{[msg.type]: msg}}))
}};
'''.format(
os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT)
p = subprocess.Popen(
['node', '-e', node_script],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
time.sleep(.2)
q = queue.Queue()
t = threading.Thread(target=enqueue_output, args=(p.stdout, q))
t.daemon = True
t.start()
time.sleep(.2)
ret_values = {}
while True:
try:
_line = q.get_nowait()
if isinstance(_line, bytes):
line = _line.decode()
line = json.loads(line)
ret_values[list(line.keys())[0]] = line[list(line.keys())[0]]
except ValueError:
pass
except queue.Empty:
break
assert ret_values['type'] == SUBSCRIPTION_FAIL
assert len(ret_values['payload']['errors']) > 0
# TODO: troubleshoot this a bit...passes, but receives extra messages which I'm
# filtering out w/ the "AttributeError" exception clause
test_subscription_transport.py 文件源码
项目:graphql-python-subscriptions
作者: hballard
项目源码
文件源码
阅读 31
收藏 0
点赞 0
评论 0
def test_correctly_sets_the_context_in_on_subscribe(server):
node_script = '''
module.paths.push('{0}')
WebSocket = require('ws')
const SubscriptionClient =
require('subscriptions-transport-ws').SubscriptionClient
const CTX = 'testContext';
const client = new SubscriptionClient('ws://localhost:{1}/socket')
client.subscribe({{
query: `subscription context {{
context
}}`,
variables: {{}},
context: CTX,
}}, (error, result) => {{
client.unsubscribeAll();
if (error) {{
console.log(JSON.stringify(error));
}}
if (result) {{
console.log(JSON.stringify({{
client: {{
result: result,
}}
}}));
}} else {{
// pass
}}
}}
);
'''.format(
os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT)
p = subprocess.Popen(
['node', '-e', node_script],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
time.sleep(.2)
requests.post(
'http://localhost:{0}/publish'.format(TEST_PORT), json=['context', {}])
q = queue.Queue()
t = threading.Thread(target=enqueue_output, args=(p.stdout, q))
t.daemon = True
t.start()
time.sleep(.2)
ret_values = {}
while True:
try:
_line = q.get_nowait()
if isinstance(_line, bytes):
line = _line.decode()
line = json.loads(line)
ret_values[list(line.keys())[0]] = line[list(line.keys())[0]]
except ValueError:
pass
except queue.Empty:
break
client = ret_values['client']
assert client['result']['context']
assert client['result']['context'] == 'testContext'
test_subscription_transport.py 文件源码
项目:graphql-python-subscriptions
作者: hballard
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def test_rejects_unparsable_message(server):
node_script = '''
module.paths.push('{0}');
WebSocket = require('ws');
const GRAPHQL_SUBSCRIPTIONS = 'graphql-subscriptions';
const client = new WebSocket('ws://localhost:{1}/socket',
GRAPHQL_SUBSCRIPTIONS);
client.onmessage = (message) => {{
let msg = JSON.parse(message.data)
console.log(JSON.stringify({{[msg.type]: msg}}))
client.close();
}};
client.onopen = () => {{
client.send('HI');
}}
'''.format(
os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT)
p = subprocess.Popen(
['node', '-e', node_script],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
q = queue.Queue()
t = threading.Thread(target=enqueue_output, args=(p.stdout, q))
t.daemon = True
t.start()
time.sleep(.2)
ret_values = {}
while True:
try:
_line = q.get_nowait()
if isinstance(_line, bytes):
line = _line.decode()
line = json.loads(line)
ret_values[list(line.keys())[0]] = line[list(line.keys())[0]]
except ValueError:
pass
except queue.Empty:
break
assert ret_values['subscription_fail']
assert len(ret_values['subscription_fail']['payload']['errors']) > 0
test_subscription_transport.py 文件源码
项目:graphql-python-subscriptions
作者: hballard
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def test_rejects_nonsense_message(server):
node_script = '''
module.paths.push('{0}');
WebSocket = require('ws');
const GRAPHQL_SUBSCRIPTIONS = 'graphql-subscriptions';
const client = new WebSocket('ws://localhost:{1}/socket',
GRAPHQL_SUBSCRIPTIONS);
client.onmessage = (message) => {{
let msg = JSON.parse(message.data)
console.log(JSON.stringify({{[msg.type]: msg}}))
client.close();
}};
client.onopen = () => {{
client.send(JSON.stringify({{}}));
}}
'''.format(
os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT)
p = subprocess.Popen(
['node', '-e', node_script],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
q = queue.Queue()
t = threading.Thread(target=enqueue_output, args=(p.stdout, q))
t.daemon = True
t.start()
time.sleep(.2)
ret_values = {}
while True:
try:
_line = q.get_nowait()
if isinstance(_line, bytes):
line = _line.decode()
line = json.loads(line)
ret_values[list(line.keys())[0]] = line[list(line.keys())[0]]
except ValueError:
pass
except queue.Empty:
break
assert ret_values['subscription_fail']
assert len(ret_values['subscription_fail']['payload']['errors']) > 0