files.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import os
  2. from pathlib import Path
  3. from typing import Iterable, Iterator, List, Set
  4. from isort.settings import Config
  5. def find(
  6. paths: Iterable[str], config: Config, skipped: List[str], broken: List[str]
  7. ) -> Iterator[str]:
  8. """Fines and provides an iterator for all Python source files defined in paths."""
  9. visited_dirs: Set[Path] = set()
  10. for path in paths:
  11. if os.path.isdir(path):
  12. for dirpath, dirnames, filenames in os.walk(
  13. path, topdown=True, followlinks=config.follow_links
  14. ):
  15. base_path = Path(dirpath)
  16. for dirname in list(dirnames):
  17. full_path = base_path / dirname
  18. resolved_path = full_path.resolve()
  19. if config.is_skipped(full_path):
  20. skipped.append(dirname)
  21. dirnames.remove(dirname)
  22. else:
  23. if resolved_path in visited_dirs: # pragma: no cover
  24. dirnames.remove(dirname)
  25. visited_dirs.add(resolved_path)
  26. for filename in filenames:
  27. filepath = os.path.join(dirpath, filename)
  28. if config.is_supported_filetype(filepath):
  29. if config.is_skipped(Path(os.path.abspath(filepath))):
  30. skipped.append(filename)
  31. else:
  32. yield filepath
  33. elif not os.path.exists(path):
  34. broken.append(path)
  35. else:
  36. yield path