You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
6.5 KiB

4 years ago
  1. import os
  2. import stat
  3. try:
  4. from pathlib import Path
  5. except ImportError:
  6. try:
  7. # Python 2 backport
  8. from pathlib2 import Path
  9. except ImportError:
  10. class Path(object):
  11. """Dummy for isinstance checks"""
  12. pass
  13. __all__ = ['assert_path_exists', 'assert_not_path_exists',
  14. 'assert_isfile', 'assert_not_isfile',
  15. 'assert_isdir', 'assert_not_isdir',
  16. 'assert_islink', 'assert_not_islink',
  17. 'assert_ispipe', 'assert_not_ispipe',
  18. 'assert_issocket', 'assert_not_issocket',
  19. ]
  20. def _strpath(p):
  21. if isinstance(p, Path):
  22. return str(p)
  23. return p
  24. def _stat_for_assert(path, follow_symlinks=True, msg=None):
  25. stat = os.stat if follow_symlinks else os.lstat
  26. try:
  27. return stat(path)
  28. except OSError:
  29. if msg is None:
  30. msg = "Path does not exist, or can't be stat-ed: %r" % path
  31. raise AssertionError(msg)
  32. def assert_path_exists(path, msg=None):
  33. """Assert that something exists at the given path.
  34. """
  35. _stat_for_assert(_strpath(path), True, msg)
  36. def assert_not_path_exists(path, msg=None):
  37. """Assert that nothing exists at the given path.
  38. """
  39. path = _strpath(path)
  40. if os.path.exists(path):
  41. if msg is None:
  42. msg = "Path exists: %r" % path
  43. raise AssertionError(msg)
  44. def assert_isfile(path, follow_symlinks=True, msg=None):
  45. """Assert that path exists and is a regular file.
  46. With follow_symlinks=True, the default, this will pass if path is a symlink
  47. to a regular file. With follow_symlinks=False, it will fail in that case.
  48. """
  49. path = _strpath(path)
  50. st = _stat_for_assert(path, follow_symlinks, msg)
  51. if not stat.S_ISREG(st.st_mode):
  52. if msg is None:
  53. msg = "Path exists, but is not a regular file: %r" % path
  54. raise AssertionError(msg)
  55. def assert_not_isfile(path, follow_symlinks=True, msg=None):
  56. """Assert that path exists but is not a regular file.
  57. With follow_symlinks=True, the default, this will fail if path is a symlink
  58. to a regular file. With follow_symlinks=False, it will pass in that case.
  59. """
  60. path = _strpath(path)
  61. st = _stat_for_assert(path, follow_symlinks, msg)
  62. if stat.S_ISREG(st.st_mode):
  63. if msg is None:
  64. msg = "Path is a regular file: %r" % path
  65. raise AssertionError(msg)
  66. def assert_isdir(path, follow_symlinks=True, msg=None):
  67. """Assert that path exists and is a directory.
  68. With follow_symlinks=True, the default, this will pass if path is a symlink
  69. to a directory. With follow_symlinks=False, it will fail in that case.
  70. """
  71. path = _strpath(path)
  72. st = _stat_for_assert(path, follow_symlinks, msg)
  73. if not stat.S_ISDIR(st.st_mode):
  74. if msg is None:
  75. msg = "Path exists, but is not a directory: %r" % path
  76. raise AssertionError(msg)
  77. def assert_not_isdir(path, follow_symlinks=True, msg=None):
  78. """Assert that path exists but is not a directory.
  79. With follow_symlinks=True, the default, this will fail if path is a symlink
  80. to a directory. With follow_symlinks=False, it will pass in that case.
  81. """
  82. path = _strpath(path)
  83. st = _stat_for_assert(path, follow_symlinks, msg)
  84. if stat.S_ISDIR(st.st_mode):
  85. if msg is None:
  86. msg = "Path is a directory: %r" % path
  87. raise AssertionError(msg)
  88. _link_target_msg = """Symlink target of:
  89. {path}
  90. Expected:
  91. {expected}
  92. Actual:
  93. {actual}
  94. """
  95. def assert_islink(path, to=None, msg=None):
  96. """Assert that path exists and is a symlink.
  97. If to is specified, also check that it is the target of the symlink.
  98. """
  99. path = _strpath(path)
  100. st = _stat_for_assert(path, False, msg)
  101. if not stat.S_ISLNK(st.st_mode):
  102. if msg is None:
  103. msg = "Path exists, but is not a symlink: %r" % path
  104. raise AssertionError(msg)
  105. if to is not None:
  106. to = _strpath(to)
  107. target = os.readlink(path)
  108. # TODO: Normalise the target to an absolute path?
  109. if target != to:
  110. if msg is None:
  111. msg = _link_target_msg.format(path=path, expected=to, actual=target)
  112. raise AssertionError(msg)
  113. def assert_not_islink(path, msg=None):
  114. """Assert that path exists but is not a symlink.
  115. """
  116. path = _strpath(path)
  117. st = _stat_for_assert(path, False, msg)
  118. if stat.S_ISLNK(st.st_mode):
  119. if msg is None:
  120. msg = "Path is a symlink: %r" % path
  121. raise AssertionError(msg)
  122. def assert_ispipe(path, follow_symlinks=True, msg=None):
  123. """Assert that path exists and is a named pipe (FIFO).
  124. With follow_symlinks=True, the default, this will pass if path is a symlink
  125. to a named pipe. With follow_symlinks=False, it will fail in that case.
  126. """
  127. path = _strpath(path)
  128. st = _stat_for_assert(path, follow_symlinks, msg)
  129. if not stat.S_ISFIFO(st.st_mode):
  130. if msg is None:
  131. msg = "Path exists, but is not a named pipe: %r" % path
  132. raise AssertionError(msg)
  133. def assert_not_ispipe(path, follow_symlinks=True, msg=None):
  134. """Assert that path exists but is not a named pipe (FIFO).
  135. With follow_symlinks=True, the default, this will fail if path is a symlink
  136. to a named pipe. With follow_symlinks=False, it will pass in that case.
  137. """
  138. path = _strpath(path)
  139. st = _stat_for_assert(path, follow_symlinks, msg)
  140. if stat.S_ISFIFO(st.st_mode):
  141. if msg is None:
  142. msg = "Path is a named pipe: %r" % path
  143. raise AssertionError(msg)
  144. def assert_issocket(path, follow_symlinks=True, msg=None):
  145. """Assert that path exists and is a Unix domain socket.
  146. With follow_symlinks=True, the default, this will pass if path is a symlink
  147. to a Unix domain socket. With follow_symlinks=False, it will fail in that case.
  148. """
  149. path = _strpath(path)
  150. st = _stat_for_assert(path, follow_symlinks, msg)
  151. if not stat.S_ISSOCK(st.st_mode):
  152. if msg is None:
  153. msg = "Path exists, but is not a socket: %r" % path
  154. raise AssertionError(msg)
  155. def assert_not_issocket(path, follow_symlinks=True, msg=None):
  156. """Assert that path exists but is not a Unix domain socket.
  157. With follow_symlinks=True, the default, this will fail if path is a symlink
  158. to a Unix domain socket. With follow_symlinks=False, it will pass in that case.
  159. """
  160. path = _strpath(path)
  161. st = _stat_for_assert(path, follow_symlinks, msg)
  162. if stat.S_ISSOCK(st.st_mode):
  163. if msg is None:
  164. msg = "Path is a socket: %r" % path
  165. raise AssertionError(msg)