diff options
Diffstat (limited to 'scripts/lib/wic/utils/runner.py')
| -rw-r--r-- | scripts/lib/wic/utils/runner.py | 50 |
1 files changed, 6 insertions, 44 deletions
diff --git a/scripts/lib/wic/utils/runner.py b/scripts/lib/wic/utils/runner.py index 348557aee8..4aa00fbe20 100644 --- a/scripts/lib/wic/utils/runner.py +++ b/scripts/lib/wic/utils/runner.py | |||
| @@ -14,32 +14,17 @@ | |||
| 14 | # You should have received a copy of the GNU General Public License along | 14 | # You should have received a copy of the GNU General Public License along |
| 15 | # with this program; if not, write to the Free Software Foundation, Inc., 59 | 15 | # with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 16 | # Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 16 | # Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | |||
| 18 | import logging | ||
| 19 | import os | ||
| 20 | import subprocess | 17 | import subprocess |
| 21 | 18 | ||
| 22 | from wic import WicError | 19 | from wic import WicError |
| 23 | 20 | ||
| 24 | logger = logging.getLogger('wic') | 21 | def runtool(cmdln_or_args): |
| 25 | |||
| 26 | def runtool(cmdln_or_args, catch=1): | ||
| 27 | """ wrapper for most of the subprocess calls | 22 | """ wrapper for most of the subprocess calls |
| 28 | input: | 23 | input: |
| 29 | cmdln_or_args: can be both args and cmdln str (shell=True) | 24 | cmdln_or_args: can be both args and cmdln str (shell=True) |
| 30 | catch: 0, quitely run | ||
| 31 | 1, only STDOUT | ||
| 32 | 2, only STDERR | ||
| 33 | 3, both STDOUT and STDERR | ||
| 34 | return: | 25 | return: |
| 35 | (rc, output) | 26 | rc, output |
| 36 | if catch==0: the output will always None | ||
| 37 | """ | 27 | """ |
| 38 | |||
| 39 | if catch not in (0, 1, 2, 3): | ||
| 40 | # invalid catch selection, will cause exception, that's good | ||
| 41 | return None | ||
| 42 | |||
| 43 | if isinstance(cmdln_or_args, list): | 28 | if isinstance(cmdln_or_args, list): |
| 44 | cmd = cmdln_or_args[0] | 29 | cmd = cmdln_or_args[0] |
| 45 | shell = False | 30 | shell = False |
| @@ -48,26 +33,13 @@ def runtool(cmdln_or_args, catch=1): | |||
| 48 | cmd = shlex.split(cmdln_or_args)[0] | 33 | cmd = shlex.split(cmdln_or_args)[0] |
| 49 | shell = True | 34 | shell = True |
| 50 | 35 | ||
| 51 | if catch != 3: | 36 | sout = subprocess.PIPE |
| 52 | dev_null = os.open("/dev/null", os.O_WRONLY) | 37 | serr = subprocess.STDOUT |
| 53 | |||
| 54 | if catch == 0: | ||
| 55 | sout = dev_null | ||
| 56 | serr = dev_null | ||
| 57 | elif catch == 1: | ||
| 58 | sout = subprocess.PIPE | ||
| 59 | serr = dev_null | ||
| 60 | elif catch == 2: | ||
| 61 | sout = dev_null | ||
| 62 | serr = subprocess.PIPE | ||
| 63 | elif catch == 3: | ||
| 64 | sout = subprocess.PIPE | ||
| 65 | serr = subprocess.STDOUT | ||
| 66 | 38 | ||
| 67 | try: | 39 | try: |
| 68 | process = subprocess.Popen(cmdln_or_args, stdout=sout, | 40 | process = subprocess.Popen(cmdln_or_args, stdout=sout, |
| 69 | stderr=serr, shell=shell) | 41 | stderr=serr, shell=shell) |
| 70 | (sout, serr) = process.communicate() | 42 | sout, serr = process.communicate() |
| 71 | # combine stdout and stderr, filter None out and decode | 43 | # combine stdout and stderr, filter None out and decode |
| 72 | out = ''.join([out.decode('utf-8') for out in [sout, serr] if out]) | 44 | out = ''.join([out.decode('utf-8') for out in [sout, serr] if out]) |
| 73 | except OSError as err: | 45 | except OSError as err: |
| @@ -76,15 +48,5 @@ def runtool(cmdln_or_args, catch=1): | |||
| 76 | raise WicError('Cannot run command: %s, lost dependency?' % cmd) | 48 | raise WicError('Cannot run command: %s, lost dependency?' % cmd) |
| 77 | else: | 49 | else: |
| 78 | raise # relay | 50 | raise # relay |
| 79 | finally: | ||
| 80 | if catch != 3: | ||
| 81 | os.close(dev_null) | ||
| 82 | |||
| 83 | return (process.returncode, out) | ||
| 84 | |||
| 85 | def outs(cmdln_or_args, catch=1): | ||
| 86 | # get the outputs of tools | ||
| 87 | return runtool(cmdln_or_args, catch)[1].strip() | ||
| 88 | 51 | ||
| 89 | def quiet(cmdln_or_args): | 52 | return process.returncode, out |
| 90 | return runtool(cmdln_or_args, catch=0)[0] | ||
