[UDT] examples to python3; [FIX] which command uses shutils.which

This commit is contained in:
Benedek Racz 2019-05-04 18:58:46 +02:00
parent 257e9de8b4
commit 75042b1658
3 changed files with 27 additions and 47 deletions

View File

@ -1,4 +1,4 @@
# A simple example code for wexpect written in python-2.7 # A simple example code for wexpect
from __future__ import print_function from __future__ import print_function
@ -12,24 +12,25 @@ sys.path.insert(0, wexpectPath)
import wexpect import wexpect
# Path of cmd executable: # Path of cmd executable:
cmdPath = 'C:\Windows\System32\cmd.exe' cmdPathes = ['C:\Windows\System32\cmd.exe', 'cmd.exe', 'cmd']
cmdPrompt = '>' cmdPrompt = '>'
# Start the child process for cmdPath in cmdPathes:
p = wexpect.spawn(cmdPath) # Start the child process
p = wexpect.spawn(cmdPath)
# Wait for prompt # Wait for prompt
p.expect(cmdPrompt) p.expect(cmdPrompt)
# print the texts # print the texts
print(p.before, end='') print(p.before, end='')
print(p.match.group(0), end='') print(p.match.group(0), end='')
# Send a command # Send a command
p.sendline('ls') p.sendline('ls')
p.expect(cmdPrompt) p.expect(cmdPrompt)
# print the texts # print the texts
print(p.before, end='') print(p.before, end='')
print(p.match.group(0), end='') print(p.match.group(0), end='')

View File

@ -4,10 +4,10 @@ import time
print('Give a small integer: ', end='') print('Give a small integer: ', end='')
num = input() num = input()
for i in range(num): for i in range(int(num)):
print('waiter ' + str(i)) print('waiter ' + str(i))
time.sleep(0.2) time.sleep(0.2)
print('Give your name: ', end='') print('Give your name: ', end='')
name = raw_input() name = input()
print('Hello ' + str(name), end='') print('Hello ' + str(name), end='')

View File

@ -66,14 +66,13 @@ $Id: pexpect.py 507 2007-12-27 02:40:52Z noah $
try: try:
import os, sys, time import os, sys, time
import select import select
import string import shutil
import re import re
import struct import struct
import types import types
import errno import errno
import traceback import traceback
import signal import signal
import subprocess
import pkg_resources import pkg_resources
if sys.platform != 'win32': if sys.platform != 'win32':
@ -545,7 +544,7 @@ class spawn_unix (object):
self.args.insert (0, command) self.args.insert (0, command)
self.command = command self.command = command
command_with_path = which(self.command) command_with_path = shutil.which(self.command)
if command_with_path is None: if command_with_path is None:
raise ExceptionPexpect ('The command was not found or was not executable: %s.' % self.command) raise ExceptionPexpect ('The command was not found or was not executable: %s.' % self.command)
self.command = command_with_path self.command = command_with_path
@ -1710,7 +1709,7 @@ class spawn_windows (spawn_unix, object):
self.args.insert (0, command) self.args.insert (0, command)
self.command = command self.command = command
command_with_path = which(self.command) command_with_path = shutil.which(self.command)
if command_with_path is None: if command_with_path is None:
raise ExceptionPexpect ('The command was not found or was not executable: %s.' % self.command) raise ExceptionPexpect ('The command was not found or was not executable: %s.' % self.command)
self.command = command_with_path self.command = command_with_path
@ -2140,6 +2139,8 @@ class Wtty:
return "" return ""
consinfo = self.__consout.GetConsoleScreenBufferInfo() consinfo = self.__consout.GetConsoleScreenBufferInfo()
startCo = consinfo['CursorPosition'] startCo = consinfo['CursorPosition']
print(records)
print(consinfo)
wrote = self.__consin.WriteConsoleInput(records) wrote = self.__consin.WriteConsoleInput(records)
ts = time.time() ts = time.time()
while self.__consin and self.__consin.PeekConsoleInput(8) != (): while self.__consin and self.__consin.PeekConsoleInput(8) != ():
@ -2827,34 +2828,12 @@ def log(e, suffix='', logdir=None):
def excepthook(etype, value, tb): def excepthook(etype, value, tb):
log(''.join(traceback.format_exception(etype, value, tb))) log(''.join(traceback.format_exception(etype, value, tb)))
#sys.excepthook = excepthook
def which (filename): def which (filename):
from warnings import warn
warn("Which is deprecated. You should use shutil.which instead")
return shutil.which(filename)
"""This takes a given filename; tries to find it in the environment path;
then checks if it is executable. This returns the full path to the filename
if found and executable. Otherwise this returns None."""
# Special case where filename already contains a path.
if os.path.dirname(filename) != '':
if os.access (filename, os.X_OK):
return filename
if 'PATH' not in os.environ or os.environ['PATH'] == '':
p = os.defpath
else:
p = os.environ['PATH']
# Oddly enough this was the one line that made Pexpect
# incompatible with Python 1.5.2.
#pathlist = p.split (os.pathsep)
pathlist = string.split (p, os.pathsep)
for path in pathlist:
f = os.path.join(path, filename)
if os.access(f, os.X_OK):
return f
return None
def join_args(args): def join_args(args):
"""Joins arguments into a command line. It quotes all arguments that contain """Joins arguments into a command line. It quotes all arguments that contain