From 6b711187838db49506f0971dc25cb92deb6f9e8d Mon Sep 17 00:00:00 2001 From: Eric Harney Date: Tue, 17 May 2022 11:21:51 -0400 Subject: [PATCH] cmd/manage and coordination: Clean up exception handling This makes it easier for pylint and mypy to validate this code, and avoids isinstance() checks. Follows the same pattern used in commit 1075d1d. Change-Id: I64c38e74a6b7e8180d63cc5d5d13b8cb76d52d3e --- cinder/cmd/manage.py | 12 ++++++++---- cinder/coordination.py | 13 +++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/cinder/cmd/manage.py b/cinder/cmd/manage.py index 48952d6c314..23e0ee859b2 100644 --- a/cinder/cmd/manage.py +++ b/cinder/cmd/manage.py @@ -930,6 +930,10 @@ class UtilCommands(object): if not objects.Snapshot.exists(self.ctxt, snap_id)} self._exclude_running_backups(backups) + def _err(filename: str, exc: Exception) -> None: + print('Failed to cleanup lock %(name)s: %(exc)s', + {'name': filename, 'exc': exc}) + # Now clean for filenames in itertools.chain(volumes.values(), snapshots.values(), @@ -937,11 +941,11 @@ class UtilCommands(object): for filename in filenames: try: os.remove(filename) + except OSError as exc: + if (exc.errno != errno.ENOENT): + _err(filename, exc) except Exception as exc: - if not (isinstance(exc, OSError) and - exc.errno == errno.ENOENT): - print('Failed to cleanup lock %(name)s: %(exc)s', - {'name': filename, 'exc': exc}) + _err(filename, exc) CATEGORIES = { diff --git a/cinder/coordination.py b/cinder/coordination.py index 6b499e7d62c..9db79e80088 100644 --- a/cinder/coordination.py +++ b/cinder/coordination.py @@ -110,16 +110,21 @@ class Coordinator(object): def remove_lock(self, glob_name): # Most locks clean up on release, but not the file lock, so we manually # clean them. + + def _err(file_name: str, exc: Exception) -> None: + LOG.warning('Failed to cleanup lock %(name)s: %(exc)s', + {'name': file_name, 'exc': exc}) + if self._file_path: files = glob.glob(self._file_path + glob_name) for file_name in files: try: os.remove(file_name) + except OSError as exc: + if (exc.errno != errno.ENOENT): + _err(file_name, exc) except Exception as exc: - if not (isinstance(exc, OSError) and - exc.errno == errno.ENOENT): - LOG.warning('Failed to cleanup lock %(name)s: %(exc)s', - {'name': file_name, 'exc': exc}) + _err(file_name, exc) COORDINATOR = Coordinator(prefix='cinder-')