Run a command. If quiet is True, or if V is not set in the environment, eat its stdout and stderr by default (we'll print both to stderr on any failure though). If V is set and we're not forced to be quiet, just let stdout and stderr flow through as usual. The name V is from
(cmd, quiet=False, shell=False)
| 44 | |
| 45 | |
| 46 | def cmd(cmd, quiet=False, shell=False): |
| 47 | """ |
| 48 | Run a command. If quiet is True, or if V is not set in the |
| 49 | environment, eat its stdout and stderr by default (we'll print |
| 50 | both to stderr on any failure though). |
| 51 | |
| 52 | If V is set and we're not forced to be quiet, just let stdout |
| 53 | and stderr flow through as usual. The name V is from the |
| 54 | standard Linux kernel build ("V=1 make" => print everything). |
| 55 | |
| 56 | (We use quiet=True for build environment test cleanup steps; |
| 57 | the tests themselves use use cmd_success() to check for failures.) |
| 58 | """ |
| 59 | if not quiet: |
| 60 | quiet = os.getenv('V') is None |
| 61 | |
| 62 | kwargs = {'universal_newlines': True} |
| 63 | if quiet: |
| 64 | kwargs['stdout'] = subprocess.PIPE |
| 65 | kwargs['stderr'] = subprocess.STDOUT |
| 66 | if shell: |
| 67 | proc = subprocess.Popen(cmd, shell=True, **kwargs) |
| 68 | else: |
| 69 | proc = subprocess.Popen(shlex.split(cmd), **kwargs) |
| 70 | |
| 71 | # There is never any stderr output here - either it went straight |
| 72 | # to os.STDERR_FILENO, or it went to the pipe for stdout. |
| 73 | out, _ = proc.communicate() |
| 74 | |
| 75 | if proc.returncode: |
| 76 | # We only have output if we ran in quiet mode. |
| 77 | if quiet: |
| 78 | print('Log:\n', out, file=sys.stderr) |
| 79 | print('Error has occured running command: %s' % cmd, file=sys.stderr) |
| 80 | sys.exit(proc.returncode) |
| 81 | |
| 82 | return out |
| 83 | |
| 84 | |
| 85 | BESS_DIR = os.path.dirname(os.path.abspath(__file__)) |
no outgoing calls
no test coverage detected