Skip to content

Commit

Permalink
findXenSourceBackups: refactor and log reason for ignoring
Browse files Browse the repository at this point in the history
Previous code structure was that of 2 nested if's, with nominal code
path being in the positive branch of each if.  Adding another
condition will bring a need for logging the reason to ignore a backup,
but doing that by converting inner `if` to an `elif` chain (arguably
the simplest modification) would bring the nominal code path to switch
to the negative/last branch of the inner `elif` chain.  At the same
time, the outer `if` would not seem special enough to deserve it
special place (and the cyclomatic complexity).

This commit leverages the existing `try:except:` block to switch to an
"error out the loop" pattern, where the nominal code path is just
linear.

Using `StopIteration` may feel like an abuse, but:
- it is the only standard `Exception` that is not a `StandardError` or
  a `Warning`, and defining a new one could seem overkill
- the closest alternative would be a `while True` loop and breaking out
  in both exceptional code paths and at the end of nominal path, where
  the `break` statements would "stop the iteration" as well

Signed-off-by: Yann Dirson <[email protected]>
  • Loading branch information
ydirson committed Oct 6, 2023
1 parent 1781d88 commit c3f126e
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions product.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,12 +534,25 @@ def findXenSourceBackups():
b = None
try:
b = util.TempMount(p, 'backup-', ['ro'], 'ext3')
if os.path.exists(os.path.join(b.mount_point, '.xen-backup-partition')):
backup = XenServerBackup(p, b.mount_point)
logger.log("Found a backup: %s" % (repr(backup),))
if backup.version >= XENSERVER_MIN_VERSION and \
backup.version <= THIS_PLATFORM_VERSION:
backups.append(backup)
if not os.path.exists(os.path.join(b.mount_point, '.xen-backup-partition')):
raise StopIteration()

backup = XenServerBackup(p, b.mount_point)
logger.log("Found a backup: %s" % (repr(backup),))

if backup.version < XENSERVER_MIN_VERSION:
logger.log("findXenSourceBackups: ignoring, platform too old: %s < %s" %
(backup.version, XENSERVER_MIN_VERSION))
raise StopIteration()
if backup.version > THIS_PLATFORM_VERSION:
logger.log("findXenSourceBackups: ignoring later platform: %s > %s" %
(backup.version, THIS_PLATFORM_VERSION))
raise StopIteration()

backups.append(backup)

except StopIteration:
pass
except Exception as ex:
logger.log("findXenSourceBackups caught exception for partition %s: %s" % (p, ex))
pass
Expand Down

0 comments on commit c3f126e

Please sign in to comment.