~ubuntu-branches/ubuntu/trusty/pexpect/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/pexpectTest.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio, Thomas Kluyver, Jakub Wilk, Jackson Doak, Andrew Starr-Bochicchio
  • Date: 2013-12-06 20:20:26 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20131206202026-9k9oixbv7e8ke30q
Tags: 3.0-1
* Team upload.

[ Thomas Kluyver ]
* New upstream release. Closes: #729518
* Add packaging for Python 3.
* Use pybuild for packaging.

[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Jackson Doak ]
* Create debian/python3-pexpect.docs

[ Andrew Starr-Bochicchio ]
* Remove empty debian/patches dir.
* Move documentation and examples into a new python-pexpect-doc
  package. They are shared between the Python 2 and Python 3 packages,
  so there is no need to install them with both.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
'''
 
3
PEXPECT LICENSE
 
4
 
 
5
    This license is approved by the OSI and FSF as GPL-compatible.
 
6
        http://opensource.org/licenses/isc-license.txt
 
7
 
 
8
    Copyright (c) 2012, Noah Spurrier <noah@noah.org>
 
9
    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
 
10
    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
 
11
    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
 
12
    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 
13
    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 
14
    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 
15
    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 
16
    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 
17
    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 
18
    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
19
 
 
20
'''
 
21
 
 
22
import os, time, pexpect, sys
 
23
 
 
24
def getProcessResults(cmd, timeLimit=20):
 
25
  '''
 
26
  executes 'cmd' as a child process and returns the child's output,
 
27
  the duration of execution, and the process exit status. Aborts if
 
28
  child process does not generate output for 'timeLimit' seconds.
 
29
  '''
 
30
  output = ""
 
31
  startTime = time.time()
 
32
  child = pexpect.spawn(cmd, timeout=10)
 
33
  child.logfile = sys.stdout
 
34
 
 
35
  while 1:
 
36
    try:
 
37
      # read_nonblocking will add to 'outout' one byte at a time
 
38
      # newlines can show up as '\r\n' so we kill any '\r's which
 
39
      # will mess up the formatting for the viewer
 
40
      output += child.read_nonblocking(timeout=timeLimit).replace("\r","")
 
41
    except pexpect.EOF as e:
 
42
      print(str(e))
 
43
      # process terminated normally
 
44
      break
 
45
    except pexpect.TIMEOUT as e:
 
46
      print(str(e))
 
47
      output += "\nProcess aborted by FlashTest after %s seconds.\n" % timeLimit
 
48
      print(child.isalive())
 
49
      child.kill(9)
 
50
      break
 
51
 
 
52
  endTime = time.time()
 
53
  child.close(force=True)
 
54
 
 
55
  duration = endTime - startTime
 
56
  exitStatus = child.exitstatus
 
57
 
 
58
  return (output, duration, exitStatus)
 
59
 
 
60
cmd = "./ticker.py"
 
61
 
 
62
result, duration, exitStatus = getProcessResults(cmd)
 
63
 
 
64
print("result: %s" % result)
 
65
print("duration: %s" % duration)
 
66
print("exit-status: %s" % exitStatus)
 
67