OLD | NEW |
1 # coding: utf-8 | 1 # coding: utf-8 |
2 | 2 |
3 # This Source Code is subject to the terms of the Mozilla Public License | 3 # This Source Code is subject to the terms of the Mozilla Public License |
4 # version 2.0 (the "License"). You can obtain a copy of the License at | 4 # version 2.0 (the "License"). You can obtain a copy of the License at |
5 # http://mozilla.org/MPL/2.0/. | 5 # http://mozilla.org/MPL/2.0/. |
6 | 6 |
7 import sys, os, urllib, zipfile | 7 import sys, os, urllib, zipfile, subprocess |
8 from StringIO import StringIO | 8 from StringIO import StringIO |
9 | 9 |
10 def ensureJSShell(): | 10 def ensureJSShell(): |
11 baseDir = os.path.dirname(__file__) | 11 baseDir = os.path.dirname(__file__) |
12 shell_dir = os.path.join(baseDir, 'mozilla') | 12 shell_dir = os.path.join(baseDir, 'mozilla') |
13 if not os.path.exists(shell_dir): | 13 if not os.path.exists(shell_dir): |
14 os.makedirs(shell_dir) | 14 os.makedirs(shell_dir) |
15 if sys.platform == 'win32': | 15 if sys.platform == 'win32': |
16 path = os.path.join(shell_dir, 'js.exe') | 16 path = os.path.join(shell_dir, 'js.exe') |
17 else: | 17 else: |
(...skipping 17 matching lines...) Expand all Loading... |
35 | 35 |
36 if not os.path.exists(path): | 36 if not os.path.exists(path): |
37 raise Exception('Downloaded package didn\'t contain JS shell executable') | 37 raise Exception('Downloaded package didn\'t contain JS shell executable') |
38 | 38 |
39 try: | 39 try: |
40 os.chmod(path, 0700) | 40 os.chmod(path, 0700) |
41 except: | 41 except: |
42 pass | 42 pass |
43 | 43 |
44 return path | 44 return path |
| 45 |
| 46 STREAMS_NONE = 0 |
| 47 STREAMS_STDOUT = 1 |
| 48 STREAMS_STDERR = 2 |
| 49 STREAMS_BOTH = 3 |
| 50 def run(command, streams, **kwargs): |
| 51 """ |
| 52 This function runs a command and returns the selected streams as result. |
| 53 streams parameter indicates which streams need to be redirected and |
| 54 returned, it can be either STREAMS_NONE, STREAMS_STDOUT, STREAMS_STDERR or |
| 55 STREAMS_BOTH. An exception is thrown if the command cannot be executed or if |
| 56 it returns a non-zero result. |
| 57 """ |
| 58 |
| 59 if streams == STREAMS_STDOUT or streams == STREAMS_BOTH: |
| 60 kwargs["stdout"] = subprocess.PIPE |
| 61 if streams == STREAMS_STDERR or streams == STREAMS_BOTH: |
| 62 kwargs["stderr"] = subprocess.PIPE |
| 63 process = subprocess.Popen(command, **kwargs) |
| 64 stdout, stderr = process.communicate() |
| 65 if process.returncode != 0: |
| 66 raise IOError("Subprocess didn't return a success code") |
| 67 if streams == STREAMS_NONE: |
| 68 return None |
| 69 elif streams == STREAMS_STDOUT: |
| 70 return stdout |
| 71 elif streams == STREAMS_STDERR: |
| 72 return stderr |
| 73 elif streams == STREAMS_BOTH: |
| 74 return (stdout, stderr) |
| 75 else: |
| 76 raise Exception("Invalid value for the streams parameter") |
OLD | NEW |