info.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. from __future__ import annotations
  2. import logging
  3. import os
  4. import platform
  5. import sys
  6. import tempfile
  7. IMPLEMENTATION = platform.python_implementation()
  8. IS_PYPY = IMPLEMENTATION == "PyPy"
  9. IS_GRAALPY = IMPLEMENTATION == "GraalVM"
  10. IS_CPYTHON = IMPLEMENTATION == "CPython"
  11. IS_WIN = sys.platform == "win32"
  12. IS_MAC_ARM64 = sys.platform == "darwin" and platform.machine() == "arm64"
  13. ROOT = os.path.realpath(os.path.join(os.path.abspath(__file__), os.path.pardir, os.path.pardir))
  14. IS_ZIPAPP = os.path.isfile(ROOT)
  15. _CAN_SYMLINK = _FS_CASE_SENSITIVE = _CFG_DIR = _DATA_DIR = None
  16. LOGGER = logging.getLogger(__name__)
  17. def fs_is_case_sensitive():
  18. global _FS_CASE_SENSITIVE # noqa: PLW0603
  19. if _FS_CASE_SENSITIVE is None:
  20. with tempfile.NamedTemporaryFile(prefix="TmP") as tmp_file:
  21. _FS_CASE_SENSITIVE = not os.path.exists(tmp_file.name.lower())
  22. LOGGER.debug("filesystem is %scase-sensitive", "" if _FS_CASE_SENSITIVE else "not ")
  23. return _FS_CASE_SENSITIVE
  24. def fs_supports_symlink():
  25. global _CAN_SYMLINK # noqa: PLW0603
  26. if _CAN_SYMLINK is None:
  27. can = False
  28. if hasattr(os, "symlink"):
  29. # Creating a symlink can fail for a variety of reasons, indicating that the filesystem does not support it.
  30. # E.g. on Linux with a VFAT partition mounted.
  31. with tempfile.NamedTemporaryFile(prefix="TmP") as tmp_file:
  32. temp_dir = os.path.dirname(tmp_file.name)
  33. dest = os.path.join(temp_dir, f"{tmp_file.name}-{'b'}")
  34. try:
  35. os.symlink(tmp_file.name, dest)
  36. can = True
  37. except (OSError, NotImplementedError):
  38. pass # symlink is not supported
  39. finally:
  40. if os.path.lexists(dest):
  41. os.remove(dest)
  42. LOGGER.debug("symlink on filesystem does%s work", "" if can else " not")
  43. _CAN_SYMLINK = can
  44. return _CAN_SYMLINK
  45. def fs_path_id(path: str) -> str:
  46. return path.casefold() if fs_is_case_sensitive() else path
  47. __all__ = (
  48. "IS_CPYTHON",
  49. "IS_GRAALPY",
  50. "IS_MAC_ARM64",
  51. "IS_PYPY",
  52. "IS_WIN",
  53. "IS_ZIPAPP",
  54. "ROOT",
  55. "fs_is_case_sensitive",
  56. "fs_path_id",
  57. "fs_supports_symlink",
  58. )