| 1 |
""" |
| 2 |
Implementations of ReadablePath and WritablePath for local paths, for use in |
| 3 |
pathlib tests. |
| 4 |
|
| 5 |
LocalPathGround is also defined here. It helps establish the "ground truth" |
| 6 |
about local paths in tests. |
| 7 |
""" |
| 8 |
|
| 9 |
import os |
| 10 |
|
| 11 |
from . import is_pypi |
| 12 |
from .lexical_path import LexicalPath |
| 13 |
|
| 14 |
if is_pypi: |
| 15 |
from shutil import rmtree |
| 16 |
from pathlib_abc import PathInfo, _ReadablePath, _WritablePath |
| 17 |
can_symlink = True |
| 18 |
testfn = "TESTFN" |
| 19 |
else: |
| 20 |
from pathlib.types import PathInfo, _ReadablePath, _WritablePath |
| 21 |
from test.support import os_helper |
| 22 |
can_symlink = os_helper.can_symlink() |
| 23 |
testfn = os_helper.TESTFN |
| 24 |
rmtree = os_helper.rmtree |
| 25 |
|
| 26 |
|
| 27 |
class LocalPathGround: |
| 28 |
can_symlink = can_symlink |
| 29 |
|
| 30 |
def __init__(self, path_cls): |
| 31 |
self.path_cls = path_cls |
| 32 |
|
| 33 |
def setup(self, local_suffix=""): |
| 34 |
root = self.path_cls(testfn + local_suffix) |
| 35 |
os.mkdir(root) |
| 36 |
return root |
| 37 |
|
| 38 |
def teardown(self, root): |
| 39 |
rmtree(root) |
| 40 |
|
| 41 |
def create_file(self, p, data=b''): |
| 42 |
with open(p, 'wb') as f: |
| 43 |
f.write(data) |
| 44 |
|
| 45 |
def create_dir(self, p): |
| 46 |
os.mkdir(p) |
| 47 |
|
| 48 |
def create_symlink(self, p, target): |
| 49 |
os.symlink(target, p) |
| 50 |
|
| 51 |
def create_hierarchy(self, p): |
| 52 |
os.mkdir(os.path.join(p, 'dirA')) |
| 53 |
os.mkdir(os.path.join(p, 'dirB')) |
| 54 |
os.mkdir(os.path.join(p, 'dirC')) |
| 55 |
os.mkdir(os.path.join(p, 'dirC', 'dirD')) |
| 56 |
with open(os.path.join(p, 'fileA'), 'wb') as f: |
| 57 |
f.write(b"this is file A\n") |
| 58 |
with open(os.path.join(p, 'dirB', 'fileB'), 'wb') as f: |
| 59 |
f.write(b"this is file B\n") |
| 60 |
with open(os.path.join(p, 'dirC', 'fileC'), 'wb') as f: |
| 61 |
f.write(b"this is file C\n") |
| 62 |
with open(os.path.join(p, 'dirC', 'novel.txt'), 'wb') as f: |
| 63 |
f.write(b"this is a novel\n") |
| 64 |
with open(os.path.join(p, 'dirC', 'dirD', 'fileD'), 'wb') as f: |
| 65 |
f.write(b"this is file D\n") |
| 66 |
if self.can_symlink: |
| 67 |
# Relative symlinks. |
| 68 |
os.symlink('fileA', os.path.join(p, 'linkA')) |
| 69 |
os.symlink('non-existing', os.path.join(p, 'brokenLink')) |
| 70 |
os.symlink('dirB', |
| 71 |
os.path.join(p, 'linkB'), |
| 72 |
target_is_directory=True) |
| 73 |
os.symlink(os.path.join('..', 'dirB'), |
| 74 |
os.path.join(p, 'dirA', 'linkC'), |
| 75 |
target_is_directory=True) |
| 76 |
# Broken symlink (pointing to itself). |
| 77 |
os.symlink('brokenLinkLoop', os.path.join(p, 'brokenLinkLoop')) |
| 78 |
|
| 79 |
isdir = staticmethod(os.path.isdir) |
| 80 |
isfile = staticmethod(os.path.isfile) |
| 81 |
islink = staticmethod(os.path.islink) |
| 82 |
readlink = staticmethod(os.readlink) |
| 83 |
|
| 84 |
def readtext(self, p): |
| 85 |
with open(p, 'r', encoding='utf-8') as f: |
| 86 |
return f.read() |
| 87 |
|
| 88 |
def readbytes(self, p): |
| 89 |
with open(p, 'rb') as f: |
| 90 |
return f.read() |
| 91 |
|
| 92 |
|
| 93 |
class LocalPathInfo(PathInfo): |
| 94 |
""" |
| 95 |
Simple implementation of PathInfo for a local path |
| 96 |
""" |
| 97 |
__slots__ = ('_path', '_exists', '_is_dir', '_is_file', '_is_symlink') |
| 98 |
|
| 99 |
def __init__(self, path): |
| 100 |
self._path = str(path) |
| 101 |
self._exists = None |
| 102 |
self._is_dir = None |
| 103 |
self._is_file = None |
| 104 |
self._is_symlink = None |
| 105 |
|
| 106 |
def exists(self, *, follow_symlinks=True): |
| 107 |
"""Whether this path exists.""" |
| 108 |
if not follow_symlinks and self.is_symlink(): |
| 109 |
return True |
| 110 |
if self._exists is None: |
| 111 |
self._exists = os.path.exists(self._path) |
| 112 |
return self._exists |
| 113 |
|
| 114 |
def is_dir(self, *, follow_symlinks=True): |
| 115 |
"""Whether this path is a directory.""" |
| 116 |
if not follow_symlinks and self.is_symlink(): |
| 117 |
return False |
| 118 |
if self._is_dir is None: |
| 119 |
self._is_dir = os.path.isdir(self._path) |
| 120 |
return self._is_dir |
| 121 |
|
| 122 |
def is_file(self, *, follow_symlinks=True): |
| 123 |
"""Whether this path is a regular file.""" |
| 124 |
if not follow_symlinks and self.is_symlink(): |
| 125 |
return False |
| 126 |
if self._is_file is None: |
| 127 |
self._is_file = os.path.isfile(self._path) |
| 128 |
return self._is_file |
| 129 |
|
| 130 |
def is_symlink(self): |
| 131 |
"""Whether this path is a symbolic link.""" |
| 132 |
if self._is_symlink is None: |
| 133 |
self._is_symlink = os.path.islink(self._path) |
| 134 |
return self._is_symlink |
| 135 |
|
| 136 |
|
| 137 |
class ReadableLocalPath(_ReadablePath, LexicalPath): |
| 138 |
""" |
| 139 |
Simple implementation of a ReadablePath class for local filesystem paths. |
| 140 |
""" |
| 141 |
__slots__ = ('info',) |
| 142 |
|
| 143 |
def __init__(self, *pathsegments): |
| 144 |
super().__init__(*pathsegments) |
| 145 |
self.info = LocalPathInfo(self) |
| 146 |
|
| 147 |
def __fspath__(self): |
| 148 |
return str(self) |
| 149 |
|
| 150 |
def __open_rb__(self, buffering=-1): |
| 151 |
return open(self, 'rb') |
| 152 |
|
| 153 |
def iterdir(self): |
| 154 |
return (self / name for name in os.listdir(self)) |
| 155 |
|
| 156 |
def readlink(self): |
| 157 |
return self.with_segments(os.readlink(self)) |
| 158 |
|
| 159 |
|
| 160 |
class WritableLocalPath(_WritablePath, LexicalPath): |
| 161 |
""" |
| 162 |
Simple implementation of a WritablePath class for local filesystem paths. |
| 163 |
""" |
| 164 |
|
| 165 |
__slots__ = () |
| 166 |
|
| 167 |
def __fspath__(self): |
| 168 |
return str(self) |
| 169 |
|
| 170 |
def __open_wb__(self, buffering=-1): |
| 171 |
return open(self, 'wb') |
| 172 |
|
| 173 |
def mkdir(self, mode=0o777): |
| 174 |
os.mkdir(self, mode) |
| 175 |
|
| 176 |
def symlink_to(self, target, target_is_directory=False): |
| 177 |
os.symlink(target, self, target_is_directory) |