Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. from StringIO import StringIO
  2. process = subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  3. output, error = process.communicate()
  4. log_subprocess_output(StringIO(output))
  5.  
  6. import shlex
  7. import logging
  8. import subprocess
  9. from StringIO import StringIO
  10.  
  11. def run_shell_command(command_line):
  12. command_line_args = shlex.split(command_line)
  13.  
  14. logging.info('Subprocess: "' + command_line + '"')
  15.  
  16. try:
  17. command_line_process = subprocess.Popen(
  18. command_line_args,
  19. stdout=subprocess.PIPE,
  20. stderr=subprocess.STDOUT,
  21. )
  22.  
  23. process_output, _ = command_line_process.communicate()
  24.  
  25. # process_output is now a string, not a file,
  26. # you may want to do:
  27. # process_output = StringIO(process_output)
  28. log_subprocess_output(process_output)
  29. except (OSError, CalledProcessError) as exception:
  30. logging.info('Exception occured: ' + str(exception))
  31. logging.info('Subprocess failed')
  32. return False
  33. else:
  34. # no exception was raised
  35. logging.info('Subprocess finished')
  36.  
  37. return True
  38.  
  39. from subprocess Popen, PIPE, STDOUT
  40.  
  41. command_line_process = Popen(command_line_args, stdout=PIPE, stderr=STDOUT)
  42. log_subprocess_output(p.stdout)
  43. p.stdout.close()
  44. exitcode = p.wait() # 0 means success
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement