本文整理匯總了Python中twisted.internet.utils.getProcessOutputAndValue方法的典型用法代碼示例。如果您正苦於以下問題:Python utils.getProcessOutputAndValue方法的具體用法?Python utils.getProcessOutputAndValue怎麽用?Python utils.getProcessOutputAndValue使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類twisted.internet.utils
的用法示例。
在下文中一共展示了utils.getProcessOutputAndValue方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_get_processOutputAndValueStdin
# 需要導入模塊: from twisted.internet import utils [as 別名]
# 或者: from twisted.internet.utils import getProcessOutputAndValue [as 別名]
def test_get_processOutputAndValueStdin(self):
"""
Standard input can be made available to the child process by passing
bytes for the `stdinBytes` parameter.
"""
scriptFile = self.makeSourceFile([
"import sys",
"sys.stdout.write(sys.stdin.read())",
])
stdinBytes = b"These are the bytes to see."
d = utils.getProcessOutputAndValue(
self.exe,
['-u', scriptFile],
stdinBytes=stdinBytes,
)
def gotOutputAndValue(out_err_code):
out, err, code = out_err_code
# Avoid making an exact equality comparison in case there is extra
# random output on stdout (warnings, stray print statements,
# logging, who knows).
self.assertIn(stdinBytes, out)
self.assertEqual(0, code)
d.addCallback(gotOutputAndValue)
return d
示例2: _find_utf8_locale
# 需要導入模塊: from twisted.internet import utils [as 別名]
# 或者: from twisted.internet.utils import getProcessOutputAndValue [as 別名]
def _find_utf8_locale(self):
# Click really wants to be running under a unicode-capable locale,
# especially on python3. macOS has en-US.UTF-8 but not C.UTF-8, and
# most linux boxes have C.UTF-8 but not en-US.UTF-8 . For tests,
# figure out which one is present and use that. For runtime, it's a
# mess, as really the user must take responsibility for setting their
# locale properly. I'm thinking of abandoning Click and going back to
# twisted.python.usage to avoid this problem in the future.
(out, err, rc) = yield getProcessOutputAndValue("locale", ["-a"])
if rc != 0:
log.msg("error running 'locale -a', rc=%s" % (rc, ))
log.msg("stderr: %s" % (err, ))
returnValue(None)
out = out.decode("utf-8") # make sure we get a string
utf8_locales = {}
for locale in out.splitlines():
locale = locale.strip()
if locale.lower().endswith((".utf-8", ".utf8")):
utf8_locales[locale.lower()] = locale
for wanted in ["C.utf8", "C.UTF-8", "en_US.utf8", "en_US.UTF-8"]:
if wanted.lower() in utf8_locales:
returnValue(utf8_locales[wanted.lower()])
if utf8_locales:
returnValue(list(utf8_locales.values())[0])
returnValue(None)
示例3: test_version
# 需要導入模塊: from twisted.internet import utils [as 別名]
# 或者: from twisted.internet.utils import getProcessOutputAndValue [as 別名]
def test_version(self):
# "wormhole" must be on the path, so e.g. "pip install -e ." in a
# virtualenv. This guards against an environment where the tests
# below might run the wrong executable.
self.maxDiff = None
wormhole = self.find_executable()
# we must pass on the environment so that "something" doesn't
# get sad about UTF8 vs. ascii encodings
out, err, rc = yield getProcessOutputAndValue(
wormhole, ["--version"], env=os.environ)
err = err.decode("utf-8")
if "DistributionNotFound" in err:
log.msg("stderr was %s" % err)
last = err.strip().split("\n")[-1]
self.fail("wormhole not runnable: %s" % last)
ver = out.decode("utf-8") or err
self.failUnlessEqual(ver.strip(),
"magic-wormhole {}".format(__version__))
self.failUnlessEqual(rc, 0)
示例4: call_and_check_rc
# 需要導入模塊: from twisted.internet import utils [as 別名]
# 或者: from twisted.internet.utils import getProcessOutputAndValue [as 別名]
def call_and_check_rc(cmd, *args):
def _check_rc(_args):
(stdout, stderr, rc) = _args
if rc != 0:
err = ExecError('RC %d: %s %s' % (rc, cmd, ' '.join(args)))
err.stdout = stdout.strip()
err.stderr = stderr.strip()
raise err
log.msg('# %s' % (' '.join((cmd,) + args),))
if stdout:
log.msg(stdout)
def _got_signal(f):
f.trap(tuple)
stdout, stderr, signum = f.value
err = ExecError('Got signal %d: %s %s' % (signum, cmd, ' '.join(args)))
err.stdout = stdout.strip()
err.stderr = stderr.strip()
raise err
return utils.getProcessOutputAndValue(cmd, args, env=os.environ).addCallbacks(_check_rc, _got_signal)
示例5: test_outputAndValue
# 需要導入模塊: from twisted.internet import utils [as 別名]
# 或者: from twisted.internet.utils import getProcessOutputAndValue [as 別名]
def test_outputAndValue(self):
"""
The L{Deferred} returned by L{getProcessOutputAndValue} fires with a
three-tuple, the elements of which give the data written to the child's
stdout, the data written to the child's stderr, and the exit status of
the child.
"""
exe = sys.executable
scriptFile = self.makeSourceFile([
"import sys",
"sys.stdout.write('hello world!\\n')",
"sys.stderr.write('goodbye world!\\n')",
"sys.exit(1)"
])
def gotOutputAndValue((out, err, code)):
self.assertEquals(out, "hello world!\n")
self.assertEquals(err, "goodbye world!" + os.linesep)
self.assertEquals(code, 1)
d = utils.getProcessOutputAndValue(self.exe, ["-u", scriptFile])
return d.addCallback(gotOutputAndValue)
示例6: _getBatchOutput
# 需要導入模塊: from twisted.internet import utils [as 別名]
# 或者: from twisted.internet.utils import getProcessOutputAndValue [as 別名]
def _getBatchOutput(self, f):
fn = tempfile.mktemp()
open(fn, 'w').write(f)
port = self.server.getHost().port
cmds = ('-p %i -l testuser '
'-K unix '
'-a '
'-v -b %s 127.0.0.1') % (port, fn)
cmds = test_conch._makeArgs(cmds.split(), mod='cftp')[1:]
log.msg('running %s %s' % (sys.executable, cmds))
env = os.environ.copy()
env['PYTHONPATH'] = os.pathsep.join(sys.path)
if hasattr(self.server.factory, 'proto'):
self.server.factory.proto.expectedLoseConnection = 1
d = getProcessOutputAndValue(sys.executable, cmds, env=env)
def cleanup(res):
os.remove(fn)
return res[0]
d.addBoth(cleanup)
return d
示例7: testOutputSignal
# 需要導入模塊: from twisted.internet import utils [as 別名]
# 或者: from twisted.internet.utils import getProcessOutputAndValue [as 別名]
def testOutputSignal(self):
# Use SIGKILL here because it's guaranteed to be delivered. Using
# SIGHUP might not work in, e.g., a buildbot slave run under the
# 'nohup' command.
exe = sys.executable
scriptFile = self.makeSourceFile([
"import sys, os, signal",
"sys.stdout.write('stdout bytes\\n')",
"sys.stderr.write('stderr bytes\\n')",
"sys.stdout.flush()",
"sys.stderr.flush()",
"os.kill(os.getpid(), signal.SIGKILL)"
])
def gotOutputAndValue(err):
(out, err, sig) = err.value # XXX Sigh wtf
self.assertEquals(out, "stdout bytes" + os.linesep)
self.assertEquals(err, "stderr bytes" + os.linesep)
self.assertEquals(sig, signal.SIGKILL)
d = utils.getProcessOutputAndValue(exe, ['-u', scriptFile])
return d.addErrback(gotOutputAndValue)
示例8: _catch_up
# 需要導入模塊: from twisted.internet import utils [as 別名]
# 或者: from twisted.internet.utils import getProcessOutputAndValue [as 別名]
def _catch_up(self, res):
if self.changeCount == 0:
log.msg('LLVMGitPoller: no changes, no catch_up')
return
log.msg('LLVMGitPoller: catching up tracking branch')
args = ['reset', '--hard', 'origin/%s' % (self.branch,)]
d = utils.getProcessOutputAndValue(self.gitbin, args, path=self.workdir, env=dict(PATH=os.environ['PATH']))
d.addCallback(self._convert_nonzero_to_failure)
return d
示例9: _convert_nonzero_to_failure
# 需要導入模塊: from twisted.internet import utils [as 別名]
# 或者: from twisted.internet.utils import getProcessOutputAndValue [as 別名]
def _convert_nonzero_to_failure(self, res):
"utility method to handle the result of getProcessOutputAndValue"
(stdout, stderr, code) = res
if code != 0:
raise EnvironmentError('command failed with exit code %d: %s' % (code, stderr))
return (stdout, stderr, code)
示例10: test_shebang
# 需要導入模塊: from twisted.internet import utils [as 別名]
# 或者: from twisted.internet.utils import getProcessOutputAndValue [as 別名]
def test_shebang(self):
"""
Spawning a process with an executable which is a script starting
with an interpreter definition line (#!) uses that interpreter to
evaluate the script.
"""
shebangOutput = b'this is the shebang output'
scriptFile = self.makeSourceFile([
"#!%s" % (pyExe.decode('ascii'),),
"import sys",
"sys.stdout.write('%s')" % (shebangOutput.decode('ascii'),),
"sys.stdout.flush()"])
os.chmod(scriptFile, 0o700)
reactor = self.buildReactor()
def cbProcessExited(args):
out, err, code = args
msg("cbProcessExited((%r, %r, %d))" % (out, err, code))
self.assertEqual(out, shebangOutput)
self.assertEqual(err, b"")
self.assertEqual(code, 0)
def shutdown(passthrough):
reactor.stop()
return passthrough
def start():
d = utils.getProcessOutputAndValue(scriptFile, reactor=reactor)
d.addBoth(shutdown)
d.addCallback(cbProcessExited)
d.addErrback(err)
reactor.callWhenRunning(start)
self.runReactor(reactor)
示例11: test_processCommandLineArguments
# 需要導入模塊: from twisted.internet import utils [as 別名]
# 或者: from twisted.internet.utils import getProcessOutputAndValue [as 別名]
def test_processCommandLineArguments(self):
"""
Arguments given to spawnProcess are passed to the child process as
originally intended.
"""
us = b"twisted.internet.test.process_cli"
args = [b'hello', b'"', b' \t|<>^&', br'"\\"hello\\"', br'"foo\ bar baz\""']
# Ensure that all non-NUL characters can be passed too.
if _PY3:
args.append("".join(map(chr, xrange(1,255))).encode("utf8"))
else:
args.append("".join(map(chr, xrange(1,255))))
reactor = self.buildReactor()
def processFinished(finishedArgs):
output, err, code = finishedArgs
output = output.split(b'\0')
# Drop the trailing \0.
output.pop()
self.assertEqual(args, output)
def shutdown(result):
reactor.stop()
return result
def spawnChild():
d = succeed(None)
d.addCallback(lambda dummy: utils.getProcessOutputAndValue(
pyExe, [b"-m", us] + args, env=properEnv,
reactor=reactor))
d.addCallback(processFinished)
d.addBoth(shutdown)
reactor.callWhenRunning(spawnChild)
self.runReactor(reactor)
示例12: _getBatchOutput
# 需要導入模塊: from twisted.internet import utils [as 別名]
# 或者: from twisted.internet.utils import getProcessOutputAndValue [as 別名]
def _getBatchOutput(self, f):
fn = self.mktemp()
with open(fn, 'w') as fp:
fp.write(f)
port = self.server.getHost().port
cmds = ('-p %i -l testuser '
'--known-hosts kh_test '
'--user-authentications publickey '
'--host-key-algorithms ssh-rsa '
'-i dsa_test '
'-a '
'-v -b %s 127.0.0.1') % (port, fn)
cmds = test_conch._makeArgs(cmds.split(), mod='cftp')[1:]
log.msg('running %s %s' % (sys.executable, cmds))
env = os.environ.copy()
env['PYTHONPATH'] = os.pathsep.join(sys.path)
self.server.factory.expectedLoseConnection = 1
d = getProcessOutputAndValue(sys.executable, cmds, env=env)
def _cleanup(res):
os.remove(fn)
return res
d.addCallback(lambda res: res[0])
d.addBoth(_cleanup)
return d
示例13: test_outputAndValue
# 需要導入模塊: from twisted.internet import utils [as 別名]
# 或者: from twisted.internet.utils import getProcessOutputAndValue [as 別名]
def test_outputAndValue(self):
"""
The L{Deferred} returned by L{getProcessOutputAndValue} fires with a
three-tuple, the elements of which give the data written to the child's
stdout, the data written to the child's stderr, and the exit status of
the child.
"""
scriptFile = self.makeSourceFile([
"import sys",
"if hasattr(sys.stdout, 'buffer'):",
" # Python 3",
" sys.stdout.buffer.write(b'hello world!\\n')",
" sys.stderr.buffer.write(b'goodbye world!\\n')",
"else:",
" # Python 2",
" sys.stdout.write(b'hello world!\\n')",
" sys.stderr.write(b'goodbye world!\\n')",
"sys.exit(1)"
])
def gotOutputAndValue(out_err_code):
out, err, code = out_err_code
self.assertEqual(out, b"hello world!\n")
if _PY3:
self.assertEqual(err, b"goodbye world!\n")
else:
self.assertEqual(err, b"goodbye world!" +
os.linesep)
self.assertEqual(code, 1)
d = utils.getProcessOutputAndValue(self.exe, ["-u", scriptFile])
return d.addCallback(gotOutputAndValue)
示例14: test_getProcessOutputAndValuePath
# 需要導入模塊: from twisted.internet import utils [as 別名]
# 或者: from twisted.internet.utils import getProcessOutputAndValue [as 別名]
def test_getProcessOutputAndValuePath(self):
"""
L{getProcessOutputAndValue} runs the given command with the working
directory given by the C{path} parameter.
"""
def check(out_err_status, dir):
out, err, status = out_err_status
self.assertEqual(out, dir)
self.assertEqual(status, 0)
return self._pathTest(utils.getProcessOutputAndValue, check)
示例15: test_getProcessOutputAndValueDefaultPath
# 需要導入模塊: from twisted.internet import utils [as 別名]
# 或者: from twisted.internet.utils import getProcessOutputAndValue [as 別名]
def test_getProcessOutputAndValueDefaultPath(self):
"""
If no value is supplied for the C{path} parameter,
L{getProcessOutputAndValue} runs the given command in the same working
directory as the parent process and succeeds even if the current
working directory is not accessible.
"""
def check(out_err_status, dir):
out, err, status = out_err_status
self.assertEqual(out, dir)
self.assertEqual(status, 0)
return self._defaultPathTest(
utils.getProcessOutputAndValue, check)