From d942a1b44a61580feef2d0add90f8b763019ee9f Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Sat, 18 Nov 2023 13:57:48 -0500 Subject: [PATCH] cephadm: add unlink_file function Add the unlink_file function to file_utils. This fills in a gap between python 3.6 and features provided in pathlib.Path in later versions of python. Adds an option to ignore all errors for good measure. Signed-off-by: John Mulligan --- src/cephadm/cephadmlib/file_utils.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/cephadm/cephadmlib/file_utils.py b/src/cephadm/cephadmlib/file_utils.py index 1b9f11499a4..9c1ca50b925 100644 --- a/src/cephadm/cephadmlib/file_utils.py +++ b/src/cephadm/cephadmlib/file_utils.py @@ -143,3 +143,21 @@ def get_file_timestamp(fn): def make_run_dir(fsid: str, uid: int, gid: int) -> None: makedirs(f'/var/run/ceph/{fsid}', uid, gid, 0o770) + + +def unlink_file( + path: Union[str, Path], + missing_ok: bool = False, + ignore_errors: bool = False, +) -> None: + """Wrapper around unlink that can either ignore missing files or all + errors. + """ + try: + Path(path).unlink() + except FileNotFoundError: + if not missing_ok and not ignore_errors: + raise + except Exception: + if not ignore_errors: + raise -- 2.39.5