gnunet_pyexpect.py.in 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!@PYTHON@
  2. # This file is part of GNUnet.
  3. # (C) 2010, 2018 Christian Grothoff (and other contributing authors)
  4. #
  5. # GNUnet is free software: you can redistribute it and/or modify it
  6. # under the terms of the GNU Affero General Public License as published
  7. # by the Free Software Foundation, either version 3 of the License, or
  8. # (at your 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. # Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. # SPDX-License-Identifier: AGPL3.0-or-later
  19. #
  20. # Testcase for gnunet-peerinfo
  21. import os
  22. import re
  23. import subprocess
  24. import sys
  25. import shutil
  26. import time
  27. class pexpect (object):
  28. def __init__(self):
  29. super(pexpect, self).__init__()
  30. def spawn(self, stdin, arglist, *pargs, **kwargs):
  31. env = kwargs.pop('env', None)
  32. if env is None:
  33. env = os.environ.copy()
  34. # This messes up some testcases, disable log redirection
  35. env.pop('GNUNET_FORCE_LOGFILE', None)
  36. self.proc = subprocess.Popen(arglist, *pargs, env=env, **kwargs)
  37. if self.proc is None:
  38. print("Failed to spawn a process {0}".format(arglist))
  39. sys.exit(1)
  40. if stdin is not None:
  41. self.stdo, self.stde = self.proc.communicate(stdin)
  42. else:
  43. self.stdo, self.stde = self.proc.communicate()
  44. return self.proc
  45. def expect(self, s, r, flags=0):
  46. stream = self.stdo if s == 'stdout' else self.stde
  47. if isinstance(r, str):
  48. if r == "EOF":
  49. if len(stream) == 0:
  50. return True
  51. else:
  52. print("Failed to find `{1}' in {0}, which is `{2}' ({3})".format(s, r, stream, len(stream)))
  53. sys.exit(2)
  54. raise ValueError("Argument `r' should be an instance of re.RegexObject or a special string, but is `{0}'".format(r))
  55. m = r.search(stream.decode(), flags)
  56. if not m:
  57. print("Failed to find `{1}' in {0}, which is is `{2}'".format(s, r.pattern, stream))
  58. sys.exit(2)
  59. stream = stream[m.end():]
  60. if s == 'stdout':
  61. self.stdo = stream
  62. else:
  63. self.stde = stream
  64. return m
  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