diff options
author | Benjamin Schubert <ben.c.schubert@gmail.com> | 2019-03-20 14:15:27 +0000 |
---|---|---|
committer | bst-marge-bot <marge-bot@buildstream.build> | 2019-03-21 12:52:55 +0000 |
commit | bd7e75f99c28fbebedfe8d5db98a922b0e140a62 (patch) | |
tree | f73bd1c0576fc2a63945eb3a95bafcfacc0b7e0e | |
parent | c74701413ed32d721188b2a7eab8081d4629380c (diff) | |
download | buildstream-bd7e75f99c28fbebedfe8d5db98a922b0e140a62.tar.gz |
plugin.py: move the creation and handling of the plugin table in Plugin
This encapsulates the logic of how we handle ids and the table in the
plugin class itself, making it easier to refactor afterwards
-rw-r--r-- | buildstream/_context.py | 4 | ||||
-rw-r--r-- | buildstream/_frontend/widget.py | 8 | ||||
-rw-r--r-- | buildstream/_scheduler/queues/trackqueue.py | 4 | ||||
-rw-r--r-- | buildstream/plugin.py | 83 |
4 files changed, 51 insertions, 48 deletions
diff --git a/buildstream/_context.py b/buildstream/_context.py index 476032f39..0c7f81264 100644 --- a/buildstream/_context.py +++ b/buildstream/_context.py @@ -35,7 +35,7 @@ from ._artifactcache import ArtifactCache from ._sourcecache import SourceCache from ._cas import CASCache, CASQuota, CASCacheUsage from ._workspaces import Workspaces, WorkspaceProjectCache -from .plugin import _plugin_lookup +from .plugin import Plugin from .sandbox import SandboxRemote @@ -651,7 +651,7 @@ class Context(): plugin_name = "" if message.unique_id: template += " {plugin}" - plugin = _plugin_lookup(message.unique_id) + plugin = Plugin._lookup(message.unique_id) plugin_name = plugin.name template += ": {message}" diff --git a/buildstream/_frontend/widget.py b/buildstream/_frontend/widget.py index 45be6d136..ef31b8ba7 100644 --- a/buildstream/_frontend/widget.py +++ b/buildstream/_frontend/widget.py @@ -32,7 +32,7 @@ from .. import _yaml from .. import __version__ as bst_version from .._exceptions import ImplError from .._message import MessageType -from ..plugin import _plugin_lookup +from ..plugin import Plugin # These messages are printed a bit differently @@ -191,7 +191,7 @@ class ElementName(Widget): action_name = message.action_name element_id = message.task_id or message.unique_id if element_id is not None: - plugin = _plugin_lookup(element_id) + plugin = Plugin._lookup(element_id) name = plugin._get_full_name() name = '{: <30}'.format(name) else: @@ -232,7 +232,7 @@ class CacheKey(Widget): missing = False key = ' ' * self._key_length - plugin = _plugin_lookup(element_id) + plugin = Plugin._lookup(element_id) if isinstance(plugin, Element): _, key, missing = plugin._get_display_key() @@ -621,7 +621,7 @@ class LogLine(Widget): # Track logfiles for later use element_id = message.task_id or message.unique_id if message.message_type in ERROR_MESSAGES and element_id is not None: - plugin = _plugin_lookup(element_id) + plugin = Plugin._lookup(element_id) self._failure_messages[plugin].append(message) return self._render(message) diff --git a/buildstream/_scheduler/queues/trackqueue.py b/buildstream/_scheduler/queues/trackqueue.py index 245b528e6..d7e6546f3 100644 --- a/buildstream/_scheduler/queues/trackqueue.py +++ b/buildstream/_scheduler/queues/trackqueue.py @@ -19,7 +19,7 @@ # Jürg Billeter <juerg.billeter@codethink.co.uk> # BuildStream toplevel imports -from ...plugin import _plugin_lookup +from ...plugin import Plugin # Local imports from . import Queue, QueueStatus @@ -55,7 +55,7 @@ class TrackQueue(Queue): # Set the new refs in the main process one by one as they complete for unique_id, new_ref in result: - source = _plugin_lookup(unique_id) + source = Plugin._lookup(unique_id) source._save_ref(new_ref) element._tracking_done() diff --git a/buildstream/plugin.py b/buildstream/plugin.py index f5daf31bf..bbea045b6 100644 --- a/buildstream/plugin.py +++ b/buildstream/plugin.py @@ -109,6 +109,7 @@ Class Reference --------------- """ +import itertools import os import subprocess import sys @@ -183,6 +184,23 @@ class Plugin(): """ + # Unique id generator for Plugins + # + # Each plugin gets a unique id at creation. + # Ids are a monotically increasing integer + __id_generator = itertools.count() + + # Hold on to a lookup table by counter of all instantiated plugins. + # We use this to send the id back from child processes so we can lookup + # corresponding element/source in the master process. + # + # Use WeakValueDictionary() so the map we use to lookup objects does not + # keep the plugins alive after pipeline destruction. + # + # Note that Plugins can only be instantiated in the main process before + # scheduling tasks. + __TABLE = WeakValueDictionary() + def __init__(self, name, context, project, provenance, type_tag): self.name = name @@ -195,11 +213,18 @@ class Plugin(): For sources this is for display purposes only. """ + # Unique ID + # + # This id allows to uniquely identify a plugin. + self.__unique_id = next(self.__id_generator) + + # register ourself in the table containing all existing plugins + self.__TABLE[self.__unique_id] = self + self.__context = context # The Context object self.__project = project # The Project object self.__provenance = provenance # The Provenance information self.__type_tag = type_tag # The type of plugin (element or source) - self.__unique_id = _plugin_register(self) # Unique ID self.__configuring = False # Whether we are currently configuring # Infer the kind identifier @@ -669,6 +694,23 @@ class Plugin(): # Private Methods used in BuildStream # ############################################################# + # _lookup(): + # + # Fetch a plugin in the current process by its + # unique identifier + # + # Args: + # unique_id: The unique identifier as returned by + # plugin._unique_id + # + # Returns: + # (Plugin): The plugin for the given ID, or None + # + @classmethod + def _lookup(cls, unique_id): + assert unique_id in cls.__TABLE, "Could not find plugin with ID {}".format(unique_id) + return cls.__TABLE[unique_id] + # _get_context() # # Fetches the invocation context @@ -806,45 +848,6 @@ class Plugin(): return self.get_kind() in silenced_warnings -# Hold on to a lookup table by counter of all instantiated plugins. -# We use this to send the id back from child processes so we can lookup -# corresponding element/source in the master process. -# -# Use WeakValueDictionary() so the map we use to lookup objects does not -# keep the plugins alive after pipeline destruction. -# -# Note that Plugins can only be instantiated in the main process before -# scheduling tasks. -__PLUGINS_UNIQUE_ID = 0 -__PLUGINS_TABLE = WeakValueDictionary() - - -# _plugin_lookup(): -# -# Fetch a plugin in the current process by its -# unique identifier -# -# Args: -# unique_id: The unique identifier as returned by -# plugin._get_unique_id() -# -# Returns: -# (Plugin): The plugin for the given ID, or None -# -def _plugin_lookup(unique_id): - assert unique_id in __PLUGINS_TABLE, "Could not find plugin with ID {}".format(unique_id) - return __PLUGINS_TABLE[unique_id] - - -# No need for unregister, WeakValueDictionary() will remove entries -# in itself when the referenced plugins are garbage collected. -def _plugin_register(plugin): - global __PLUGINS_UNIQUE_ID # pylint: disable=global-statement - __PLUGINS_UNIQUE_ID += 1 - __PLUGINS_TABLE[__PLUGINS_UNIQUE_ID] = plugin - return __PLUGINS_UNIQUE_ID - - # A local table for _prefix_warning() # __CORE_WARNINGS = [ |