Clean up download and install output

Commit d2ae92b4b3 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.
This commit is contained in:
Brian C. Lane 2014-04-24 10:05:43 -07:00
parent 63d4e7adf2
commit 9e15705bb9
4 changed files with 38 additions and 31 deletions

View File

@ -106,7 +106,10 @@ class Lorax(BaseLoraxClass):
self.debug = self.conf.getboolean("lorax", "debug") self.debug = self.conf.getboolean("lorax", "debug")
output_level = output.DEBUG if self.debug else output.INFO output_level = output.DEBUG if self.debug else output.INFO
if sys.stdout.isatty():
colors = self.conf.getboolean("output", "colors") colors = self.conf.getboolean("output", "colors")
else:
colors = False
encoding = self.conf.get("output", "encoding") encoding = self.conf.get("output", "encoding")
self.output.basic_config(output_level=output_level, self.output.basic_config(output_level=output_level,

View File

@ -483,8 +483,9 @@ class LoraxTemplateRunner(object):
commands. commands.
''' '''
self.yum.buildTransaction() self.yum.buildTransaction()
self.yum.repos.setProgressBar(LoraxDownloadCallback()) dl_callback = LoraxDownloadCallback()
self.yum.processTransaction(callback=LoraxTransactionCallback(), self.yum.repos.setProgressBar(dl_callback)
self.yum.processTransaction(callback=LoraxTransactionCallback(dl_callback),
rpmDisplay=LoraxRpmCallback()) rpmDisplay=LoraxRpmCallback())
# verify if all packages that were supposed to be installed, # verify if all packages that were supposed to be installed,

View File

@ -74,7 +74,8 @@ class LinuxTerminalOutput(object):
def basic_config(self, output_level=None, colors=None, encoding=None): def basic_config(self, output_level=None, colors=None, encoding=None):
self._output_level = output_level or self._output_level 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 self._encoding = encoding or self._encoding
def ignore(self, message): def ignore(self, message):

View File

@ -21,7 +21,7 @@
import logging import logging
logger = logging.getLogger("pylorax.yumhelper") logger = logging.getLogger("pylorax.yumhelper")
import sys, os, re import sys
import yum, yum.callbacks, yum.rpmtrans import yum, yum.callbacks, yum.rpmtrans
import output import output
@ -29,13 +29,14 @@ __all__ = ['LoraxDownloadCallback', 'LoraxTransactionCallback',
'LoraxRpmCallback'] 'LoraxRpmCallback']
class LoraxDownloadCallback(yum.callbacks.DownloadBaseCallback): class LoraxDownloadCallback(yum.callbacks.DownloadBaseCallback):
def __init__(self): def __init__(self):
yum.callbacks.DownloadBaseCallback.__init__(self) yum.callbacks.DownloadBaseCallback.__init__(self)
self.pkgno = 0
self.total = 0
self.output = output.LoraxOutput() self.output = output.LoraxOutput()
pattern = "\((?P<pkgno>\d+)/(?P<total>\d+)\):\s+(?P<pkgname>.*)"
self.pattern = re.compile(pattern)
def updateProgress(self, name, frac, fread, ftime): def updateProgress(self, name, frac, fread, ftime):
""" """
@ -45,40 +46,35 @@ class LoraxDownloadCallback(yum.callbacks.DownloadBaseCallback):
@param fread: formated string containing BytesRead @param fread: formated string containing BytesRead
@param ftime: formated string containing remaining or elapsed time @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 infolen, pkglen = len(info), len(name)
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)
if (infolen + pkglen) > self.output.width: 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}<b>{1}</b>\r".format(info, pkgname) msg = "{0}<b>{1}</b>\n".format(info, name)
self.output.write(msg) self.output.write(msg)
if frac == 1:
self.output.write("\n")
class LoraxTransactionCallback(object): class LoraxTransactionCallback(object):
def __init__(self): def __init__(self, dl_callback):
self.output = output.LoraxOutput() self.output = output.LoraxOutput()
self.dl_callback = dl_callback
def event(self, state, data=None): def event(self, state, data=None):
if state == yum.callbacks.PT_DOWNLOAD: if state == yum.callbacks.PT_DOWNLOAD:
self.output.write("downloading packages\n") self.output.write("downloading packages\n")
elif state == yum.callbacks.PT_DOWNLOAD_PKGS: 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: elif state == yum.callbacks.PT_GPGCHECK:
self.output.write("checking package signatures\n") self.output.write("checking package signatures\n")
elif state == yum.callbacks.PT_TEST_TRANS: elif state == yum.callbacks.PT_TEST_TRANS:
@ -108,10 +104,16 @@ class LoraxRpmCallback(yum.rpmtrans.RPMBaseCallback):
if (infolen + pkglen) > self.output.width: if (infolen + pkglen) > self.output.width:
pkg = "{0}...".format(pkg[:self.output.width-infolen-3]) pkg = "{0}...".format(pkg[:self.output.width-infolen-3])
msg = "{0}<b>{1}</b>\r".format(info, pkg) msg = "{0}<b>{1}</b>".format(info, pkg)
self.output.write(msg)
# 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: if te_current == te_total:
self.output.write("\n") self.output.write("\n")
elif te_current == te_total:
self.output.write(msg + "\n")
def filelog(self, package, action): def filelog(self, package, action):
if self.fileaction.get(action) == "Installed": if self.fileaction.get(action) == "Installed":