diff options
author | Peter Kjellerstedt <peter.kjellerstedt@axis.com> | 2020-12-09 18:05:07 +0100 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2021-01-12 14:25:14 +0000 |
commit | 4f7fddd8483c9ff012ac192478f52ed45e8fdb33 (patch) | |
tree | 113475079a0ccb46200e9968d1b063eb354fd1dc /meta/lib | |
parent | 3c451a84374749105f584199a1345f7313c1c0ac (diff) | |
download | poky-4f7fddd8483c9ff012ac192478f52ed45e8fdb33.tar.gz |
lib/oe/path: Add canonicalize()
oe.path.canonicalize() is used to canonicalize paths (i.e., remove
symbolic links and "..", and make them absolute). It takes a string
with paths separated by commas, and returns the canonicalized path in
the same format.
(From OE-Core rev: 5c9931e0ffcb1663f529289f2e03c549fdb3c4da)
Signed-off-by: Peter Kjellerstedt <peter.kjellerstedt@axis.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
(cherry picked from commit 282b19c0e27488ec119f00fb2542ffdc1af54e2a)
Signed-off-by: Anuj Mittal <anuj.mittal@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/lib')
-rw-r--r-- | meta/lib/oe/path.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/meta/lib/oe/path.py b/meta/lib/oe/path.py index 082972457b..c8d8ad05b9 100644 --- a/meta/lib/oe/path.py +++ b/meta/lib/oe/path.py | |||
@@ -320,3 +320,24 @@ def which_wild(pathname, path=None, mode=os.F_OK, *, reverse=False, candidates=F | |||
320 | 320 | ||
321 | return files | 321 | return files |
322 | 322 | ||
323 | def canonicalize(paths, sep=','): | ||
324 | """Given a string with paths (separated by commas by default), expand | ||
325 | each path using os.path.realpath() and return the resulting paths as a | ||
326 | string (separated using the same separator a the original string). | ||
327 | """ | ||
328 | # Ignore paths containing "$" as they are assumed to be unexpanded bitbake | ||
329 | # variables. Normally they would be ignored, e.g., when passing the paths | ||
330 | # through the shell they would expand to empty strings. However, when they | ||
331 | # are passed through os.path.realpath(), it will cause them to be prefixed | ||
332 | # with the absolute path to the current directory and thus not be empty | ||
333 | # anymore. | ||
334 | # | ||
335 | # Also maintain trailing slashes, as the paths may actually be used as | ||
336 | # prefixes in sting compares later on, where the slashes then are important. | ||
337 | canonical_paths = [] | ||
338 | for path in (paths or '').split(sep): | ||
339 | if '$' not in path: | ||
340 | trailing_slash = path.endswith('/') and '/' or '' | ||
341 | canonical_paths.append(os.path.realpath(path) + trailing_slash) | ||
342 | |||
343 | return sep.join(canonical_paths) | ||