python/mozbuild/mozpack/path.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial