]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm: add unlink_file function
authorJohn Mulligan <jmulligan@redhat.com>
Sat, 18 Nov 2023 18:57:48 +0000 (13:57 -0500)
committerJohn Mulligan <jmulligan@redhat.com>
Tue, 2 Jan 2024 14:30:21 +0000 (09:30 -0500)
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 <jmulligan@redhat.com>
src/cephadm/cephadmlib/file_utils.py

index 1b9f11499a49f4b0673d53c310820a9ebdb06f0b..9c1ca50b925542c15442800b0623c9c9c9384f78 100644 (file)
@@ -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