diff options
Diffstat (limited to 'contrib/gnunet_pyexpect.py.in')
-rw-r--r-- | contrib/gnunet_pyexpect.py.in | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/contrib/gnunet_pyexpect.py.in b/contrib/gnunet_pyexpect.py.in new file mode 100644 index 000000000..15d19fe5a --- /dev/null +++ b/contrib/gnunet_pyexpect.py.in | |||
@@ -0,0 +1,78 @@ | |||
1 | #!@PYTHON@ | ||
2 | # This file is part of GNUnet. | ||
3 | # (C) 2010 Christian Grothoff (and other contributing authors) | ||
4 | # | ||
5 | # GNUnet is free software; you can redistribute it and/or modify | ||
6 | # it under the terms of the GNU General Public License as published | ||
7 | # by the Free Software Foundation; either version 2, or (at your | ||
8 | # option) any later version. | ||
9 | # | ||
10 | # GNUnet is distributed in the hope that it will be useful, but | ||
11 | # WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | # General Public License for more details. | ||
14 | # | ||
15 | # You should have received a copy of the GNU General Public License | ||
16 | # along with GNUnet; see the file COPYING. If not, write to the | ||
17 | # Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | # Boston, MA 02111-1307, USA. | ||
19 | # | ||
20 | # Testcase for gnunet-peerinfo | ||
21 | from __future__ import print_function | ||
22 | import os | ||
23 | import re | ||
24 | import subprocess | ||
25 | import sys | ||
26 | import shutil | ||
27 | import time | ||
28 | |||
29 | class pexpect (object): | ||
30 | def __init__ (self): | ||
31 | super (pexpect, self).__init__ () | ||
32 | |||
33 | def spawn (self, stdin, arglist, *pargs, **kwargs): | ||
34 | self.proc = subprocess.Popen (arglist, *pargs, **kwargs) | ||
35 | if self.proc is None: | ||
36 | print ("Failed to spawn a process {0}".format (arglist)) | ||
37 | sys.exit (1) | ||
38 | if stdin is not None: | ||
39 | self.stdo, self.stde = self.proc.communicate (stdin) | ||
40 | else: | ||
41 | self.stdo, self.stde = self.proc.communicate () | ||
42 | return self.proc | ||
43 | |||
44 | def expect (self, s, r, flags=0): | ||
45 | stream = self.stdo if s == 'stdout' else self.stde | ||
46 | if isinstance (r, str): | ||
47 | if r == "EOF": | ||
48 | if len (stream) == 0: | ||
49 | return True | ||
50 | else: | ||
51 | print ("Failed to find `{1}' in {0}, which is `{2}' ({3})".format (s, r, stream, len (stream))) | ||
52 | sys.exit (2) | ||
53 | raise ValueError ("Argument `r' should be an instance of re.RegexObject or a special string, but is `{0}'".format (r)) | ||
54 | m = r.search (stream, flags) | ||
55 | if not m: | ||
56 | print ("Failed to find `{1}' in {0}, which is is `{2}'".format (s, r.pattern, stream)) | ||
57 | sys.exit (2) | ||
58 | stream = stream[m.end ():] | ||
59 | if s == 'stdout': | ||
60 | self.stdo = stream | ||
61 | else: | ||
62 | self.stde = stream | ||
63 | return m | ||
64 | |||
65 | def read (self, s, size=-1): | ||
66 | stream = self.stdo if s == 'stdout' else self.stde | ||
67 | result = "" | ||
68 | if size < 0: | ||
69 | result = stream | ||
70 | new_stream = "" | ||
71 | else: | ||
72 | result = stream[0:size] | ||
73 | new_stream = stream[size:] | ||
74 | if s == 'stdout': | ||
75 | self.stdo = new_stream | ||
76 | else: | ||
77 | self.stde = new_stream | ||
78 | return result | ||