def _sort_profiles_by_dependencies(self, profiles): """Sort the profiles so that the profiles are listed after its dependencies since it is safer to first install dependencies. """ sorted_profile_ids = get_sorted_profile_ids(self.portal_setup) return sorted(profiles, key=lambda p: sorted_profile_ids.index(p.get("id")))
def _get_sorted_post_upgrade_adapters(self): """Returns a list of post upgrade adapters, sorted by profile dependencies. Assumes that the names of the adapters are profile names (e.g. "ftw.upgrade:default"). """ profile_order = get_sorted_profile_ids(self.portal_setup) portal_url = getToolByName(self.portal_setup, 'portal_url') portal = portal_url.getPortalObject() adapters = list(getAdapters((portal, portal.REQUEST), IPostUpgrade)) def _sorter(a, b): name_a = a[0] name_b = b[0] if name_a not in profile_order and name_b not in profile_order: return 0 elif name_a not in profile_order: return -1 elif name_b not in profile_order: return 1 else: return cmp(profile_order.index(name_a), profile_order.index(name_b)) adapters.sort(_sorter) return [adapter for name, adapter in adapters]
def test_cyclic_dependencies(self): portal_setup = self.mocker.mock() self.expect(portal_setup.listProfileInfo()).result([ {'id': 'foo', 'dependencies': ['profile-bar']}, {'id': 'bar', 'dependencies': ['profile-foo']}]).count(1, 2) self.replay() with self.assertRaises(CyclicDependencies) as cm: get_sorted_profile_ids(portal_setup) self.assertEqual(cm.exception.dependencies, [('foo', 'bar'), ('bar', 'foo')])
def _sort_profiles_by_dependencies(self, profiles): """Sort the profiles so that the profiles are listed after its dependencies since it is safer to first install dependencies. """ sorted_profile_ids = get_sorted_profile_ids(self.portal_setup) return sorted(profiles, key=lambda p: sorted_profile_ids.index(p.get('id')))
def test_cyclic_dependencies(self): portal_setup = self.mocker.mock() self.expect(portal_setup.listProfileInfo()).result([{ 'id': 'foo', 'dependencies': ['profile-bar'] }, { 'id': 'bar', 'dependencies': ['profile-foo'] }]).count(1, 2) self.replay() with self.assertRaises(CyclicDependencies) as cm: get_sorted_profile_ids(portal_setup) self.assertEqual(cm.exception.dependencies, [('foo', 'bar'), ('bar', 'foo')])
def test_cyclic_dependencies(self): listProfileInfo = self.mock() listProfileInfo.return_value = [ {'id': 'foo', 'dependencies': ['profile-bar']}, {'id': 'bar', 'dependencies': ['profile-foo']}, {'id': 'baz', 'dependencies': []} ] portal_setup = self.create_dummy() portal_setup.listProfileInfo = listProfileInfo with self.assertRaises(CyclicDependencies) as cm: get_sorted_profile_ids(portal_setup) self.assertEqual([('foo', 'bar')], cm.exception.cyclic_dependencies) self.assertEqual([('foo', 'bar'), ('bar', 'foo')], cm.exception.dependencies)
def test_declaring_upgrades_dependency(self): self.package.with_profile( Builder('genericsetup profile').named('bar').with_upgrade( Builder('ftw upgrade step').to(datetime( 2010, 1, 1, 1, 1)).with_zcml_directory_options( soft_dependencies="the.package:baz"))) self.package.with_profile( Builder('genericsetup profile').named('foo').with_upgrade( Builder('ftw upgrade step').to(datetime( 2010, 1, 1, 1, 1)).with_zcml_directory_options( soft_dependencies="the.package:bar the.package:baz"))) self.package.with_profile( Builder('genericsetup profile').named('baz').with_upgrade( Builder('ftw upgrade step').to(datetime( 2010, 1, 1, 1, 1)).with_zcml_directory_options( soft_dependencies="the.package:default"))) with self.package_created() as package: self.assert_profile({ 'id': u'the.package:foo', 'title': u'the.package', 'description': u'', 'ftw.upgrade:dependencies': [u'the.package:bar', u'the.package:baz'], 'path': package.package_path.joinpath('profiles', 'foo'), 'version': u'20100101010100', 'product': 'the.package', 'type': EXTENSION, 'for': None }) portal_setup = getToolByName(self.portal, 'portal_setup') self.assertEqual([ u'the.package:default', u'the.package:baz', u'the.package:bar', u'the.package:foo' ], [ profile_id for profile_id in get_sorted_profile_ids(portal_setup) if profile_id.startswith('the.package:') ])
def test_declaring_upgrades_dependency(self): self.package.with_profile( Builder('genericsetup profile') .named('bar') .with_upgrade( Builder('ftw upgrade step') .to(datetime(2010, 1, 1, 1, 1)) .with_zcml_directory_options( soft_dependencies="the.package:baz"))) self.package.with_profile( Builder('genericsetup profile') .named('foo') .with_upgrade( Builder('ftw upgrade step') .to(datetime(2010, 1, 1, 1, 1)) .with_zcml_directory_options( soft_dependencies="the.package:bar the.package:baz"))) self.package.with_profile( Builder('genericsetup profile') .named('baz') .with_upgrade( Builder('ftw upgrade step') .to(datetime(2010, 1, 1, 1, 1)) .with_zcml_directory_options( soft_dependencies="the.package:default"))) with self.package_created() as package: self.assert_profile( {'id': u'the.package:foo', 'title': u'the.package', 'description': u'', 'ftw.upgrade:dependencies': [u'the.package:bar', u'the.package:baz'], 'path': package.package_path.joinpath('profiles', 'foo'), 'version': u'20100101010100', 'product': 'the.package', 'type': EXTENSION, 'for': None}) portal_setup = getToolByName(self.portal, 'portal_setup') self.assertEquals( [u'the.package:default', u'the.package:baz', u'the.package:bar', u'the.package:foo'], filter(lambda profile_id: profile_id.startswith('the.package:'), get_sorted_profile_ids(portal_setup)))
def test_dependencies_resolved(self): listProfileInfo = self.mock() listProfileInfo.return_value = [{ 'id': 'baz', 'dependencies': ['profile-foo', 'profile-bar'] }, { 'id': 'foo' }, { 'id': 'bar', 'dependencies': ['profile-foo'] }] portal_setup = self.create_dummy() portal_setup.listProfileInfo = listProfileInfo self.assertEqual(['foo', 'bar', 'baz'], get_sorted_profile_ids(portal_setup))
def test_dependencies_resolved(self): portal_setup = self.mocker.mock() self.expect(portal_setup.listProfileInfo()).result([ {'id': 'baz', 'dependencies': [ 'profile-foo', 'profile-bar']}, {'id': 'foo'}, {'id': 'bar', 'dependencies': ['profile-foo']}]).count(1, 2) self.replay() self.assertEqual( ['foo', 'bar', 'baz'], get_sorted_profile_ids(portal_setup))
def test_root_profiles_are_ordered_by_profile_name(self): """In the this example the profiles "baz" and "xy" have no dependencies to another and thus might be ordered in any order from the graph point of view. However, we want a cosistent ordern and therefore order those root nodes by name. """ portal_setup = self.mocker.mock() self.expect(portal_setup.listProfileInfo()).result([ {'id': 'baz', 'dependencies': [ 'profile-foo']}, {'id': 'foo'}, {'id': 'xy'}]).count(1, 2) self.replay() self.assertEqual( ['foo', 'baz', 'xy'], get_sorted_profile_ids(portal_setup))
def test_root_profiles_are_ordered_by_profile_name(self): """In the this example the profiles "baz" and "xy" have no dependencies to another and thus might be ordered in any order from the graph point of view. However, we want a cosistent ordern and therefore order those root nodes by name. """ listProfileInfo = self.mock() listProfileInfo.return_value = [ {'id': 'baz', 'dependencies': ['profile-foo']}, {'id': 'foo'}, {'id': 'xy'} ] portal_setup = self.create_dummy() portal_setup.listProfileInfo = listProfileInfo self.assertEqual( ['foo', 'baz', 'xy'], get_sorted_profile_ids(portal_setup))
def _get_sorted_post_upgrade_adapters(self): """Returns a list of post upgrade adapters, sorted by profile dependencies. Assumes that the names of the adapters are profile names (e.g. "ftw.upgrade:default"). """ profile_order = get_sorted_profile_ids(self.portal_setup) portal_url = getToolByName(self.portal_setup, 'portal_url') portal = portal_url.getPortalObject() adapters = list(getAdapters((portal, portal.REQUEST), IPostUpgrade)) def sort_key(item): name = item[0] if name not in profile_order: return -1 else: return profile_order.index(name) adapters.sort(key=sort_key) return [adapter for name, adapter in adapters]
def test_root_profiles_are_ordered_by_profile_name(self): """In the this example the profiles "baz" and "xy" have no dependencies to another and thus might be ordered in any order from the graph point of view. However, we want a cosistent ordern and therefore order those root nodes by name. """ portal_setup = self.mocker.mock() self.expect(portal_setup.listProfileInfo()).result([{ 'id': 'baz', 'dependencies': ['profile-foo'] }, { 'id': 'foo' }, { 'id': 'xy' }]).count(1, 2) self.replay() self.assertEqual(['foo', 'baz', 'xy'], get_sorted_profile_ids(portal_setup))