Skip to content
GitLab
Menu
Why GitLab
Pricing
Contact Sales
Explore
Why GitLab
Pricing
Contact Sales
Explore
Sign in
Get free trial
Primary navigation
Search or go to…
Project
buildstream
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Privacy statement
Keyboard shortcuts
?
What's new
6
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
BuildStream
buildstream
Commits
6c14a05a
Commit
6c14a05a
authored
7 years ago
by
Tristan Van Berkom
Browse files
Options
Downloads
Patches
Plain Diff
_plugincontext.py: Adhere to policy on private symbols
This is a part of issue
#285
parent
5c33a984
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!355
More refactoring related to private symbol policy
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
buildstream/_plugincontext.py
+46
-41
46 additions, 41 deletions
buildstream/_plugincontext.py
with
46 additions
and
41 deletions
buildstream/_plugincontext.py
+
46
−
41
View file @
6c14a05a
...
...
@@ -44,16 +44,21 @@ class PluginContext():
def
__init__
(
self
,
plugin_base
,
base_type
,
site_plugin_path
,
plugin_origins
=
None
,
dependencies
=
None
):
self
.
dependencies
=
dependencies
# The plugin kinds which were loaded
self
.
loaded_dependencies
=
[]
self
.
base_type
=
base_type
# The base class plugins derive from
self
.
types
=
{}
# Plugin type lookup table by kind
self
.
plugin_origins
=
plugin_origins
or
[]
#
# Private members
#
self
.
_dependencies
=
dependencies
self
.
_base_type
=
base_type
# The base class plugins derive from
self
.
_types
=
{}
# Plugin type lookup table by kind
self
.
_plugin_origins
=
plugin_origins
or
[]
# The PluginSource object
self
.
plugin_base
=
plugin_base
self
.
site_source
=
plugin_base
.
make_plugin_source
(
searchpath
=
site_plugin_path
)
self
.
alternate_sources
=
{}
self
.
_
plugin_base
=
plugin_base
self
.
_
site_source
=
plugin_base
.
make_plugin_source
(
searchpath
=
site_plugin_path
)
self
.
_
alternate_sources
=
{}
# lookup():
#
...
...
@@ -67,22 +72,22 @@ class PluginContext():
# Raises: PluginError
#
def
lookup
(
self
,
kind
):
return
self
.
ensure_plugin
(
kind
)
return
self
.
_
ensure_plugin
(
kind
)
def
_get_local_plugin_source
(
self
,
path
):
if
(
'
local
'
,
path
)
not
in
self
.
alternate_sources
:
if
(
'
local
'
,
path
)
not
in
self
.
_
alternate_sources
:
# key by a tuple to avoid collision
source
=
self
.
plugin_base
.
make_plugin_source
(
searchpath
=
[
path
])
source
=
self
.
_
plugin_base
.
make_plugin_source
(
searchpath
=
[
path
])
# Ensure that sources never get garbage collected,
# as they'll take the plugins with them.
self
.
alternate_sources
[(
'
local
'
,
path
)]
=
source
self
.
_
alternate_sources
[(
'
local
'
,
path
)]
=
source
else
:
source
=
self
.
alternate_sources
[(
'
local
'
,
path
)]
source
=
self
.
_
alternate_sources
[(
'
local
'
,
path
)]
return
source
def
_get_pip_plugin_source
(
self
,
package_name
,
kind
):
defaults
=
None
if
(
'
pip
'
,
package_name
)
not
in
self
.
alternate_sources
:
if
(
'
pip
'
,
package_name
)
not
in
self
.
_
alternate_sources
:
import
pkg_resources
# key by a tuple to avoid collision
try
:
...
...
@@ -91,7 +96,7 @@ class PluginContext():
kind
)
except
pkg_resources
.
DistributionNotFound
as
e
:
raise
PluginError
(
"
Failed to load {} plugin
'
{}
'
: {}
"
.
format
(
self
.
base_type
.
__name__
,
kind
,
e
))
from
e
.
format
(
self
.
_
base_type
.
__name__
,
kind
,
e
))
from
e
if
package
is
None
:
raise
PluginError
(
"
Pip package {} does not contain a plugin named
'
{}
'"
...
...
@@ -113,22 +118,22 @@ class PluginContext():
# The plugin didn't have an accompanying YAML file
defaults
=
None
source
=
self
.
plugin_base
.
make_plugin_source
(
searchpath
=
[
os
.
path
.
dirname
(
location
)])
self
.
alternate_sources
[(
'
pip
'
,
package_name
)]
=
source
source
=
self
.
_
plugin_base
.
make_plugin_source
(
searchpath
=
[
os
.
path
.
dirname
(
location
)])
self
.
_
alternate_sources
[(
'
pip
'
,
package_name
)]
=
source
else
:
source
=
self
.
alternate_sources
[(
'
pip
'
,
package_name
)]
source
=
self
.
_
alternate_sources
[(
'
pip
'
,
package_name
)]
return
source
,
defaults
def
ensure_plugin
(
self
,
kind
):
def
_
ensure_plugin
(
self
,
kind
):
if
kind
not
in
self
.
types
:
if
kind
not
in
self
.
_
types
:
# Check whether the plugin is specified in plugins
source
=
None
defaults
=
None
loaded_dependency
=
False
for
origin
in
self
.
plugin_origins
:
for
origin
in
self
.
_
plugin_origins
:
if
kind
not
in
origin
[
'
plugins
'
]:
continue
...
...
@@ -145,19 +150,19 @@ class PluginContext():
# Fall back to getting the source from site
if
not
source
:
if
kind
not
in
self
.
site_source
.
list_plugins
():
if
kind
not
in
self
.
_
site_source
.
list_plugins
():
raise
PluginError
(
"
No {} type registered for kind
'
{}
'"
.
format
(
self
.
base_type
.
__name__
,
kind
))
.
format
(
self
.
_
base_type
.
__name__
,
kind
))
source
=
self
.
site_source
source
=
self
.
_
site_source
self
.
types
[
kind
]
=
self
.
load_plugin
(
source
,
kind
,
defaults
)
self
.
_
types
[
kind
]
=
self
.
_
load_plugin
(
source
,
kind
,
defaults
)
if
loaded_dependency
:
self
.
loaded_dependencies
.
append
(
kind
)
return
self
.
types
[
kind
]
return
self
.
_
types
[
kind
]
def
load_plugin
(
self
,
source
,
kind
,
defaults
):
def
_
load_plugin
(
self
,
source
,
kind
,
defaults
):
try
:
plugin
=
source
.
load_plugin
(
kind
)
...
...
@@ -170,38 +175,38 @@ class PluginContext():
except
ImportError
as
e
:
raise
PluginError
(
"
Failed to load {} plugin
'
{}
'
: {}
"
.
format
(
self
.
base_type
.
__name__
,
kind
,
e
))
from
e
.
format
(
self
.
_
base_type
.
__name__
,
kind
,
e
))
from
e
try
:
plugin_type
=
plugin
.
setup
()
except
AttributeError
as
e
:
raise
PluginError
(
"
{} plugin
'
{}
'
did not provide a setup() function
"
.
format
(
self
.
base_type
.
__name__
,
kind
))
from
e
.
format
(
self
.
_
base_type
.
__name__
,
kind
))
from
e
except
TypeError
as
e
:
raise
PluginError
(
"
setup symbol in {} plugin
'
{}
'
is not a function
"
.
format
(
self
.
base_type
.
__name__
,
kind
))
from
e
.
format
(
self
.
_
base_type
.
__name__
,
kind
))
from
e
self
.
assert_plugin
(
kind
,
plugin_type
)
self
.
assert_version
(
kind
,
plugin_type
)
self
.
_
assert_plugin
(
kind
,
plugin_type
)
self
.
_
assert_version
(
kind
,
plugin_type
)
return
(
plugin_type
,
defaults
)
def
assert_plugin
(
self
,
kind
,
plugin_type
):
if
kind
in
self
.
types
:
def
_
assert_plugin
(
self
,
kind
,
plugin_type
):
if
kind
in
self
.
_
types
:
raise
PluginError
(
"
Tried to register {} plugin for existing kind
'
{}
'
"
"
(already registered {})
"
.
format
(
self
.
base_type
.
__name__
,
kind
,
self
.
types
[
kind
].
__name__
))
.
format
(
self
.
_
base_type
.
__name__
,
kind
,
self
.
_
types
[
kind
].
__name__
))
try
:
if
not
issubclass
(
plugin_type
,
self
.
base_type
):
if
not
issubclass
(
plugin_type
,
self
.
_
base_type
):
raise
PluginError
(
"
{} plugin
'
{}
'
returned type
'
{}
'
, which is not a subclass of {}
"
.
format
(
self
.
base_type
.
__name__
,
kind
,
.
format
(
self
.
_
base_type
.
__name__
,
kind
,
plugin_type
.
__name__
,
self
.
base_type
.
__name__
))
self
.
_
base_type
.
__name__
))
except
TypeError
as
e
:
raise
PluginError
(
"
{} plugin
'
{}
'
returned something that is not a type (expected subclass of {})
"
.
format
(
self
.
base_type
.
__name__
,
kind
,
self
.
base_type
.
__name__
))
from
e
.
format
(
self
.
_
base_type
.
__name__
,
kind
,
self
.
_
base_type
.
__name__
))
from
e
def
assert_version
(
self
,
kind
,
plugin_type
):
def
_
assert_version
(
self
,
kind
,
plugin_type
):
# Now assert BuildStream version
bst_major
,
bst_minor
=
utils
.
get_bst_version
()
...
...
@@ -212,6 +217,6 @@ class PluginContext():
raise
PluginError
(
"
BuildStream {}.{} is too old for {} plugin
'
{}
'
(requires {}.{})
"
.
format
(
bst_major
,
bst_minor
,
self
.
base_type
.
__name__
,
kind
,
self
.
_
base_type
.
__name__
,
kind
,
plugin_type
.
BST_REQUIRED_VERSION_MAJOR
,
plugin_type
.
BST_REQUIRED_VERSION_MINOR
))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment