Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

85 righe
2.6 KiB

  1. from __future__ import annotations
  2. import contextlib
  3. import os
  4. import sys
  5. from typing import TYPE_CHECKING, TypeVar, Union
  6. from more_itertools import unique_everseen
  7. if TYPE_CHECKING:
  8. from typing_extensions import TypeAlias
  9. StrPath: TypeAlias = Union[str, os.PathLike[str]] # Same as _typeshed.StrPath
  10. StrPathT = TypeVar("StrPathT", bound=Union[str, os.PathLike[str]])
  11. def ensure_directory(path):
  12. """Ensure that the parent directory of `path` exists"""
  13. dirname = os.path.dirname(path)
  14. os.makedirs(dirname, exist_ok=True)
  15. def same_path(p1: StrPath, p2: StrPath) -> bool:
  16. """Differs from os.path.samefile because it does not require paths to exist.
  17. Purely string based (no comparison between i-nodes).
  18. >>> same_path("a/b", "./a/b")
  19. True
  20. >>> same_path("a/b", "a/./b")
  21. True
  22. >>> same_path("a/b", "././a/b")
  23. True
  24. >>> same_path("a/b", "./a/b/c/..")
  25. True
  26. >>> same_path("a/b", "../a/b/c")
  27. False
  28. >>> same_path("a", "a/b")
  29. False
  30. """
  31. return normpath(p1) == normpath(p2)
  32. def normpath(filename: StrPath) -> str:
  33. """Normalize a file/dir name for comparison purposes."""
  34. # See pkg_resources.normalize_path for notes about cygwin
  35. file = os.path.abspath(filename) if sys.platform == 'cygwin' else filename
  36. return os.path.normcase(os.path.realpath(os.path.normpath(file)))
  37. @contextlib.contextmanager
  38. def paths_on_pythonpath(paths):
  39. """
  40. Add the indicated paths to the head of the PYTHONPATH environment
  41. variable so that subprocesses will also see the packages at
  42. these paths.
  43. Do this in a context that restores the value on exit.
  44. >>> getfixture('monkeypatch').setenv('PYTHONPATH', 'anything')
  45. >>> with paths_on_pythonpath(['foo', 'bar']):
  46. ... assert 'foo' in os.environ['PYTHONPATH']
  47. ... assert 'anything' in os.environ['PYTHONPATH']
  48. >>> os.environ['PYTHONPATH']
  49. 'anything'
  50. >>> getfixture('monkeypatch').delenv('PYTHONPATH')
  51. >>> with paths_on_pythonpath(['foo', 'bar']):
  52. ... assert 'foo' in os.environ['PYTHONPATH']
  53. >>> os.environ.get('PYTHONPATH')
  54. """
  55. nothing = object()
  56. orig_pythonpath = os.environ.get('PYTHONPATH', nothing)
  57. current_pythonpath = os.environ.get('PYTHONPATH', '')
  58. try:
  59. prefix = os.pathsep.join(unique_everseen(paths))
  60. to_join = filter(None, [prefix, current_pythonpath])
  61. new_path = os.pathsep.join(to_join)
  62. if new_path:
  63. os.environ['PYTHONPATH'] = new_path
  64. yield
  65. finally:
  66. if orig_pythonpath is nothing:
  67. os.environ.pop('PYTHONPATH', None)
  68. else:
  69. os.environ['PYTHONPATH'] = orig_pythonpath