Commit 57c177af authored by Iustin Pop's avatar Iustin Pop
Browse files

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
parent c34c0cfd
......@@ -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:
......
......@@ -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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment