executils.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from msysutils import msysActive, msysShell
  2. from os import environ
  3. from shlex import split as shsplit
  4. from subprocess import PIPE, Popen
  5. def captureStdout(log, commandLine):
  6. '''Run a command and capture what it writes to stdout.
  7. If the command fails or writes something to stderr, that is logged.
  8. Returns the captured string, or None if the command failed.
  9. '''
  10. # TODO: This is a modified copy-paste from compilers._Command.
  11. commandParts = shsplit(commandLine)
  12. env = dict(environ)
  13. while commandParts:
  14. if '=' in commandParts[0]:
  15. name, value = commandParts[0].split('=', 1)
  16. del commandParts[0]
  17. env[name] = value
  18. else:
  19. break
  20. else:
  21. raise ValueError(
  22. 'No command specified in "%s"' % commandLine
  23. )
  24. if msysActive() and commandParts[0] != 'sh':
  25. commandParts = [
  26. msysShell(), '-c', shjoin(commandParts)
  27. ]
  28. try:
  29. proc = Popen(
  30. commandParts, bufsize = -1, env = env,
  31. stdin = None, stdout = PIPE, stderr = PIPE,
  32. )
  33. except OSError, ex:
  34. print >> log, 'Failed to execute "%s": %s' % (commandLine, ex)
  35. return None
  36. stdoutdata, stderrdata = proc.communicate()
  37. if stderrdata:
  38. severity = 'warning' if proc.returncode == 0 else 'error'
  39. log.write('%s executing "%s"\n' % (severity.capitalize(), commandLine))
  40. # pylint 0.18.0 somehow thinks stderrdata is a list, not a string.
  41. # pylint: disable-msg=E1103
  42. stderrdata = stderrdata.replace('\r', '')
  43. log.write(stderrdata)
  44. if not stderrdata.endswith('\n'):
  45. log.write('\n')
  46. if proc.returncode == 0:
  47. return stdoutdata
  48. else:
  49. print >> log, 'Execution failed with exit code %d' % proc.returncode
  50. return None
  51. def shjoin(parts):
  52. '''Joins the given sequence into a single string with space as a separator.
  53. Characters that have a special meaning for the shell are escaped.
  54. This is the counterpart of shlex.split().
  55. '''
  56. def escape(part):
  57. return ''.join(
  58. '\\' + ch if ch in '\\ \'"$()[]' else ch
  59. for ch in part
  60. )
  61. return ' '.join(escape(part) for part in parts)