From ff86ae755602b5296fc7ae3bd21147f35ba591a9 Mon Sep 17 00:00:00 2001 From: Rishabh Dave Date: Thu, 19 May 2022 21:03:54 +0530 Subject: [PATCH] cephfs-shell: check version before importing Cmd2ArgparseError Cmd2ArgparseError is available only cmd2 version 1.0.1 onwards. Before that, SystemExit(2) is raised. This commit creates an empty class Cmd2ArgparseError for earlier version so that similar error won't creep up again. Fixes: https://tracker.ceph.com/issues/55716 Signed-off-by: Rishabh Dave --- src/tools/cephfs/cephfs-shell | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/tools/cephfs/cephfs-shell b/src/tools/cephfs/cephfs-shell index 7b8597a457b..ca50588b01d 100755 --- a/src/tools/cephfs/cephfs-shell +++ b/src/tools/cephfs/cephfs-shell @@ -16,10 +16,19 @@ import shlex import stat import errno +from distutils.version import LooseVersion + from cmd2 import Cmd from cmd2 import __version__ as cmd2_version -from cmd2.exceptions import Cmd2ArgparseError -from distutils.version import LooseVersion +# XXX: In cmd2 versions < 1.0.1, we'll get SystemExit(2) instead of +# Cmd2ArgparseError +if LooseVersion(cmd2_version) >= LooseVersion("1.0.1"): + from cmd2.exceptions import Cmd2ArgparseError +else: + # HACK: so that we don't have check for version everywhere + # Cmd2ArgparseError is used. + class Cmd2ArgparseError: + pass if sys.version_info.major < 3: raise RuntimeError("cephfs-shell is only compatible with python3") @@ -476,6 +485,10 @@ class CephFSShell(Cmd): set_exit_code_msg(msg=None) else: set_exit_code_msg(msg=f'{type(e).__name__}: {e}') + # In cmd2 versions < 1.1.0 we'll get SystemExit(2) instead of + # Cmd2ArgparseError + except SystemExit: + raise class path_to_bytes(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): -- 2.39.5