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") |