From: John Mulligan Date: Wed, 16 Aug 2023 18:03:50 +0000 (-0400) Subject: cephadm: move executable discovery funcs to exe_utils.py X-Git-Tag: v19.0.0~561^2~21 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=ac6688729d328d9c853eef85be9bb035b369e1ee;p=ceph.git cephadm: move executable discovery funcs to exe_utils.py Signed-off-by: John Mulligan Pair-programmed-with: Adam King Co-authored-by: Adam King --- diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index 198ecc0e8ddbf..fbb48a80ab7e7 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -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 index 0000000000000..2f7fe82f18678 --- /dev/null +++ b/src/cephadm/cephadmlib/exe_utils.py @@ -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 diff --git a/src/cephadm/tests/fixtures.py b/src/cephadm/tests/fixtures.py index db437e20fee3f..266b7e0894d56 100644 --- a/src/cephadm/tests/fixtures.py +++ b/src/cephadm/tests/fixtures.py @@ -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}), \