Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2....
File: popen2.py
"""Spawn a command with pipes to its stdin, stdout, and optionally stderr.
[0] Fix | Delete
[1] Fix | Delete
The normal os.popen(cmd, mode) call spawns a shell command and provides a
[2] Fix | Delete
file interface to just the input or output of the process depending on
[3] Fix | Delete
whether mode is 'r' or 'w'. This module provides the functions popen2(cmd)
[4] Fix | Delete
and popen3(cmd) which return two or three pipes to the spawned command.
[5] Fix | Delete
"""
[6] Fix | Delete
[7] Fix | Delete
import os
[8] Fix | Delete
import sys
[9] Fix | Delete
import warnings
[10] Fix | Delete
warnings.warn("The popen2 module is deprecated. Use the subprocess module.",
[11] Fix | Delete
DeprecationWarning, stacklevel=2)
[12] Fix | Delete
[13] Fix | Delete
__all__ = ["popen2", "popen3", "popen4"]
[14] Fix | Delete
[15] Fix | Delete
try:
[16] Fix | Delete
MAXFD = os.sysconf('SC_OPEN_MAX')
[17] Fix | Delete
except (AttributeError, ValueError):
[18] Fix | Delete
MAXFD = 256
[19] Fix | Delete
[20] Fix | Delete
_active = []
[21] Fix | Delete
[22] Fix | Delete
def _cleanup():
[23] Fix | Delete
for inst in _active[:]:
[24] Fix | Delete
if inst.poll(_deadstate=sys.maxint) >= 0:
[25] Fix | Delete
try:
[26] Fix | Delete
_active.remove(inst)
[27] Fix | Delete
except ValueError:
[28] Fix | Delete
# This can happen if two threads create a new Popen instance.
[29] Fix | Delete
# It's harmless that it was already removed, so ignore.
[30] Fix | Delete
pass
[31] Fix | Delete
[32] Fix | Delete
class Popen3:
[33] Fix | Delete
"""Class representing a child process. Normally, instances are created
[34] Fix | Delete
internally by the functions popen2() and popen3()."""
[35] Fix | Delete
[36] Fix | Delete
sts = -1 # Child not completed yet
[37] Fix | Delete
[38] Fix | Delete
def __init__(self, cmd, capturestderr=False, bufsize=-1):
[39] Fix | Delete
"""The parameter 'cmd' is the shell command to execute in a
[40] Fix | Delete
sub-process. On UNIX, 'cmd' may be a sequence, in which case arguments
[41] Fix | Delete
will be passed directly to the program without shell intervention (as
[42] Fix | Delete
with os.spawnv()). If 'cmd' is a string it will be passed to the shell
[43] Fix | Delete
(as with os.system()). The 'capturestderr' flag, if true, specifies
[44] Fix | Delete
that the object should capture standard error output of the child
[45] Fix | Delete
process. The default is false. If the 'bufsize' parameter is
[46] Fix | Delete
specified, it specifies the size of the I/O buffers to/from the child
[47] Fix | Delete
process."""
[48] Fix | Delete
_cleanup()
[49] Fix | Delete
self.cmd = cmd
[50] Fix | Delete
p2cread, p2cwrite = os.pipe()
[51] Fix | Delete
c2pread, c2pwrite = os.pipe()
[52] Fix | Delete
if capturestderr:
[53] Fix | Delete
errout, errin = os.pipe()
[54] Fix | Delete
self.pid = os.fork()
[55] Fix | Delete
if self.pid == 0:
[56] Fix | Delete
# Child
[57] Fix | Delete
os.dup2(p2cread, 0)
[58] Fix | Delete
os.dup2(c2pwrite, 1)
[59] Fix | Delete
if capturestderr:
[60] Fix | Delete
os.dup2(errin, 2)
[61] Fix | Delete
self._run_child(cmd)
[62] Fix | Delete
os.close(p2cread)
[63] Fix | Delete
self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
[64] Fix | Delete
os.close(c2pwrite)
[65] Fix | Delete
self.fromchild = os.fdopen(c2pread, 'r', bufsize)
[66] Fix | Delete
if capturestderr:
[67] Fix | Delete
os.close(errin)
[68] Fix | Delete
self.childerr = os.fdopen(errout, 'r', bufsize)
[69] Fix | Delete
else:
[70] Fix | Delete
self.childerr = None
[71] Fix | Delete
[72] Fix | Delete
def __del__(self):
[73] Fix | Delete
# In case the child hasn't been waited on, check if it's done.
[74] Fix | Delete
self.poll(_deadstate=sys.maxint)
[75] Fix | Delete
if self.sts < 0:
[76] Fix | Delete
if _active is not None:
[77] Fix | Delete
# Child is still running, keep us alive until we can wait on it.
[78] Fix | Delete
_active.append(self)
[79] Fix | Delete
[80] Fix | Delete
def _run_child(self, cmd):
[81] Fix | Delete
if isinstance(cmd, basestring):
[82] Fix | Delete
cmd = ['/bin/sh', '-c', cmd]
[83] Fix | Delete
os.closerange(3, MAXFD)
[84] Fix | Delete
try:
[85] Fix | Delete
os.execvp(cmd[0], cmd)
[86] Fix | Delete
finally:
[87] Fix | Delete
os._exit(1)
[88] Fix | Delete
[89] Fix | Delete
def poll(self, _deadstate=None):
[90] Fix | Delete
"""Return the exit status of the child process if it has finished,
[91] Fix | Delete
or -1 if it hasn't finished yet."""
[92] Fix | Delete
if self.sts < 0:
[93] Fix | Delete
try:
[94] Fix | Delete
pid, sts = os.waitpid(self.pid, os.WNOHANG)
[95] Fix | Delete
# pid will be 0 if self.pid hasn't terminated
[96] Fix | Delete
if pid == self.pid:
[97] Fix | Delete
self.sts = sts
[98] Fix | Delete
except os.error:
[99] Fix | Delete
if _deadstate is not None:
[100] Fix | Delete
self.sts = _deadstate
[101] Fix | Delete
return self.sts
[102] Fix | Delete
[103] Fix | Delete
def wait(self):
[104] Fix | Delete
"""Wait for and return the exit status of the child process."""
[105] Fix | Delete
if self.sts < 0:
[106] Fix | Delete
pid, sts = os.waitpid(self.pid, 0)
[107] Fix | Delete
# This used to be a test, but it is believed to be
[108] Fix | Delete
# always true, so I changed it to an assertion - mvl
[109] Fix | Delete
assert pid == self.pid
[110] Fix | Delete
self.sts = sts
[111] Fix | Delete
return self.sts
[112] Fix | Delete
[113] Fix | Delete
[114] Fix | Delete
class Popen4(Popen3):
[115] Fix | Delete
childerr = None
[116] Fix | Delete
[117] Fix | Delete
def __init__(self, cmd, bufsize=-1):
[118] Fix | Delete
_cleanup()
[119] Fix | Delete
self.cmd = cmd
[120] Fix | Delete
p2cread, p2cwrite = os.pipe()
[121] Fix | Delete
c2pread, c2pwrite = os.pipe()
[122] Fix | Delete
self.pid = os.fork()
[123] Fix | Delete
if self.pid == 0:
[124] Fix | Delete
# Child
[125] Fix | Delete
os.dup2(p2cread, 0)
[126] Fix | Delete
os.dup2(c2pwrite, 1)
[127] Fix | Delete
os.dup2(c2pwrite, 2)
[128] Fix | Delete
self._run_child(cmd)
[129] Fix | Delete
os.close(p2cread)
[130] Fix | Delete
self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
[131] Fix | Delete
os.close(c2pwrite)
[132] Fix | Delete
self.fromchild = os.fdopen(c2pread, 'r', bufsize)
[133] Fix | Delete
[134] Fix | Delete
[135] Fix | Delete
if sys.platform[:3] == "win" or sys.platform == "os2emx":
[136] Fix | Delete
# Some things don't make sense on non-Unix platforms.
[137] Fix | Delete
del Popen3, Popen4
[138] Fix | Delete
[139] Fix | Delete
def popen2(cmd, bufsize=-1, mode='t'):
[140] Fix | Delete
"""Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
[141] Fix | Delete
be a sequence, in which case arguments will be passed directly to the
[142] Fix | Delete
program without shell intervention (as with os.spawnv()). If 'cmd' is a
[143] Fix | Delete
string it will be passed to the shell (as with os.system()). If
[144] Fix | Delete
'bufsize' is specified, it sets the buffer size for the I/O pipes. The
[145] Fix | Delete
file objects (child_stdout, child_stdin) are returned."""
[146] Fix | Delete
w, r = os.popen2(cmd, mode, bufsize)
[147] Fix | Delete
return r, w
[148] Fix | Delete
[149] Fix | Delete
def popen3(cmd, bufsize=-1, mode='t'):
[150] Fix | Delete
"""Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
[151] Fix | Delete
be a sequence, in which case arguments will be passed directly to the
[152] Fix | Delete
program without shell intervention (as with os.spawnv()). If 'cmd' is a
[153] Fix | Delete
string it will be passed to the shell (as with os.system()). If
[154] Fix | Delete
'bufsize' is specified, it sets the buffer size for the I/O pipes. The
[155] Fix | Delete
file objects (child_stdout, child_stdin, child_stderr) are returned."""
[156] Fix | Delete
w, r, e = os.popen3(cmd, mode, bufsize)
[157] Fix | Delete
return r, w, e
[158] Fix | Delete
[159] Fix | Delete
def popen4(cmd, bufsize=-1, mode='t'):
[160] Fix | Delete
"""Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
[161] Fix | Delete
be a sequence, in which case arguments will be passed directly to the
[162] Fix | Delete
program without shell intervention (as with os.spawnv()). If 'cmd' is a
[163] Fix | Delete
string it will be passed to the shell (as with os.system()). If
[164] Fix | Delete
'bufsize' is specified, it sets the buffer size for the I/O pipes. The
[165] Fix | Delete
file objects (child_stdout_stderr, child_stdin) are returned."""
[166] Fix | Delete
w, r = os.popen4(cmd, mode, bufsize)
[167] Fix | Delete
return r, w
[168] Fix | Delete
else:
[169] Fix | Delete
def popen2(cmd, bufsize=-1, mode='t'):
[170] Fix | Delete
"""Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
[171] Fix | Delete
be a sequence, in which case arguments will be passed directly to the
[172] Fix | Delete
program without shell intervention (as with os.spawnv()). If 'cmd' is a
[173] Fix | Delete
string it will be passed to the shell (as with os.system()). If
[174] Fix | Delete
'bufsize' is specified, it sets the buffer size for the I/O pipes. The
[175] Fix | Delete
file objects (child_stdout, child_stdin) are returned."""
[176] Fix | Delete
inst = Popen3(cmd, False, bufsize)
[177] Fix | Delete
return inst.fromchild, inst.tochild
[178] Fix | Delete
[179] Fix | Delete
def popen3(cmd, bufsize=-1, mode='t'):
[180] Fix | Delete
"""Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
[181] Fix | Delete
be a sequence, in which case arguments will be passed directly to the
[182] Fix | Delete
program without shell intervention (as with os.spawnv()). If 'cmd' is a
[183] Fix | Delete
string it will be passed to the shell (as with os.system()). If
[184] Fix | Delete
'bufsize' is specified, it sets the buffer size for the I/O pipes. The
[185] Fix | Delete
file objects (child_stdout, child_stdin, child_stderr) are returned."""
[186] Fix | Delete
inst = Popen3(cmd, True, bufsize)
[187] Fix | Delete
return inst.fromchild, inst.tochild, inst.childerr
[188] Fix | Delete
[189] Fix | Delete
def popen4(cmd, bufsize=-1, mode='t'):
[190] Fix | Delete
"""Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
[191] Fix | Delete
be a sequence, in which case arguments will be passed directly to the
[192] Fix | Delete
program without shell intervention (as with os.spawnv()). If 'cmd' is a
[193] Fix | Delete
string it will be passed to the shell (as with os.system()). If
[194] Fix | Delete
'bufsize' is specified, it sets the buffer size for the I/O pipes. The
[195] Fix | Delete
file objects (child_stdout_stderr, child_stdin) are returned."""
[196] Fix | Delete
inst = Popen4(cmd, bufsize)
[197] Fix | Delete
return inst.fromchild, inst.tochild
[198] Fix | Delete
[199] Fix | Delete
__all__.extend(["Popen3", "Popen4"])
[200] Fix | Delete
[201] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function