run_command_print should behave similar to run_command

The existing implementation of `run_command_print` uses `shlex.split`
to split command line arguments into an array, but it doesn't handle
wildcards like `run_command`. This breaks Mock build because of failure
of copying generated RPMs.

This PR also adds `print_on_success` argument and unify the behavior of
error handling.

Reproduction:
``` bash
$ tito build --test --rpm --output=artifacts/el6 --builder=tito.builder.MockBuilder --arg=mock=eng-rhel-6-candidate-x86_64
...
INFO: Results and/or logs in: /var/lib/mock/ERROR: Error running command: cp -v /home/fedora/jenkins/workspace/rcm-pulp-rpm-package/artifacts/el6/rpmbuild-pulp-rpm4f6rzL/mockoutput/*.rpm /home/fedora/jenkins/workspace/rcm-pulp-rpm-package/artifacts/el6
```
This commit is contained in:
Yuxiang Zhu 2018-01-03 18:42:59 +08:00
parent 18babd175f
commit 03b4656823

View file

@ -436,7 +436,7 @@ def run_command(command, print_on_success=False):
return output return output
def run_command_print(command): def run_command_print(command, print_on_success=False):
""" """
Simliar to run_command but prints each line of output on the fly. Simliar to run_command but prints each line of output on the fly.
""" """
@ -445,19 +445,25 @@ def run_command_print(command):
env['LC_ALL'] = 'C' env['LC_ALL'] = 'C'
p = None p = None
try: try:
p = subprocess.Popen(shlex.split(command), p = subprocess.Popen(command,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env,
universal_newlines=True) universal_newlines=True, shell=True)
except OSError as e: except OSError as e:
status = e.errno status = e.errno
output = e.strerror output = e.strerror
if p: msgs = [
for line in run_subprocess(p): "Error to run command: %s\n" % command,
line = line.rstrip('\n') "Error code: %s\n" % e.errno,
print(line) "Error description: %s\n" % e.strerror,
output.append(line) ]
print("\n"), error_out(msgs, die=False)
status = p.poll() raise RunCommandException(command, status, "\n".join(output))
for line in run_subprocess(p):
line = line.rstrip('\n')
print(line)
output.append(line)
print("\n")
status = p.poll()
if status > 0: if status > 0:
msgs = [ msgs = [
"Error running command: %s\n" % command, "Error running command: %s\n" % command,
@ -466,6 +472,14 @@ def run_command_print(command):
] ]
error_out(msgs, die=False) error_out(msgs, die=False)
raise RunCommandException(command, status, "\n".join(output)) raise RunCommandException(command, status, "\n".join(output))
elif print_on_success:
print("Command: %s\n" % command)
print("Status code: %s\n" % status)
print("Command output: %s\n" % output)
else:
debug("Command: %s\n" % command)
debug("Status code: %s\n" % status)
debug("Command output: %s\n" % output)
return '\n'.join(output) return '\n'.join(output)