From 527e60bab4e9b59309de9b36ac602ac2a3f701ee Mon Sep 17 00:00:00 2001 From: Devan Goodwin Date: Tue, 11 Mar 2014 09:12:49 -0300 Subject: [PATCH] Print output live for longer running rpmbuild commands. --- .gitignore | 2 ++ src/tito/builder/main.py | 9 ++++----- src/tito/common.py | 33 +++++++++++++++++++++++++++------ src/tito/exception.py | 4 ++-- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 4852451..ed5beb9 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ *.pyo *$py.class *.swp +*.swo +.noseids *.patch *# *~ diff --git a/src/tito/builder/main.py b/src/tito/builder/main.py index 6b58705..13fbec6 100644 --- a/src/tito/builder/main.py +++ b/src/tito/builder/main.py @@ -194,11 +194,11 @@ class BuilderBase(object): rpmbuild_options = self.rpmbuild_options + self._scl_to_rpmbuild_option() - cmd = ('LC_ALL=C rpmbuild --define "_source_filedigest_algorithm md5" --define' + cmd = ('rpmbuild --define "_source_filedigest_algorithm md5" --define' ' "_binary_filedigest_algorithm md5" %s %s %s --nodeps -bs %s' % ( rpmbuild_options, self._get_rpmbuild_dir_options(), define_dist, self.spec_file)) - output = run_command(cmd) + output = run_command_print(cmd) print(output) self.srpm_location = find_wrote_in_rpmbuild_output(output)[0] self.artifacts.append(self.srpm_location) @@ -215,13 +215,13 @@ class BuilderBase(object): rpmbuild_options = self.rpmbuild_options + self._scl_to_rpmbuild_option() - cmd = ('LC_ALL=C rpmbuild --define "_source_filedigest_algorithm md5" ' + cmd = ('rpmbuild --define "_source_filedigest_algorithm md5" ' '--define "_binary_filedigest_algorithm md5" %s %s %s --clean ' '-ba %s' % (rpmbuild_options, self._get_rpmbuild_dir_options(), define_dist, self.spec_file)) debug(cmd) try: - output = run_command(cmd) + output = run_command_print(cmd) except (KeyboardInterrupt, SystemExit): print("") exit(1) @@ -235,7 +235,6 @@ class BuilderBase(object): except Exception: err = sys.exc_info()[1] error_out('%s' % str(err)) - print(output) files_written = find_wrote_in_rpmbuild_output(output) if len(files_written) < 2: error_out("Error parsing rpmbuild output") diff --git a/src/tito/common.py b/src/tito/common.py index 0120b29..83a724c 100644 --- a/src/tito/common.py +++ b/src/tito/common.py @@ -18,8 +18,11 @@ import os import re import sys import traceback +import subprocess +import shlex from tito.compat import * +from tito.exception import RunCommandException DEFAULT_BUILD_DIR = "/tmp/tito" DEFAULT_BUILDER = "builder" @@ -222,10 +225,27 @@ def run_command(command, print_on_success=False): return output -def runProcess(cmd): - p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) +def run_command_print(command): + """ + Simliar to run_command but prints each line of output on the fly. + """ + output = [] + env = os.environ.copy() + env['LC_ALL'] = 'C' + p = subprocess.Popen(shlex.split(command), + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env) + for line in run_subprocess(p): + print(line), + output.append(line.rstrip('\n')) + print("\n"), + if p.poll() > 0: + raise RunCommandException(command, p.poll(), "\n".join(output)) + return '\n'.join(output) + + +def run_subprocess(p): while(True): - retcode = p.poll() #returns None while subprocess is running + retcode = p.poll() line = p.stdout.readline() yield line if(retcode is not None): @@ -243,7 +263,7 @@ def tag_exists_locally(tag): def tag_exists_remotely(tag): """ Returns True if the tag exists in the remote git repo. """ try: - get_git_repo_url() + get_git_repo_url() except: sys.stderr.write('Warning: remote.origin do not exist. Assuming --offline, for remote tag checking.\n') return False @@ -505,7 +525,7 @@ def create_tgz(git_root, prefix, commit, relative_dir, rel_eng_dir, #if not os.path.exists(timestamp_script): # error_out("Unable to locate required script: %s" % timestamp_script) - # Accomodate standalone projects with specfile in root of git repo: + # Accomodate standalone projects with specfile i root of git repo: relative_git_dir = "%s" % relative_dir if relative_git_dir in ['/', './']: relative_git_dir = "" @@ -637,7 +657,8 @@ def find_wrote_in_rpmbuild_output(output): """ paths = [] look_for = "Wrote: " - for line in output.split("\n"): + for line in output.split('\n'): + print("Checking: %s" % line) if line.startswith(look_for): paths.append(line[len(look_for):]) debug("Found wrote line: %s" % paths[-1]) diff --git a/src/tito/exception.py b/src/tito/exception.py index f3673c9..fcb756f 100644 --- a/src/tito/exception.py +++ b/src/tito/exception.py @@ -32,8 +32,8 @@ class TitoException(Exception): class RunCommandException(Exception): """ Raised by run_command() """ - def __init__(self, msg, command, status, output): - Exception.__init__(self, msg) + def __init__(self, command, status, output): + Exception.__init__(self, "Error running command: %s" % command) self.command = command self.status = status self.output = output