Execute cmd line in sub-shell
(cmd, show_stdout=True, in_shell=False,
cwd=None, extra_env=None)
| 463 | |
| 464 | |
| 465 | def callit(cmd, show_stdout=True, in_shell=False, |
| 466 | cwd=None, extra_env=None): |
| 467 | """ |
| 468 | Execute cmd line in sub-shell |
| 469 | """ |
| 470 | all_output = [] |
| 471 | cmd_parts = [] |
| 472 | |
| 473 | for part in cmd: |
| 474 | if len(part) > 45: |
| 475 | part = part[:20] + "..." + part[-20:] |
| 476 | if ' ' in part or '\n' in part or '"' in part or "'" in part: |
| 477 | part = '"%s"' % part.replace('"', '\\"') |
| 478 | cmd_parts.append(part) |
| 479 | cmd_desc = ' '.join(cmd_parts) |
| 480 | logger.debug(" ** Running command %s" % cmd_desc) |
| 481 | |
| 482 | if in_shell: |
| 483 | cmd = ' '.join(cmd) |
| 484 | |
| 485 | # output |
| 486 | stdout = subprocess.PIPE |
| 487 | |
| 488 | # env |
| 489 | if extra_env: |
| 490 | env = os.environ.copy() |
| 491 | if extra_env: |
| 492 | env.update(extra_env) |
| 493 | else: |
| 494 | env = None |
| 495 | |
| 496 | # execute |
| 497 | try: |
| 498 | proc = subprocess.Popen( |
| 499 | cmd, stderr=subprocess.STDOUT, stdin=None, stdout=stdout, |
| 500 | cwd=cwd, env=env, shell=in_shell) |
| 501 | except Exception: |
| 502 | e = sys.exc_info()[1] |
| 503 | logger.error("Error %s while executing command %s" % (e, cmd_desc)) |
| 504 | raise |
| 505 | |
| 506 | stdout = proc.stdout |
| 507 | while stdout: |
| 508 | line = stdout.readline() |
| 509 | if not line: |
| 510 | break |
| 511 | try: |
| 512 | if is_WIN: |
| 513 | line = line.decode('mbcs').rstrip() |
| 514 | else: |
| 515 | line = line.decode('utf8').rstrip() |
| 516 | except UnicodeDecodeError: |
| 517 | line = line.decode('cp866').rstrip() |
| 518 | all_output.append(line) |
| 519 | if show_stdout: |
| 520 | logger.info(line) |
| 521 | proc.wait() |
| 522 |
no outgoing calls
no test coverage detected