Return a tuple of common roots that are shared between the given paths. File system watchers operate on directories and aren't cheap to create. Try to find the minimum set of directories to watch that encompass all of the files that need to be watched.
(paths)
| 175 | |
| 176 | @lru_cache(maxsize=1) |
| 177 | def common_roots(paths): |
| 178 | """ |
| 179 | Return a tuple of common roots that are shared between the given paths. |
| 180 | File system watchers operate on directories and aren't cheap to create. |
| 181 | Try to find the minimum set of directories to watch that encompass all of |
| 182 | the files that need to be watched. |
| 183 | """ |
| 184 | # Inspired from Werkzeug: |
| 185 | # https://github.com/pallets/werkzeug/blob/7477be2853df70a022d9613e765581b9411c3c39/werkzeug/_reloader.py |
| 186 | # Create a sorted list of the path components, longest first. |
| 187 | path_parts = sorted([x.parts for x in paths], key=len, reverse=True) |
| 188 | tree = {} |
| 189 | for chunks in path_parts: |
| 190 | node = tree |
| 191 | # Add each part of the path to the tree. |
| 192 | for chunk in chunks: |
| 193 | node = node.setdefault(chunk, {}) |
| 194 | # Clear the last leaf in the tree. |
| 195 | node.clear() |
| 196 | |
| 197 | # Turn the tree into a list of Path instances. |
| 198 | def _walk(node, path): |
| 199 | for prefix, child in node.items(): |
| 200 | yield from _walk(child, [*path, prefix]) |
| 201 | if not node: |
| 202 | yield Path(*path) |
| 203 | |
| 204 | return tuple(_walk(tree, ())) |
| 205 | |
| 206 | |
| 207 | def sys_path_directories(): |
no test coverage detected
searching dependent graphs…