From 57c177af0052760a14f36db9fb1881655bbb1873 Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Thu, 10 Apr 2008 13:41:01 +0000 Subject: [PATCH] Move the OS search code into an abstract function Based on the previous OS search code changes, we can now move the OS search code into a generic look-for-file function in utils.py. This means that the allocator code can use the same function. Reviewed-by: ultrotter --- lib/backend.py | 24 +----------------------- lib/utils.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/lib/backend.py b/lib/backend.py index 533b88396..a6b676d74 100644 --- a/lib/backend.py +++ b/lib/backend.py @@ -962,28 +962,6 @@ def _ErrnoOrStr(err): return detail -def _OSSearch(name, search_path=None): - """Search for OSes with the given name in the search_path. - - Args: - name: The name of the OS to look for - search_path: List of dirs to search (defaults to constants.OS_SEARCH_PATH) - - Returns: - The base_dir the OS resides in - - """ - if search_path is None: - search_path = constants.OS_SEARCH_PATH - - for dir_name in search_path: - t_os_dir = os.path.sep.join([dir_name, name]) - if os.path.isdir(t_os_dir): - return t_os_dir - - return None - - def _OSOndiskVersion(name, os_dir): """Compute and return the API version of a given OS. @@ -1073,7 +1051,7 @@ def OSFromDisk(name, base_dir=None): """ if base_dir is None: - os_dir = _OSSearch(name) + os_dir = utils.FindFile(name, constants.OS_SEARCH_PATH, os.path.isdir) if os_dir is None: raise errors.InvalidOS(name, None, "OS dir not found in search path") else: diff --git a/lib/utils.py b/lib/utils.py index a1f15d50b..74845402d 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -1134,3 +1134,27 @@ def Daemonize(logfile, noclose_fds=None): os.dup2(0, 1) # standard output (1) os.dup2(0, 2) # standard error (2) return 0 + + +def FindFile(name, search_path, test=os.path.exists): + """Look for a filesystem object in a given path. + + This is an abstract method to search for filesystem object (files, + dirs) under a given search path. + + Args: + - name: the name to look for + - search_path: list of directory names + - test: the test which the full path must satisfy + (defaults to os.path.exists) + + Returns: + - full path to the item if found + - None otherwise + + """ + for dir_name in search_path: + item_name = os.path.sep.join([dir_name, name]) + if test(item_name): + return item_name + return None -- GitLab