Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python -tt |
| 2 | # |
| 3 | # Copyright (c) 2011 Intel, Inc. |
| 4 | # |
| 5 | # This program is free software; you can redistribute it and/or modify it |
| 6 | # under the terms of the GNU General Public License as published by the Free |
| 7 | # Software Foundation; version 2 of the License |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, but |
| 10 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 11 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | # for more details. |
| 13 | # |
| 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 |
| 16 | # Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | |
| 18 | import os |
| 19 | import subprocess |
| 20 | |
| 21 | from wic import msger |
| 22 | |
| 23 | def runtool(cmdln_or_args, catch=1): |
| 24 | """ wrapper for most of the subprocess calls |
| 25 | input: |
| 26 | cmdln_or_args: can be both args and cmdln str (shell=True) |
| 27 | catch: 0, quitely run |
| 28 | 1, only STDOUT |
| 29 | 2, only STDERR |
| 30 | 3, both STDOUT and STDERR |
| 31 | return: |
| 32 | (rc, output) |
| 33 | if catch==0: the output will always None |
| 34 | """ |
| 35 | |
| 36 | if catch not in (0, 1, 2, 3): |
| 37 | # invalid catch selection, will cause exception, that's good |
| 38 | return None |
| 39 | |
| 40 | if isinstance(cmdln_or_args, list): |
| 41 | cmd = cmdln_or_args[0] |
| 42 | shell = False |
| 43 | else: |
| 44 | import shlex |
| 45 | cmd = shlex.split(cmdln_or_args)[0] |
| 46 | shell = True |
| 47 | |
| 48 | if catch != 3: |
| 49 | dev_null = os.open("/dev/null", os.O_WRONLY) |
| 50 | |
| 51 | if catch == 0: |
| 52 | sout = dev_null |
| 53 | serr = dev_null |
| 54 | elif catch == 1: |
| 55 | sout = subprocess.PIPE |
| 56 | serr = dev_null |
| 57 | elif catch == 2: |
| 58 | sout = dev_null |
| 59 | serr = subprocess.PIPE |
| 60 | elif catch == 3: |
| 61 | sout = subprocess.PIPE |
| 62 | serr = subprocess.STDOUT |
| 63 | |
| 64 | try: |
| 65 | process = subprocess.Popen(cmdln_or_args, stdout=sout, |
| 66 | stderr=serr, shell=shell) |
| 67 | (sout, serr) = process.communicate() |
| 68 | # combine stdout and stderr, filter None out |
| 69 | out = ''.join(filter(None, [sout, serr])) |
| 70 | except OSError, err: |
| 71 | if err.errno == 2: |
| 72 | # [Errno 2] No such file or directory |
| 73 | msger.error('Cannot run command: %s, lost dependency?' % cmd) |
| 74 | else: |
| 75 | raise # relay |
| 76 | finally: |
| 77 | if catch != 3: |
| 78 | os.close(dev_null) |
| 79 | |
| 80 | return (process.returncode, out) |
| 81 | |
| 82 | def show(cmdln_or_args): |
| 83 | # show all the message using msger.verbose |
| 84 | |
| 85 | rcode, out = runtool(cmdln_or_args, catch=3) |
| 86 | |
| 87 | if isinstance(cmdln_or_args, list): |
| 88 | cmd = ' '.join(cmdln_or_args) |
| 89 | else: |
| 90 | cmd = cmdln_or_args |
| 91 | |
| 92 | msg = 'running command: "%s"' % cmd |
| 93 | if out: |
| 94 | out = out.strip() |
| 95 | if out: |
| 96 | msg += ', with output::' |
| 97 | msg += '\n +----------------' |
| 98 | for line in out.splitlines(): |
| 99 | msg += '\n | %s' % line |
| 100 | msg += '\n +----------------' |
| 101 | |
| 102 | msger.verbose(msg) |
| 103 | return rcode |
| 104 | |
| 105 | def outs(cmdln_or_args, catch=1): |
| 106 | # get the outputs of tools |
| 107 | return runtool(cmdln_or_args, catch)[1].strip() |
| 108 | |
| 109 | def quiet(cmdln_or_args): |
| 110 | return runtool(cmdln_or_args, catch=0)[0] |