| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
""" |
| 6 |
Like :py:mod:`os.path`, with a reduced set of functions, and with normalized path |
| 7 |
separators (always use forward slashes). |
| 8 |
Also contains a few additional utilities not found in :py:mod:`os.path`. |
| 9 |
""" |
| 10 |
|
| 11 |
# Imported from |
| 12 |
# https://searchfox.org/mozilla-central/rev/c3ebaf6de2d481c262c04bb9657eaf76bf47e2ac/python/mozbuild/mozpack/path.py |
| 13 |
|
| 14 |
import os |
| 15 |
import posixpath |
| 16 |
import re |
| 17 |
|
| 18 |
|
| 19 |
def normsep(path): |
| 20 |
""" |
| 21 |
Normalize path separators, by using forward slashes instead of whatever |
| 22 |
:py:const:`os.sep` is. |
| 23 |
""" |
| 24 |
if os.sep != "/": |
| 25 |
path = path.replace(os.sep, "/") |
| 26 |
if os.altsep and os.altsep != "/": |
| 27 |
path = path.replace(os.altsep, "/") |
| 28 |
return path |
| 29 |
|
| 30 |
|
| 31 |
def relpath(path, start): |
| 32 |
rel = normsep(os.path.relpath(path, start)) |
| 33 |
return "" if rel == "." else rel |
| 34 |
|
| 35 |
|
| 36 |
def realpath(path): |
| 37 |
return normsep(os.path.realpath(path)) |
| 38 |
|
| 39 |
|
| 40 |
def abspath(path): |
| 41 |
return normsep(os.path.abspath(path)) |
| 42 |
|
| 43 |
|
| 44 |
def join(*paths): |
| 45 |
return normsep(os.path.join(*paths)) |
| 46 |
|
| 47 |
|
| 48 |
def normpath(path): |
| 49 |
return posixpath.normpath(normsep(path)) |
| 50 |
|
| 51 |
|
| 52 |
def dirname(path): |
| 53 |
return posixpath.dirname(normsep(path)) |
| 54 |
|
| 55 |
|
| 56 |
def commonprefix(paths): |
| 57 |
return posixpath.commonprefix([normsep(path) for path in paths]) |
| 58 |
|
| 59 |
|
| 60 |
def basename(path): |
| 61 |
return os.path.basename(path) |
| 62 |
|
| 63 |
|
| 64 |
def splitext(path): |
| 65 |
return posixpath.splitext(normsep(path)) |
| 66 |
|
| 67 |
|
| 68 |
def split(path): |
| 69 |
""" |
| 70 |
Return the normalized path as a list of its components. |
| 71 |
|
| 72 |
``split('foo/bar/baz')`` returns ``['foo', 'bar', 'baz']`` |
| 73 |
""" |
| 74 |
return normsep(path).split("/") |
| 75 |
|
| 76 |
|
| 77 |
def basedir(path, bases): |
| 78 |
""" |
| 79 |
Given a list of directories (`bases`), return which one contains the given |
| 80 |
path. If several matches are found, the deepest base directory is returned. |
| 81 |
|
| 82 |
``basedir('foo/bar/baz', ['foo', 'baz', 'foo/bar'])`` returns ``'foo/bar'`` |
| 83 |
(`'foo'` and `'foo/bar'` both match, but `'foo/bar'` is the deepest match) |
| 84 |
""" |
| 85 |
path = normsep(path) |
| 86 |
bases = [normsep(b) for b in bases] |
| 87 |
if path in bases: |
| 88 |
return path |
| 89 |
for b in sorted(bases, reverse=True): |
| 90 |
if not b or path.startswith(b + "/"): |
| 91 |
return b |
| 92 |
|
| 93 |
|
| 94 |
re_cache = {} |
| 95 |
MATCH_STAR_STAR_RE = re.compile(r"(^|/)\\\*\\\*/") |
| 96 |
MATCH_STAR_STAR_END_RE = re.compile(r"(^|/)\\\*\\\*$") |
| 97 |
|
| 98 |
|
| 99 |
def match(path, pattern): |
| 100 |
""" |
| 101 |
Return whether the given path matches the given pattern. |
| 102 |
An asterisk can be used to match any string, including the null string, in |
| 103 |
one part of the path: |
| 104 |
|
| 105 |
``foo`` matches ``*``, ``f*`` or ``fo*o`` |
| 106 |
|
| 107 |
However, an asterisk matching a subdirectory may not match the null string: |
| 108 |
|
| 109 |
``foo/bar`` does *not* match ``foo/*/bar`` |
| 110 |
|
| 111 |
If the pattern matches one of the ancestor directories of the path, the |
| 112 |
patch is considered matching: |
| 113 |
|
| 114 |
``foo/bar`` matches ``foo`` |
| 115 |
|
| 116 |
Two adjacent asterisks can be used to match files and zero or more |
| 117 |
directories and subdirectories. |
| 118 |
|
| 119 |
``foo/bar`` matches ``foo/**/bar``, or ``**/bar`` |
| 120 |
""" |
| 121 |
if not pattern: |
| 122 |
return True |
| 123 |
if pattern not in re_cache: |
| 124 |
p = re.escape(pattern) |
| 125 |
p = MATCH_STAR_STAR_RE.sub(r"\1(?:.+/)?", p) |
| 126 |
p = MATCH_STAR_STAR_END_RE.sub(r"(?:\1.+)?", p) |
| 127 |
p = p.replace(r"\*", "[^/]*") + "(?:/.*)?$" |
| 128 |
re_cache[pattern] = re.compile(p) |
| 129 |
return re_cache[pattern].match(path) is not None |
| 130 |
|
| 131 |
|
| 132 |
def rebase(oldbase, base, relativepath): |
| 133 |
""" |
| 134 |
Return `relativepath` relative to `base` instead of `oldbase`. |
| 135 |
""" |
| 136 |
if base == oldbase: |
| 137 |
return relativepath |
| 138 |
if len(base) < len(oldbase): |
| 139 |
assert basedir(oldbase, [base]) == base |
| 140 |
relbase = relpath(oldbase, base) |
| 141 |
result = join(relbase, relativepath) |
| 142 |
else: |
| 143 |
assert basedir(base, [oldbase]) == oldbase |
| 144 |
relbase = relpath(base, oldbase) |
| 145 |
result = relpath(relativepath, relbase) |
| 146 |
result = normpath(result) |
| 147 |
if relativepath.endswith("/") and not result.endswith("/"): |
| 148 |
result += "/" |
| 149 |
return result |
| 150 |
|
| 151 |
|
| 152 |
def ancestors(path): |
| 153 |
"""Emit the parent directories of a path. |
| 154 |
|
| 155 |
Args: |
| 156 |
path (str): Path to emit parents of. |
| 157 |
|
| 158 |
Yields: |
| 159 |
str: Path of parent directory. |
| 160 |
""" |
| 161 |
while path: |
| 162 |
yield path |
| 163 |
newpath = os.path.dirname(path) |
| 164 |
if newpath == path: |
| 165 |
break |
| 166 |
path = newpath |