本文整理汇总了Python中cpopen.CPopen类的典型用法代码示例。如果您正苦于以下问题:Python CPopen类的具体用法?Python CPopen怎么用?Python CPopen使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CPopen类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: execCmd
def execCmd(command, sudo=False, cwd=None, data=None, raw=False,
printable=None, env=None, sync=True, nice=None, ioclass=None,
ioclassdata=None, setsid=False, execCmdLogger=logging.root,
deathSignal=0, childUmask=None):
"""
Executes an external command, optionally via sudo.
IMPORTANT NOTE: the new process would receive `deathSignal` when the
controlling thread dies, which may not be what you intended: if you create
a temporary thread, spawn a sync=False sub-process, and have the thread
finish, the new subprocess would die immediately.
"""
if ioclass is not None:
command = cmdutils.ionice(command, ioclass=ioclass,
ioclassdata=ioclassdata)
if nice is not None:
command = cmdutils.nice(command, nice=nice)
if setsid:
command = cmdutils.setsid(command)
if sudo:
command = cmdutils.sudo(command)
# Unsubscriptable objects (e.g. generators) need conversion
if not callable(getattr(command, '__getitem__', None)):
command = tuple(command)
if not printable:
printable = command
execCmdLogger.debug("%s (cwd %s)", _list2cmdline(printable), cwd)
p = CPopen(command, close_fds=True, cwd=cwd, env=env,
deathSignal=deathSignal, childUmask=childUmask)
if not sync:
p = AsyncProc(p)
if data is not None:
p.stdin.write(data)
p.stdin.flush()
return p
(out, err) = p.communicate(data)
if out is None:
# Prevent splitlines() from barfing later on
out = ""
execCmdLogger.debug("%s: <err> = %r; <rc> = %d",
"SUCCESS" if p.returncode == 0 else "FAILED",
err, p.returncode)
if not raw:
out = out.splitlines(False)
err = err.splitlines(False)
return p.returncode, out, err
示例2: execCmd
def execCmd(command, sudo=False, cwd=None, data=None, raw=False, logErr=True,
printable=None, env=None, sync=True, nice=None, ioclass=None,
ioclassdata=None, setsid=False, execCmdLogger=logging.root,
deathSignal=0, childUmask=None):
"""
Executes an external command, optionally via sudo.
IMPORTANT NOTE: the new process would receive `deathSignal` when the
controlling thread dies, which may not be what you intended: if you create
a temporary thread, spawn a sync=False sub-process, and have the thread
finish, the new subprocess would die immediately.
"""
if ioclass is not None:
cmd = command
command = [ioniceCmdPath.cmd, '-c', str(ioclass)]
if ioclassdata is not None:
command.extend(("-n", str(ioclassdata)))
command = command + cmd
if nice is not None:
command = [niceCmdPath.cmd, '-n', str(nice)] + command
if setsid:
command = [setsidCmdPath.cmd] + command
if sudo:
command = [sudoCmdPath.cmd, SUDO_NON_INTERACTIVE_FLAG] + command
if not printable:
printable = command
cmdline = repr(subprocess.list2cmdline(printable))
execCmdLogger.debug("%s (cwd %s)", cmdline, cwd)
p = CPopen(command, close_fds=True, cwd=cwd, env=env,
deathSignal=deathSignal, childUmask=childUmask)
p = AsyncProc(p)
if not sync:
if data is not None:
p.stdin.write(data)
p.stdin.flush()
return p
(out, err) = p.communicate(data)
if out is None:
# Prevent splitlines() from barfing later on
out = ""
execCmdLogger.debug("%s: <err> = %s; <rc> = %d",
{True: "SUCCESS", False: "FAILED"}[p.returncode == 0],
repr(err), p.returncode)
if not raw:
out = out.splitlines(False)
err = err.splitlines(False)
return (p.returncode, out, err)
示例3: _subTest
def _subTest(self, name, params, *args, **kwargs):
p = CPopen(["python", EXT_HELPER, name] + params,
*args, **kwargs)
p.wait()
self.assertTrue(p.returncode == 0,
"Process failed: %s" % os.strerror(p.returncode))
self.assertEquals(p.stdout.read().strip(), "True")
示例4: testCwd
def testCwd(self):
cwd = "/proc"
p = CPopen(["python", "-c", "import os; print os.getcwd()"], cwd=cwd)
p.wait()
self.assertTrue(p.returncode == 0,
"Process failed: %s" % os.strerror(p.returncode))
self.assertEquals(p.stdout.read().strip(), cwd)
示例5: testEcho
def testEcho(self):
data = "Hello"
p = CPopen([EXT_ECHO, "-n", data])
out, err = p.communicate()
self.assertTrue(p.returncode == 0,
"Process failed: %s" % os.strerror(p.returncode))
self.assertEquals(out, data)
示例6: testNoStreams
def testNoStreams(self):
p = CPopen(['true'], stdin=None, stdout=None, stderr=None)
self.assertEqual(p.stdin, None)
self.assertEqual(p.stdout, None)
self.assertEqual(p.stderr, None)
p.wait()
self.assertEquals(p.returncode, 0)
示例7: testEcho
def testEcho(self):
data = "Hello"
p = CPopen([EXT_ECHO, "-n", data])
p.wait()
self.assertTrue(p.returncode == 0,
"Process failed: %s" % os.strerror(p.returncode))
self.assertEquals(p.stdout.read(), data)
示例8: testUserFileWithRedirect
def testUserFileWithRedirect(self):
data = "it works!"
with tempfile.TemporaryFile() as f:
p = CPopen(["echo", "-n", data], stdout=f, stderr=f)
p.wait()
f.seek(0)
self.assertEqual(data, f.read())
示例9: testCat
def testCat(self):
path = "/etc/passwd"
p = CPopen(["cat", path])
p.wait()
self.assertTrue(p.returncode == 0,
"Process failed: %s" % os.strerror(p.returncode))
with open(path, "r") as f:
self.assertEquals(p.stdout.read(), f.read())
示例10: testStdin
def testStdin(self):
data = "Hello World"
p = CPopen(["cat"])
out, err = p.communicate(data)
self.assertTrue(p.returncode == 0,
"Process failed: %s" % os.strerror(p.returncode))
self.assertEquals(out, data)
示例11: testCloseOnExecStderr
def testCloseOnExecStderr(self):
# Unset close-on-exec on stderr fd
with tempfile.NamedTemporaryFile() as f:
set_close_on_exec(f.fileno())
p = CPopen(["sh", "-c", "echo -n data >&2"], stderr=f.fileno())
p.wait()
f.seek(0)
self.assertEqual(f.read(), "data")
示例12: testCwd
def testCwd(self):
cwd = "/proc"
p = CPopen(["python", "-c", "import os; print os.getcwd()"],
cwd=cwd)
out, err = p.communicate()
self.assertTrue(p.returncode == 0,
"Process failed: %s" % os.strerror(p.returncode))
self.assertEquals(out.strip(), cwd)
示例13: testCat
def testCat(self):
path = "/etc/passwd"
p = CPopen(["cat", path])
out, err = p.communicate()
self.assertTrue(p.returncode == 0,
"Process failed: %s" % os.strerror(p.returncode))
with open(path, "r") as f:
self.assertEquals(out, f.read())
示例14: execCmd
def execCmd(command, sudo=False, cwd=None, data=None, raw=False, logErr=True,
printable=None, env=None, sync=True, nice=None, ioclass=None,
ioclassdata=None, setsid=False, execCmdLogger=logging.root,
deathSignal=0):
"""
Executes an external command, optionally via sudo.
IMPORTANT NOTE: don't define a deathSignal when sync=False
"""
if ioclass is not None:
cmd = command
command = [constants.EXT_IONICE, '-c', str(ioclass)]
if ioclassdata is not None:
command.extend(("-n", str(ioclassdata)))
command = command + cmd
if nice is not None:
command = [constants.EXT_NICE, '-n', str(nice)] + command
if setsid:
command = [constants.EXT_SETSID] + command
if sudo:
command = [constants.EXT_SUDO, SUDO_NON_INTERACTIVE_FLAG] + command
if not printable:
printable = command
cmdline = repr(subprocess.list2cmdline(printable))
execCmdLogger.debug("%s (cwd %s)", cmdline, cwd)
p = CPopen(command, close_fds=True, cwd=cwd, env=env,
deathSignal=deathSignal)
p = AsyncProc(p)
if not sync:
if data is not None:
p.stdin.write(data)
p.stdin.flush()
return p
(out, err) = p.communicate(data)
if out is None:
# Prevent splitlines() from barfing later on
out = ""
execCmdLogger.debug("%s: <err> = %s; <rc> = %d",
{True: "SUCCESS", False: "FAILED"}[p.returncode == 0],
repr(err), p.returncode)
if not raw:
out = out.splitlines(False)
err = err.splitlines(False)
return (p.returncode, out, err)
示例15: testCloseFDs
def testCloseFDs(self):
with open("/dev/zero") as f:
p = CPopen(["sleep", "1"], close_fds=True)
try:
child_fds = set(os.listdir("/proc/%s/fd" % p.pid))
finally:
p.kill()
p.wait()
self.assertEqual(child_fds, set(["0", "1", "2"]))