[oe-commits] [openembedded-core] 01/19: oe.path: add which_wild function

git at git.openembedded.org git at git.openembedded.org
Mon Jul 2 21:20:53 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 ca276fe139129eec383d77768ba91b808c462b04
Author: Christopher Larson <chris_larson at mentor.com>
AuthorDate: Fri Jun 22 02:08:19 2018 +0500

    oe.path: add which_wild function
    
    This is a function much like shutil.which or bb.utils.which, retaining
    shutil.which-like function semantics, bb.utils.which's support for
    returning available candidates for signatures, and most importantly,
    supports wildcards, returning only the first occurrance of each found
    pathname in the search path.
    
    Signed-off-by: Christopher Larson <chris_larson at mentor.com>
    Signed-off-by: Ross Burton <ross.burton at intel.com>
---
 meta/lib/oe/path.py | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/meta/lib/oe/path.py b/meta/lib/oe/path.py
index 76c58fa..be02218 100644
--- a/meta/lib/oe/path.py
+++ b/meta/lib/oe/path.py
@@ -259,3 +259,37 @@ def is_path_parent(possible_parent, *paths):
         if not path_abs.startswith(possible_parent_abs):
             return False
     return True
+
+def which_wild(pathname, path=None, mode=os.F_OK, *, reverse=False, candidates=False):
+    """Search a search path for pathname, supporting wildcards.
+
+    Return all paths in the specific search path matching the wildcard pattern
+    in pathname, returning only the first encountered for each file. If
+    candidates is True, information on all potential candidate paths are
+    included.
+    """
+    paths = (path or os.environ.get('PATH', os.defpath)).split(':')
+    if reverse:
+        paths.reverse()
+
+    seen, files = set(), []
+    for index, element in enumerate(paths):
+        if not os.path.isabs(element):
+            element = os.path.abspath(element)
+
+        candidate = os.path.join(element, pathname)
+        globbed = glob.glob(candidate)
+        if globbed:
+            for found_path in sorted(globbed):
+                if not os.access(found_path, mode):
+                    continue
+                rel = os.path.relpath(found_path, element)
+                if rel not in seen:
+                    seen.add(rel)
+                    if candidates:
+                        files.append((found_path, [os.path.join(p, rel) for p in paths[:index+1]]))
+                    else:
+                        files.append(found_path)
+
+    return files
+

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


More information about the Openembedded-commits mailing list