From ffbe3779fd0dab9b223cab0dddc1e9daf5a976b7 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Wed, 18 Jan 2023 12:49:40 -0500 Subject: [PATCH] RF/BF: make find_parent_directory_containing operate on absolute path The problem was that it would not work whenever path is relative path -- it would simply not go up the hierarchy since parents would not include parent directory above current directory so we would not be able to determine the parent above CWD given relative path from CWD. As a side effect we change return value of find_parent_directory_containing to always be absolute path --- dandi/utils.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/dandi/utils.py b/dandi/utils.py index 23ec7e4c1..be6d3986f 100644 --- a/dandi/utils.py +++ b/dandi/utils.py @@ -408,14 +408,20 @@ def find_parent_directory_containing( ) -> Optional[Path]: """Find a directory, on the path to 'path' containing filename - if no 'path' - path from cwd - Returns None if no such found, pathlib's Path to the directory if found + if no 'path' - path from cwd. If 'path' is not absolute, absolute path + is taken assuming relative to cwd. + + Returns None if no such found, pathlib's Path (absolute) to the directory + if found. """ if not path: path = Path.cwd() else: # assure pathlib object path = Path(path) + if not path.is_absolute(): + path = path.absolute() + while True: if (path / filename).exists(): return path