summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeter Kjellerstedt <peter.kjellerstedt@axis.com>2020-12-09 18:05:07 +0100
committerRichard Purdie <richard.purdie@linuxfoundation.org>2020-12-10 13:40:07 +0000
commit912b0c15c69efe2dec50cff6a65bde3bc0d8d5a8 (patch)
treeb7a607423f09124bb44ce92b5c917830669d7b42
parent7782d4650a3b463b271af37fc0b5e363697696df (diff)
downloadopenembedded-core-contrib-912b0c15c69efe2dec50cff6a65bde3bc0d8d5a8.tar.gz
openembedded-core-contrib-912b0c15c69efe2dec50cff6a65bde3bc0d8d5a8.tar.bz2
openembedded-core-contrib-912b0c15c69efe2dec50cff6a65bde3bc0d8d5a8.zip
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. Signed-off-by: Peter Kjellerstedt <peter.kjellerstedt@axis.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r--meta/lib/oe/path.py21
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
return files
+def canonicalize(paths, sep=','):
+ """Given a string with paths (separated by commas by default), expand
+ each path using os.path.realpath() and return the resulting paths as a
+ string (separated using the same separator a the original string).
+ """
+ # Ignore paths containing "$" as they are assumed to be unexpanded bitbake
+ # variables. Normally they would be ignored, e.g., when passing the paths
+ # through the shell they would expand to empty strings. However, when they
+ # are passed through os.path.realpath(), it will cause them to be prefixed
+ # with the absolute path to the current directory and thus not be empty
+ # anymore.
+ #
+ # Also maintain trailing slashes, as the paths may actually be used as
+ # prefixes in sting compares later on, where the slashes then are important.
+ canonical_paths = []
+ for path in (paths or '').split(sep):
+ if '$' not in path:
+ trailing_slash = path.endswith('/') and '/' or ''
+ canonical_paths.append(os.path.realpath(path) + trailing_slash)
+
+ return sep.join(canonical_paths)