gnunet_pyexpect.py.in 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. class pexpect (object):
  29. def __init__ (self):
  30. super (pexpect, self).__init__ ()
  31. def spawn (self, stdin, arglist, *pargs, **kwargs):
  32. env = kwargs.pop ('env', None)
  33. if env is None:
  34. env = os.environ.copy ()
  35. # This messes up some testcases, disable log redirection
  36. env.pop ('GNUNET_FORCE_LOGFILE', None)
  37. self.proc = subprocess.Popen (arglist, *pargs, env=env, **kwargs)
  38. if self.proc is None:
  39. print ("Failed to spawn a process {0}".format (arglist))
  40. sys.exit (1)
  41. if stdin is not None:
  42. self.stdo, self.stde = self.proc.communicate (stdin)
  43. else:
  44. self.stdo, self.stde = self.proc.communicate ()
  45. return self.proc
  46. def expect (self, s, r, flags=0):
  47. stream = self.stdo if s == 'stdout' else self.stde
  48. if isinstance (r, str):
  49. if r == "EOF":
  50. if len (stream) == 0:
  51. return True
  52. else:
  53. print ("Failed to find `{1}' in {0}, which is `{2}' ({3})".format (s, r, stream, len (stream)))
  54. sys.exit (2)
  55. raise ValueError ("Argument `r' should be an instance of re.RegexObject or a special string, but is `{0}'".format (r))
  56. m = r.search (stream, flags)
  57. if not m:
  58. print ("Failed to find `{1}' in {0}, which is is `{2}'".format (s, r.pattern, stream))
  59. sys.exit (2)
  60. stream = stream[m.end ():]
  61. if s == 'stdout':
  62. self.stdo = stream
  63. else:
  64. self.stde = stream
  65. return m
  66. def read (self, s, size=-1):
  67. stream = self.stdo if s == 'stdout' else self.stde
  68. result = ""
  69. if size < 0:
  70. result = stream
  71. new_stream = ""
  72. else:
  73. result = stream[0:size]
  74. new_stream = stream[size:]
  75. if s == 'stdout':
  76. self.stdo = new_stream
  77. else:
  78. self.stde = new_stream
  79. return result