# Copyright 2005 Duke University
# Copyright (C) 2012-2016 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""Handle actual output from the cli."""
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
import fnmatch
import hawkey
import itertools
import libdnf.transaction
import logging
import operator
import pwd
import re
import sys
import time
from dnf.cli.format import format_number, format_time
from dnf.i18n import _, C_, P_, ucd, fill_exact_width, textwrap_fill, exact_width, select_short_long
from dnf.pycomp import xrange, basestring, long, unicode, sys_maxsize
from dnf.yum.rpmtrans import TransactionDisplay
from dnf.db.history import MergedTransactionWrapper
import dnf.base
import dnf.callback
import dnf.cli.progress
import dnf.cli.term
import dnf.conf
import dnf.crypto
import dnf.i18n
import dnf.transaction
import dnf.util
import dnf.yum.misc
logger = logging.getLogger('dnf')
def _spread_in_columns(cols_count, label, lst):
left = itertools.chain((label,), itertools.repeat(''))
lst_length = len(lst)
right_count = cols_count - 1
missing_items = -lst_length % right_count
if not lst_length:
lst = itertools.repeat('', right_count)
elif missing_items:
lst.extend(('',) * missing_items)
lst_iter = iter(lst)
return list(zip(left, *[lst_iter] * right_count))
class Output(object):
"""Main output class for the yum command line."""
GRP_PACKAGE_INDENT = ' ' * 3
FILE_PROVIDE_RE = re.compile(r'^\*{0,2}/')
def __init__(self, base, conf):
self.conf = conf
self.base = base
self.term = dnf.cli.term.Term()
self.progress = None
def _banner(self, col_data, row):
term_width = self.term.columns
rule = '%s' % '=' * term_width
header = self.fmtColumns(zip(row, col_data), ' ')
return rule, header, rule
def _col_widths(self, rows):
col_data = [dict() for _ in rows[0]]
for row in rows:
for (i, val) in enumerate(row):
col_dct = col_data[i]
length = len(val)
col_dct[length] = col_dct.get(length, 0) + 1
cols = self.calcColumns(col_data, None, indent=' ')
# align to the left
return list(map(operator.neg, cols))
def _highlight(self, highlight):
hibeg = ''
hiend = ''
if not highlight:
pass
elif not isinstance(highlight, basestring) or highlight == 'bold':
hibeg = self.term.MODE['bold']
elif highlight == 'normal':
pass # Minor opt.
else:
# Turn a string into a specific output: colour, bold, etc.
for high in highlight.replace(',', ' ').split():
if high == 'normal':
hibeg = ''
elif high in self.term.MODE:
hibeg += self.term.MODE[high]
elif high in self.term.FG_COLOR:
hibeg += self.term.FG_COLOR[high]
elif (high.startswith('fg:') and
high[3:] in self.term.FG_COLOR):
hibeg += self.term.FG_COLOR[high[3:]]
elif (high.startswith('bg:') and
high[3:] in self.term.BG_COLOR):
hibeg += self.term.BG_COLOR[high[3:]]
if hibeg:
hiend = self.term.MODE['normal']
return (hibeg, hiend)
def _sub_highlight(self, haystack, highlight, needles, **kwds):
hibeg, hiend = self._highlight(highlight)
return self.term.sub(haystack, hibeg, hiend, needles, **kwds)
@staticmethod
def _calc_columns_spaces_helps(current, data_tups, left):
""" Spaces left on the current field will help how many pkgs? """
ret = 0
for tup in data_tups:
if left < (tup[0] - current):
break
ret += tup[1]
return ret
@property
def history(self):
return self.base.history
@property
def sack(self):
return self.base.sack
def calcColumns(self, data, columns=None, remainder_column=0,
total_width=None, indent=''):
"""Dynamically calculate the widths of the columns that the
fields in data should be placed into for output.
:param data: a list of dictionaries that represent the data to
be output. Each dictionary in the list corresponds to a
column of output. The keys of the dictionary are the
lengths of the items to be output, and the value associated
with a key is the number of items of that length.
:param columns: a list containing the minimum amount of space
that must be allocated for each row. This can be used to
ensure that there is space available in a column if, for
example, the actual lengths of the items being output
cannot be given in *data*
:param remainder_column: number of the column to receive a few
extra spaces that may remain after other allocation has
taken place
:param total_width: the total width of the output.
self.term.real_columns is used by default
:param indent: string that will be prefixed to a line of
output to create e.g. an indent
:return: a list of the widths of the columns that the fields
in data should be placed into for output
"""
cols = len(data)
# Convert the data to ascending list of tuples, (field_length, pkgs)
pdata = data
data = [None] * cols # Don't modify the passed in data
for d in range(0, cols):
data[d] = sorted(pdata[d].items())
if total_width is None:
total_width = self.term.real_columns
# We start allocating 1 char to everything but the last column, and a
# space between each (again, except for the last column). Because
# at worst we are better with:
# |one two three|
# | four |
# ...than:
# |one two three|
# | f|
# |our |
# ...the later being what we get if we pre-allocate the last column, and
# thus. the space, due to "three" overflowing it's column by 2 chars.
if columns is None:
columns = [1] * (cols - 1)
columns.append(0)
# i'm not able to get real terminal width so i'm probably
# running in non interactive terminal (pipe to grep, redirect to file...)
# avoid splitting lines to enable filtering output
if not total_width:
full_columns = []
for d in xrange(0, cols):
col = data[d]
if col:
full_columns.append(col[-1][0])
else:
full_columns.append(columns[d] + 1)
full_columns[0] += len(indent)
# if possible, try to keep default width (usually 80 columns)
default_width = self.term.columns
if sum(full_columns) > default_width:
return full_columns
total_width = default_width
total_width -= (sum(columns) + (cols - 1) + exact_width(indent))
if not columns[-1]:
total_width += 1
while total_width > 0:
# Find which field all the spaces left will help best
helps = 0
val = 0
for d in xrange(0, cols):
thelps = self._calc_columns_spaces_helps(columns[d], data[d],
total_width)
if not thelps:
continue
# We prefer to overflow: the last column, and then earlier
# columns. This is so that in the best case (just overflow the
# last) ... grep still "works", and then we make it prettier.
if helps and (d == (cols - 1)) and (thelps / 2) < helps:
continue
if thelps < helps:
continue
helps = thelps
val = d
# If we found a column to expand, move up to the next level with
# that column and start again with any remaining space.
if helps:
diff = data[val].pop(0)[0] - columns[val]
if not columns[val] and (val == (cols - 1)):
# If we are going from 0 => N on the last column, take 1
# for the space before the column.
total_width -= 1
columns[val] += diff
total_width -= diff
continue
overflowed_columns = 0
for d in xrange(0, cols):
if not data[d]:
continue
overflowed_columns += 1
if overflowed_columns:
# Split the remaining spaces among each overflowed column
# equally
norm = total_width // overflowed_columns
for d in xrange(0, cols):
if not data[d]:
continue
columns[d] += norm
total_width -= norm
# Split the remaining spaces among each column equally, except the
# last one. And put the rest into the remainder column
cols -= 1
norm = total_width // cols
for d in xrange(0, cols):
columns[d] += norm
columns[remainder_column] += total_width - (cols * norm)
total_width = 0
return columns
@staticmethod
def _fmt_column_align_width(width):
"""Returns tuple of (align_left, width)"""
if width < 0:
return (True, -width)
return (False, width)
def _col_data(self, col_data):
assert len(col_data) == 2 or len(col_data) == 3
if len(col_data) == 2:
(val, width) = col_data
hibeg = hiend = ''
if len(col_data) == 3:
(val, width, highlight) = col_data
(hibeg, hiend) = self._highlight(highlight)
return (ucd(val), width, hibeg, hiend)
def fmtColumns(self, columns, msg=u'', end=u''):
"""Return a row of data formatted into a string for output.
Items can overflow their columns.
:param columns: a list of tuples containing the data to
output. Each tuple contains first the item to be output,
then the amount of space allocated for the column, and then
optionally a type of highlighting for the item
:param msg: a string to begin the line of output with
:param end: a string to end the line of output with
:return: a row of data formatted into a string for output
"""
columns = list(columns)
total_width = len(msg)
data = []
for col_data in columns[:-1]:
(val, width, hibeg, hiend) = self._col_data(col_data)
if not width: # Don't count this column, invisible text
msg += u"%s"
data.append(val)
continue
(align_left, width) = self._fmt_column_align_width(width)
val_width = exact_width(val)
if val_width <= width:
# Don't use fill_exact_width() because it sucks performance
# wise for 1,000s of rows. Also allows us to use len(), when
# we can.
msg += u"%s%s%s%s "
if align_left:
data.extend([hibeg, val, " " * (width - val_width), hiend])
else:
data.extend([hibeg, " " * (width - val_width), val, hiend])
else:
msg += u"%s%s%s\n" + " " * (total_width + width + 1)
data.extend([hibeg, val, hiend])
total_width += width
total_width += 1
(val, width, hibeg, hiend) = self._col_data(columns[-1])
(align_left, width) = self._fmt_column_align_width(width)
val = fill_exact_width(val, width, left=align_left,
prefix=hibeg, suffix=hiend)
msg += u"%%s%s" % end
data.append(val)
return msg % tuple(data)
def simpleList(self, pkg, ui_overflow=False, indent='', highlight=False,
columns=None):
"""Print a package as a line.
:param pkg: the package to be printed
:param ui_overflow: unused
:param indent: string to be prefixed onto the line to provide
e.g. an indent
:param highlight: highlighting options for the name of the
package
:param columns: tuple containing the space allocated for each
column of output. The columns are the package name, version,
and repository
"""
if columns is None:
columns = (-40, -22, -16) # Old default
na = '%s%s.%s' % (indent, pkg.name, pkg.arch)
hi_cols = [highlight, 'normal', 'normal']
columns = zip((na, pkg.evr, pkg._from_repo), columns, hi_cols)
print(self.fmtColumns(columns))
def simpleEnvraList(self, pkg, ui_overflow=False,
indent='', highlight=False, columns=None):
"""Print a package as a line, with the package itself in envra
format so it can be passed to list/install/etc.
:param pkg: the package to be printed
:param ui_overflow: unused
:param indent: string to be prefixed onto the line to provide
e.g. an indent
:param highlight: highlighting options for the name of the
package
:param columns: tuple containing the space allocated for each
column of output. The columns the are the package envra and
repository
"""
if columns is None:
columns = (-63, -16) # Old default
envra = '%s%s' % (indent, ucd(pkg))
hi_cols = [highlight, 'normal', 'normal']
rid = pkg.ui_from_repo
columns = zip((envra, rid), columns, hi_cols)
print(self.fmtColumns(columns))
def simple_name_list(self, pkg):
"""Print a package as a line containing its name."""
print(ucd(pkg.name))
def simple_nevra_list(self, pkg):
"""Print a package as a line containing its NEVRA."""
print(ucd(pkg))
def fmtKeyValFill(self, key, val):
"""Return a key value pair in the common two column output
format.
:param key: the key to be formatted
:param val: the value associated with *key*
:return: the key value pair formatted in two columns for output
"""
keylen = exact_width(key)
cols = self.term.real_columns
if not cols:
cols = sys_maxsize
elif cols < 20:
cols = 20
nxt = ' ' * (keylen - 2) + ': '
if not val:
# textwrap.fill in case of empty val returns empty string
return key
val = ucd(val)
ret = textwrap_fill(val, width=cols, initial_indent=key,
subsequent_indent=nxt)
if ret.count("\n") > 1 and keylen > (cols // 3):
# If it's big, redo it again with a smaller subsequent off
ret = textwrap_fill(val, width=cols, initial_indent=key,
subsequent_indent=' ...: ')
return ret
def fmtSection(self, name, fill='='):
"""Format and return a section header. The format of the
header is a line with *name* centered, and *fill* repeated on
either side to fill an entire line on the terminal.
:param name: the name of the section
:param fill: the character to repeat on either side of *name*
to fill an entire line. *fill* must be a single character.
:return: a string formatted to be a section header
"""
name = ucd(name)
cols = self.term.columns - 2
name_len = exact_width(name)
if name_len >= (cols - 4):
beg = end = fill * 2
else:
beg = fill * ((cols - name_len) // 2)
end = fill * (cols - name_len - len(beg))
return "%s %s %s" % (beg, name, end)
def infoOutput(self, pkg, highlight=False):
"""Print information about the given package.
:param pkg: the package to print information about
:param highlight: highlighting options for the name of the
package
"""
def format_key_val(key, val):
return " ".join([fill_exact_width(key, 12, 12), ":", str(val)])
def format_key_val_fill(key, val):
return self.fmtKeyValFill(fill_exact_width(key, 12, 12) + " : ", val or "")
output_list = []
(hibeg, hiend) = self._highlight(highlight)
# Translators: This is abbreviated 'Name'. Should be no longer
# than 12 characters. You can use the full version if it is short
# enough in your language.
key = select_short_long(12, C_("short", "Name"),
C_("long", "Name"))
output_list.append(format_key_val(key,
"%s%s%s" % (hibeg, pkg.name, hiend)))
if pkg.epoch:
# Translators: This message should be no longer than 12 characters.
output_list.append(format_key_val(_("Epoch"), pkg.epoch))
key = select_short_long(12, C_("short", "Version"),
C_("long", "Version"))
output_list.append(format_key_val(key, pkg.version))
# Translators: This message should be no longer than 12 characters.
output_list.append(format_key_val(_("Release"), pkg.release))
key = select_short_long(12, C_("short", "Arch"),
C_("long", "Architecture"))
output_list.append(format_key_val(key, pkg.arch))
key = select_short_long(12, C_("short", "Size"), C_("long", "Size"))
output_list.append(format_key_val(key,
format_number(float(pkg._size))))
# Translators: This message should be no longer than 12 characters.
output_list.append(format_key_val(_("Source"), pkg.sourcerpm))
key = select_short_long(12, C_("short", "Repo"),
C_("long", "Repository"))
output_list.append(format_key_val(key, pkg.repoid))
if pkg._from_system:
history_repo = self.history.repo(pkg)
if history_repo:
# Translators: This message should be no longer than 12 chars.
output_list.append(format_key_val(_("From repo"), history_repo))
if self.conf.verbose:
# :hawkey does not support changelog information
# print(_("Committer : %s") % ucd(pkg.committer))
# print(_("Committime : %s") % time.ctime(pkg.committime))
# Translators: This message should be no longer than 12 characters.
output_list.append(format_key_val(_("Packager"), pkg.packager))
# Translators: This message should be no longer than 12 characters.
output_list.append(format_key_val(_("Buildtime"),
dnf.util.normalize_time(pkg.buildtime)))
if pkg.installtime:
# Translators: This message should be no longer than 12 characters.
output_list.append(format_key_val(_("Install time"),
dnf.util.normalize_time(pkg.installtime)))
history_pkg = self.history.package_data(pkg)
if history_pkg:
try:
uid = int(history_pkg._item.getInstalledBy())
except ValueError: # In case int() fails
uid = None
# Translators: This message should be no longer than 12 chars.
output_list.append(format_key_val(_("Installed by"), self._pwd_ui_username(uid)))
# Translators: This is abbreviated 'Summary'. Should be no longer
# than 12 characters. You can use the full version if it is short
# enough in your language.
key = select_short_long(12, C_("short", "Summary"),
C_("long", "Summary"))
output_list.append(format_key_val_fill(key, pkg.summary))
if pkg.url:
output_list.append(format_key_val(_("URL"), ucd(pkg.url)))
# Translators: This message should be no longer than 12 characters.
output_list.append(format_key_val_fill(_("License"), pkg.license))
# Translators: This is abbreviated 'Description'. Should be no longer
# than 12 characters. You can use the full version if it is short
# enough in your language.
key = select_short_long(12, C_("short", "Description"),
C_("long", "Description"))
output_list.append(format_key_val_fill(key, pkg.description))
return "\n".join(output_list)
def updatesObsoletesList(self, uotup, changetype, columns=None):
"""Print a simple string that explains the relationship
between the members of an update or obsoletes tuple.
:param uotup: an update or obsoletes tuple. The first member
is the new package, and the second member is the old
package
:param changetype: a string indicating what the change between
the packages is, e.g. 'updates' or 'obsoletes'
:param columns: a tuple containing information about how to
format the columns of output. The absolute value of each
number in the tuple indicates how much space has been
allocated for the corresponding column. If the number is
negative, the text in the column will be left justified,
and if it is positive, the text will be right justified.
The columns of output are the package name, version, and repository
"""
(changePkg, instPkg) = uotup
if columns is not None:
# New style, output all info. for both old/new with old indented
chi = self.conf.color_update_remote
if changePkg.reponame != hawkey.SYSTEM_REPO_NAME:
chi = self.conf.color_update_local
self.simpleList(changePkg, columns=columns, highlight=chi)
self.simpleList(instPkg, columns=columns, indent=' ' * 4,
highlight=self.conf.color_update_installed)
return
# Old style
c_compact = changePkg.compactPrint()
i_compact = '%s.%s' % (instPkg.name, instPkg.arch)
c_repo = changePkg.repoid
print('%-35.35s [%.12s] %.10s %-20.20s' %
(c_compact, c_repo, changetype, i_compact))
def listPkgs(self, lst, description, outputType, highlight_na={},
columns=None, highlight_modes={}):
"""Prints information about the given list of packages.
:param lst: a list of packages to print information about
:param description: string describing what the list of
packages contains, e.g. 'Available Packages'
:param outputType: The type of information to be printed.
Current options::
'list' - simple pkg list
'info' - similar to rpm -qi output
'name' - simple name list
'nevra' - simple nevra list
:param highlight_na: a dictionary containing information about
packages that should be highlighted in the output. The
dictionary keys are (name, arch) tuples for the package,
and the associated values are the package objects
themselves.
:param columns: a tuple containing information about how to
format the columns of output. The absolute value of each
number in the tuple indicates how much space has been
allocated for the corresponding column. If the number is
negative, the text in the column will be left justified,
and if it is positive, the text will be right justified.
The columns of output are the package name, version, and
repository
:param highlight_modes: dictionary containing information
about to highlight the packages in *highlight_na*.
*highlight_modes* should contain the following keys::
'not_in' - highlighting used for packages not in *highlight_na*
'=' - highlighting used when the package versions are equal
'<' - highlighting used when the package has a lower version
number
'>' - highlighting used when the package has a higher version
number
:return: number of packages listed
"""
if outputType in ['list', 'info', 'name', 'nevra']:
if len(lst) > 0:
print('%s' % description)
info_set = set()
if outputType == 'list':
unique_item_dict = {}
for pkg in lst:
unique_item_dict[str(pkg) + str(pkg._from_repo)] = pkg
lst = unique_item_dict.values()
for pkg in sorted(lst):
key = (pkg.name, pkg.arch)
highlight = False
if key not in highlight_na:
highlight = highlight_modes.get('not in', 'normal')
elif pkg.evr_eq(highlight_na[key]):
highlight = highlight_modes.get('=', 'normal')
elif pkg.evr_lt(highlight_na[key]):
highlight = highlight_modes.get('>', 'bold')
else:
highlight = highlight_modes.get('<', 'normal')
if outputType == 'list':
self.simpleList(pkg, ui_overflow=True,
highlight=highlight, columns=columns)
elif outputType == 'info':
info_set.add(self.infoOutput(pkg, highlight=highlight) + "\n")
elif outputType == 'name':
self.simple_name_list(pkg)
elif outputType == 'nevra':
self.simple_nevra_list(pkg)
else:
pass
if info_set:
print("\n".join(sorted(info_set)))
return len(lst)
def userconfirm(self, msg=None, defaultyes_msg=None):
"""Get a yes or no from the user, and default to No
:msg: String for case with [y/N]
:defaultyes_msg: String for case with [Y/n]
:return: True if the user selects yes, and False if the user
selects no
"""
yui = (ucd(_('y')), ucd(_('yes')))
nui = (ucd(_('n')), ucd(_('no')))
aui = yui + nui
while True:
if msg is None:
msg = _('Is this ok [y/N]: ')
choice = ''
if self.conf.defaultyes:
if defaultyes_msg is None:
msg = _('Is this ok [Y/n]: ')
else:
msg = defaultyes_msg
try:
choice = dnf.i18n.ucd_input(msg)
except EOFError:
pass
except KeyboardInterrupt:
choice = nui[0]
choice = ucd(choice).lower()
if len(choice) == 0:
choice = yui[0] if self.conf.defaultyes else nui[0]
if choice in aui:
break
# If the English one letter names don't mix with the translated
# letters, allow them too:
if u'y' == choice and u'y' not in aui:
choice = yui[0]
break
if u'n' == choice and u'n' not in aui:
choice = nui[0]
break
if choice in yui:
return True
return False
def _pkgs2name_dict(self, sections):
installed = self.sack.query().installed()._name_dict()
available = self.sack.query().available()._name_dict()
d = {}
for pkg_name in itertools.chain(*list(zip(*sections))[1]):
if pkg_name in installed:
d[pkg_name] = installed[pkg_name][0]
elif pkg_name in available:
d[pkg_name] = available[pkg_name][0]
return d
def _pkgs2col_lengths(self, sections, name_dict):
nevra_lengths = {}
repo_lengths = {}
for pkg_name in itertools.chain(*list(zip(*sections))[1]):
pkg = name_dict.get(pkg_name)
if pkg is None:
continue
nevra_l = exact_width(ucd(pkg)) + exact_width(self.GRP_PACKAGE_INDENT)
repo_l = exact_width(ucd(pkg.reponame))
nevra_lengths[nevra_l] = nevra_lengths.get(nevra_l, 0) + 1
repo_lengths[repo_l] = repo_lengths.get(repo_l, 0) + 1
return (nevra_lengths, repo_lengths)
def _display_packages(self, pkg_names):
for name in pkg_names:
print('%s%s' % (self.GRP_PACKAGE_INDENT, name))
def _display_packages_verbose(self, pkg_names, name_dict, columns):
for name in pkg_names:
try:
pkg = name_dict[name]
except KeyError:
# package not in any repo -> print only package name
print('%s%s' % (self.GRP_PACKAGE_INDENT, name))
continue
highlight = False
if not pkg._from_system:
highlight = self.conf.color_list_available_install
self.simpleEnvraList(pkg, ui_overflow=True,
indent=self.GRP_PACKAGE_INDENT,
highlight=highlight,
columns=columns)
def display_pkgs_in_groups(self, group):
"""Output information about the packages in a given group
:param group: a Group object to output information about
"""
def names(packages):
return sorted(dnf.util._name_unset_wrapper(pkg.name) for pkg in packages)
print(_('Group: %s') % dnf.util._name_unset_wrapper(group.ui_name))
verbose = self.conf.verbose
if verbose:
print(_(' Group-Id: %s') % ucd(group.id))
if group.ui_description:
print(_(' Description: %s') % ucd(group.ui_description) or "")
if group.lang_only:
print(_(' Language: %s') % group.lang_only)
sections = (
(_(' Mandatory Packages:'), names(group.mandatory_packages)),
(_(' Default Packages:'), names(group.default_packages)),
(_(' Optional Packages:'), names(group.optional_packages)),
(_(' Conditional Packages:'), names(group.conditional_packages)))
if verbose:
name_dict = self._pkgs2name_dict(sections)
col_lengths = self._pkgs2col_lengths(sections, name_dict)
columns = self.calcColumns(col_lengths)
columns = (-columns[0], -columns[1])
for (section_name, packages) in sections:
if len(packages) < 1:
continue
print(section_name)
self._display_packages_verbose(packages, name_dict, columns)
else:
for (section_name, packages) in sections:
if len(packages) < 1:
continue
print(section_name)
self._display_packages(packages)
def display_groups_in_environment(self, environment):
"""Output information about the packages in a given environment
:param environment: an Environment object to output information about
"""
def names(groups):
return sorted(dnf.util._name_unset_wrapper(group.name) for group in groups)
print(_('Environment Group: %s') % dnf.util._name_unset_wrapper(environment.ui_name))
if self.conf.verbose:
print(_(' Environment-Id: %s') % ucd(environment.id))
if environment.ui_description:
description = ucd(environment.ui_description) or ""
print(_(' Description: %s') % description)
sections = (
(_(' Mandatory Groups:'), names(environment.mandatory_groups)),
(_(' Optional Groups:'), names(environment.optional_groups)))
for (section_name, packages) in sections:
if len(packages) < 1:
continue
print(section_name)
self._display_packages(packages)
def matchcallback(self, po, values, matchfor=None, verbose=None,
highlight=None):
"""Output search/provides type callback matches.
:param po: the package object that matched the search
:param values: the information associated with *po* that
matched the search
:param matchfor: a list of strings to be highlighted in the
output
:param verbose: whether to output extra verbose information
:param highlight: highlighting options for the highlighted matches
"""
def print_highlighted_key_item(key, item, printed_headline, can_overflow=False):
if not printed_headline:
print(_('Matched from:'))
item = ucd(item) or ""
if item == "":
return
if matchfor:
item = self._sub_highlight(item, highlight, matchfor, ignore_case=True)
if can_overflow:
print(self.fmtKeyValFill(key, item))
else:
print(key % item)
def print_file_provides(item, printed_match):
if not self.FILE_PROVIDE_RE.match(item):
return False
key = _("Filename : %s")
file_match = False
for filename in po.files:
if fnmatch.fnmatch(filename, item):
print_highlighted_key_item(
key, filename, file_match or printed_match, can_overflow=False)
file_match = True
return file_match
if self.conf.showdupesfromrepos:
msg = '%s : ' % po
else:
msg = '%s.%s : ' % (po.name, po.arch)
msg = self.fmtKeyValFill(msg, po.summary or "")
if matchfor:
if highlight is None:
highlight = self.conf.color_search_match
msg = self._sub_highlight(msg, highlight, matchfor, ignore_case=True)
print(msg)
if verbose is None:
verbose = self.conf.verbose
if not verbose:
return
print(_("Repo : %s") % po.ui_from_repo)
printed_match = False
name_match = False
for item in set(values):
if po.summary == item:
name_match = True
continue # Skip double name/summary printing
if po.description == item:
key = _("Description : ")
print_highlighted_key_item(key, item, printed_match, can_overflow=True)
printed_match = True
elif po.url == item:
key = _("URL : %s")
print_highlighted_key_item(key, item, printed_match, can_overflow=False)
printed_match = True
elif po.license == item:
key = _("License : %s")
print_highlighted_key_item(key, item, printed_match, can_overflow=False)
printed_match = True
elif print_file_provides(item, printed_match):
printed_match = True
else:
key = _("Provide : %s")
for provide in po.provides:
provide = str(provide)
if fnmatch.fnmatch(provide, item):
print_highlighted_key_item(key, provide, printed_match, can_overflow=False)
printed_match = True
else:
first_provide = provide.split()[0]
possible = set('=<>')
if any((char in possible) for char in item):
item_new = item.split()[0]
else:
item_new = item
if fnmatch.fnmatch(first_provide, item_new):
print_highlighted_key_item(
key, provide, printed_match, can_overflow=False)
printed_match = True
if not any([printed_match, name_match]):
for item in set(values):
key = _("Other : %s")
print_highlighted_key_item(key, item, printed_match, can_overflow=False)
print()
def matchcallback_verbose(self, po, values, matchfor=None):
"""Output search/provides type callback matches. This will
output more information than :func:`matchcallback`.
:param po: the package object that matched the search
:param values: the information associated with *po* that
matched the search
:param matchfor: a list of strings to be highlighted in the
output
"""
return self.matchcallback(po, values, matchfor, verbose=True)
def reportDownloadSize(self, packages, installonly=False):
"""Report the total download size for a set of packages
:param packages: a list of package objects
:param installonly: whether the transaction consists only of installations
"""
totsize = 0
locsize = 0
insize = 0
error = False
for pkg in packages:
# Just to be on the safe side, if for some reason getting
# the package size fails, log the error and don't report download
# size
try:
size = int(pkg._size)
totsize += size
try:
if pkg.verifyLocalPkg():
locsize += size
except Exception:
pass
if not installonly:
continue
try:
size = int(pkg.installsize)
except Exception:
pass
insize += size
except Exception:
error = True
msg = _('There was an error calculating total download size')
logger.error(msg)
break
if not error:
if locsize:
logger.info(_("Total size: %s"),
format_number(totsize))
if locsize != totsize:
logger.info(_("Total download size: %s"),
format_number(totsize - locsize))
if installonly:
logger.info(_("Installed size: %s"), format_number(insize))
def reportRemoveSize(self, packages):
"""Report the total size of packages being removed.
:param packages: a list of package objects
"""
totsize = 0
error = False
for pkg in packages:
# Just to be on the safe side, if for some reason getting
# the package size fails, log the error and don't report download
# size
try:
size = pkg._size
totsize += size
except Exception:
error = True
msg = _('There was an error calculating installed size')
logger.error(msg)
break
if not error:
logger.info(_("Freed space: %s"), format_number(totsize))
def list_group_transaction(self, comps, history, diff):
if not diff:
return None
out = []
rows = []
if diff.new_groups:
out.append(_('Marking packages as installed by the group:'))
for grp_id in diff.new_groups:
pkgs = list(diff.added_packages(grp_id))
group_object = comps._group_by_id(grp_id)
grp_name = group_object.ui_name if group_object else grp_id
rows.extend(_spread_in_columns(4, "@" + grp_name, pkgs))
if diff.removed_groups:
out.append(_('Marking packages as removed by the group:'))
for grp_id in diff.removed_groups:
pkgs = list(diff.removed_packages(grp_id))
grp_name = history.group.get(grp_id).ui_name
rows.extend(_spread_in_columns(4, "@" + grp_name, pkgs))
if rows:
col_data = self._col_widths(rows)
for row in rows:
out.append(self.fmtColumns(zip(row, col_data), ' '))
out[0:0] = self._banner(col_data, (_('Group'), _('Packages'), '', ''))
return '\n'.join(out)
def list_transaction(self, transaction, total_width=None):
"""Return a string representation of the transaction in an
easy-to-read format.
"""
forward_actions = hawkey.UPGRADE | hawkey.UPGRADE_ALL | hawkey.DISTUPGRADE | \
hawkey.DISTUPGRADE_ALL | hawkey.DOWNGRADE | hawkey.INSTALL
skipped_conflicts = set()
skipped_broken = set()
if transaction is None:
# set empty transaction list instead of returning None
# in order to display module changes when RPM transaction is empty
transaction = []
list_bunch = dnf.util._make_lists(transaction)
pkglist_lines = []
data = {'n' : {}, 'v' : {}, 'r' : {}}
a_wid = 0 # Arch can't get "that big" ... so always use the max.
def _add_line(lines, data, a_wid, po, obsoletes=[]):
(n, a, e, v, r) = po.pkgtup
evr = po.evr
repoid = po._from_repo
size = format_number(po._size)
if a is None: # gpgkeys are weird
a = 'noarch'
# none, partial, full?
if po._from_system:
hi = self.conf.color_update_installed
elif po._from_cmdline:
hi = self.conf.color_update_local
else:
hi = self.conf.color_update_remote
lines.append((n, a, evr, repoid, size, obsoletes, hi))
# Create a dict of field_length => number of packages, for
# each field.
for (d, v) in (("n", len(n)), ("v", len(evr)), ("r", len(repoid))):
data[d].setdefault(v, 0)
data[d][v] += 1
a_wid = max(a_wid, len(a))
return a_wid
ins_group_msg = _('Installing group/module packages') if dnf.base.WITH_MODULES \
else _('Installing group packages')
for (action, pkglist) in [
# TRANSLATORS: This is for a list of packages to be installed.
(C_('summary', 'Installing'), list_bunch.installed),
# TRANSLATORS: This is for a list of packages to be upgraded.
(C_('summary', 'Upgrading'), list_bunch.upgraded),
# TRANSLATORS: This is for a list of packages to be reinstalled.
(C_('summary', 'Reinstalling'), list_bunch.reinstalled),
(ins_group_msg, list_bunch.installed_group),
(_('Installing dependencies'), list_bunch.installed_dep),
(_('Installing weak dependencies'), list_bunch.installed_weak),
# TRANSLATORS: This is for a list of packages to be removed.
(_('Removing'), list_bunch.erased),
(_('Removing dependent packages'), list_bunch.erased_dep),
(_('Removing unused dependencies'), list_bunch.erased_clean),
# TRANSLATORS: This is for a list of packages to be downgraded.
(C_('summary', 'Downgrading'), list_bunch.downgraded)]:
lines = []
# build a reverse mapping to 'replaced_by'
# this is required to achieve reasonable speed
replaces = {}
for tsi in transaction:
if tsi.action != libdnf.transaction.TransactionItemAction_OBSOLETED:
continue
for i in tsi._item.getReplacedBy():
replaces.setdefault(i, set()).add(tsi)
for tsi in sorted(pkglist, key=lambda x: x.pkg):
if tsi.action not in dnf.transaction.FORWARD_ACTIONS + [libdnf.transaction.TransactionItemAction_REMOVE]:
continue
# get TransactionItems obsoleted by tsi
obsoleted = sorted(replaces.get(tsi._item, []))
a_wid = _add_line(lines, data, a_wid, tsi.pkg, obsoleted)
pkglist_lines.append((action, lines))
installedProfiles = sorted(dict(self.base._moduleContainer.getInstalledProfiles()).items())
if installedProfiles:
action = _("Installing module profiles")
lines = []
for name, profiles in installedProfiles:
for profile in list(profiles):
lines.append(("%s/%s" % (name, profile), "", "", "", "", "", ""))
pkglist_lines.append((action, lines))
removedProfiles = sorted(dict(self.base._moduleContainer.getRemovedProfiles()).items())
if removedProfiles:
action = _("Disabling module profiles")
lines = []
for name, profiles in removedProfiles:
for profile in list(profiles):
lines.append(("%s/%s" % (name, profile), "", "", "", "", "", ""))
pkglist_lines.append((action, lines))
enabledStreams = sorted(dict(self.base._moduleContainer.getEnabledStreams()).items())
if enabledStreams:
action = _("Enabling module streams")
lines = []
for name, stream in enabledStreams:
lines.append((name, "", stream, "", "", "", ""))
pkglist_lines.append((action, lines))
switchedStreams = sorted(dict(self.base._moduleContainer.getSwitchedStreams()).items())
if switchedStreams:
action = _("Switching module streams")
lines = []
for name, stream in switchedStreams:
lines.append((name, "", "%s -> %s" % (stream[0], stream[1]), "", "", "", ""))
pkglist_lines.append((action, lines))
disabledModules = sorted(list(self.base._moduleContainer.getDisabledModules()))
if disabledModules:
action = _("Disabling modules")
lines = []
for name in disabledModules:
lines.append((name, "", "", "", "", "", ""))
pkglist_lines.append((action, lines))
resetModules = sorted(list(self.base._moduleContainer.getResetModules()))
if resetModules:
action = _("Resetting modules")
lines = []
for name in resetModules:
lines.append((name, "", "", "", "", "", ""))
pkglist_lines.append((action, lines))
if self.base._history:
def format_line(group):
return (dnf.util._name_unset_wrapper(group.getName()), "", "", "", "", "", "")
install_env_group = self.base._history.env._installed
if install_env_group:
action = _("Installing Environment Groups")
lines = []
for group in install_env_group.values():
lines.append(format_line(group))
pkglist_lines.append((action, lines))
upgrade_env_group = self.base._history.env._upgraded
if upgrade_env_group:
action = _("Upgrading Environment Groups")
lines = []
for group in upgrade_env_group.values():
lines.append(format_line(group))
pkglist_lines.append((action, lines))
remove_env_group = self.base._history.env._removed
if remove_env_group:
action = _("Removing Environment Groups")
lines = []
for group in remove_env_group.values():
lines.append(format_line(group))
pkglist_lines.append((action, lines))
install_group = self.base._history.group._installed
if install_group:
action = _("Installing Groups")
lines = []
for group in install_group.values():
lines.append(format_line(group))
pkglist_lines.append((action, lines))
upgrade_group = self.base._history.group._upgraded
if upgrade_group:
action = _("Upgrading Groups")
lines = []
for group in upgrade_group.values():
lines.append(format_line(group))
pkglist_lines.append((action, lines))
remove_group = self.base._history.group._removed
if remove_group:
action = _("Removing Groups")
lines = []
for group in remove_group.values():
lines.append(format_line(group))
pkglist_lines.append((action, lines))
# show skipped conflicting packages
if (not self.conf.best or not self.conf.strict) and self.base._goal.actions & forward_actions:
lines = []
skipped_conflicts, skipped_broken = self.base._skipped_packages(
report_problems=True, transaction=transaction)
skipped_broken = dict((str(pkg), pkg) for pkg in skipped_broken)
for pkg in sorted(skipped_conflicts):
a_wid = _add_line(lines, data, a_wid, pkg, [])
recommendations = ["--best"]
if not self.base._allow_erasing:
recommendations.append("--allowerasing")
skip_str = _("Skipping packages with conflicts:\n"
"(add '%s' to command line "
"to force their upgrade)") % " ".join(recommendations)
# remove misleading green color from the "packages with conflicts" lines
lines = [i[:-1] + ("", ) for i in lines]
pkglist_lines.append((skip_str, lines))
lines = []
for nevra, pkg in sorted(skipped_broken.items()):
a_wid = _add_line(lines, data, a_wid, pkg, [])
skip_str = _("Skipping packages with broken dependencies%s")
if self.base.conf.upgrade_group_objects_upgrade:
skip_str = skip_str % ""
else:
skip_str = skip_str % _(" or part of a group")
# remove misleading green color from the "broken dependencies" lines
lines = [i[:-1] + ("", ) for i in lines]
pkglist_lines.append((skip_str, lines))
output_width = self.term.columns
if not data['n'] and not self.base._moduleContainer.isChanged() and not \
(self.base._history and (self.base._history.group or self.base._history.env)):
return u''
else:
data = [data['n'], {}, data['v'], data['r'], {}]
columns = [1, a_wid, 1, 1, 5]
columns = self.calcColumns(data, indent=" ", columns=columns,
remainder_column=2, total_width=total_width)
(n_wid, a_wid, v_wid, r_wid, s_wid) = columns
real_width = sum(columns) + 5
output_width = output_width if output_width >= real_width else real_width
# Do not use 'Package' without context. Using context resolves
# RhBug 1302935 as a side effect.
msg_package = select_short_long(n_wid,
# Translators: This is the short version of 'Package'. You can
# use the full (unabbreviated) term 'Package' if you think that
# the translation to your language is not too long and will
# always fit to limited space.
C_('short', 'Package'),
# Translators: This is the full (unabbreviated) term 'Package'.
C_('long', 'Package'))
msg_arch = select_short_long(a_wid,
# Translators: This is abbreviated 'Architecture', used when
# we have not enough space to display the full word.
C_('short', 'Arch'),
# Translators: This is the full word 'Architecture', used when
# we have enough space.
C_('long', 'Architecture'))
msg_version = select_short_long(v_wid,
# Translators: This is the short version of 'Version'. You can
# use the full (unabbreviated) term 'Version' if you think that
# the translation to your language is not too long and will
# always fit to limited space.
C_('short', 'Version'),
# Translators: This is the full (unabbreviated) term 'Version'.
C_('long', 'Version'))
msg_repository = select_short_long(r_wid,
# Translators: This is abbreviated 'Repository', used when
# we have not enough space to display the full word.
C_('short', 'Repo'),
# Translators: This is the full word 'Repository', used when
# we have enough space.
C_('long', 'Repository'))
msg_size = select_short_long(s_wid,
# Translators: This is the short version of 'Size'. It should
# not be longer than 5 characters. If the term 'Size' in your
# language is not longer than 5 characters then you can use it
# unabbreviated.
C_('short', 'Size'),
# Translators: This is the full (unabbreviated) term 'Size'.
C_('long', 'Size'))
out = [u"%s\n%s\n%s\n" % ('=' * output_width,
self.fmtColumns(((msg_package, -n_wid),
(msg_arch, -a_wid),
(msg_version, -v_wid),
(msg_repository, -r_wid),
(msg_size, s_wid)), u" "),
'=' * output_width)]
for (action, lines) in pkglist_lines:
if lines:
totalmsg = u"%s:\n" % action
for (n, a, evr, repoid, size, obsoletes, hi) in lines:
columns = ((n, -n_wid, hi), (a, -a_wid),
(evr, -v_wid), (repoid, -r_wid), (size, s_wid))
msg = self.fmtColumns(columns, u" ", u"\n")
hibeg, hiend = self._highlight(self.conf.color_update_installed)
for obspo in sorted(obsoletes):
appended = ' ' + _('replacing') + ' %s%s%s.%s %s\n'
appended %= (hibeg, obspo.name, hiend, obspo.arch, obspo.evr)
msg += appended
totalmsg = totalmsg + msg
if lines:
out.append(totalmsg)
out.append(_("""
Transaction Summary
%s
""") % ('=' * output_width))
summary_data = (
(_('Install'), len(list_bunch.installed) +
len(list_bunch.installed_group) +
len(list_bunch.installed_weak) +
len(list_bunch.installed_dep), 0),
(_('Upgrade'), len(list_bunch.upgraded), 0),
(_('Remove'), len(list_bunch.erased) + len(list_bunch.erased_dep) +
len(list_bunch.erased_clean), 0),
(_('Downgrade'), len(list_bunch.downgraded), 0),
(_('Skip'), len(skipped_conflicts) + len(skipped_broken), 0))
max_msg_action = 0
max_msg_count = 0
max_msg_pkgs = 0
max_msg_depcount = 0
for action, count, depcount in summary_data:
if not count and not depcount:
continue
msg_pkgs = P_('Package', 'Packages', count)
len_msg_action = exact_width(action)
len_msg_count = exact_width(unicode(count))
len_msg_pkgs = exact_width(msg_pkgs)
if depcount:
len_msg_depcount = exact_width(unicode(depcount))
else:
len_msg_depcount = 0
max_msg_action = max(len_msg_action, max_msg_action)
max_msg_count = max(len_msg_count, max_msg_count)
max_msg_pkgs = max(len_msg_pkgs, max_msg_pkgs)
max_msg_depcount = max(len_msg_depcount, max_msg_depcount)
for action, count, depcount in summary_data:
msg_pkgs = P_('Package', 'Packages', count)
if depcount:
msg_deppkgs = P_('Dependent package', 'Dependent packages',
depcount)
action_msg = fill_exact_width(action, max_msg_action)
if count:
msg = '%s %*d %s (+%*d %s)\n'
out.append(msg % (action_msg,
max_msg_count, count,
"%-*s" % (max_msg_pkgs, msg_pkgs),
max_msg_depcount, depcount, msg_deppkgs))
else:
msg = '%s %s ( %*d %s)\n'
out.append(msg % (action_msg,
(max_msg_count + max_msg_pkgs) * ' ',
max_msg_depcount, depcount, msg_deppkgs))
elif count:
msg = '%s %*d %s\n'
out.append(msg % (fill_exact_width(action, max_msg_action),
max_msg_count, count, msg_pkgs))
return ''.join(out)
def _pto_callback(self, action, tsis):
# Works a bit like calcColumns, but we never overflow a column we just
# have a dynamic number of columns.
def _fits_in_cols(msgs, num):
""" Work out how many columns we can use to display stuff, in
the post trans output. """
if len(msgs) < num:
return []
left = self.term.columns - ((num - 1) + 2)
if left <= 0:
return []
col_lens = [0] * num
col = 0
for msg in msgs:
if len(msg) > col_lens[col]:
diff = (len(msg) - col_lens[col])
if left <= diff:
return []
left -= diff
col_lens[col] = len(msg)
col += 1
col %= len(col_lens)
for col in range(len(col_lens)):
col_lens[col] += left // num
col_lens[col] *= -1
return col_lens
if not tsis:
return ''
out = []
msgs = []
out.append('{}:'.format(action))
for tsi in tsis:
msgs.append(str(tsi))
for num in (8, 7, 6, 5, 4, 3, 2):
cols = _fits_in_cols(msgs, num)
if cols:
break
if not cols:
cols = [-(self.term.columns - 2)]
while msgs:
current_msgs = msgs[:len(cols)]
out.append(' {}'.format(self.fmtColumns(zip(current_msgs, cols))))
msgs = msgs[len(cols):]
return out
def post_transaction_output(self, transaction):
"""
Return a human-readable summary of the transaction. Packages in sections
are arranged to columns.
"""
return dnf.util._post_transaction_output(self.base, transaction, self._pto_callback)
def setup_progress_callbacks(self):
"""Set up the progress callbacks and various
output bars based on debug level.
"""
progressbar = None
if self.conf.debuglevel >= 2:
progressbar = dnf.cli.progress.MultiFileProgressMeter(fo=sys.stdout)
self.progress = dnf.cli.progress.MultiFileProgressMeter(fo=sys.stdout)
# setup our depsolve progress callback
return (progressbar, DepSolveProgressCallBack())
def download_callback_total_cb(self, remote_size, download_start_timestamp):
"""Outputs summary information about the download process.
:param remote_size: the total amount of information that was
downloaded, in bytes
:param download_start_timestamp: the time when the download
process started, in seconds since the epoch
"""
if remote_size <= 0:
return
width = self.term.columns
logger.info("-" * width)
dl_time = max(0.01, time.time() - download_start_timestamp)
msg = ' %5sB/s | %5sB %9s ' % (
format_number(remote_size // dl_time),
format_number(remote_size),
format_time(dl_time))
msg = fill_exact_width(_("Total"), width - len(msg)) + msg
logger.info(msg)
def _history_uiactions(self, hpkgs):
actions = set()
actions_short = set()
count = 0
for pkg in hpkgs:
if pkg.action in (libdnf.transaction.TransactionItemAction_UPGRADED, libdnf.transaction.TransactionItemAction_DOWNGRADED):
# skip states we don't want to display in user input
continue
actions.add(pkg.action_name)
actions_short.add(pkg.action_short)
count += 1
if len(actions) > 1:
return count, ", ".join(sorted(actions_short))
# So empty transactions work, although that "shouldn't" really happen
return count, "".join(list(actions))
def _pwd_ui_username(self, uid, limit=None):
if isinstance(uid, list):
return [self._pwd_ui_username(u, limit) for u in uid]
# loginuid is set to -1 (0xFFFF_FFFF) on init, in newer kernels.
# loginuid is set to INT_MAX (0x7FFF_FFFF) on init, in older kernels.
if uid is None or uid in (0xFFFFFFFF, 0x7FFFFFFF):
loginid = _("