mirror of
https://github.com/clearml/wexpect-venv
synced 2025-06-26 18:15:52 +00:00
[ADD] First working version on python-3.x
This commit is contained in:
commit
257e9de8b4
@ -1,6 +1,14 @@
|
|||||||
# A simple example code for wexpect written in python-2.7
|
# A simple example code for wexpect written in python-2.7
|
||||||
|
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
here = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
wexpectPath = os.path.dirname(here)
|
||||||
|
sys.path.insert(0, wexpectPath)
|
||||||
|
|
||||||
import wexpect
|
import wexpect
|
||||||
|
|
||||||
# Path of cmd executable:
|
# Path of cmd executable:
|
||||||
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
pywin32>=220
|
14
setup.cfg
14
setup.cfg
@ -1,8 +1,8 @@
|
|||||||
[metadata]
|
[metadata]
|
||||||
name = wexpect
|
name = wexpect
|
||||||
author = Noah Spurrier, Richard Holden, Marco Molteni, Kimberley Burchett, Robert Stone, Hartmut Goebel, Chad Schroeder, Erick Tryzelaar, Dave Kirby, Ids vander Molen, George Todd, Noel Taylor, Nicolas D. Cesar, Alexander Gattin, Geoffrey Marshall, Francisco Lourenco, Glen Mabey, Karthik Gurusamy, Fernando Perez, Corey Minyard, Jon Cohen, Guillaume Chazarain, Andrew Ryan, Nick Craig-Wood, Andrew Stone, Jorgen Grahn, Benedek Racz
|
author = Noah Spurrier, Richard Holden, Marco Molteni, Kimberley Burchett, Robert Stone, Hartmut Goebel, Chad Schroeder, Erick Tryzelaar, Dave Kirby, Ids vander Molen, George Todd, Noel Taylor, Nicolas D. Cesar, Alexander Gattin, Geoffrey Marshall, Francisco Lourenco, Glen Mabey, Karthik Gurusamy, Fernando Perez, Corey Minyard, Jon Cohen, Guillaume Chazarain, Andrew Ryan, Nick Craig-Wood, Andrew Stone, Jorgen Grahn, Benedek Racz
|
||||||
author-email = betontalpfa@gmail.com
|
author-email = betontalpfa@gmail.com
|
||||||
summary = Windows alternative of pexpect
|
summary = Windows alternative of pexpect
|
||||||
description-file = README.md
|
description-file = README.md
|
||||||
description-content-type = text/markdown; charset=UTF-8
|
description-content-type = text/markdown; charset=UTF-8
|
||||||
requires-python = >=2.6
|
requires-python = >=2.6
|
||||||
@ -15,7 +15,11 @@ classifier =
|
|||||||
Intended Audience :: Developers
|
Intended Audience :: Developers
|
||||||
Intended Audience :: Information Technology
|
Intended Audience :: Information Technology
|
||||||
Operating System :: Microsoft :: Windows
|
Operating System :: Microsoft :: Windows
|
||||||
Programming Language :: Python
|
Programming Language :: Python :: 3.3
|
||||||
|
Programming Language :: Python :: 3.4
|
||||||
|
Programming Language :: Python :: 3.5
|
||||||
|
Programming Language :: Python :: 3.6
|
||||||
|
Programming Language :: Python :: 3.7
|
||||||
keywords =
|
keywords =
|
||||||
scripting, automation, expect, pexpect, wexpect
|
scripting, automation, expect, pexpect, wexpect
|
||||||
|
|
||||||
|
2
setup.py
2
setup.py
@ -6,6 +6,6 @@ setup(
|
|||||||
setup_requires=['pbr'],
|
setup_requires=['pbr'],
|
||||||
pbr=True,
|
pbr=True,
|
||||||
|
|
||||||
packages=[''],
|
# packages=[''],
|
||||||
py_modules=['wexpect'],
|
py_modules=['wexpect'],
|
||||||
)
|
)
|
128
wexpect.py
128
wexpect.py
@ -83,7 +83,7 @@ try:
|
|||||||
import resource
|
import resource
|
||||||
import fcntl
|
import fcntl
|
||||||
else:
|
else:
|
||||||
from StringIO import StringIO
|
from io import StringIO
|
||||||
try:
|
try:
|
||||||
from ctypes import windll
|
from ctypes import windll
|
||||||
import pywintypes
|
import pywintypes
|
||||||
@ -96,11 +96,11 @@ try:
|
|||||||
import win32api
|
import win32api
|
||||||
import win32file
|
import win32file
|
||||||
import winerror
|
import winerror
|
||||||
except ImportError, e:
|
except ImportError as e:
|
||||||
raise ImportError(str(e) + "\nThis package requires the win32 python packages.")
|
raise ImportError(str(e) + "\nThis package requires the win32 python packages.")
|
||||||
screenbufferfillchar = u'\4'
|
screenbufferfillchar = '\4'
|
||||||
maxconsoleY = 8000
|
maxconsoleY = 8000
|
||||||
except ImportError, e:
|
except ImportError as e:
|
||||||
raise ImportError (str(e) + """
|
raise ImportError (str(e) + """
|
||||||
|
|
||||||
A critical module was not found. Probably this operating system does not
|
A critical module was not found. Probably this operating system does not
|
||||||
@ -233,8 +233,8 @@ def run (command, timeout=-1, withexitstatus=False, events=None, extra_args=None
|
|||||||
else:
|
else:
|
||||||
child = spawn(command, timeout=timeout, maxread=2000, logfile=logfile, cwd=cwd, env=env)
|
child = spawn(command, timeout=timeout, maxread=2000, logfile=logfile, cwd=cwd, env=env)
|
||||||
if events is not None:
|
if events is not None:
|
||||||
patterns = events.keys()
|
patterns = list(events.keys())
|
||||||
responses = events.values()
|
responses = list(events.values())
|
||||||
else:
|
else:
|
||||||
patterns=None # We assume that EOF or TIMEOUT will save us.
|
patterns=None # We assume that EOF or TIMEOUT will save us.
|
||||||
responses=None
|
responses=None
|
||||||
@ -243,26 +243,26 @@ def run (command, timeout=-1, withexitstatus=False, events=None, extra_args=None
|
|||||||
while 1:
|
while 1:
|
||||||
try:
|
try:
|
||||||
index = child.expect (patterns)
|
index = child.expect (patterns)
|
||||||
if type(child.after) in types.StringTypes:
|
if type(child.after) in (str,):
|
||||||
child_result_list.append(child.before + child.after)
|
child_result_list.append(child.before + child.after)
|
||||||
else: # child.after may have been a TIMEOUT or EOF, so don't cat those.
|
else: # child.after may have been a TIMEOUT or EOF, so don't cat those.
|
||||||
child_result_list.append(child.before)
|
child_result_list.append(child.before)
|
||||||
if type(responses[index]) in types.StringTypes:
|
if type(responses[index]) in (str,):
|
||||||
child.send(responses[index])
|
child.send(responses[index])
|
||||||
elif type(responses[index]) is types.FunctionType:
|
elif type(responses[index]) is types.FunctionType:
|
||||||
callback_result = responses[index](locals())
|
callback_result = responses[index](locals())
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
if type(callback_result) in types.StringTypes:
|
if type(callback_result) in (str,):
|
||||||
child.send(callback_result)
|
child.send(callback_result)
|
||||||
elif callback_result:
|
elif callback_result:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
raise TypeError ('The callback must be a string or function type.')
|
raise TypeError ('The callback must be a string or function type.')
|
||||||
event_count = event_count + 1
|
event_count = event_count + 1
|
||||||
except TIMEOUT, e:
|
except TIMEOUT as e:
|
||||||
child_result_list.append(child.before)
|
child_result_list.append(child.before)
|
||||||
break
|
break
|
||||||
except EOF, e:
|
except EOF as e:
|
||||||
child_result_list.append(child.before)
|
child_result_list.append(child.before)
|
||||||
break
|
break
|
||||||
child_result = ''.join(child_result_list)
|
child_result = ''.join(child_result_list)
|
||||||
@ -443,7 +443,7 @@ class spawn_unix (object):
|
|||||||
self.name = '<' + repr(self) + '>' # File-like object.
|
self.name = '<' + repr(self) + '>' # File-like object.
|
||||||
self.encoding = None # File-like object.
|
self.encoding = None # File-like object.
|
||||||
self.closed = True # File-like object.
|
self.closed = True # File-like object.
|
||||||
self.ocwd = os.getcwdu()
|
self.ocwd = os.getcwd()
|
||||||
self.cwd = cwd
|
self.cwd = cwd
|
||||||
self.env = env
|
self.env = env
|
||||||
self.__irix_hack = (sys.platform.lower().find('irix')>=0) # This flags if we are running on irix
|
self.__irix_hack = (sys.platform.lower().find('irix')>=0) # This flags if we are running on irix
|
||||||
@ -559,7 +559,7 @@ class spawn_unix (object):
|
|||||||
if self.use_native_pty_fork:
|
if self.use_native_pty_fork:
|
||||||
try:
|
try:
|
||||||
self.pid, self.child_fd = pty.fork()
|
self.pid, self.child_fd = pty.fork()
|
||||||
except OSError, e:
|
except OSError as e:
|
||||||
raise ExceptionPexpect('Error! pty.fork() failed: ' + str(e))
|
raise ExceptionPexpect('Error! pty.fork() failed: ' + str(e))
|
||||||
else: # Use internal __fork_pty
|
else: # Use internal __fork_pty
|
||||||
self.pid, self.child_fd = self.__fork_pty()
|
self.pid, self.child_fd = self.__fork_pty()
|
||||||
@ -619,11 +619,11 @@ class spawn_unix (object):
|
|||||||
|
|
||||||
parent_fd, child_fd = os.openpty()
|
parent_fd, child_fd = os.openpty()
|
||||||
if parent_fd < 0 or child_fd < 0:
|
if parent_fd < 0 or child_fd < 0:
|
||||||
raise ExceptionPexpect, "Error! Could not open pty with os.openpty()."
|
raise ExceptionPexpect("Error! Could not open pty with os.openpty().")
|
||||||
|
|
||||||
pid = os.fork()
|
pid = os.fork()
|
||||||
if pid < 0:
|
if pid < 0:
|
||||||
raise ExceptionPexpect, "Error! Failed os.fork()."
|
raise ExceptionPexpect("Error! Failed os.fork().")
|
||||||
elif pid == 0:
|
elif pid == 0:
|
||||||
# Child.
|
# Child.
|
||||||
os.close(parent_fd)
|
os.close(parent_fd)
|
||||||
@ -661,7 +661,7 @@ class spawn_unix (object):
|
|||||||
fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY);
|
fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY);
|
||||||
if fd >= 0:
|
if fd >= 0:
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
raise ExceptionPexpect, "Error! We are not disconnected from a controlling tty."
|
raise ExceptionPexpect("Error! We are not disconnected from a controlling tty.")
|
||||||
except:
|
except:
|
||||||
# Good! We are disconnected from a controlling tty.
|
# Good! We are disconnected from a controlling tty.
|
||||||
pass
|
pass
|
||||||
@ -669,14 +669,14 @@ class spawn_unix (object):
|
|||||||
# Verify we can open child pty.
|
# Verify we can open child pty.
|
||||||
fd = os.open(child_name, os.O_RDWR);
|
fd = os.open(child_name, os.O_RDWR);
|
||||||
if fd < 0:
|
if fd < 0:
|
||||||
raise ExceptionPexpect, "Error! Could not open child pty, " + child_name
|
raise ExceptionPexpect("Error! Could not open child pty, " + child_name)
|
||||||
else:
|
else:
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
|
|
||||||
# Verify we now have a controlling tty.
|
# Verify we now have a controlling tty.
|
||||||
fd = os.open("/dev/tty", os.O_WRONLY)
|
fd = os.open("/dev/tty", os.O_WRONLY)
|
||||||
if fd < 0:
|
if fd < 0:
|
||||||
raise ExceptionPexpect, "Error! Could not open controlling tty, /dev/tty"
|
raise ExceptionPexpect("Error! Could not open controlling tty, /dev/tty")
|
||||||
else:
|
else:
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
|
|
||||||
@ -864,7 +864,7 @@ class spawn_unix (object):
|
|||||||
if self.child_fd in r:
|
if self.child_fd in r:
|
||||||
try:
|
try:
|
||||||
s = os.read(self.child_fd, size)
|
s = os.read(self.child_fd, size)
|
||||||
except OSError, e: # Linux does this
|
except OSError as e: # Linux does this
|
||||||
self.flag_eof = True
|
self.flag_eof = True
|
||||||
raise EOF ('End Of File (EOF) in read_nonblocking(). Exception style platform.')
|
raise EOF ('End Of File (EOF) in read_nonblocking(). Exception style platform.')
|
||||||
if s == '': # BSD style
|
if s == '': # BSD style
|
||||||
@ -935,7 +935,7 @@ class spawn_unix (object):
|
|||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def next (self): # File-like object.
|
def __next__ (self): # File-like object.
|
||||||
|
|
||||||
"""This is to support iterators over a file-like object.
|
"""This is to support iterators over a file-like object.
|
||||||
"""
|
"""
|
||||||
@ -1109,7 +1109,7 @@ class spawn_unix (object):
|
|||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
return False
|
return False
|
||||||
except OSError, e:
|
except OSError as e:
|
||||||
# I think there are kernel timing issues that sometimes cause
|
# I think there are kernel timing issues that sometimes cause
|
||||||
# this to happen. I think isalive() reports True, but the
|
# this to happen. I think isalive() reports True, but the
|
||||||
# process is dead to the kernel.
|
# process is dead to the kernel.
|
||||||
@ -1168,7 +1168,7 @@ class spawn_unix (object):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
pid, status = os.waitpid(self.pid, waitpid_options)
|
pid, status = os.waitpid(self.pid, waitpid_options)
|
||||||
except OSError, e: # No child processes
|
except OSError as e: # No child processes
|
||||||
if e[0] == errno.ECHILD:
|
if e[0] == errno.ECHILD:
|
||||||
raise ExceptionPexpect ('isalive() encountered condition where "terminated" is 0, but there was no child process. Did someone else call waitpid() on our process?')
|
raise ExceptionPexpect ('isalive() encountered condition where "terminated" is 0, but there was no child process. Did someone else call waitpid() on our process?')
|
||||||
else:
|
else:
|
||||||
@ -1180,7 +1180,7 @@ class spawn_unix (object):
|
|||||||
if pid == 0:
|
if pid == 0:
|
||||||
try:
|
try:
|
||||||
pid, status = os.waitpid(self.pid, waitpid_options) ### os.WNOHANG) # Solaris!
|
pid, status = os.waitpid(self.pid, waitpid_options) ### os.WNOHANG) # Solaris!
|
||||||
except OSError, e: # This should never happen...
|
except OSError as e: # This should never happen...
|
||||||
if e[0] == errno.ECHILD:
|
if e[0] == errno.ECHILD:
|
||||||
raise ExceptionPexpect ('isalive() encountered condition that should never happen. There was no child process. Did someone else call waitpid() on our process?')
|
raise ExceptionPexpect ('isalive() encountered condition that should never happen. There was no child process. Did someone else call waitpid() on our process?')
|
||||||
else:
|
else:
|
||||||
@ -1247,7 +1247,7 @@ class spawn_unix (object):
|
|||||||
|
|
||||||
if patterns is None:
|
if patterns is None:
|
||||||
return []
|
return []
|
||||||
if type(patterns) is not types.ListType:
|
if type(patterns) is not list:
|
||||||
patterns = [patterns]
|
patterns = [patterns]
|
||||||
|
|
||||||
compile_flags = re.DOTALL # Allow dot to match \n
|
compile_flags = re.DOTALL # Allow dot to match \n
|
||||||
@ -1255,7 +1255,7 @@ class spawn_unix (object):
|
|||||||
compile_flags = compile_flags | re.IGNORECASE
|
compile_flags = compile_flags | re.IGNORECASE
|
||||||
compiled_pattern_list = []
|
compiled_pattern_list = []
|
||||||
for p in patterns:
|
for p in patterns:
|
||||||
if type(p) in types.StringTypes:
|
if type(p) in (str,):
|
||||||
compiled_pattern_list.append(re.compile(p, compile_flags))
|
compiled_pattern_list.append(re.compile(p, compile_flags))
|
||||||
elif p is EOF:
|
elif p is EOF:
|
||||||
compiled_pattern_list.append(EOF)
|
compiled_pattern_list.append(EOF)
|
||||||
@ -1376,7 +1376,7 @@ class spawn_unix (object):
|
|||||||
This method is also useful when you don't want to have to worry about
|
This method is also useful when you don't want to have to worry about
|
||||||
escaping regular expression characters that you want to match."""
|
escaping regular expression characters that you want to match."""
|
||||||
|
|
||||||
if type(pattern_list) in types.StringTypes or pattern_list in (TIMEOUT, EOF):
|
if type(pattern_list) in (str,) or pattern_list in (TIMEOUT, EOF):
|
||||||
pattern_list = [pattern_list]
|
pattern_list = [pattern_list]
|
||||||
return self.expect_loop(searcher_string(pattern_list), timeout, searchwindowsize)
|
return self.expect_loop(searcher_string(pattern_list), timeout, searchwindowsize)
|
||||||
|
|
||||||
@ -1413,13 +1413,14 @@ class spawn_unix (object):
|
|||||||
if timeout < 0 and timeout is not None:
|
if timeout < 0 and timeout is not None:
|
||||||
raise TIMEOUT ('Timeout exceeded in expect_any().')
|
raise TIMEOUT ('Timeout exceeded in expect_any().')
|
||||||
# Still have time left, so read more data
|
# Still have time left, so read more data
|
||||||
c = self.read_nonblocking (self.maxread, timeout)
|
c = self.read_nonblocking(self.maxread, timeout)
|
||||||
|
# c = c.decode("utf-8")
|
||||||
freshlen = len(c)
|
freshlen = len(c)
|
||||||
time.sleep (0.0001)
|
time.sleep (0.0001)
|
||||||
incoming = incoming + c
|
incoming += c
|
||||||
if timeout is not None:
|
if timeout is not None:
|
||||||
timeout = end_time - time.time()
|
timeout = end_time - time.time()
|
||||||
except EOF, e:
|
except EOF as e:
|
||||||
self.buffer = ''
|
self.buffer = ''
|
||||||
self.before = incoming
|
self.before = incoming
|
||||||
self.after = EOF
|
self.after = EOF
|
||||||
@ -1432,7 +1433,7 @@ class spawn_unix (object):
|
|||||||
self.match = None
|
self.match = None
|
||||||
self.match_index = None
|
self.match_index = None
|
||||||
raise EOF (str(e) + '\n' + str(self))
|
raise EOF (str(e) + '\n' + str(self))
|
||||||
except TIMEOUT, e:
|
except TIMEOUT as e:
|
||||||
self.buffer = incoming
|
self.buffer = incoming
|
||||||
self.before = incoming
|
self.before = incoming
|
||||||
self.after = TIMEOUT
|
self.after = TIMEOUT
|
||||||
@ -1457,7 +1458,7 @@ class spawn_unix (object):
|
|||||||
"""This returns the terminal window size of the child tty. The return
|
"""This returns the terminal window size of the child tty. The return
|
||||||
value is a tuple of (rows, cols). """
|
value is a tuple of (rows, cols). """
|
||||||
|
|
||||||
TIOCGWINSZ = getattr(termios, 'TIOCGWINSZ', 1074295912L)
|
TIOCGWINSZ = getattr(termios, 'TIOCGWINSZ', 1074295912)
|
||||||
s = struct.pack('HHHH', 0, 0, 0, 0)
|
s = struct.pack('HHHH', 0, 0, 0, 0)
|
||||||
x = fcntl.ioctl(self.fileno(), TIOCGWINSZ, s)
|
x = fcntl.ioctl(self.fileno(), TIOCGWINSZ, s)
|
||||||
return struct.unpack('HHHH', x)[0:2]
|
return struct.unpack('HHHH', x)[0:2]
|
||||||
@ -1479,7 +1480,7 @@ class spawn_unix (object):
|
|||||||
# Newer versions of Linux have totally different values for TIOCSWINSZ.
|
# Newer versions of Linux have totally different values for TIOCSWINSZ.
|
||||||
# Note that this fix is a hack.
|
# Note that this fix is a hack.
|
||||||
TIOCSWINSZ = getattr(termios, 'TIOCSWINSZ', -2146929561)
|
TIOCSWINSZ = getattr(termios, 'TIOCSWINSZ', -2146929561)
|
||||||
if TIOCSWINSZ == 2148037735L: # L is not required in Python >= 2.2.
|
if TIOCSWINSZ == 2148037735: # L is not required in Python >= 2.2.
|
||||||
TIOCSWINSZ = -2146929561 # Same bits, but with sign.
|
TIOCSWINSZ = -2146929561 # Same bits, but with sign.
|
||||||
# Note, assume ws_xpixel and ws_ypixel are zero.
|
# Note, assume ws_xpixel and ws_ypixel are zero.
|
||||||
s = struct.pack('HHHH', r, c, 0, 0)
|
s = struct.pack('HHHH', r, c, 0, 0)
|
||||||
@ -1556,7 +1557,7 @@ class spawn_unix (object):
|
|||||||
if self.child_fd in r:
|
if self.child_fd in r:
|
||||||
try:
|
try:
|
||||||
data = self.__interact_read(self.child_fd)
|
data = self.__interact_read(self.child_fd)
|
||||||
except OSError, e:
|
except OSError as e:
|
||||||
break
|
break
|
||||||
if output_filter: data = output_filter(data)
|
if output_filter: data = output_filter(data)
|
||||||
if self.logfile is not None:
|
if self.logfile is not None:
|
||||||
@ -1587,7 +1588,7 @@ class spawn_unix (object):
|
|||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
return select.select (iwtd, owtd, ewtd, timeout)
|
return select.select (iwtd, owtd, ewtd, timeout)
|
||||||
except select.error, e:
|
except select.error as e:
|
||||||
if e[0] == errno.EINTR:
|
if e[0] == errno.EINTR:
|
||||||
# if we loop back we have to subtract the amount of time we already waited.
|
# if we loop back we have to subtract the amount of time we already waited.
|
||||||
if timeout is not None:
|
if timeout is not None:
|
||||||
@ -1655,7 +1656,7 @@ class spawn_windows (spawn_unix, object):
|
|||||||
self.name = '<' + repr(self) + '>' # File-like object.
|
self.name = '<' + repr(self) + '>' # File-like object.
|
||||||
self.encoding = None # File-like object.
|
self.encoding = None # File-like object.
|
||||||
self.closed = True # File-like object.
|
self.closed = True # File-like object.
|
||||||
self.ocwd = os.getcwdu()
|
self.ocwd = os.getcwd()
|
||||||
self.cwd = cwd
|
self.cwd = cwd
|
||||||
self.env = env
|
self.env = env
|
||||||
self.codepage = codepage
|
self.codepage = codepage
|
||||||
@ -1784,20 +1785,10 @@ class spawn_windows (spawn_unix, object):
|
|||||||
It will not wait for 30 seconds for another 99 characters to come in.
|
It will not wait for 30 seconds for another 99 characters to come in.
|
||||||
|
|
||||||
This is a wrapper around Wtty.read(). """
|
This is a wrapper around Wtty.read(). """
|
||||||
|
|
||||||
|
|
||||||
if self.closed:
|
if self.closed:
|
||||||
raise ValueError ('I/O operation on closed file in read_nonblocking().')
|
raise ValueError ('I/O operation on closed file in read_nonblocking().')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if timeout == -1:
|
if timeout == -1:
|
||||||
timeout = self.timeout
|
timeout = self.timeout
|
||||||
|
|
||||||
@ -1971,7 +1962,7 @@ class Wtty:
|
|||||||
if childPid:
|
if childPid:
|
||||||
try:
|
try:
|
||||||
self.__childProcess = win32api.OpenProcess(win32con.PROCESS_TERMINATE | win32con.PROCESS_QUERY_INFORMATION, False, childPid)
|
self.__childProcess = win32api.OpenProcess(win32con.PROCESS_TERMINATE | win32con.PROCESS_QUERY_INFORMATION, False, childPid)
|
||||||
except pywintypes.error, e:
|
except pywintypes.error as e:
|
||||||
if time.time() > ts + self.timeout:
|
if time.time() > ts + self.timeout:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
@ -2066,11 +2057,11 @@ class Wtty:
|
|||||||
win32console.AttachConsole(self.conpid)
|
win32console.AttachConsole(self.conpid)
|
||||||
self.__consin = win32console.GetStdHandle(win32console.STD_INPUT_HANDLE)
|
self.__consin = win32console.GetStdHandle(win32console.STD_INPUT_HANDLE)
|
||||||
self.__consout = self.getConsoleOut()
|
self.__consout = self.getConsoleOut()
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
#e = traceback.format_exc()
|
#e = traceback.format_exc()
|
||||||
try:
|
try:
|
||||||
win32console.AttachConsole(self.__parentPid)
|
win32console.AttachConsole(self.__parentPid)
|
||||||
except Exception, ex:
|
except Exception as ex:
|
||||||
pass
|
pass
|
||||||
#log(e)
|
#log(e)
|
||||||
#log(ex)
|
#log(ex)
|
||||||
@ -2144,7 +2135,7 @@ class Wtty:
|
|||||||
try:
|
try:
|
||||||
if s[-1] == '\n':
|
if s[-1] == '\n':
|
||||||
s = s[:-1]
|
s = s[:-1]
|
||||||
records = [self.createKeyEvent(c) for c in unicode(s)]
|
records = [self.createKeyEvent(c) for c in str(s)]
|
||||||
if not self.__consout:
|
if not self.__consout:
|
||||||
return ""
|
return ""
|
||||||
consinfo = self.__consout.GetConsoleScreenBufferInfo()
|
consinfo = self.__consout.GetConsoleScreenBufferInfo()
|
||||||
@ -2228,11 +2219,8 @@ class Wtty:
|
|||||||
strlist.append(c)
|
strlist.append(c)
|
||||||
|
|
||||||
s = ''.join(strlist)
|
s = ''.join(strlist)
|
||||||
try:
|
return s
|
||||||
return s.encode(str(self.codepage), 'replace')
|
|
||||||
except LookupError:
|
|
||||||
return s.encode(getattr(sys.stdout, 'encoding', None) or
|
|
||||||
sys.getdefaultencoding(), 'replace')
|
|
||||||
|
|
||||||
def readConsoleToCursor(self):
|
def readConsoleToCursor(self):
|
||||||
"""Reads from the current read position to the current cursor
|
"""Reads from the current read position to the current cursor
|
||||||
@ -2317,6 +2305,7 @@ class Wtty:
|
|||||||
|
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
def read_nonblocking(self, timeout, size):
|
def read_nonblocking(self, timeout, size):
|
||||||
"""Reads data from the console if available, otherwise
|
"""Reads data from the console if available, otherwise
|
||||||
waits timeout seconds, and writes the string 'None'
|
waits timeout seconds, and writes the string 'None'
|
||||||
@ -2347,10 +2336,8 @@ class Wtty:
|
|||||||
time.sleep(0.001)
|
time.sleep(0.001)
|
||||||
end = time.clock()
|
end = time.clock()
|
||||||
timeout -= end - start
|
timeout -= end - start
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
log(e)
|
log(e)
|
||||||
log('End Of File (EOF) in Wtty.read_nonblocking().')
|
log('End Of File (EOF) in Wtty.read_nonblocking().')
|
||||||
self.switchBack()
|
self.switchBack()
|
||||||
@ -2359,6 +2346,7 @@ class Wtty:
|
|||||||
self.switchBack()
|
self.switchBack()
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
def refreshConsole(self):
|
def refreshConsole(self):
|
||||||
"""Clears the console after pausing the child and
|
"""Clears the console after pausing the child and
|
||||||
reading all the data currently on the console."""
|
reading all the data currently on the console."""
|
||||||
@ -2489,7 +2477,7 @@ class ConsoleReader:
|
|||||||
log("Setting console output code page to %s" % cp, 'consolereader', logdir)
|
log("Setting console output code page to %s" % cp, 'consolereader', logdir)
|
||||||
try:
|
try:
|
||||||
win32console.SetConsoleOutputCP(cp)
|
win32console.SetConsoleOutputCP(cp)
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
log(e, 'consolereader', logdir)
|
log(e, 'consolereader', logdir)
|
||||||
else:
|
else:
|
||||||
log("Console output code page: %s" % windll.kernel32.GetConsoleOutputCP(), 'consolereader', logdir)
|
log("Console output code page: %s" % windll.kernel32.GetConsoleOutputCP(), 'consolereader', logdir)
|
||||||
@ -2502,7 +2490,7 @@ class ConsoleReader:
|
|||||||
si = win32process.GetStartupInfo()
|
si = win32process.GetStartupInfo()
|
||||||
self.__childProcess, _, childPid, self.__tid = win32process.CreateProcess(None, path, None, None, False,
|
self.__childProcess, _, childPid, self.__tid = win32process.CreateProcess(None, path, None, None, False,
|
||||||
0, None, None, si)
|
0, None, None, si)
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
log(e, 'consolereader', logdir)
|
log(e, 'consolereader', logdir)
|
||||||
time.sleep(.1)
|
time.sleep(.1)
|
||||||
win32api.PostThreadMessage(int(tid), win32con.WM_USER, 0, 0)
|
win32api.PostThreadMessage(int(tid), win32con.WM_USER, 0, 0)
|
||||||
@ -2523,7 +2511,7 @@ class ConsoleReader:
|
|||||||
time.sleep(.1)
|
time.sleep(.1)
|
||||||
try:
|
try:
|
||||||
win32process.TerminateProcess(self.__childProcess, 0)
|
win32process.TerminateProcess(self.__childProcess, 0)
|
||||||
except pywintypes.error, e:
|
except pywintypes.error as e:
|
||||||
# 'Access denied' happens always? Perhaps if not
|
# 'Access denied' happens always? Perhaps if not
|
||||||
# running as admin (or UAC enabled under Vista/7).
|
# running as admin (or UAC enabled under Vista/7).
|
||||||
# Don't log. Child process will exit regardless when
|
# Don't log. Child process will exit regardless when
|
||||||
@ -2547,7 +2535,7 @@ class ConsoleReader:
|
|||||||
paused = False
|
paused = False
|
||||||
|
|
||||||
time.sleep(.1)
|
time.sleep(.1)
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
log(e, 'consolereader', logdir)
|
log(e, 'consolereader', logdir)
|
||||||
time.sleep(.1)
|
time.sleep(.1)
|
||||||
|
|
||||||
@ -2613,7 +2601,7 @@ class searcher_string (object):
|
|||||||
self.eof_index = -1
|
self.eof_index = -1
|
||||||
self.timeout_index = -1
|
self.timeout_index = -1
|
||||||
self._strings = []
|
self._strings = []
|
||||||
for n, s in zip(range(len(strings)), strings):
|
for n, s in zip(list(range(len(strings))), strings):
|
||||||
if s is EOF:
|
if s is EOF:
|
||||||
self.eof_index = n
|
self.eof_index = n
|
||||||
continue
|
continue
|
||||||
@ -2634,7 +2622,7 @@ class searcher_string (object):
|
|||||||
if self.timeout_index >= 0:
|
if self.timeout_index >= 0:
|
||||||
ss.append ((self.timeout_index,' %d: TIMEOUT' % self.timeout_index))
|
ss.append ((self.timeout_index,' %d: TIMEOUT' % self.timeout_index))
|
||||||
ss.sort()
|
ss.sort()
|
||||||
ss = zip(*ss)[1]
|
ss = list(zip(*ss))[1]
|
||||||
return '\n'.join(ss)
|
return '\n'.join(ss)
|
||||||
|
|
||||||
def search(self, buffer, freshlen, searchwindowsize=None):
|
def search(self, buffer, freshlen, searchwindowsize=None):
|
||||||
@ -2711,7 +2699,7 @@ class searcher_re (object):
|
|||||||
self.eof_index = -1
|
self.eof_index = -1
|
||||||
self.timeout_index = -1
|
self.timeout_index = -1
|
||||||
self._searches = []
|
self._searches = []
|
||||||
for n, s in zip(range(len(patterns)), patterns):
|
for n, s in zip(list(range(len(patterns))), patterns):
|
||||||
if s is EOF:
|
if s is EOF:
|
||||||
self.eof_index = n
|
self.eof_index = n
|
||||||
continue
|
continue
|
||||||
@ -2732,7 +2720,7 @@ class searcher_re (object):
|
|||||||
if self.timeout_index >= 0:
|
if self.timeout_index >= 0:
|
||||||
ss.append ((self.timeout_index,' %d: TIMEOUT' % self.timeout_index))
|
ss.append ((self.timeout_index,' %d: TIMEOUT' % self.timeout_index))
|
||||||
ss.sort()
|
ss.sort()
|
||||||
ss = zip(*ss)[1]
|
ss = list(zip(*ss))[1]
|
||||||
return '\n'.join(ss)
|
return '\n'.join(ss)
|
||||||
|
|
||||||
def search(self, buffer, freshlen, searchwindowsize=None):
|
def search(self, buffer, freshlen, searchwindowsize=None):
|
||||||
@ -2806,12 +2794,12 @@ def log(e, suffix='', logdir=None):
|
|||||||
if os.path.isfile(logfile):
|
if os.path.isfile(logfile):
|
||||||
try:
|
try:
|
||||||
logstat = os.stat(logfile)
|
logstat = os.stat(logfile)
|
||||||
except Exception, exception:
|
except Exception as exception:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
mtime = time.localtime(logstat.st_mtime)
|
mtime = time.localtime(logstat.st_mtime)
|
||||||
except ValueError, exception:
|
except ValueError as exception:
|
||||||
# This can happen on Windows because localtime() is buggy on
|
# This can happen on Windows because localtime() is buggy on
|
||||||
# that platform. See:
|
# that platform. See:
|
||||||
# http://stackoverflow.com/questions/4434629/zipfile-module-in-python-runtime-problems
|
# http://stackoverflow.com/questions/4434629/zipfile-module-in-python-runtime-problems
|
||||||
@ -2823,7 +2811,7 @@ def log(e, suffix='', logdir=None):
|
|||||||
# do rollover
|
# do rollover
|
||||||
try:
|
try:
|
||||||
os.remove(logfile)
|
os.remove(logfile)
|
||||||
except Exception, exception:
|
except Exception as exception:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
fout = open(logfile, 'a')
|
fout = open(logfile, 'a')
|
||||||
@ -2852,7 +2840,7 @@ def which (filename):
|
|||||||
if os.access (filename, os.X_OK):
|
if os.access (filename, os.X_OK):
|
||||||
return filename
|
return filename
|
||||||
|
|
||||||
if not os.environ.has_key('PATH') or os.environ['PATH'] == '':
|
if 'PATH' not in os.environ or os.environ['PATH'] == '':
|
||||||
p = os.defpath
|
p = os.defpath
|
||||||
else:
|
else:
|
||||||
p = os.environ['PATH']
|
p = os.environ['PATH']
|
||||||
@ -2874,7 +2862,7 @@ def join_args(args):
|
|||||||
commandline = []
|
commandline = []
|
||||||
for arg in args:
|
for arg in args:
|
||||||
if re.search('[\^!$%&()[\]{}=;\'+,`~\s]', arg):
|
if re.search('[\^!$%&()[\]{}=;\'+,`~\s]', arg):
|
||||||
arg = '"%s"' % arg
|
arg = '"%s"' % arg
|
||||||
commandline.append(arg)
|
commandline.append(arg)
|
||||||
return ' '.join(commandline)
|
return ' '.join(commandline)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user