A wrapper around `os.walk` that makes it non-deterministic. Makes sure that directories and files are always returned in a different order each time it is called. Typically used like: `unittest.mock.patch("os.walk", new=NonDeterministicWalk())`
| 996 | |
| 997 | |
| 998 | class NonDeterministicWalk: |
| 999 | """A wrapper around `os.walk` that makes it non-deterministic. |
| 1000 | |
| 1001 | Makes sure that directories and files are always returned in a different |
| 1002 | order each time it is called. |
| 1003 | |
| 1004 | Typically used like: `unittest.mock.patch("os.walk", new=NonDeterministicWalk())` |
| 1005 | """ |
| 1006 | |
| 1007 | def __init__(self): |
| 1008 | self._counter = Counter() # type: Counter[str, int] |
| 1009 | self._original_walk = os.walk |
| 1010 | |
| 1011 | def __call__(self, *args, **kwargs): |
| 1012 | # type: (*Any, **Any) -> Iterator[Tuple[str, List[str], List[str]]] |
| 1013 | for root, dirs, files in self._original_walk(*args, **kwargs): |
| 1014 | self._increment_counter(root) |
| 1015 | dirs[:] = self._rotate(root, dirs) |
| 1016 | files[:] = self._rotate(root, files) |
| 1017 | yield root, dirs, files |
| 1018 | |
| 1019 | def _increment_counter(self, counter_key): |
| 1020 | # type: (str) -> int |
| 1021 | self._counter[counter_key] += 1 |
| 1022 | return self._counter[counter_key] |
| 1023 | |
| 1024 | def _rotate(self, counter_key, x): |
| 1025 | # type: (str, List[str]) -> List[str] |
| 1026 | if not x: |
| 1027 | return x |
| 1028 | rotate_by = self._counter[counter_key] % len(x) |
| 1029 | return x[-rotate_by:] + x[:-rotate_by] |
| 1030 | |
| 1031 | |
| 1032 | def installed_pex_wheel_venv_python(python): |
no outgoing calls