You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
6.5 KiB

4 years ago
  1. import sys
  2. import types
  3. from .exceptions import EOF, TIMEOUT
  4. from .pty_spawn import spawn
  5. def run(command, timeout=30, withexitstatus=False, events=None,
  6. extra_args=None, logfile=None, cwd=None, env=None, **kwargs):
  7. '''
  8. This function runs the given command; waits for it to finish; then
  9. returns all output as a string. STDERR is included in output. If the full
  10. path to the command is not given then the path is searched.
  11. Note that lines are terminated by CR/LF (\\r\\n) combination even on
  12. UNIX-like systems because this is the standard for pseudottys. If you set
  13. 'withexitstatus' to true, then run will return a tuple of (command_output,
  14. exitstatus). If 'withexitstatus' is false then this returns just
  15. command_output.
  16. The run() function can often be used instead of creating a spawn instance.
  17. For example, the following code uses spawn::
  18. from pexpect import *
  19. child = spawn('scp foo user@example.com:.')
  20. child.expect('(?i)password')
  21. child.sendline(mypassword)
  22. The previous code can be replace with the following::
  23. from pexpect import *
  24. run('scp foo user@example.com:.', events={'(?i)password': mypassword})
  25. **Examples**
  26. Start the apache daemon on the local machine::
  27. from pexpect import *
  28. run("/usr/local/apache/bin/apachectl start")
  29. Check in a file using SVN::
  30. from pexpect import *
  31. run("svn ci -m 'automatic commit' my_file.py")
  32. Run a command and capture exit status::
  33. from pexpect import *
  34. (command_output, exitstatus) = run('ls -l /bin', withexitstatus=1)
  35. The following will run SSH and execute 'ls -l' on the remote machine. The
  36. password 'secret' will be sent if the '(?i)password' pattern is ever seen::
  37. run("ssh username@machine.example.com 'ls -l'",
  38. events={'(?i)password':'secret\\n'})
  39. This will start mencoder to rip a video from DVD. This will also display
  40. progress ticks every 5 seconds as it runs. For example::
  41. from pexpect import *
  42. def print_ticks(d):
  43. print d['event_count'],
  44. run("mencoder dvd://1 -o video.avi -oac copy -ovc copy",
  45. events={TIMEOUT:print_ticks}, timeout=5)
  46. The 'events' argument should be either a dictionary or a tuple list that
  47. contains patterns and responses. Whenever one of the patterns is seen
  48. in the command output, run() will send the associated response string.
  49. So, run() in the above example can be also written as:
  50. run("mencoder dvd://1 -o video.avi -oac copy -ovc copy",
  51. events=[(TIMEOUT,print_ticks)], timeout=5)
  52. Use a tuple list for events if the command output requires a delicate
  53. control over what pattern should be matched, since the tuple list is passed
  54. to pexpect() as its pattern list, with the order of patterns preserved.
  55. Note that you should put newlines in your string if Enter is necessary.
  56. Like the example above, the responses may also contain a callback, either
  57. a function or method. It should accept a dictionary value as an argument.
  58. The dictionary contains all the locals from the run() function, so you can
  59. access the child spawn object or any other variable defined in run()
  60. (event_count, child, and extra_args are the most useful). A callback may
  61. return True to stop the current run process. Otherwise run() continues
  62. until the next event. A callback may also return a string which will be
  63. sent to the child. 'extra_args' is not used by directly run(). It provides
  64. a way to pass data to a callback function through run() through the locals
  65. dictionary passed to a callback.
  66. Like :class:`spawn`, passing *encoding* will make it work with unicode
  67. instead of bytes. You can pass *codec_errors* to control how errors in
  68. encoding and decoding are handled.
  69. '''
  70. if timeout == -1:
  71. child = spawn(command, maxread=2000, logfile=logfile, cwd=cwd, env=env,
  72. **kwargs)
  73. else:
  74. child = spawn(command, timeout=timeout, maxread=2000, logfile=logfile,
  75. cwd=cwd, env=env, **kwargs)
  76. if isinstance(events, list):
  77. patterns= [x for x,y in events]
  78. responses = [y for x,y in events]
  79. elif isinstance(events, dict):
  80. patterns = list(events.keys())
  81. responses = list(events.values())
  82. else:
  83. # This assumes EOF or TIMEOUT will eventually cause run to terminate.
  84. patterns = None
  85. responses = None
  86. child_result_list = []
  87. event_count = 0
  88. while True:
  89. try:
  90. index = child.expect(patterns)
  91. if isinstance(child.after, child.allowed_string_types):
  92. child_result_list.append(child.before + child.after)
  93. else:
  94. # child.after may have been a TIMEOUT or EOF,
  95. # which we don't want appended to the list.
  96. child_result_list.append(child.before)
  97. if isinstance(responses[index], child.allowed_string_types):
  98. child.send(responses[index])
  99. elif (isinstance(responses[index], types.FunctionType) or
  100. isinstance(responses[index], types.MethodType)):
  101. callback_result = responses[index](locals())
  102. sys.stdout.flush()
  103. if isinstance(callback_result, child.allowed_string_types):
  104. child.send(callback_result)
  105. elif callback_result:
  106. break
  107. else:
  108. raise TypeError("parameter `event' at index {index} must be "
  109. "a string, method, or function: {value!r}"
  110. .format(index=index, value=responses[index]))
  111. event_count = event_count + 1
  112. except TIMEOUT:
  113. child_result_list.append(child.before)
  114. break
  115. except EOF:
  116. child_result_list.append(child.before)
  117. break
  118. child_result = child.string_type().join(child_result_list)
  119. if withexitstatus:
  120. child.close()
  121. return (child_result, child.exitstatus)
  122. else:
  123. return child_result
  124. def runu(command, timeout=30, withexitstatus=False, events=None,
  125. extra_args=None, logfile=None, cwd=None, env=None, **kwargs):
  126. """Deprecated: pass encoding to run() instead.
  127. """
  128. kwargs.setdefault('encoding', 'utf-8')
  129. return run(command, timeout=timeout, withexitstatus=withexitstatus,
  130. events=events, extra_args=extra_args, logfile=logfile, cwd=cwd,
  131. env=env, **kwargs)