pathutils.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import os
  2. from pathlib import Path
  3. def is_python_package(path: Path) -> bool:
  4. return path.is_dir() and (path / "__init__.py").exists()
  5. def is_python_module(path: Path) -> bool:
  6. # TODO: is this too simple?
  7. return path.suffix == ".py"
  8. def is_virtualenv(path: Path) -> bool:
  9. if os.name == "nt":
  10. # Windows!
  11. clues = ("Scripts", "lib", "include")
  12. else:
  13. clues = ("bin", "lib", "include")
  14. try:
  15. # just get the name, iterdir returns absolute paths by default
  16. dircontents = [obj.name for obj in path.iterdir()]
  17. except (OSError, TypeError):
  18. # listdir failed, probably due to path length issues in windows
  19. return False
  20. if not all(clue in dircontents for clue in clues):
  21. # we don't have the 3 directories which would imply
  22. # this is a virtualenvironment
  23. return False
  24. if not all((path / clue).is_dir() for clue in clues):
  25. # some of them are not actually directories
  26. return False
  27. # if we do have all three directories, make sure that it's not
  28. # just a coincidence by doing some heuristics on the rest of
  29. # the directory
  30. if len(dircontents) > 7:
  31. # if there are more than 7 things it's probably not a virtualenvironment
  32. return False
  33. return True