From dab868b20b936add86967eafe32711413b80dd6e Mon Sep 17 00:00:00 2001 From: Daniel Narvaez Date: Wed, 26 Dec 2012 17:16:07 +0000 Subject: Rework logging and output Use the logs for all the commands output. Tail the log if there is an error. Create a link to the latest log so that buildbot can find it easily. --- (limited to 'devbot/command.py') diff --git a/devbot/command.py b/devbot/command.py index 8ccef6c..3b2bc30 100644 --- a/devbot/command.py +++ b/devbot/command.py @@ -1,7 +1,15 @@ import subprocess import time +from devbot import utils + _logger = None +_log_path = None + + +def set_log_path(path): + global _log_path + _log_path = path def set_logger(logger): @@ -9,32 +17,42 @@ def set_logger(logger): _logger = logger -def run(args, log=None, test=False, retry=0): - print " ".join(args) +def run(args, test=False, retry=0): if test: + print " ".join(args) return - full_args = args[:] - if log is not None: - full_args.insert(0, _logger) - full_args.append(log) + if _log_path: + stdout = open(_log_path, "w") + stderr = subprocess.STDOUT + else: + stdout = None + stderr = None tries = 0 while tries < retry + 1: try: tries = tries + 1 - subprocess.check_call(full_args) - return + subprocess.check_call(args, stdout=stdout, stderr=stderr) + break except subprocess.CalledProcessError, e: + print "\nCommand failed, tail of %s\n" % _log_path + if _log_path: + subprocess.call(["tail", _log_path]) + if tries < retry + 1: print "Retrying (attempt %d) in 1 minute" % tries time.sleep(60) else: raise e + if stdout: + stdout.close() def run_with_sudo(args, test=False, retry=0): args_with_sudo = ["sudo"] args_with_sudo.extend(args) + print " ".join(args_with_sudo) + run(args_with_sudo, test=test, retry=retry) -- cgit v0.9.1