[oe-commits] [openembedded-core] 07/15: lib/oe/path: implement is_path_parent()

git at git.openembedded.org git at git.openembedded.org
Wed Feb 28 16:32:47 UTC 2018


This is an automated email from the git hooks/post-receive script.

rpurdie pushed a commit to branch master-next
in repository openembedded-core.

commit a6891e0b3d60396764364769d8e3a2e68d3fa4a0
Author: Paul Eggleton <paul.eggleton at linux.intel.com>
AuthorDate: Mon Feb 26 14:49:54 2018 +1300

    lib/oe/path: implement is_path_parent()
    
    In a few places we have checks to see path B is the parent of path A, by
    adding / to the end of the path B and then seeing if path A starts with
    the suffixed path B. Unfortunately there are two potential flaws:
    (1) path A needs to be suffixed with / as well or the directory itself
    won't match (semantics perhaps, but in a lot of scenarios returning True
    is correct); (2) you need to run os.path.abspath() on both paths first
    or you will wrongly return False for some relative paths where you
    should return True. Let's solve this once and for all by writing a
    function that takes care of these and put it in oe.path.
    
    Signed-off-by: Paul Eggleton <paul.eggleton at linux.intel.com>
    Signed-off-by: Richard Purdie <richard.purdie at linuxfoundation.org>
---
 meta/lib/oe/path.py | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/meta/lib/oe/path.py b/meta/lib/oe/path.py
index 1ea03d5..76c58fa 100644
--- a/meta/lib/oe/path.py
+++ b/meta/lib/oe/path.py
@@ -237,3 +237,25 @@ def realpath(file, root, use_physdir = True, loop_cnt = 100, assume_dir = False)
         raise
 
     return file
+
+def is_path_parent(possible_parent, *paths):
+    """
+    Return True if a path is the parent of another, False otherwise.
+    Multiple paths to test can be specified in which case all
+    specified test paths must be under the parent in order to
+    return True.
+    """
+    def abs_path_trailing(pth):
+        pth_abs = os.path.abspath(pth)
+        if not pth_abs.endswith(os.sep):
+            pth_abs += os.sep
+        return pth_abs
+
+    possible_parent_abs = abs_path_trailing(possible_parent)
+    if not paths:
+        return False
+    for path in paths:
+        path_abs = abs_path_trailing(path)
+        if not path_abs.startswith(possible_parent_abs):
+            return False
+    return True

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Openembedded-commits mailing list