From 9e15705bb90d2ba9eb995716a0cc444a37dcbbd1 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Thu, 24 Apr 2014 10:05:43 -0700 Subject: [PATCH] Clean up download and install output Commit d2ae92b4b3707d0 patched up the download counter and progress display. Yum no longer provides the needed information so now we get the total number of packages from the start of the transaction. This also turns off colors when stdout is not a tty, and only prints the install progress once so that piping to a logfile isn't flooded with useless characters. --- src/pylorax/__init__.py | 5 +++- src/pylorax/ltmpl.py | 5 ++-- src/pylorax/output.py | 3 ++- src/pylorax/yumhelper.py | 56 +++++++++++++++++++++------------------- 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/pylorax/__init__.py b/src/pylorax/__init__.py index f7029492..01f3d65c 100644 --- a/src/pylorax/__init__.py +++ b/src/pylorax/__init__.py @@ -106,7 +106,10 @@ class Lorax(BaseLoraxClass): self.debug = self.conf.getboolean("lorax", "debug") output_level = output.DEBUG if self.debug else output.INFO - colors = self.conf.getboolean("output", "colors") + if sys.stdout.isatty(): + colors = self.conf.getboolean("output", "colors") + else: + colors = False encoding = self.conf.get("output", "encoding") self.output.basic_config(output_level=output_level, diff --git a/src/pylorax/ltmpl.py b/src/pylorax/ltmpl.py index d7165863..9fcc8a73 100644 --- a/src/pylorax/ltmpl.py +++ b/src/pylorax/ltmpl.py @@ -483,8 +483,9 @@ class LoraxTemplateRunner(object): commands. ''' self.yum.buildTransaction() - self.yum.repos.setProgressBar(LoraxDownloadCallback()) - self.yum.processTransaction(callback=LoraxTransactionCallback(), + dl_callback = LoraxDownloadCallback() + self.yum.repos.setProgressBar(dl_callback) + self.yum.processTransaction(callback=LoraxTransactionCallback(dl_callback), rpmDisplay=LoraxRpmCallback()) # verify if all packages that were supposed to be installed, diff --git a/src/pylorax/output.py b/src/pylorax/output.py index 9c2b3b8b..e60a300f 100644 --- a/src/pylorax/output.py +++ b/src/pylorax/output.py @@ -74,7 +74,8 @@ class LinuxTerminalOutput(object): def basic_config(self, output_level=None, colors=None, encoding=None): self._output_level = output_level or self._output_level - self._colors = colors or self._colors + if colors is not None: + self._colors = colors self._encoding = encoding or self._encoding def ignore(self, message): diff --git a/src/pylorax/yumhelper.py b/src/pylorax/yumhelper.py index 6732c35f..6ad4bbe2 100644 --- a/src/pylorax/yumhelper.py +++ b/src/pylorax/yumhelper.py @@ -21,7 +21,7 @@ import logging logger = logging.getLogger("pylorax.yumhelper") -import sys, os, re +import sys import yum, yum.callbacks, yum.rpmtrans import output @@ -29,13 +29,14 @@ __all__ = ['LoraxDownloadCallback', 'LoraxTransactionCallback', 'LoraxRpmCallback'] class LoraxDownloadCallback(yum.callbacks.DownloadBaseCallback): - def __init__(self): yum.callbacks.DownloadBaseCallback.__init__(self) + + self.pkgno = 0 + self.total = 0 + self.output = output.LoraxOutput() - pattern = "\((?P\d+)/(?P\d+)\):\s+(?P.*)" - self.pattern = re.compile(pattern) def updateProgress(self, name, frac, fread, ftime): """ @@ -45,40 +46,35 @@ class LoraxDownloadCallback(yum.callbacks.DownloadBaseCallback): @param fread: formated string containing BytesRead @param ftime: formated string containing remaining or elapsed time """ + # Only update when it is finished downloading + if frac < 1: + return - match = self.pattern.match(name) + self.pkgno += 1 + info = "({0:3d}/{1:3d}) " + info = info.format(self.pkgno, self.total) - pkgno = 0 - total = 0 - pkgname = name - if match: - pkgno = int(match.group("pkgno")) - total = int(match.group("total")) - pkgname = match.group("pkgname") - - info = "({0:3d}/{1:3d}) [{2:3.0f}%] downloading " - info = info.format(pkgno, total, frac * 100) - - infolen, pkglen = len(info), len(pkgname) + infolen, pkglen = len(info), len(name) if (infolen + pkglen) > self.output.width: - pkgname = "{0}...".format(pkgname[:self.output.width-infolen-3]) + name = "{0}...".format(name[:self.output.width-infolen-3]) - msg = "{0}{1}\r".format(info, pkgname) + msg = "{0}{1}\n".format(info, name) self.output.write(msg) - if frac == 1: - self.output.write("\n") class LoraxTransactionCallback(object): - def __init__(self): + def __init__(self, dl_callback): self.output = output.LoraxOutput() + self.dl_callback = dl_callback + def event(self, state, data=None): if state == yum.callbacks.PT_DOWNLOAD: self.output.write("downloading packages\n") elif state == yum.callbacks.PT_DOWNLOAD_PKGS: - pass + # Initialize the total number of packages being downloaded + self.dl_callback.total = len(data) elif state == yum.callbacks.PT_GPGCHECK: self.output.write("checking package signatures\n") elif state == yum.callbacks.PT_TEST_TRANS: @@ -108,10 +104,16 @@ class LoraxRpmCallback(yum.rpmtrans.RPMBaseCallback): if (infolen + pkglen) > self.output.width: pkg = "{0}...".format(pkg[:self.output.width-infolen-3]) - msg = "{0}{1}\r".format(info, pkg) - self.output.write(msg) - if te_current == te_total: - self.output.write("\n") + msg = "{0}{1}".format(info, pkg) + + # When not outputting to a tty we only want to print it once at the end + if sys.stdout.isatty(): + self.output.write(msg + "\r") + if te_current == te_total: + self.output.write("\n") + elif te_current == te_total: + self.output.write(msg + "\n") + def filelog(self, package, action): if self.fileaction.get(action) == "Installed":