Phil Dawson pushed to branch phil/848-plugin-deprecation-warnings at BuildStream / buildstream
Commits:
-
3493ca78
by Phil Dawson at 2019-01-23T11:19:42Z
3 changed files:
- buildstream/_project.py
- buildstream/plugin.py
- + tests/plugins/basics/deprecationwarnings/deprecationwarnings.py
Changes:
| ... | ... | @@ -232,7 +232,8 @@ class Project(): |
| 232 | 232 |
'artifacts', 'options',
|
| 233 | 233 |
'fail-on-overlap', 'shell', 'fatal-warnings',
|
| 234 | 234 |
'ref-storage', 'sandbox', 'mirrors', 'remote-execution',
|
| 235 |
- 'sources', '(@)'
|
|
| 235 |
+ 'sources', '(@)',
|
|
| 236 |
+ 'supress-deprecation-warnings'
|
|
| 236 | 237 |
])
|
| 237 | 238 |
|
| 238 | 239 |
# create_element()
|
| ... | ... | @@ -428,6 +429,10 @@ class Project(): |
| 428 | 429 |
|
| 429 | 430 |
self._validate_node(pre_config_node)
|
| 430 | 431 |
|
| 432 |
+ # Load plugin deprecation warning supression config
|
|
| 433 |
+ self.supressed_deprecation_warnings = _yaml.node_get(
|
|
| 434 |
+ pre_config_node, list, 'supress-deprecation-warnings', default_value=[])
|
|
| 435 |
+ |
|
| 431 | 436 |
# FIXME:
|
| 432 | 437 |
#
|
| 433 | 438 |
# Performing this check manually in the absense
|
| ... | ... | @@ -164,6 +164,25 @@ class Plugin(): |
| 164 | 164 |
core format version :ref:`core format version <project_format_version>`.
|
| 165 | 165 |
"""
|
| 166 | 166 |
|
| 167 |
+ BST_PLUGIN_DEPRECATED = False
|
|
| 168 |
+ """True if this element plugin has been deprecated.
|
|
| 169 |
+ |
|
| 170 |
+ If this is set to true, the plugin will call self.eimit_deprecation_warning()
|
|
| 171 |
+ on instantiation. Plugin authors may override this method to provide
|
|
| 172 |
+ custom deprecation warnings
|
|
| 173 |
+ |
|
| 174 |
+ *Since 1.4*
|
|
| 175 |
+ """
|
|
| 176 |
+ |
|
| 177 |
+ BST_PLUGIN_DEPRECATION_MESSAGE = ""
|
|
| 178 |
+ """ The message printed when this element shows a deprecation warning.
|
|
| 179 |
+ |
|
| 180 |
+ This should be set if BST_PLUGIN_DEPRECATED is True and should direct the user
|
|
| 181 |
+ to the deprecated plug-in's replacement.
|
|
| 182 |
+ |
|
| 183 |
+ *Since 1.4*
|
|
| 184 |
+ """
|
|
| 185 |
+ |
|
| 167 | 186 |
def __init__(self, name, context, project, provenance, type_tag):
|
| 168 | 187 |
|
| 169 | 188 |
self.name = name
|
| ... | ... | @@ -188,6 +207,13 @@ class Plugin(): |
| 188 | 207 |
self.__kind = modulename.split('.')[-1]
|
| 189 | 208 |
self.debug("Created: {}".format(self))
|
| 190 | 209 |
|
| 210 |
+ # If this plugin has been deprecated, emit a warning.
|
|
| 211 |
+ silenced_warnings = self.__project.supressed_deprecation_warnings
|
|
| 212 |
+ if self.BST_PLUGIN_DEPRECATED and self.__kind not in silenced_warnings:
|
|
| 213 |
+ detail = "Using deprecated plugin {}: {}".format(self.__kind,
|
|
| 214 |
+ self.BST_PLUGIN_DEPRECATION_MESSAGE)
|
|
| 215 |
+ self.__message(MessageType.WARN, detail)
|
|
| 216 |
+ |
|
| 191 | 217 |
def __del__(self):
|
| 192 | 218 |
# Dont send anything through the Message() pipeline at destruction time,
|
| 193 | 219 |
# any subsequent lookup of plugin by unique id would raise KeyError.
|
| 1 |
+import pytest
|
|
| 2 |
+import tempfile
|
|
| 3 |
+import os
|
|
| 4 |
+ |
|
| 5 |
+from tests.testutils import cli, generate_junction
|
|
| 6 |
+from buildstream import _yaml
|
|
| 7 |
+import buildstream.plugins.elements.manual as manual
|
|
| 8 |
+ |
|
| 9 |
+ |
|
| 10 |
+_DEPRECATION_MESSAGE = "Here is some detail."
|
|
| 11 |
+ |
|
| 12 |
+ |
|
| 13 |
+def test_deprecation_warning_present(cli):
|
|
| 14 |
+ |
|
| 15 |
+ manual.ManualElement.BST_PLUGIN_DEPRECATED = True
|
|
| 16 |
+ manual.ManualElement.BST_PLUGIN_DEPRECATION_MESSAGE = _DEPRECATION_MESSAGE
|
|
| 17 |
+ |
|
| 18 |
+ with tempfile.TemporaryDirectory() as project:
|
|
| 19 |
+ result = cli.run(project=project, args=['init', '--project-name', 'deprectedplugins'])
|
|
| 20 |
+ result.assert_success()
|
|
| 21 |
+ |
|
| 22 |
+ manual_element = os.path.join(project, 'elements', 'manual.bst')
|
|
| 23 |
+ with open(manual_element, 'w') as f:
|
|
| 24 |
+ f.write('kind: manual')
|
|
| 25 |
+ |
|
| 26 |
+ |
|
| 27 |
+ result = cli.run(project=project, args=['show', 'manual.bst'])
|
|
| 28 |
+ |
|
| 29 |
+ result.assert_success()
|
|
| 30 |
+ expected_warning = "Using deprecated plugin manual: {}".format(_DEPRECATION_MESSAGE)
|
|
| 31 |
+ assert expected_warning in result.stderr
|
|
| 32 |
+ |
|
| 33 |
+ |
|
| 34 |
+def test_supress_deprecation_warning(cli):
|
|
| 35 |
+ |
|
| 36 |
+ manual.ManualElement.BST_PLUGIN_DEPRECATED = True
|
|
| 37 |
+ manual.ManualElement.BST_PLUGIN_DEPRECATION_MESSAGE = _DEPRECATION_MESSAGE
|
|
| 38 |
+ |
|
| 39 |
+ with tempfile.TemporaryDirectory() as project:
|
|
| 40 |
+ result = cli.run(project=project, args=['init', '--project-name', 'deprectedplugins'])
|
|
| 41 |
+ result.assert_success()
|
|
| 42 |
+ |
|
| 43 |
+ project_conf = os.path.join(project, 'project.conf')
|
|
| 44 |
+ with open(project_conf, 'a') as f:
|
|
| 45 |
+ f.write("\nsupress-deprecation-warnings: [manual]")
|
|
| 46 |
+ |
|
| 47 |
+ manual_element = os.path.join(project, 'elements', 'manual.bst')
|
|
| 48 |
+ with open(manual_element, 'w') as f:
|
|
| 49 |
+ f.write('kind: manual')
|
|
| 50 |
+ |
|
| 51 |
+ |
|
| 52 |
+ result = cli.run(project=project, args=['show', 'manual.bst'])
|
|
| 53 |
+ |
|
| 54 |
+ result.assert_success()
|
|
| 55 |
+ expected_warning = "Using deprecated plugin manual: {}".format(_DEPRECATION_MESSAGE)
|
|
| 56 |
+ assert not expected_warning in result.stderr
|
