diff options
| author | Peter Kjellerstedt <peter.kjellerstedt@axis.com> | 2020-12-09 18:05:07 +0100 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2020-12-20 00:03:04 +0000 |
| commit | c37aed34afbb61df6e6156857d523b69bfc1c4fe (patch) | |
| tree | c9f28697ec99a6222a8c5cd3703f25eb7311a6a6 | |
| parent | e90cea97d29c3a8d38b0add420cc099e79abd2a6 (diff) | |
| download | poky-c37aed34afbb61df6e6156857d523b69bfc1c4fe.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: 282b19c0e27488ec119f00fb2542ffdc1af54e2a)
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.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) | ||
