]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm: move executable discovery funcs to exe_utils.py
authorJohn Mulligan <jmulligan@redhat.com>
Wed, 16 Aug 2023 18:03:50 +0000 (14:03 -0400)
committerJohn Mulligan <jmulligan@redhat.com>
Wed, 30 Aug 2023 17:57:42 +0000 (13:57 -0400)
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Pair-programmed-with: Adam King <adking@redhat.com>
Co-authored-by: Adam King <adking@redhat.com>
src/cephadm/cephadm.py
src/cephadm/cephadmlib/exe_utils.py [new file with mode: 0644]
src/cephadm/tests/fixtures.py

index 198ecc0e8ddbf3eee26e4ae42804d5ec50ad9e5c..fbb48a80ab7e72dacf20c7c7d4581b2a576ba431 100755 (executable)
@@ -98,6 +98,7 @@ from cephadmlib.exceptions import (
     TimeoutExpired,
     UnauthorizedRegistryError,
 )
+from cephadmlib.exe_utils import find_executable, find_program
 
 FuncT = TypeVar('FuncT', bound=Callable)
 
@@ -2632,51 +2633,6 @@ def recursive_chown(path: str, uid: int, gid: int) -> None:
             os.chown(os.path.join(dirpath, filename), uid, gid)
 
 
-# copied from distutils
-def find_executable(executable: str, path: Optional[str] = None) -> Optional[str]:
-    """Tries to find 'executable' in the directories listed in 'path'.
-    A string listing directories separated by 'os.pathsep'; defaults to
-    os.environ['PATH'].  Returns the complete filename or None if not found.
-    """
-    _, ext = os.path.splitext(executable)
-    if (sys.platform == 'win32') and (ext != '.exe'):
-        executable = executable + '.exe'  # pragma: no cover
-
-    if os.path.isfile(executable):
-        return executable
-
-    if path is None:
-        path = os.environ.get('PATH', None)
-        if path is None:
-            try:
-                path = os.confstr('CS_PATH')
-            except (AttributeError, ValueError):
-                # os.confstr() or CS_PATH is not available
-                path = os.defpath
-        # bpo-35755: Don't use os.defpath if the PATH environment variable is
-        # set to an empty string
-
-    # PATH='' doesn't match, whereas PATH=':' looks in the current directory
-    if not path:
-        return None
-
-    paths = path.split(os.pathsep)
-    for p in paths:
-        f = os.path.join(p, executable)
-        if os.path.isfile(f):
-            # the file exists, we have a shot at spawn working
-            return f
-    return None
-
-
-def find_program(filename):
-    # type: (str) -> str
-    name = find_executable(filename)
-    if name is None:
-        raise ValueError('%s not found' % filename)
-    return name
-
-
 def find_container_engine(ctx: CephadmContext) -> Optional[ContainerEngine]:
     if ctx.docker:
         return Docker()
diff --git a/src/cephadm/cephadmlib/exe_utils.py b/src/cephadm/cephadmlib/exe_utils.py
new file mode 100644 (file)
index 0000000..2f7fe82
--- /dev/null
@@ -0,0 +1,52 @@
+# exe_utils.py - functions used for finding external executables
+
+
+import os
+import sys
+
+from typing import Optional
+
+
+# copied from distutils
+def find_executable(executable: str, path: Optional[str] = None) -> Optional[str]:
+    """Tries to find 'executable' in the directories listed in 'path'.
+    A string listing directories separated by 'os.pathsep'; defaults to
+    os.environ['PATH'].  Returns the complete filename or None if not found.
+    """
+    _, ext = os.path.splitext(executable)
+    if (sys.platform == 'win32') and (ext != '.exe'):
+        executable = executable + '.exe'  # pragma: no cover
+
+    if os.path.isfile(executable):
+        return executable
+
+    if path is None:
+        path = os.environ.get('PATH', None)
+        if path is None:
+            try:
+                path = os.confstr('CS_PATH')
+            except (AttributeError, ValueError):
+                # os.confstr() or CS_PATH is not available
+                path = os.defpath
+        # bpo-35755: Don't use os.defpath if the PATH environment variable is
+        # set to an empty string
+
+    # PATH='' doesn't match, whereas PATH=':' looks in the current directory
+    if not path:
+        return None
+
+    paths = path.split(os.pathsep)
+    for p in paths:
+        f = os.path.join(p, executable)
+        if os.path.isfile(f):
+            # the file exists, we have a shot at spawn working
+            return f
+    return None
+
+
+def find_program(filename):
+    # type: (str) -> str
+    name = find_executable(filename)
+    if name is None:
+        raise ValueError('%s not found' % filename)
+    return name
index db437e20fee3f4b1914d0e1a60debcc3f250d2fd..266b7e0894d566e87e4c5261650d46446d1cde0f 100644 (file)
@@ -148,7 +148,7 @@ def with_cephadm_ctx(
     with mock.patch('cephadm.attempt_bind'), \
          mock.patch('cephadm.call', return_value=('', '', 0)), \
          mock.patch('cephadm.call_timeout', return_value=0), \
-         mock.patch('cephadm.find_executable', return_value='foo'), \
+         mock.patch('cephadmlib.exe_utils.find_executable', return_value='foo'), \
          mock.patch('cephadm.get_container_info', return_value=None), \
          mock.patch('cephadm.is_available', return_value=True), \
          mock.patch('cephadm.json_loads_retry', return_value={'epoch' : 1}), \