Merge ~kissiel/plainbox:sideloaded-providers into plainbox:master

Proposed by Maciej Kisielewski
Status: Merged
Approved by: Maciej Kisielewski
Approved revision: ae821a2cb6a568b2cbec4de57f62d9121608a012
Merged at revision: 9b9abb3a5013bbb56424a5d3afb2492db59c0023
Proposed branch: ~kissiel/plainbox:sideloaded-providers
Merge into: plainbox:master
Diff against target: 124 lines (+118/-0)
1 file modified
plainbox/impl/providers/embedded_providers.py (+118/-0)
Reviewer Review Type Date Requested Status
Jonathan Cave (community) Approve
Maciej Kisielewski Needs Resubmitting
Review via email: mp+334347@code.launchpad.net

Description of the change

migrate embedded_providers from checkbox-converged

that module is needed to side-load provider

To post a comment you must log in.
Revision history for this message
Jonathan Cave (jocave) wrote :

Looks good. Do you just want to tweak the header to not reference Checkbox-Converged?

review: Needs Information
Revision history for this message
Maciej Kisielewski (kissiel) wrote :

Good catch!
fixed, amended, pushed

review: Needs Resubmitting
Revision history for this message
Jonathan Cave (jocave) wrote :

Thanks, LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/plainbox/impl/providers/embedded_providers.py b/plainbox/impl/providers/embedded_providers.py
2new file mode 100644
3index 0000000..2718469
4--- /dev/null
5+++ b/plainbox/impl/providers/embedded_providers.py
6@@ -0,0 +1,118 @@
7+# This file is part of Checkbox.
8+#
9+# Copyright 2014-2017 Canonical Ltd.
10+# Written by:
11+# Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
12+# Maciej Kisielewski <maciej.kisielewski@canonical.com>
13+#
14+# Checkbox is free software: you can redistribute it and/or modify
15+# it under the terms of the GNU General Public License version 3,
16+# as published by the Free Software Foundation.
17+#
18+# Checkbox is distributed in the hope that it will be useful,
19+# but WITHOUT ANY WARRANTY; without even the implied warranty of
20+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+# GNU General Public License for more details.
22+#
23+# You should have received a copy of the GNU General Public License
24+# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
25+from importlib.machinery import SourceFileLoader
26+import os
27+
28+from plainbox.impl.secure.config import Unset
29+from plainbox.impl.secure.plugins import FsPlugInCollection
30+from plainbox.impl.secure.plugins import now
31+from plainbox.impl.secure.providers.v1 import Provider1
32+from plainbox.impl.secure.providers.v1 import Provider1Definition
33+from plainbox.impl.secure.providers.v1 import Provider1PlugIn
34+
35+
36+class ManagePyProvider1PlugIn(Provider1PlugIn):
37+ """
38+ Provider1PlugIn that is built from manage.py file.
39+ """
40+
41+ def __init__(self, filename, file_contents, load_time, *, validate=None,
42+ validation_kwargs=None, check=None, context=None):
43+ """
44+ Initialize plug-in and create provider from definition extracted
45+ from manage.py pointed by `filename`
46+ """
47+ self._load_time = load_time
48+ # override provider_manager.setup() to capture setup's parameters
49+ setup_kwargs = []
50+ start_time = now()
51+
52+ def fake_setup(**kwargs):
53+ setup_kwargs.append(kwargs)
54+
55+ from plainbox import provider_manager
56+ provider_manager.setup = fake_setup
57+
58+ loader = SourceFileLoader('manage', filename)
59+ loader.load_module()
60+ location = os.path.dirname(os.path.abspath(filename))
61+ if len(setup_kwargs) < 1:
62+ # did not load any provider from given manage.py
63+ # creating empty definition
64+ definition = Provider1Definition()
65+ else:
66+ setup_kwargs = setup_kwargs.pop()
67+ definition = Provider1Definition()
68+ definition.location = location
69+ definition.name = setup_kwargs.get('name', None)
70+ definition.namespace = setup_kwargs.get('namespace', Unset)
71+ definition.version = setup_kwargs.get('version', None)
72+ definition.description = setup_kwargs.get('description', None)
73+ definition.gettext_domain = setup_kwargs.get(
74+ 'gettext_domain', Unset)
75+ self._provider = Provider1.from_definition(
76+ definition, secure=False, validate=validate,
77+ validation_kwargs=validation_kwargs, check=check, context=context)
78+ self._wrap_time = now() - start_time
79+
80+ @property
81+ def plugin_name(self):
82+ """
83+ plugin name, the namespace of the provider
84+ """
85+ return self._provider.name
86+
87+ @property
88+ def plugin_object(self):
89+ """
90+ plugin object, the actual Provider1 instance
91+ """
92+ return self._provider
93+
94+ @property
95+ def plugin_load_time(self) -> float:
96+ """
97+ time, in fractional seconds, that was needed to load the provider
98+ definition file from the file system
99+ """
100+ return self._load_time
101+
102+ @property
103+ def plugin_wrap_time(self) -> float:
104+ """
105+ time, in fractional seconds, that was needed to load the provider from
106+ the definition text
107+ """
108+ return self._wrap_time
109+
110+
111+class EmbeddedProvider1PlugInCollection(FsPlugInCollection):
112+ """
113+ A collection of v1 provider plugins loaded from 'manage.py' files.
114+
115+ """
116+ def __init__(self, path, **kwargs):
117+ """
118+ Initiates collection with all providers loaded from manage.py files
119+ found in `path` and subdirectories.
120+ """
121+ # get all manage.py files to load providers from
122+ super().__init__(
123+ [path], 'manage.py', recursive=True, load=True,
124+ wrapper=ManagePyProvider1PlugIn, **kwargs)

Subscribers

People subscribed via source and target branches