|
Packit Service |
21c75c |
# plugin.py
|
|
Packit Service |
21c75c |
# The interface for building DNF plugins.
|
|
Packit Service |
21c75c |
#
|
|
Packit Service |
21c75c |
# Copyright (C) 2012-2016 Red Hat, Inc.
|
|
Packit Service |
21c75c |
#
|
|
Packit Service |
21c75c |
# This copyrighted material is made available to anyone wishing to use,
|
|
Packit Service |
21c75c |
# modify, copy, or redistribute it subject to the terms and conditions of
|
|
Packit Service |
21c75c |
# the GNU General Public License v.2, or (at your option) any later version.
|
|
Packit Service |
21c75c |
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
Packit Service |
21c75c |
# ANY WARRANTY expressed or implied, including the implied warranties of
|
|
Packit Service |
21c75c |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
Packit Service |
21c75c |
# Public License for more details. You should have received a copy of the
|
|
Packit Service |
21c75c |
# GNU General Public License along with this program; if not, write to the
|
|
Packit Service |
21c75c |
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
Packit Service |
21c75c |
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
|
|
Packit Service |
21c75c |
# source code or documentation are not subject to the GNU General Public
|
|
Packit Service |
21c75c |
# License and may only be used or replicated with the express permission of
|
|
Packit Service |
21c75c |
# Red Hat, Inc.
|
|
Packit Service |
21c75c |
#
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
from __future__ import absolute_import
|
|
Packit Service |
21c75c |
from __future__ import print_function
|
|
Packit Service |
21c75c |
from __future__ import unicode_literals
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
import fnmatch
|
|
Packit Service |
21c75c |
import glob
|
|
Packit Service |
21c75c |
import importlib
|
|
Packit Service |
21c75c |
import inspect
|
|
Packit Service |
21c75c |
import logging
|
|
Packit Service |
21c75c |
import operator
|
|
Packit Service |
21c75c |
import os
|
|
Packit Service |
21c75c |
import sys
|
|
Packit Service |
21c75c |
import traceback
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
import libdnf
|
|
Packit Service |
21c75c |
import dnf.logging
|
|
Packit Service |
21c75c |
import dnf.pycomp
|
|
Packit Service |
21c75c |
import dnf.util
|
|
Packit Service |
21c75c |
from dnf.i18n import _
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
logger = logging.getLogger('dnf')
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
DYNAMIC_PACKAGE = 'dnf.plugin.dynamic'
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
class Plugin(object):
|
|
Packit Service |
21c75c |
"""The base class custom plugins must derive from. #:api"""
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
name = '<invalid>'
|
|
Packit Service |
21c75c |
config_name = None
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
@classmethod
|
|
Packit Service |
21c75c |
def read_config(cls, conf):
|
|
Packit Service |
21c75c |
# :api
|
|
Packit Service |
21c75c |
parser = libdnf.conf.ConfigParser()
|
|
Packit Service |
21c75c |
name = cls.config_name if cls.config_name else cls.name
|
|
Packit Service |
21c75c |
files = ['%s/%s.conf' % (path, name) for path in conf.pluginconfpath]
|
|
Packit Service |
21c75c |
for file in files:
|
|
Packit Service |
21c75c |
if os.path.isfile(file):
|
|
Packit Service |
21c75c |
try:
|
|
Packit Service |
21c75c |
parser.read(file)
|
|
Packit Service |
21c75c |
except Exception as e:
|
|
Packit Service |
21c75c |
raise dnf.exceptions.ConfigError(_("Parsing file failed: %s") % str(e))
|
|
Packit Service |
21c75c |
return parser
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def __init__(self, base, cli):
|
|
Packit Service |
21c75c |
# :api
|
|
Packit Service |
21c75c |
self.base = base
|
|
Packit Service |
21c75c |
self.cli = cli
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def pre_config(self):
|
|
Packit Service |
21c75c |
# :api
|
|
Packit Service |
21c75c |
pass
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def config(self):
|
|
Packit Service |
21c75c |
# :api
|
|
Packit Service |
21c75c |
pass
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def resolved(self):
|
|
Packit Service |
21c75c |
# :api
|
|
Packit Service |
21c75c |
pass
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def sack(self):
|
|
Packit Service |
21c75c |
# :api
|
|
Packit Service |
21c75c |
pass
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def pre_transaction(self):
|
|
Packit Service |
21c75c |
# :api
|
|
Packit Service |
21c75c |
pass
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def transaction(self):
|
|
Packit Service |
21c75c |
# :api
|
|
Packit Service |
21c75c |
pass
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
class Plugins(object):
|
|
Packit Service |
21c75c |
def __init__(self):
|
|
Packit Service |
21c75c |
self.plugin_cls = []
|
|
Packit Service |
21c75c |
self.plugins = []
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def _caller(self, method):
|
|
Packit Service |
21c75c |
for plugin in self.plugins:
|
|
Packit Service |
21c75c |
try:
|
|
Packit Service |
21c75c |
getattr(plugin, method)()
|
|
Packit Service |
21c75c |
except dnf.exceptions.Error:
|
|
Packit Service |
21c75c |
raise
|
|
Packit Service |
21c75c |
except Exception:
|
|
Packit Service |
21c75c |
exc_type, exc_value, exc_traceback = sys.exc_info()
|
|
Packit Service |
21c75c |
except_list = traceback.format_exception(exc_type, exc_value, exc_traceback)
|
|
Packit Service |
21c75c |
logger.critical(''.join(except_list))
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def _check_enabled(self, conf, enable_plugins):
|
|
Packit Service |
21c75c |
"""Checks whether plugins are enabled or disabled in configuration files
|
|
Packit Service |
21c75c |
and removes disabled plugins from list"""
|
|
Packit Service |
21c75c |
for plug_cls in self.plugin_cls[:]:
|
|
Packit Service |
21c75c |
name = plug_cls.name
|
|
Packit Service |
21c75c |
if any(fnmatch.fnmatch(name, pattern) for pattern in enable_plugins):
|
|
Packit Service |
21c75c |
continue
|
|
Packit Service |
21c75c |
parser = plug_cls.read_config(conf)
|
|
Packit Service |
21c75c |
# has it enabled = False?
|
|
Packit Service |
21c75c |
disabled = (parser.has_section('main')
|
|
Packit Service |
21c75c |
and parser.has_option('main', 'enabled')
|
|
Packit Service |
21c75c |
and not parser.getboolean('main', 'enabled'))
|
|
Packit Service |
21c75c |
if disabled:
|
|
Packit Service |
21c75c |
self.plugin_cls.remove(plug_cls)
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def _load(self, conf, skips, enable_plugins):
|
|
Packit Service |
21c75c |
"""Dynamically load relevant plugin modules."""
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
if DYNAMIC_PACKAGE in sys.modules:
|
|
Packit Service |
21c75c |
raise RuntimeError("load_plugins() called twice")
|
|
Packit Service |
21c75c |
sys.modules[DYNAMIC_PACKAGE] = package = dnf.pycomp.ModuleType(DYNAMIC_PACKAGE)
|
|
Packit Service |
21c75c |
package.__path__ = []
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
files = _get_plugins_files(conf.pluginpath, skips, enable_plugins)
|
|
Packit Service |
21c75c |
_import_modules(package, files)
|
|
Packit Service |
21c75c |
self.plugin_cls = _plugin_classes()[:]
|
|
Packit Service |
21c75c |
self._check_enabled(conf, enable_plugins)
|
|
Packit Service |
21c75c |
if len(self.plugin_cls) > 0:
|
|
Packit Service |
21c75c |
names = sorted(plugin.name for plugin in self.plugin_cls)
|
|
Packit Service |
21c75c |
logger.debug(_('Loaded plugins: %s'), ', '.join(names))
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def _run_pre_config(self):
|
|
Packit Service |
21c75c |
self._caller('pre_config')
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def _run_config(self):
|
|
Packit Service |
21c75c |
self._caller('config')
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def _run_init(self, base, cli=None):
|
|
Packit Service |
21c75c |
for p_cls in self.plugin_cls:
|
|
Packit Service |
21c75c |
plugin = p_cls(base, cli)
|
|
Packit Service |
21c75c |
self.plugins.append(plugin)
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def run_sack(self):
|
|
Packit Service |
21c75c |
self._caller('sack')
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def run_resolved(self):
|
|
Packit Service |
21c75c |
self._caller('resolved')
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def run_pre_transaction(self):
|
|
Packit Service |
21c75c |
self._caller('pre_transaction')
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def run_transaction(self):
|
|
Packit Service |
21c75c |
self._caller('transaction')
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def _unload(self):
|
|
Packit Service |
21c75c |
del sys.modules[DYNAMIC_PACKAGE]
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def unload_removed_plugins(self, transaction):
|
|
Packit Service |
21c75c |
erased = set([package.name for package in transaction.remove_set])
|
|
Packit Service |
21c75c |
if not erased:
|
|
Packit Service |
21c75c |
return
|
|
Packit Service |
21c75c |
installed = set([package.name for package in transaction.install_set])
|
|
Packit Service |
21c75c |
transaction_diff = erased - installed
|
|
Packit Service |
21c75c |
if not transaction_diff:
|
|
Packit Service |
21c75c |
return
|
|
Packit Service |
21c75c |
files_erased = set()
|
|
Packit Service |
21c75c |
for pkg in transaction.remove_set:
|
|
Packit Service |
21c75c |
if pkg.name in transaction_diff:
|
|
Packit Service |
21c75c |
files_erased.update(pkg.files)
|
|
Packit Service |
21c75c |
for plugin in self.plugins[:]:
|
|
Packit Service |
21c75c |
if inspect.getfile(plugin.__class__) in files_erased:
|
|
Packit Service |
21c75c |
self.plugins.remove(plugin)
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def _plugin_classes():
|
|
Packit Service |
21c75c |
return Plugin.__subclasses__()
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def _import_modules(package, py_files):
|
|
Packit Service |
21c75c |
for fn in py_files:
|
|
Packit Service |
21c75c |
path, module = os.path.split(fn)
|
|
Packit Service |
21c75c |
package.__path__.append(path)
|
|
Packit Service |
21c75c |
(module, ext) = os.path.splitext(module)
|
|
Packit Service |
21c75c |
name = '%s.%s' % (package.__name__, module)
|
|
Packit Service |
21c75c |
try:
|
|
Packit Service |
21c75c |
module = importlib.import_module(name)
|
|
Packit Service |
21c75c |
except Exception as e:
|
|
Packit Service |
21c75c |
logger.error(_('Failed loading plugin "%s": %s'), module, e)
|
|
Packit Service |
21c75c |
logger.log(dnf.logging.SUBDEBUG, '', exc_info=True)
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def _get_plugins_files(paths, disable_plugins, enable_plugins):
|
|
Packit Service |
21c75c |
plugins = []
|
|
Packit Service |
21c75c |
disable_plugins = set(disable_plugins)
|
|
Packit Service |
21c75c |
enable_plugins = set(enable_plugins)
|
|
Packit Service |
21c75c |
pattern_enable_found = set()
|
|
Packit Service |
21c75c |
pattern_disable_found = set()
|
|
Packit Service |
21c75c |
for p in paths:
|
|
Packit Service |
21c75c |
for fn in glob.glob('%s/*.py' % p):
|
|
Packit Service |
21c75c |
(plugin_name, dummy) = os.path.splitext(os.path.basename(fn))
|
|
Packit Service |
21c75c |
matched = True
|
|
Packit Service |
21c75c |
enable_pattern_tested = False
|
|
Packit Service |
21c75c |
for pattern_skip in disable_plugins:
|
|
Packit Service |
21c75c |
if fnmatch.fnmatch(plugin_name, pattern_skip):
|
|
Packit Service |
21c75c |
pattern_disable_found.add(pattern_skip)
|
|
Packit Service |
21c75c |
matched = False
|
|
Packit Service |
21c75c |
for pattern_enable in enable_plugins:
|
|
Packit Service |
21c75c |
if fnmatch.fnmatch(plugin_name, pattern_enable):
|
|
Packit Service |
21c75c |
matched = True
|
|
Packit Service |
21c75c |
pattern_enable_found.add(pattern_enable)
|
|
Packit Service |
21c75c |
enable_pattern_tested = True
|
|
Packit Service |
21c75c |
if not enable_pattern_tested:
|
|
Packit Service |
21c75c |
for pattern_enable in enable_plugins:
|
|
Packit Service |
21c75c |
if fnmatch.fnmatch(plugin_name, pattern_enable):
|
|
Packit Service |
21c75c |
pattern_enable_found.add(pattern_enable)
|
|
Packit Service |
21c75c |
if matched:
|
|
Packit Service |
21c75c |
plugins.append(fn)
|
|
Packit Service |
21c75c |
enable_not_found = enable_plugins.difference(pattern_enable_found)
|
|
Packit Service |
21c75c |
if enable_not_found:
|
|
Packit Service |
21c75c |
logger.warning(_("No matches found for the following enable plugin patterns: {}").format(
|
|
Packit Service |
21c75c |
", ".join(sorted(enable_not_found))))
|
|
Packit Service |
21c75c |
disable_not_found = disable_plugins.difference(pattern_disable_found)
|
|
Packit Service |
21c75c |
if disable_not_found:
|
|
Packit Service |
21c75c |
logger.warning(_("No matches found for the following disable plugin patterns: {}").format(
|
|
Packit Service |
21c75c |
", ".join(sorted(disable_not_found))))
|
|
Packit Service |
21c75c |
return plugins
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def register_command(command_class):
|
|
Packit Service |
21c75c |
# :api
|
|
Packit Service |
21c75c |
"""A class decorator for automatic command registration."""
|
|
Packit Service |
21c75c |
def __init__(self, base, cli):
|
|
Packit Service |
21c75c |
if cli:
|
|
Packit Service |
21c75c |
cli.register_command(command_class)
|
|
Packit Service |
21c75c |
plugin_class = type(str(command_class.__name__ + 'Plugin'),
|
|
Packit Service |
21c75c |
(dnf.Plugin,),
|
|
Packit Service |
21c75c |
{"__init__": __init__,
|
|
Packit Service |
21c75c |
"name": command_class.aliases[0]})
|
|
Packit Service |
21c75c |
command_class._plugin = plugin_class
|
|
Packit Service |
21c75c |
return command_class
|