#
# Copyright (C) 2015 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 dnfpluginscore import _, logger
import dnf
import dnf.cli
import dnf.exceptions
import fnmatch
import hawkey
import os
import tempfile
import time
import warnings
NOT_READABLE = _('Unable to read version lock configuration: %s')
NO_LOCKLIST = _('Locklist not set')
ADDING_SPEC = _('Adding versionlock on:')
EXCLUDING_SPEC = _('Adding exclude on:')
EXISTING_SPEC = _('Package already locked in equivalent form:')
ALREADY_LOCKED = _('Package {} is already locked')
ALREADY_EXCLUDED = _('Package {} is already excluded')
DELETING_SPEC = _('Deleting versionlock for:')
NOTFOUND_SPEC = _('No package found for:')
NO_VERSIONLOCK = _('Excludes from versionlock plugin were not applied')
APPLY_LOCK = _('Versionlock plugin: number of lock rules from file "{}" applied: {}')
APPLY_EXCLUDE = _('Versionlock plugin: number of exclude rules from file "{}" applied: {}')
NEVRA_ERROR = _('Versionlock plugin: could not parse pattern:')
locklist_fn = None
class VersionLock(dnf.Plugin):
name = 'versionlock'
def __init__(self, base, cli):
super(VersionLock, self).__init__(base, cli)
self.base = base
self.cli = cli
if self.cli is not None:
self.cli.register_command(VersionLockCommand)
def config(self):
global locklist_fn
cp = self.read_config(self.base.conf)
locklist_fn = (cp.has_section('main') and cp.has_option('main', 'locklist')
and cp.get('main', 'locklist'))
def locking_enabled(self):
if self.cli is None:
enabled = True # loaded via the api, not called by cli
else:
enabled = self.cli.demands.plugin_filtering_enabled
if enabled is None:
enabled = self.cli.demands.resolving
return enabled
def sack(self):
if not self.locking_enabled():
logger.debug(NO_VERSIONLOCK)
return
excludes_query = self.base.sack.query().filter(empty=True)
locked_query = self.base.sack.query().filter(empty=True)
locked_names = set()
# counter of applied rules [locked_count, excluded_count]
count = [0, 0]
for pat in _read_locklist():
excl = 0
if pat and pat[0] == '!':
pat = pat[1:]
excl = 1
possible_nevras = dnf.subject.Subject(pat).get_nevra_possibilities(
forms=[hawkey.FORM_NEVRA, hawkey.FORM_NEVR, hawkey.FORM_NEV,
hawkey.FORM_NA, hawkey.FORM_NAME])
if possible_nevras:
count[excl] += 1
else:
logger.error("%s %s", NEVRA_ERROR, pat)
continue
for nevra in possible_nevras:
pat_query = nevra.to_query(self.base.sack)
if excl:
excludes_query = excludes_query.union(pat_query)
else:
locked_names.add(nevra.name)
locked_query = locked_query.union(pat_query)
if pat_query:
break
if count[1]:
logger.debug(APPLY_EXCLUDE.format(locklist_fn, count[1]))
if count[0]:
logger.debug(APPLY_LOCK.format(locklist_fn, count[0]))
if locked_names:
all_versions = self.base.sack.query().filter(name__glob=list(locked_names))
other_versions = all_versions.difference(locked_query)
excludes_query = excludes_query.union(other_versions)
# exclude also anything that obsoletes the locked versions of packages
obsoletes_query = self.base.sack.query().filterm(obsoletes=locked_query)
# leave out obsoleters that are also part of locked versions (otherwise the obsoleter package
# would not be installable at all)
excludes_query = excludes_query.union(obsoletes_query.difference(locked_query))
excludes_query.filterm(reponame__neq=hawkey.SYSTEM_REPO_NAME)
if excludes_query:
self.base.sack.add_excludes(excludes_query)
EXC_CMDS = ['exclude', 'add-!', 'add!']
DEL_CMDS = ['delete', 'del']
DEP_EXC_CMDS = ['blacklist']
ALL_CMDS = ['add', 'clear', 'list'] + EXC_CMDS + DEL_CMDS + DEP_EXC_CMDS
class VersionLockCommand(dnf.cli.Command):
aliases = ("versionlock",)
summary = _("control package version locks")
usage = "[add|exclude|list|delete|clear] [