Skip to content

Commit

Permalink
Merge pull request #119 from zopefoundation/maurits-upgradeProfile-quiet
Browse files Browse the repository at this point in the history
Allow passing quiet=True to upgradeProfile.
  • Loading branch information
mauritsvanrees committed Mar 23, 2022
2 parents 482173d + 4736b61 commit 26b1084
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Changelog
2.1.6 (unreleased)
------------------

- *nothing changed yet*
- Allow passing ``quiet=True`` to ``upgradeProfile``.
Then we do not complain when the profile is not installed or needs no upgrade.


2.1.5 (2021-12-03)
Expand Down
17 changes: 17 additions & 0 deletions src/Products/GenericSetup/tests/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,13 @@ def test_upgradeProfile_no_profile_id_or_updates(self):
tool.upgradeProfile('no.such.profile:default', dest='42')
self.assertEqual(tool._profile_upgrade_versions, {})

# On Python 3.4+ we could use self.assertLogs to check that a warning
# message was logged, and on 3.10+ use self.assertNoLogs to test
# the opposite when called with quiet=True. Since we still support
# Python 2.7, never mind. Do quickly test that the quiet argument
# throws no error.
tool.upgradeProfile('no.such.profile:default', quiet=True)

def test_persistent_profile_upgrade_versions(self):
site = self._makeSite()
site.setup_tool = self._makeOne('setup_tool')
Expand Down Expand Up @@ -1639,6 +1646,12 @@ def step3_checker(tool):
# Upgrade the profile one step to version 1.
tool.upgradeProfile('foo', '1')
self.assertEqual(tool.getLastVersionForProfile('foo'), ('1',))
# Do that again and you will get a warning, which you can choose
# to silence. Note: we do not test if something really gets logged.
tool.upgradeProfile('foo', '1')
self.assertEqual(tool.getLastVersionForProfile('foo'), ('1',))
tool.upgradeProfile('foo', '1', quiet=True)
self.assertEqual(tool.getLastVersionForProfile('foo'), ('1',))
# Upgrade the profile two steps to version 3. This one has a
# checker. The profile version must be correctly updated.
tool.upgradeProfile('foo', '3')
Expand All @@ -1647,6 +1660,10 @@ def step3_checker(tool):
# should happen.
tool.upgradeProfile('foo', '5')
self.assertEqual(tool.getLastVersionForProfile('foo'), ('3',))
# It throws a warning which you can choose to ignore.
# The effect is the same.
tool.upgradeProfile('foo', '5', quiet=True)
self.assertEqual(tool.getLastVersionForProfile('foo'), ('3',))
# Upgrade the profile to the latest version.
tool.upgradeProfile('foo')
self.assertEqual(tool.getLastVersionForProfile('foo'), ('4',))
Expand Down
18 changes: 11 additions & 7 deletions src/Products/GenericSetup/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,29 +1118,33 @@ def manage_doUpgrades(self, request=None):
% (url, profile_id))

@security.protected(ManagePortal)
def upgradeProfile(self, profile_id, dest=None):
def upgradeProfile(self, profile_id, dest=None, quiet=False):
"""Upgrade a profile.
Apply all upgrade steps.
When 'dest' is given, only update to that version. If the
version is not found, give a warning and do nothing.
When 'quiet' is True, we do not complain when we cannot do anything.
If the profile was not applied previously (last version for
profile is unknown) we do nothing.
"""
if self.getLastVersionForProfile(profile_id) == UNKNOWN:
generic_logger.warning('Version of profile %s is unknown, '
'refusing to upgrade.', profile_id)
if not quiet:
generic_logger.warning('Version of profile %s is unknown, '
'refusing to upgrade.', profile_id)
return
if dest is not None:
# Upgrade to a specific destination version, if found.
if isinstance(dest, six.string_types):
dest = tuple(dest.split('.'))
if self.getLastVersionForProfile(profile_id) == dest:
generic_logger.warning('Profile %s is already at wanted '
'destination %r.', profile_id,
_version_for_print(dest))
if not quiet:
generic_logger.warning('Profile %s is already at wanted '
'destination %r.', profile_id,
_version_for_print(dest))
return
upgrades = self.listUpgrades(profile_id)
# First get a list of single steps to apply. This may be
Expand Down Expand Up @@ -1180,7 +1184,7 @@ def upgradeProfile(self, profile_id, dest=None):
profile_id,
_version_for_print(
self.getLastVersionForProfile(profile_id)))
else:
elif not quiet:
generic_logger.info(
'No upgrades available for profile %s. '
'Profile stays at version %r.', profile_id,
Expand Down

0 comments on commit 26b1084

Please sign in to comment.