Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: utils.py

Issue 11039026: Use a subprocess wrapper that will throw if a command returns a non-zero result code (Closed)
Patch Set: Created July 4, 2013, 7:18 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « autotest.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: utils.py
===================================================================
--- a/utils.py
+++ b/utils.py
@@ -1,15 +1,15 @@
# coding: utf-8
# This Source Code is subject to the terms of the Mozilla Public License
# version 2.0 (the "License"). You can obtain a copy of the License at
# http://mozilla.org/MPL/2.0/.
-import sys, os, urllib, zipfile
+import sys, os, urllib, zipfile, subprocess
from StringIO import StringIO
def ensureJSShell():
baseDir = os.path.dirname(__file__)
shell_dir = os.path.join(baseDir, 'mozilla')
if not os.path.exists(shell_dir):
os.makedirs(shell_dir)
if sys.platform == 'win32':
@@ -37,8 +37,40 @@ def ensureJSShell():
raise Exception('Downloaded package didn\'t contain JS shell executable')
try:
os.chmod(path, 0700)
except:
pass
return path
+
+STREAMS_NONE = 0
+STREAMS_STDOUT = 1
+STREAMS_STDERR = 2
+STREAMS_BOTH = 3
+def run(command, streams, **kwargs):
+ """
+ This function runs a command and returns the selected streams as result.
+ streams parameter indicates which streams need to be redirected and
+ returned, it can be either STREAMS_NONE, STREAMS_STDOUT, STREAMS_STDERR or
+ STREAMS_BOTH. An exception is thrown if the command cannot be executed or if
+ it returns a non-zero result.
+ """
+
+ if streams == STREAMS_STDOUT or streams == STREAMS_BOTH:
+ kwargs["stdout"] = subprocess.PIPE
+ if streams == STREAMS_STDERR or streams == STREAMS_BOTH:
+ kwargs["stderr"] = subprocess.PIPE
+ process = subprocess.Popen(command, **kwargs)
+ stdout, stderr = process.communicate()
+ if process.returncode != 0:
+ raise IOError("Subprocess didn't return a success code")
+ if streams == STREAMS_NONE:
+ return None
+ elif streams == STREAMS_STDOUT:
+ return stdout
+ elif streams == STREAMS_STDERR:
+ return stderr
+ elif streams == STREAMS_BOTH:
+ return (stdout, stderr)
+ else:
+ raise Exception("Invalid value for the streams parameter")
« no previous file with comments | « autotest.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld