/usr/lib/python2.7/dist-packages/pathspec/util.py is in python-pathspec 0.3.4-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | # encoding: utf-8
"""
This module provides utility methods for dealing with path-specs.
"""
import collections
import os
import os.path
import posixpath
import stat
from .compat import string_types
NORMALIZE_PATH_SEPS = [sep for sep in [os.sep, os.altsep] if sep and sep != posixpath.sep]
"""
*NORMALIZE_PATH_SEPS* (``list`` of ``str``) contains the path separators
that need to be normalized to the POSIX separator for the current
operating system.
"""
_registered_patterns = {}
"""
*_registered_patterns* (``dict``) maps a name (``str``) to the
registered pattern factory (``callable``).
"""
def iter_tree(root):
"""
Walks the specified directory for all files.
*root* (``str``) is the root directory to search for files.
Raises ``RecursionError`` if recursion is detected.
Returns an ``Iterable`` yielding the path to each file (``str``)
relative to *root*.
"""
for file_rel in _iter_tree_next(os.path.abspath(root), '', {}):
yield file_rel
def _iter_tree_next(root_full, dir_rel, memo):
"""
Scan the directory for all descendant files.
*root_full* (``str``) the absolute path to the root directory.
*dir_rel* (``str``) the path to the directory to scan relative to
*root_full*.
*memo* (``dict``) keeps track of ancestor directories encountered.
Maps each ancestor real path (``str``) to relative path (``str``).
"""
dir_full = os.path.join(root_full, dir_rel)
dir_real = os.path.realpath(dir_full)
# Remember each encountered ancestor directory and its canonical
# (real) path. If a canonical path is encountered more than once,
# recursion has occurred.
if dir_real not in memo:
memo[dir_real] = dir_rel
else:
raise RecursionError(real_path=dir_real, first_path=memo[dir_real], second_path=dir_rel)
for node in os.listdir(dir_full):
node_rel = os.path.join(dir_rel, node)
node_full = os.path.join(root_full, node_rel)
node_stat = os.stat(node_full)
if stat.S_ISDIR(node_stat.st_mode):
# Child node is a directory, recurse into it and yield its
# decendant files.
for file_rel in _iter_tree_next(root_full, node_rel, memo):
yield file_rel
elif stat.S_ISREG(node_stat.st_mode):
# Child node is a file, yield it.
yield node_rel
# NOTE: Make sure to remove the canonical (real) path of the directory
# from the ancestors memo once we are done with it. This allows the
# same directory to appear multiple times. If this is not done, the
# second occurance of the directory will be incorrectly interpreted as
# a recursion. See <https://github.com/cpburnz/python-path-specification/pull/7>.
del memo[dir_real]
def lookup_pattern(name):
"""
Lookups a registered pattern factory by name.
*name* (``str``) is the name of the pattern factory.
Returns the registered pattern factory (``callable``). If no pattern
factory is registered, raises ``KeyError``.
"""
return _registered_patterns[name]
def match_files(patterns, files):
"""
Matches the files to the patterns.
*patterns* (``Iterable`` of ``pathspec.Pattern``) contains the
patterns to use.
*files* (``Iterable`` of ``str``) contains the normalized files to be
matched against *patterns*.
Returns the matched files (``set`` of ``str``).
"""
all_files = files if isinstance(files, collections.Container) else list(files)
return_files = set()
for pattern in patterns:
if pattern.include is not None:
result_files = pattern.match(all_files)
if pattern.include:
return_files.update(result_files)
else:
return_files.difference_update(result_files)
return return_files
def normalize_files(files, separators=None):
"""
Normalizes the file paths to use the POSIX path separator (i.e., `/`).
*files* (``Iterable`` of ``str``) contains the file paths to be
normalized.
*separators* (``Container`` of ``str``) optionally contains the path
separators to normalize.
Returns a ``dict`` mapping the normalized file path (``str``) to the
original file path (``str``)
"""
if separators is None:
separators = NORMALIZE_PATH_SEPS
file_map = {}
for path in files:
norm = path
for sep in separators:
norm = norm.replace(sep, posixpath.sep)
file_map[norm] = path
return file_map
def register_pattern(name, pattern_factory, override=None):
"""
Registers the specified pattern factory.
*name* (``str``) is the name to register the pattern factory under.
*pattern_factory* (``callable``) is used to compile patterns. It must
accept an uncompiled pattern (``str``) and return the compiled pattern
(``pathspec.Pattern``).
*override* (``bool``) optionally is whether to allow overriding an
already registered pattern under the same name (``True``), instead of
raising an ``AlreadyRegisteredError`` (``False``). Default is ``None``
for ``False``.
"""
if not isinstance(name, string_types):
raise TypeError("name:{!r} is not a string.".format(name))
if not callable(pattern_factory):
raise TypeError("pattern_factory:{!r} is not callable.".format(pattern_factory))
if name in _registered_patterns and not override:
raise AlreadyRegisteredError(name, _registered_patterns[name])
_registered_patterns[name] = pattern_factory
class AlreadyRegisteredError(Exception):
"""
The ``AlreadyRegisteredError`` exception is raised when a pattern
factory is registered under a name already in use.
"""
def __init__(self, name, pattern_factory):
"""
Initializes the ``AlreadyRegisteredError`` instance.
*name* (``str``) is the name of the registered pattern.
*pattern_factory* (``callable``) is the registered pattern factory.
"""
super(AlreadyRegisteredError, self).__init__(name, pattern_factory)
@property
def message(self):
"""
*message* (``str``) is the error message.
"""
return "{name!r} is already registered for pattern factory:{!r}.".format(
name=self.name,
pattern_factory=self.pattern_factory,
)
@property
def name(self):
"""
*name* (``str``) is the name of the registered pattern.
"""
return self.args[0]
@property
def pattern_factory(self):
"""
*pattern_factory* (``callable``) is the registered pattern factory.
"""
return self.args[1]
class RecursionError(Exception):
"""
The ``RecursionError`` exception is raised when recursion is detected.
"""
def __init__(self, real_path, first_path, second_path):
"""
Initializes the ``RecursionError`` instance.
*real_path* (``str``) is the real path that recursion was
encountered on.
*first_path* (``str``) is the first path encountered for
*real_path*.
*second_path* (``str``) is the second path encountered for
*real_path*.
"""
super(RecursionError, self).__init__(real_path, first_path, second_path)
@property
def first_path(self):
"""
*first_path* (``str``) is the first path encountered for
*real_path*.
"""
return self.args[1]
@property
def message(self):
"""
*message* (``str``) is the error message.
"""
return "Real path {real!r} was encountered at {first!r} and then {second!r}.".format(
real=self.real_path,
first=self.first_path,
second=self.second_path,
)
@property
def real_path(self):
"""
*real_path* (``str``) is the real path that recursion was
encountered on.
"""
return self.args[0]
@property
def second_path(self):
"""
*second_path* (``str``) is the second path encountered for
*real_path*.
"""
return self.args[2]
|