mirror of
https://github.com/clearml/wexpect-venv
synced 2025-03-13 06:58:10 +00:00
[UDT] examples to python3; [FIX] which command uses shutils.which
This commit is contained in:
parent
257e9de8b4
commit
75042b1658
@ -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
|
||||
|
||||
@ -12,24 +12,25 @@ sys.path.insert(0, wexpectPath)
|
||||
import wexpect
|
||||
|
||||
# Path of cmd executable:
|
||||
cmdPath = 'C:\Windows\System32\cmd.exe'
|
||||
cmdPathes = ['C:\Windows\System32\cmd.exe', 'cmd.exe', 'cmd']
|
||||
cmdPrompt = '>'
|
||||
|
||||
# Start the child process
|
||||
p = wexpect.spawn(cmdPath)
|
||||
for cmdPath in cmdPathes:
|
||||
# Start the child process
|
||||
p = wexpect.spawn(cmdPath)
|
||||
|
||||
# Wait for prompt
|
||||
p.expect(cmdPrompt)
|
||||
# Wait for prompt
|
||||
p.expect(cmdPrompt)
|
||||
|
||||
# print the texts
|
||||
print(p.before, end='')
|
||||
print(p.match.group(0), end='')
|
||||
# print the texts
|
||||
print(p.before, end='')
|
||||
print(p.match.group(0), end='')
|
||||
|
||||
# Send a command
|
||||
p.sendline('ls')
|
||||
p.expect(cmdPrompt)
|
||||
# Send a command
|
||||
p.sendline('ls')
|
||||
p.expect(cmdPrompt)
|
||||
|
||||
# print the texts
|
||||
print(p.before, end='')
|
||||
print(p.match.group(0), end='')
|
||||
# print the texts
|
||||
print(p.before, end='')
|
||||
print(p.match.group(0), end='')
|
||||
|
||||
|
@ -4,10 +4,10 @@ import time
|
||||
print('Give a small integer: ', end='')
|
||||
num = input()
|
||||
|
||||
for i in range(num):
|
||||
for i in range(int(num)):
|
||||
print('waiter ' + str(i))
|
||||
time.sleep(0.2)
|
||||
|
||||
print('Give your name: ', end='')
|
||||
name = raw_input()
|
||||
name = input()
|
||||
print('Hello ' + str(name), end='')
|
39
wexpect.py
39
wexpect.py
@ -66,14 +66,13 @@ $Id: pexpect.py 507 2007-12-27 02:40:52Z noah $
|
||||
try:
|
||||
import os, sys, time
|
||||
import select
|
||||
import string
|
||||
import shutil
|
||||
import re
|
||||
import struct
|
||||
import types
|
||||
import errno
|
||||
import traceback
|
||||
import signal
|
||||
import subprocess
|
||||
import pkg_resources
|
||||
|
||||
if sys.platform != 'win32':
|
||||
@ -545,7 +544,7 @@ class spawn_unix (object):
|
||||
self.args.insert (0, command)
|
||||
self.command = command
|
||||
|
||||
command_with_path = which(self.command)
|
||||
command_with_path = shutil.which(self.command)
|
||||
if command_with_path is None:
|
||||
raise ExceptionPexpect ('The command was not found or was not executable: %s.' % self.command)
|
||||
self.command = command_with_path
|
||||
@ -1710,7 +1709,7 @@ class spawn_windows (spawn_unix, object):
|
||||
self.args.insert (0, command)
|
||||
self.command = command
|
||||
|
||||
command_with_path = which(self.command)
|
||||
command_with_path = shutil.which(self.command)
|
||||
if command_with_path is None:
|
||||
raise ExceptionPexpect ('The command was not found or was not executable: %s.' % self.command)
|
||||
self.command = command_with_path
|
||||
@ -2140,6 +2139,8 @@ class Wtty:
|
||||
return ""
|
||||
consinfo = self.__consout.GetConsoleScreenBufferInfo()
|
||||
startCo = consinfo['CursorPosition']
|
||||
print(records)
|
||||
print(consinfo)
|
||||
wrote = self.__consin.WriteConsoleInput(records)
|
||||
ts = time.time()
|
||||
while self.__consin and self.__consin.PeekConsoleInput(8) != ():
|
||||
@ -2827,34 +2828,12 @@ def log(e, suffix='', logdir=None):
|
||||
def excepthook(etype, value, tb):
|
||||
log(''.join(traceback.format_exception(etype, value, tb)))
|
||||
|
||||
#sys.excepthook = excepthook
|
||||
|
||||
|
||||
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):
|
||||
"""Joins arguments into a command line. It quotes all arguments that contain
|
||||
|
Loading…
Reference in New Issue
Block a user