From 21e5db065dafeabe8f3ddd918e7dc03fe85ab522 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Metin?= Date: Wed, 19 Apr 2006 13:33:09 +0000 Subject: [PATCH] - add stderr and stdout to api.init() // this is needed for buildfarm. - use subprocess and be consistent on return value in util.run_batch() --- pisi/actionsapi/shelltools.py | 2 +- pisi/api.py | 13 +++++++++- pisi/context.py | 4 +++ pisi/util.py | 46 ++++++++++++++++++++++------------- 4 files changed, 46 insertions(+), 19 deletions(-) diff --git a/pisi/actionsapi/shelltools.py b/pisi/actionsapi/shelltools.py index 8fd6a29c..12fb2aab 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) + return run_batch(command, True)[0] diff --git a/pisi/api.py b/pisi/api.py index 0dd0c525..3cae6657 100644 --- a/pisi/api.py +++ b/pisi/api.py @@ -49,7 +49,9 @@ import pisi.lockeddbshelve as shelve class Error(pisi.Error): pass -def init(database = True, write = True, options = None, ui = None, comar = True): +def init(database = True, write = True, + options = None, ui = None, comar = True, + stdout = None, stderr = None): """Initialize PiSi subsystem""" # UI comes first @@ -63,6 +65,15 @@ def init(database = True, write = True, options = None, ui = None, comar = True) else: ctx.ui = ui + + # If given define stdout and stderr. Needed by buildfarm currently + # but others can benefit from this too. + if stdout: + ctx.stdout = stdout + if stderr: + ctx.stderr = stderr + + import pisi.config ctx.config = pisi.config.Config(options) diff --git a/pisi/context.py b/pisi/context.py index cac71e73..e5f3bbad 100644 --- a/pisi/context.py +++ b/pisi/context.py @@ -31,6 +31,10 @@ def get_option(opt): # default UI is CLI ui = None # not now +# stdout, stderr for PiSi API +stdout = None +stderr = None + dbenv = None installdb = None packagedb = None diff --git a/pisi/util.py b/pisi/util.py index cd14e3af..47658a8e 100644 --- a/pisi/util.py +++ b/pisi/util.py @@ -26,6 +26,7 @@ import shutil import string import statvfs import operator +import subprocess import gettext __trans = gettext.translation('pisi', fallback=True) @@ -141,22 +142,33 @@ def human_readable_rate(size = 0): def run_batch(cmd, realtime = False): """run command non-interactively/realtime and report return value and output""" ctx.ui.info(_('Running ') + cmd, verbose=True) - p = os.popen(cmd) - if realtime: - while 1: - line = p.readline() - if not line: - break - ctx.ui.debug(line[:-1]) - return p.close() + + if ctx.stdout: + stdout = ctx.stdout else: - lines = p.readlines() - ret = p.close() - ctx.ui.debug(_('return value %s') % ret) - successful = ret == None - if not successful: - ctx.ui.error(_('Failed command: %s') % cmd + strlist(lines)) - return (successful,lines) + stdout = subprocess.PIPE + + if ctx.stderr: + stderr = ctx.stderr + else: + stderr = subprocess.PIPE + + 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() + + ctx.ui.debug(_('return value %s') % p.returncode) + if p.returncode: + # Non-zero means failed. + ctx.ui.error(_('Failed command: %s') % cmd + strlist(out)) + + return (p.returncode, out) ###################### # Terminal functions # @@ -475,9 +487,9 @@ def do_patch(sourceDir, patchFile, level = 0, target = ''): target = '' check_file(patchFile) - (successful, lines) = run_batch("patch -p%d %s< %s" % + (ret, out) = run_batch("patch -p%d %s< %s" % (level, target, patchFile)) - if not successful: + if ret: raise Error(_("ERROR: patch (%s) failed: %s") % (patchFile, strlist (lines)))