finder.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from pathlib import Path
  2. from typing import Callable, Iterable, Iterator, List
  3. from prospector.exceptions import PermissionMissing
  4. from prospector.pathutils import is_python_module, is_python_package, is_virtualenv
  5. _SKIP_DIRECTORIES = (".git", ".tox", ".mypy_cache", ".pytest_cache", ".venv", "__pycache__", "node_modules")
  6. class FileFinder:
  7. """
  8. This class is responsible for taking a combination of command-line arguments
  9. and configuration loaded from a profile to discover all files and modules which
  10. should be inspected.
  11. Individual tools can be told to ignore certain files, so the job of this class
  12. is basically to know which files to pass to which tools to be inspected.
  13. """
  14. def __init__(self, *provided_paths: Path, exclusion_filters: Iterable[Callable] = None):
  15. """
  16. :param provided_paths:
  17. A list of Path objects to search for files and modules - can be either directories or files
  18. :param exclusion_filters:
  19. An optional list of filters. All paths will be checked against this list - if any return True,
  20. the path is excluded.
  21. """
  22. self._provided_files = []
  23. self._provided_dirs = []
  24. self._exclusion_filters = [
  25. # we always want to ignore some things
  26. lambda _path: _path.is_dir() and _path.name in _SKIP_DIRECTORIES,
  27. is_virtualenv,
  28. ] + list(exclusion_filters or [])
  29. for path in provided_paths:
  30. if not path.exists():
  31. raise FileNotFoundError(path)
  32. # ensure all paths from now one are absolute paths; they can be converted
  33. # to relative paths for output purposes later
  34. path = path.absolute()
  35. if path.is_file():
  36. self._provided_files.append(path)
  37. if path.is_dir():
  38. self._provided_dirs.append(path)
  39. def make_syspath(self) -> List[Path]:
  40. paths = set()
  41. for path in self._provided_dirs:
  42. paths.add(path)
  43. for module in self.python_modules:
  44. paths.add(module.parent)
  45. return sorted(paths)
  46. def is_excluded(self, path: Path) -> bool:
  47. return any(filt(path) for filt in self._exclusion_filters)
  48. def _filter(self, paths: Iterable[Path]) -> List[Path]:
  49. return [path for path in paths if not self.is_excluded(path)]
  50. def _walk(self, directory: Path) -> Iterator[Path]:
  51. if not self.is_excluded(directory):
  52. yield directory
  53. for path in directory.iterdir():
  54. if self.is_excluded(path):
  55. continue
  56. if path.is_dir():
  57. yield from self._walk(path)
  58. else:
  59. yield path
  60. @property
  61. def files(self) -> List[Path]:
  62. """
  63. List every individual file found from the given configuration.
  64. This method is useful for tools which require an explicit list of files to check.
  65. """
  66. files = set()
  67. for path in self._provided_files:
  68. files.add(path)
  69. for directory in self.directories:
  70. for path in self._walk(directory):
  71. if path.is_file():
  72. files.add(path)
  73. return self._filter(files)
  74. @property
  75. def python_packages(self) -> List[Path]:
  76. """
  77. Lists every directory found in the given configuration which is a python module (that is,
  78. contains an `__init__.py` file).
  79. This method is useful for passing to tools which will do their own discovery of python files.
  80. """
  81. return self._filter(d for d in self.directories if is_python_package(d))
  82. @property
  83. def python_modules(self) -> List[Path]:
  84. """
  85. Lists every directory found in the given configuration which is a python module (that is,
  86. contains an `__init__.py` file).
  87. This method is useful for passing to tools which will do their own discovery of python files.
  88. """
  89. return self._filter(f for f in self.files if is_python_module(f))
  90. @property
  91. def directories(self) -> List[Path]:
  92. """
  93. Lists every directory found from the given configuration, regardless of its contents.
  94. This method is useful for passing to tools which will do their own discovery of python files.
  95. """
  96. dirs = set()
  97. for directory in self._provided_dirs:
  98. dirs.add(directory)
  99. try:
  100. for obj in self._walk(directory):
  101. if obj.is_dir():
  102. dirs.add(obj)
  103. except PermissionError as err:
  104. raise PermissionMissing(obj) from err
  105. return self._filter(dirs)