# group.py
# Group CLI command.
#
# Copyright (C) 2012-2016 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 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. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
from __future__ import absolute_import
from __future__ import unicode_literals
from dnf.comps import CompsQuery
from dnf.cli import commands
from dnf.i18n import _, ucd
import libdnf.transaction
import dnf.cli
import dnf.exceptions
import dnf.util
import logging
logger = logging.getLogger("dnf")
class GroupCommand(commands.Command):
""" Single sub-command interface for most groups interaction. """
direct_commands = {'grouplist' : 'list',
'groupinstall' : 'install',
'groupupdate' : 'install',
'groupremove' : 'remove',
'grouperase' : 'remove',
'groupinfo' : 'info'}
aliases = ('group', 'groups', 'grp') + tuple(direct_commands.keys())
summary = _('display, or use, the groups information')
_CMD_ALIASES = {'update' : 'upgrade',
'erase' : 'remove'}
_MARK_CMDS = ('install', 'remove')
_GROUP_SUBCOMMANDS = ('summary', 'list', 'info', 'remove', 'install', 'upgrade', 'mark')
def _canonical(self):
# were we called with direct command?
direct = self.direct_commands.get(self.opts.command)
if direct:
# canonize subcmd and args
if self.opts.subcmd is not None:
self.opts.args.insert(0, self.opts.subcmd)
self.opts.subcmd = direct
if self.opts.subcmd is None:
self.opts.subcmd = 'summary'
self.opts.subcmd = self._CMD_ALIASES.get(self.opts.subcmd,
self.opts.subcmd)
def __init__(self, cli):
super(GroupCommand, self).__init__(cli)
self._remark = False
def _assert_comps(self):
msg = _('No group data available for configured repositories.')
if not len(self.base.comps):
raise dnf.exceptions.CompsError(msg)
def _environment_lists(self, patterns):
def available_pred(env):
env_found = self.base.history.env.get(env.id)
return not(env_found)
self._assert_comps()
if patterns is None:
envs = self.base.comps.environments
else:
envs = self.base.comps.environments_by_pattern(",".join(patterns))
return dnf.util.mapall(list, dnf.util.partition(available_pred, envs))
def _group_lists(self, uservisible, patterns):
def installed_pred(group):
group_found = self.base.history.group.get(group.id)
if group_found:
return True
return False
installed = []
available = []
self._assert_comps()
if patterns is None:
grps = self.base.comps.groups
else:
grps = self.base.comps.groups_by_pattern(",".join(patterns))
for grp in grps:
tgt_list = available
if installed_pred(grp):
tgt_list = installed
if not uservisible or grp.uservisible:
tgt_list.append(grp)
return installed, available
def _info(self, userlist):
for strng in userlist:
group_matched = False
for env in self.base.comps.environments_by_pattern(strng):
self.output.display_groups_in_environment(env)
group_matched = True
for group in self.base.comps.groups_by_pattern(strng):
self.output.display_pkgs_in_groups(group)
group_matched = True
if not group_matched:
logger.error(_('Warning: Group %s does not exist.'), strng)
return 0, []
def _list(self, userlist):
uservisible = 1
showinstalled = 0
showavailable = 0
print_ids = self.base.conf.verbose or self.opts.ids
while userlist:
if userlist[0] == 'hidden':
uservisible = 0
userlist.pop(0)
elif userlist[0] == 'installed':
showinstalled = 1
userlist.pop(0)
elif userlist[0] == 'available':
showavailable = 1
userlist.pop(0)
elif userlist[0] == 'ids':
print_ids = True
userlist.pop(0)
else:
break
if self.opts.hidden:
uservisible = 0
if self.opts.installed:
showinstalled = 1
if self.opts.available:
showavailable = 1
if not userlist:
userlist = None # Match everything...
errs = False
if userlist is not None:
for group in userlist:
comps = self.base.comps
in_group = len(comps.groups_by_pattern(group)) > 0
in_environment = len(comps.environments_by_pattern(group)) > 0
if not in_group and not in_environment:
logger.error(_('Warning: No groups match:') + '\n %s',
group)
errs = True
if errs:
return 0, []
env_inst, env_avail = self._environment_lists(userlist)
installed, available = self._group_lists(uservisible, userlist)
def _out_grp(sect, group):
if not done:
print(sect)
msg = ' %s' % (group.ui_name if group.ui_name is not None else _("