diff options
author | Christopher Larson <chris_larson@mentor.com> | 2018-06-22 02:08:19 +0500 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2018-07-04 00:02:16 +0100 |
commit | b0527c08d48f33bfcfc4930fefc44f57c6c2cdff (patch) | |
tree | cf7f8202f0d93e04548c30dc9560a9963b6ddcf0 /meta/lib | |
parent | 7cb125d7ee4b80fb33d87bdd0b257bc6daaff051 (diff) | |
download | poky-b0527c08d48f33bfcfc4930fefc44f57c6c2cdff.tar.gz |
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.
(From OE-Core rev: ca276fe139129eec383d77768ba91b808c462b04)
Signed-off-by: Christopher Larson <chris_larson@mentor.com>
Signed-off-by: Ross Burton <ross.burton@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/lib')
-rw-r--r-- | meta/lib/oe/path.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/meta/lib/oe/path.py b/meta/lib/oe/path.py index 76c58fa760..be02218c31 100644 --- a/meta/lib/oe/path.py +++ b/meta/lib/oe/path.py | |||
@@ -259,3 +259,37 @@ def is_path_parent(possible_parent, *paths): | |||
259 | if not path_abs.startswith(possible_parent_abs): | 259 | if not path_abs.startswith(possible_parent_abs): |
260 | return False | 260 | return False |
261 | return True | 261 | return True |
262 | |||
263 | def which_wild(pathname, path=None, mode=os.F_OK, *, reverse=False, candidates=False): | ||
264 | """Search a search path for pathname, supporting wildcards. | ||
265 | |||
266 | Return all paths in the specific search path matching the wildcard pattern | ||
267 | in pathname, returning only the first encountered for each file. If | ||
268 | candidates is True, information on all potential candidate paths are | ||
269 | included. | ||
270 | """ | ||
271 | paths = (path or os.environ.get('PATH', os.defpath)).split(':') | ||
272 | if reverse: | ||
273 | paths.reverse() | ||
274 | |||
275 | seen, files = set(), [] | ||
276 | for index, element in enumerate(paths): | ||
277 | if not os.path.isabs(element): | ||
278 | element = os.path.abspath(element) | ||
279 | |||
280 | candidate = os.path.join(element, pathname) | ||
281 | globbed = glob.glob(candidate) | ||
282 | if globbed: | ||
283 | for found_path in sorted(globbed): | ||
284 | if not os.access(found_path, mode): | ||
285 | continue | ||
286 | rel = os.path.relpath(found_path, element) | ||
287 | if rel not in seen: | ||
288 | seen.add(rel) | ||
289 | if candidates: | ||
290 | files.append((found_path, [os.path.join(p, rel) for p in paths[:index+1]])) | ||
291 | else: | ||
292 | files.append(found_path) | ||
293 | |||
294 | return files | ||
295 | |||