Skip to content

Conversation

@ydirson
Copy link
Contributor

@ydirson ydirson commented Oct 11, 2023

This fixes the various problems seen as part of #66, and then some.

This is a continuation of #67 which got closed when xs8 was destroyed and cannot be reopened (thx github).

diskutil.py Outdated
elif os.path.exists("/sys/block/%s/size" % dev):
return int(__readOneLineFile__("/sys/block/%s/size" % dev))
else:
raise Exception("%s not found as %s or %s" % (dev, "/sys/block/%s/device/block/size",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will end up raising as e.g. sda not found as /sys/block/%s/device/block/size or /sys/block/%s/size, i.e. the %s characters will come out in the exception text, which doesn't seem quite what you'd expect?

If we don't need this for xs8 (i.e. we can rely on py3 functionality), could we use a format string to make it easier, at which point you'd just have e.g. raise Exception(f"{dev} not found as /sys/block/{dev}/device/block/size or /sys/block/{dev}/size")?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are trying to keep Python 2/3 compatibility, or at least have master and release/xs8 branches as similar as possible. Maybe a more generic and easier "Unable to get block size of %s device" % dev would be enough, also not requiring to be updated as new potential virtual files are needed to be checked.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moving the full paths inside the format string should be good for now. At least using str.format and {0} will allow to avoid repeating dev

@freddy77
Copy link
Contributor

freddy77 commented Jun 5, 2024

BTW... got the error too this morning. Pretty boring indeed.

@ydirson ydirson force-pushed the inexistant-disks branch from 7a99335 to 44624db Compare June 12, 2024 14:41
@ydirson ydirson requested review from alexbrett and freddy77 June 12, 2024 14:41
product.py Outdated
backups.append(backup)
except:
if not os.path.exists(os.path.join(b.mount_point, '.xen-backup-partition')):
raise StopIteration()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this raising StopIteration and catching it weird and hard to read. Why not using a try/finally doing the b.umount() in the finally and using continue here?

Copy link
Contributor Author

@ydirson ydirson Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lack of break equivalent for try blocks is a problem. I guess return [] here would be suitable enough if we did not have a loop.
I'm quite unsure how to make that more readable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are inside a for loop, why not using break and/or continue ? Also, you can put the b.unmount() inside a finally section.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@ydirson ydirson requested a review from freddy77 March 17, 2025 16:51
@ydirson
Copy link
Contributor Author

ydirson commented Mar 17, 2025

Rebased, and improved the exception logging in patch 2

ydirson added 3 commits May 15, 2025 11:00
- it is bad practice to "catch any"
- not logging anything just hides information, which can be especially
  important here as the reason for a try/catch is not obvious (exceptions
  thrown by XenServerBackup.__init__?)

Signed-off-by: Yann Dirson <yann.dirson@vates.fr>
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 <yann.dirson@vates.fr>
@ydirson ydirson force-pushed the inexistant-disks branch from 561a17b to 6cea3a1 Compare May 15, 2025 09:00
@ydirson
Copy link
Contributor Author

ydirson commented May 15, 2025

Dropped the first commit, made unnecessary by #237

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants