2019-04-24 14:32:02 +00:00
|
|
|
# **wexpect**
|
2019-04-24 08:10:16 +00:00
|
|
|
|
2019-09-05 14:20:37 +00:00
|
|
|
[![Build status](https://ci.appveyor.com/api/projects/status/tbji72d5s0tagrt9?svg=true)](https://ci.appveyor.com/project/raczben/wexpect)
|
2019-09-06 09:52:11 +00:00
|
|
|
[![codecov](https://codecov.io/gh/raczben/wexpect/branch/master/graph/badge.svg)](https://codecov.io/gh/raczben/wexpect)
|
2019-09-05 14:20:37 +00:00
|
|
|
|
2019-04-24 14:32:02 +00:00
|
|
|
*Wexpect* is a Windows variant of [pexpect](https://pexpect.readthedocs.io/en/stable/).
|
2019-04-24 08:10:16 +00:00
|
|
|
|
2019-04-24 14:32:02 +00:00
|
|
|
*Pexpect* is a Python module for spawning child applications and controlling
|
|
|
|
them automatically.
|
2019-04-24 08:10:16 +00:00
|
|
|
|
2019-09-05 14:20:37 +00:00
|
|
|
## You need wexpect if...
|
2019-05-05 15:54:10 +00:00
|
|
|
|
2019-09-06 09:52:11 +00:00
|
|
|
- you want to control any windows console application from python script.
|
|
|
|
- you want to write test-automation script for a windows console application.
|
|
|
|
- you want to automate your job by controlling multiple application parallel, synchoronusly.
|
2019-05-05 15:54:10 +00:00
|
|
|
|
2019-04-24 14:32:02 +00:00
|
|
|
## **Install**
|
2019-04-24 08:10:16 +00:00
|
|
|
|
2019-04-24 14:32:02 +00:00
|
|
|
pip install wexpect
|
2019-09-05 14:20:37 +00:00
|
|
|
|
|
|
|
OR
|
|
|
|
|
|
|
|
Because wexpect a tiny project dropping the wexpect.py file into your working directory is usually
|
|
|
|
good enough instead of installing. However in this case you need to install manually the pypiwin32
|
|
|
|
dependence.
|
|
|
|
|
|
|
|
|
2019-04-24 14:32:02 +00:00
|
|
|
## **Usage**
|
2019-04-24 08:10:16 +00:00
|
|
|
|
2019-04-24 14:32:02 +00:00
|
|
|
To interract with a child process use `spawn` method:
|
2019-04-24 08:10:16 +00:00
|
|
|
|
2019-05-05 15:54:10 +00:00
|
|
|
```python
|
|
|
|
import wexpect
|
|
|
|
child = wexpect.spawn('cmd.exe')
|
|
|
|
child.expect('>')
|
|
|
|
child.sendline('ls')
|
|
|
|
child.expect('>')
|
|
|
|
print(child.before)
|
2019-09-05 15:51:27 +00:00
|
|
|
child.sendline('exit')
|
2019-05-05 15:54:10 +00:00
|
|
|
```
|
2019-04-24 08:10:16 +00:00
|
|
|
|
2019-04-24 14:32:02 +00:00
|
|
|
For more information see [examples](./examples) folder.
|
2019-04-24 08:10:16 +00:00
|
|
|
|
2019-04-24 14:32:02 +00:00
|
|
|
---
|
|
|
|
## What is it?
|
2019-04-24 08:10:16 +00:00
|
|
|
|
2019-04-24 14:32:02 +00:00
|
|
|
Wexpect is a Python module for spawning child applications and controlling
|
|
|
|
them automatically. Wexpect can be used for automating interactive applications
|
|
|
|
such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup
|
|
|
|
scripts for duplicating software package installations on different servers. It
|
|
|
|
can be used for automated software testing. Wexpect is in the spirit of Don
|
|
|
|
Libes' Expect, but Wexpect is pure Python. Other Expect-like modules for Python
|
|
|
|
require TCL and Expect or require C extensions to be compiled. Wexpect does not
|
|
|
|
use C, Expect, or TCL extensions.
|
2019-04-24 08:10:16 +00:00
|
|
|
|
2019-04-24 14:32:02 +00:00
|
|
|
Original Pexpect should work on any platform that supports the standard Python pty module. While
|
|
|
|
Wexpect works on Windows platforms. The Wexpect interface focuses on ease of use so that simple
|
|
|
|
tasks are easy.
|
2019-04-24 08:10:16 +00:00
|
|
|
|
|
|
|
|
2019-04-24 14:32:02 +00:00
|
|
|
### History
|
2019-04-24 10:09:19 +00:00
|
|
|
|
2019-04-24 14:32:02 +00:00
|
|
|
Wexpect is a one-file code developed at University of Washington. There are several
|
|
|
|
[copy](https://gist.github.com/anthonyeden/8488763) and
|
|
|
|
[reference](https://mediarealm.com.au/articles/python-pexpect-windows-wexpect/)
|
|
|
|
to this code with very few (almost none) documentation nor integration.
|
|
|
|
|
|
|
|
This repo tries to fix these limitations, with a few example code and pypi integration.
|
2019-04-24 10:09:19 +00:00
|
|
|
|
2019-04-24 08:10:16 +00:00
|
|
|
|
2019-05-05 15:54:10 +00:00
|
|
|
---
|
|
|
|
## Dev
|
|
|
|
|
|
|
|
Thanks for any contributing!
|
|
|
|
|
2019-07-29 12:21:26 +00:00
|
|
|
### Test
|
|
|
|
|
2019-05-05 15:54:10 +00:00
|
|
|
To run test, enter into the folder of the wexpect's repo then:
|
|
|
|
|
2019-07-29 12:21:26 +00:00
|
|
|
`python -m unittest`
|
|
|
|
|
2019-09-23 13:58:02 +00:00
|
|
|
### Deploy
|
|
|
|
|
|
|
|
The deployment itself is automated and done by [appveyor](https://ci.appveyor.com/project/raczben/wexpect).
|
|
|
|
See `after_test` section in [appveyor.yml](appveyor.yml) for more details.
|
2019-07-29 12:21:26 +00:00
|
|
|
|
|
|
|
The wexpect uses [pbr](https://docs.openstack.org/pbr/latest/) for managing releasing procedures.
|
2019-09-23 13:58:02 +00:00
|
|
|
The versioning is handled by the pbr. The *"master-version"* is the git tag. Pbr derives the package
|
|
|
|
version from the git tags.
|
2019-07-29 12:21:26 +00:00
|
|
|
|
2019-09-19 13:26:01 +00:00
|
|
|
## Basic behaviour
|
2019-07-29 12:21:26 +00:00
|
|
|
|
2019-09-19 13:26:01 +00:00
|
|
|
Let's go through the example code:
|
2019-07-29 12:21:26 +00:00
|
|
|
|
2019-09-19 13:26:01 +00:00
|
|
|
```python
|
|
|
|
import wexpect
|
|
|
|
child = wexpect.spawn('cmd.exe')
|
|
|
|
child.expect('>')
|
|
|
|
child.sendline('ls')
|
|
|
|
child.expect('>')
|
|
|
|
print(child.before)
|
|
|
|
child.sendline('exit')
|
|
|
|
```
|
|
|
|
|
|
|
|
### spawn()
|
|
|
|
|
|
|
|
`child = wexpect.spawn('cmd.exe')`
|
|
|
|
|
|
|
|
Call trace:
|
|
|
|
|
|
|
|
- ::spawn (line 289)
|
|
|
|
- spawn_windows::__init__() (line 1639)
|
|
|
|
- spawn_unix::__init__() (line 313)
|
|
|
|
- spawn_windows::_spawn() (line 1660)
|
|
|
|
- Wtty::spawn() (line 1932)
|
|
|
|
- Wtty::startChild() (line 1978)
|
|
|
|
- win32process.CreateProcess() (line 2024)
|
|
|
|
|
|
|
|
|
|
|
|
### expect()
|
|
|
|
|
|
|
|
`child.expect('>')`
|
|
|
|
|
|
|
|
Call trace:
|
|
|
|
|
|
|
|
- spawn_linux::expect() (line 1285)
|
|
|
|
- spawn_linux::expect_list() (line 1365)
|
|
|
|
- spawn_linux::expect_loop() (line 1397)
|
|
|
|
- spawn_windows::read_nonblocking() (line 1635)
|
|
|
|
- Wtty::read_nonblocking()
|
|
|
|
- Wtty::readConsoleToCursor()
|
|
|
|
- Wtty::readConsole() (line: 2153)
|
|
|
|
- __consout.ReadConsoleOutputCharacter() (line: 2176)
|
|
|
|
|
2019-05-05 15:54:10 +00:00
|
|
|
|
2019-09-20 08:51:58 +00:00
|
|
|
### sendline()
|
|
|
|
|
|
|
|
`child.sendline('ls')`
|
|
|
|
|
|
|
|
- spawn_linux::sendline() (line 1008)
|
|
|
|
- spawn_windows::send() (line 1795)
|
|
|
|
- Wtty::write() (line 2111)
|