diff options
-rw-r--r-- | contrib/scripts/gnunet_pyexpect.py.in | 103 |
1 files changed, 52 insertions, 51 deletions
diff --git a/contrib/scripts/gnunet_pyexpect.py.in b/contrib/scripts/gnunet_pyexpect.py.in index cfeb06d8d..23f01603f 100644 --- a/contrib/scripts/gnunet_pyexpect.py.in +++ b/contrib/scripts/gnunet_pyexpect.py.in | |||
@@ -1,6 +1,6 @@ | |||
1 | #!@PYTHON@ | 1 | #!@PYTHON@ |
2 | # This file is part of GNUnet. | 2 | # This file is part of GNUnet. |
3 | # (C) 2010 Christian Grothoff (and other contributing authors) | 3 | # (C) 2010, 2018 Christian Grothoff (and other contributing authors) |
4 | # | 4 | # |
5 | # GNUnet is free software; you can redistribute it and/or modify | 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 | 6 | # it under the terms of the GNU General Public License as published |
@@ -26,58 +26,59 @@ import sys | |||
26 | import shutil | 26 | import shutil |
27 | import time | 27 | import time |
28 | 28 | ||
29 | |||
29 | class pexpect (object): | 30 | class pexpect (object): |
30 | def __init__ (self): | 31 | def __init__(self): |
31 | super (pexpect, self).__init__ () | 32 | super(pexpect, self).__init__() |
32 | 33 | ||
33 | def spawn (self, stdin, arglist, *pargs, **kwargs): | 34 | def spawn(self, stdin, arglist, *pargs, **kwargs): |
34 | env = kwargs.pop ('env', None) | 35 | env = kwargs.pop('env', None) |
35 | if env is None: | 36 | if env is None: |
36 | env = os.environ.copy () | 37 | env = os.environ.copy() |
37 | # This messes up some testcases, disable log redirection | 38 | # This messes up some testcases, disable log redirection |
38 | env.pop ('GNUNET_FORCE_LOGFILE', None) | 39 | env.pop('GNUNET_FORCE_LOGFILE', None) |
39 | self.proc = subprocess.Popen (arglist, *pargs, env=env, **kwargs) | 40 | self.proc = subprocess.Popen(arglist, *pargs, env=env, **kwargs) |
40 | if self.proc is None: | 41 | if self.proc is None: |
41 | print ("Failed to spawn a process {0}".format (arglist)) | 42 | print("Failed to spawn a process {0}".format(arglist)) |
42 | sys.exit (1) | 43 | sys.exit(1) |
43 | if stdin is not None: | 44 | if stdin is not None: |
44 | self.stdo, self.stde = self.proc.communicate (stdin) | 45 | self.stdo, self.stde = self.proc.communicate(stdin) |
45 | else: | 46 | else: |
46 | self.stdo, self.stde = self.proc.communicate () | 47 | self.stdo, self.stde = self.proc.communicate() |
47 | return self.proc | 48 | return self.proc |
48 | 49 | ||
49 | def expect (self, s, r, flags=0): | 50 | def expect(self, s, r, flags=0): |
50 | stream = self.stdo if s == 'stdout' else self.stde | 51 | stream = self.stdo if s == 'stdout' else self.stde |
51 | if isinstance (r, str): | 52 | if isinstance(r, str): |
52 | if r == "EOF": | 53 | if r == "EOF": |
53 | if len (stream) == 0: | 54 | if len(stream) == 0: |
54 | return True | 55 | return True |
56 | else: | ||
57 | print("Failed to find `{1}' in {0}, which is `{2}' ({3})".format(s, r, stream, len(stream))) | ||
58 | sys.exit(2) | ||
59 | raise ValueError("Argument `r' should be an instance of re.RegexObject or a special string, but is `{0}'".format(r)) | ||
60 | m = r.search(stream.decode(), flags) | ||
61 | if not m: | ||
62 | print("Failed to find `{1}' in {0}, which is is `{2}'".format(s, r.pattern, stream)) | ||
63 | sys.exit(2) | ||
64 | stream = stream[m.end():] | ||
65 | if s == 'stdout': | ||
66 | self.stdo = stream | ||
55 | else: | 67 | else: |
56 | print ("Failed to find `{1}' in {0}, which is `{2}' ({3})".format (s, r, stream, len (stream))) | 68 | self.stde = stream |
57 | sys.exit (2) | 69 | return m |
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 | ||
70 | def read (self, s, size=-1): | 71 | def read(self, s, size=-1): |
71 | stream = self.stdo if s == 'stdout' else self.stde | 72 | stream = self.stdo if s == 'stdout' else self.stde |
72 | result = "" | 73 | result = "" |
73 | if size < 0: | 74 | if size < 0: |
74 | result = stream | 75 | result = stream |
75 | new_stream = "" | 76 | new_stream = "" |
76 | else: | 77 | else: |
77 | result = stream[0:size] | 78 | result = stream[0:size] |
78 | new_stream = stream[size:] | 79 | new_stream = stream[size:] |
79 | if s == 'stdout': | 80 | if s == 'stdout': |
80 | self.stdo = new_stream | 81 | self.stdo = new_stream |
81 | else: | 82 | else: |
82 | self.stde = new_stream | 83 | self.stde = new_stream |
83 | return result | 84 | return result |