-move abe functionality out of util; prepare for release
[oweals/gnunet.git] / contrib / gnunet_pyexpect.py.in
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., 51 Franklin Street, Fifth Floor,
18 #    Boston, MA 02110-1301, 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     env = kwargs.pop ('env', None)
35     if env is None:
36       env = os.environ.copy ()
37     # This messes up some testcases, disable log redirection
38     env.pop ('GNUNET_FORCE_LOGFILE', None)
39     self.proc = subprocess.Popen (arglist, *pargs, env=env, **kwargs)
40     if self.proc is None:
41       print ("Failed to spawn a process {0}".format (arglist))
42       sys.exit (1)
43     if stdin is not None:
44       self.stdo, self.stde = self.proc.communicate (stdin)
45     else:
46       self.stdo, self.stde = self.proc.communicate ()
47     return self.proc
48
49   def expect (self, s, r, flags=0):
50     stream = self.stdo if s == 'stdout' else self.stde
51     if isinstance (r, str):
52       if r == "EOF":
53         if len (stream) == 0:
54           return True
55         else:
56           print ("Failed to find `{1}' in {0}, which is `{2}' ({3})".format (s, r, stream, len (stream)))
57           sys.exit (2)
58       raise ValueError ("Argument `r' should be an instance of re.RegexObject or a special string, but is `{0}'".format (r))
59     m = r.search (stream.decode(), flags)
60     if not m:
61       print ("Failed to find `{1}' in {0}, which is is `{2}'".format (s, r.pattern, stream))
62       sys.exit (2)
63     stream = stream[m.end ():]
64     if s == 'stdout':
65       self.stdo = stream
66     else:
67       self.stde = stream
68     return m
69
70   def read (self, s, size=-1):
71     stream = self.stdo if s == 'stdout' else self.stde
72     result = ""
73     if size < 0:
74       result = stream
75       new_stream = ""
76     else:
77       result = stream[0:size]
78       new_stream = stream[size:]
79     if s == 'stdout':
80       self.stdo = new_stream
81     else:
82       self.stde = new_stream
83     return result