This file is indexed.

/usr/lib/python3/dist-packages/janitor/plugincore/manager.py is in python3-update-manager 1:16.04.3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Copyright (C) 2008-2012  Canonical, Ltd.
# -*- Mode: Python; indent-tabs-mode: nil; tab-width: 4; coding: utf-8 -*-
#
# 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, version 3 of the License.
#
# 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 General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.


from __future__ import absolute_import, print_function, unicode_literals

__metaclass__ = type
__all__ = [
    'PluginManager',
]


import os
import imp
import sys
import errno
import inspect
import logging

from janitor.plugincore.plugin import Plugin


SPACE = ' '
STR_TYPES = (basestring if str is bytes else str)


class PluginManager:
    """Find and load plugins.

    Plugins are stored in files named '*_plugin.py' in the list of directories
    given to the constructor.
    """

    def __init__(self, app, plugin_dirs):
        self._app = app
        # Make a copy to immune ourselves from mutability.  For safety, double
        # check a common mistake.
        if isinstance(plugin_dirs, STR_TYPES):
            raise TypeError(
                'Expected sequence, got {}'.format(type(plugin_dirs)))
        self._plugin_dirs = list(plugin_dirs)
        self._plugins = None

    def get_plugin_files(self):
        """Return all filenames in which plugins may be stored."""

        for dirname in self._plugin_dirs:
            try:
                basenames = [filename for filename in os.listdir(dirname)
                             if filename.endswith('_plugin.py')]
            except OSError as error:
                if error.errno != errno.ENOENT:
                    raise
                logging.debug('No such plugin directory: {}'.format(dirname))
                continue
            logging.debug(
                'Plugin modules in {}: {}'.format(
                    dirname, SPACE.join(basenames)))
            # Sort the base names alphabetically for predictability.
            for filename in sorted(basenames):
                yield os.path.join(dirname, filename)

    @property
    def plugin_files(self):
        for filename in self.get_plugin_files():
            yield filename

    def _find_plugins(self, module):
        """Find and instantiate all plugins in a module."""
        def is_plugin(target):
            # Don't return the base class itself.
            return (inspect.isclass(target) and
                    issubclass(target, Plugin) and
                    target is not Plugin)
        plugin_classes = [
            member
            for name, member in inspect.getmembers(module, is_plugin)
        ]
        logging.debug('Plugins in {}: {}'.format(
            module, SPACE.join(str(plugin) for plugin in plugin_classes)))
        for plugin_class in plugin_classes:
            yield plugin_class()

    def _load_module(self, filename):
        """Load a module from a filename."""
        logging.debug('Loading module from file {}'.format(filename))
        # 2012-06-08 BAW: I don't particularly like putting an entry in
        # sys.modules with the basename of the file.  Note that
        # imp.load_module() will reload the plugin if it's already been
        # imported, so check sys.modules first and don't reload the plugin
        # (this is a change in behavior from older versions, but a valid one I
        # think - reloading modules is problematic).  Ideally, we'd be using
        # __import__() but we can't guarantee that the path to the filename is
        # on sys.path, so we'll just live with this as the most backward
        # compatible implementation.
        #
        # The other problem is that the module could be encoded, but this
        # mechanism doesn't support PEP 263 style source file encoding
        # specifications.  To make matters worse, we can't use codecs.open()
        # with encoding='UTF-8' because imp.load_module() requires an actual
        # file object, not whatever codecs wrapper is used.  If we were Python
        # 3 only, we could use the built-in open(), but since we have to also
        # support Python 3, we just have to live with the platform dependent
        # default text encoding of built-in open().
        module_name, ignore = os.path.splitext(os.path.basename(filename))
        if module_name in sys.modules:
            return sys.modules[module_name]
        with open(filename, 'r') as fp:
            try:
                module = imp.load_module(
                    module_name, fp, filename,
                    ('.py', 'r', imp.PY_SOURCE))
            except Exception as error:
                logging.warning("Failed to load plugin '{}' ({})".format(
                                module_name, error))
                return None
            else:
                return module

    def get_plugins(self, condition=None, callback=None):
        """Return all plugins that have been found.

        Loaded plugins are cached, so they will only be loaded once.

        `condition` is matched against each plugin to determine whether it
        will be returned or not.  A `condition` of the string '*' matches all
        plugins.  The default condition matches all default plugins, since by
        default, plugins have a condition of the empty list.

        If `condition` matches the plugin's condition exactly, the plugin is
        returned.  The plugin's condition can also be a sequence, and if
        `condition` is in that sequence, the plugin is returned.

        Note that even though loaded plugins are cached, calling
        `get_plugin()` with different a `condition` can return a different set
        of plugins.

        If `callback` is specified, it is called after each plugin has
        been found, with the following arguments: filename, index of
        filename in list of files to be examined (starting with 0), and
        total number of files to be examined. The purpose of this is to
        allow the callback to inform the user in case things take a long
        time.
        """
        # By default, plugins have a condition of the empty list, so unless a
        # plugin has an explicit condition set, this will match everything.
        if condition is None:
            condition = []
        # Only load the plugins once, however when different conditions are
        # given, a different set of the already loaded plugins may be
        # returned.
        if self._plugins is None:
            self._plugins = []
            filenames = list(self.plugin_files)
            total = len(filenames)
            for i, filename in enumerate(filenames):
                if callback is not None:
                    callback(filename, i, total)
                module = self._load_module(filename)
                for plugin in self._find_plugins(module):
                    plugin.set_application(self._app)
                    self._plugins.append(plugin)
        # Now match each of the plugins against the specified condition,
        # returning only those that match, or all of them if there is no
        # condition.
        plugins = [
            plugin for plugin in self._plugins
            if (plugin.condition == condition or
                condition in plugin.condition or
                condition == '*')
        ]
        logging.debug("plugins for condition '{}' are '{}'".format(
            condition, plugins))
        return plugins