From 81c37b7c894f74547dd4bfab5d76d652b8d5a501 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eray=20=C3=96zkural?= Date: Sat, 17 Jun 2006 11:35:51 +0000 Subject: [PATCH] * fix: define a new function "run_logged" that the actionsapi can gladly use --- pisi/actionsapi/shelltools.py | 2 +- pisi/util.py | 49 +++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/pisi/actionsapi/shelltools.py b/pisi/actionsapi/shelltools.py index 7e54f381..33bbeca2 100644 --- a/pisi/actionsapi/shelltools.py +++ b/pisi/actionsapi/shelltools.py @@ -222,4 +222,4 @@ def dirName(filePath): def system(command): command = string.join(string.split(command)) - return run_batch(command, True)[0] + return pisi.util.run_logged(command) diff --git a/pisi/util.py b/pisi/util.py index 797d01b1..c2045186 100644 --- a/pisi/util.py +++ b/pisi/util.py @@ -139,38 +139,49 @@ def human_readable_rate(size = 0): # Process Releated Functions # ############################## -#FIXME: the added flags make out and err defunct, they should be -#split to another function if needed, this function runs a command -#noninteractively, then returns return value, standard output and -# error. the return spec must not change. -def run_batch(cmd, realtime = False): - """run command non-interactively/realtime and report return value and output""" +def run_batch(cmd): + """run command and report return value and output""" ctx.ui.info(_('Running ') + cmd, verbose=True) + p = subprocess.Popen(cmd, shell=True, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = p.communicate() + ctx.ui.debug(_('return value for "%s" is %s') % (cmd, p.returncode)) + return (p.returncode, out, err) +# you can't use the following for Popen, oops +class TeeOutFile: + def __init__(self, file): + self.file = file + + def write(self, str): + self.write(str) + ctx.ui.debug(str) + +# TODO: it might be worthwhile to try to remove the +# use of ctx.stdout, and use run_batch()'s return +# values instead. +def run_logged(cmd): + """run command and get return value""" + ctx.ui.info(_('Running ') + cmd, verbose=True) if ctx.stdout: stdout = ctx.stdout else: - stdout = subprocess.PIPE - + if ctx.get_option('debug'): + stdout = None + else: + stdout = subprocess.PIPE if ctx.stderr: stderr = ctx.stderr else: - stderr = subprocess.PIPE + stderr = None - out = err = "" p = subprocess.Popen(cmd, shell=True, stdout=stdout, stderr=stderr) - if realtime and not ctx.stdout: - while p.poll() == None: - line = p.stdout.readline() - if line: - ctx.ui.debug(line[:-1]) - else: - out, err = p.communicate() - + out, err = p.communicate() ctx.ui.debug(_('return value for "%s" is %s') % (cmd, p.returncode)) - return (p.returncode, out, err) + return p.returncode + ###################### # Terminal functions # ######################