Commit 7747219a authored by Jonathan Maw's avatar Jonathan Maw
Browse files

utils.py: Add a helper for searching upwards for files

i.e. with a given directory and filename, check parent directories until
either a directory with the filename is found, or you reach the root of
the filesystem.
parent 2a8a3b19
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -1242,3 +1242,17 @@ def _deduplicate(iterable, key=None):
def _get_link_mtime(path):
    path_stat = os.lstat(path)
    return path_stat.st_mtime


# Returns the first directory to contain filename, or an empty string if
# none found
#
def _search_upward_for_file(directory, filename):
    directory = os.path.abspath(directory)
    while not os.path.isfile(os.path.join(directory, filename)):
        parent_dir = os.path.dirname(directory)
        if directory == parent_dir:
            return ""
        directory = parent_dir

    return directory