Use pkg_resources resolve() and require() instead of load()

setuptools 11.3 deprecated the require flag to load() in favor of two
new methods. If those methods are present, use them instead of calling
load(). If those methods are not present, continue to use load() as
before.

Change-Id: I800eec863e34ba4c7a90153e6ae2bfa953bfddd6
Closes-bug: #1407778
This commit is contained in:
Doug Hellmann 2015-01-05 14:57:32 -05:00
parent 9324bf0b67
commit 37c94cd0d6
2 changed files with 48 additions and 1 deletions

View File

@ -175,7 +175,14 @@ class ExtensionManager(object):
def _load_one_plugin(self, ep, invoke_on_load, invoke_args, invoke_kwds,
verify_requirements):
plugin = ep.load(require=verify_requirements)
# NOTE(dhellmann): Using require=False is deprecated in
# setuptools 11.3.
if hasattr(ep, 'resolve') and hasattr(ep, 'require'):
if verify_requirements:
ep.require()
plugin = ep.resolve()
else:
plugin = ep.load(require=verify_requirements)
if invoke_on_load:
obj = plugin(*invoke_args, **invoke_kwds)
else:

View File

@ -169,3 +169,43 @@ class TestCallback(utils.TestCase):
result = em.map_method('get_args_and_data', 42)
self.assertEqual(set(r[2] for r in result), set([42]))
class TestLoadRequirementsNewSetuptools(utils.TestCase):
# setuptools 11.3 and later
def setUp(self):
super(TestLoadRequirementsNewSetuptools, self).setUp()
self.mock_ep = mock.Mock(spec=['require', 'resolve', 'load', 'name'])
self.em = extension.ExtensionManager.make_test_instance([])
def test_verify_requirements(self):
self.em._load_one_plugin(self.mock_ep, False, (), {},
verify_requirements=True)
self.mock_ep.require.assert_called_once_with()
self.mock_ep.resolve.assert_called_once_with()
def test_no_verify_requirements(self):
self.em._load_one_plugin(self.mock_ep, False, (), {},
verify_requirements=False)
self.assertEqual(0, self.mock_ep.require.call_count)
self.mock_ep.resolve.assert_called_once_with()
class TestLoadRequirementsOldSetuptools(utils.TestCase):
# Before setuptools 11.3
def setUp(self):
super(TestLoadRequirementsOldSetuptools, self).setUp()
self.mock_ep = mock.Mock(spec=['load', 'name'])
self.em = extension.ExtensionManager.make_test_instance([])
def test_verify_requirements(self):
self.em._load_one_plugin(self.mock_ep, False, (), {},
verify_requirements=True)
self.mock_ep.load.assert_called_once_with(require=True)
def test_no_verify_requirements(self):
self.em._load_one_plugin(self.mock_ep, False, (), {},
verify_requirements=False)
self.mock_ep.load.assert_called_once_with(require=False)