| 1 |
""" |
| 2 |
Implementations of ReadablePath and WritablePath for zip file members, for use |
| 3 |
in pathlib tests. |
| 4 |
|
| 5 |
ZipPathGround is also defined here. It helps establish the "ground truth" |
| 6 |
about zip file members in tests. |
| 7 |
""" |
| 8 |
|
| 9 |
import errno |
| 10 |
import io |
| 11 |
import posixpath |
| 12 |
import stat |
| 13 |
import zipfile |
| 14 |
from stat import S_IFMT, S_ISDIR, S_ISREG, S_ISLNK |
| 15 |
|
| 16 |
from . import is_pypi |
| 17 |
|
| 18 |
if is_pypi: |
| 19 |
from pathlib_abc import PathInfo, _ReadablePath, _WritablePath |
| 20 |
else: |
| 21 |
from pathlib.types import PathInfo, _ReadablePath, _WritablePath |
| 22 |
|
| 23 |
|
| 24 |
class ZipPathGround: |
| 25 |
can_symlink = True |
| 26 |
|
| 27 |
def __init__(self, path_cls): |
| 28 |
self.path_cls = path_cls |
| 29 |
|
| 30 |
def setup(self, local_suffix=""): |
| 31 |
return self.path_cls(zip_file=zipfile.ZipFile(io.BytesIO(), "w")) |
| 32 |
|
| 33 |
def teardown(self, root): |
| 34 |
root.zip_file.close() |
| 35 |
|
| 36 |
def create_file(self, path, data=b''): |
| 37 |
path.zip_file.writestr(str(path), data) |
| 38 |
|
| 39 |
def create_dir(self, path): |
| 40 |
zip_info = zipfile.ZipInfo(str(path) + '/') |
| 41 |
zip_info.external_attr |= stat.S_IFDIR << 16 |
| 42 |
zip_info.external_attr |= stat.FILE_ATTRIBUTE_DIRECTORY |
| 43 |
path.zip_file.writestr(zip_info, '') |
| 44 |
|
| 45 |
def create_symlink(self, path, target): |
| 46 |
zip_info = zipfile.ZipInfo(str(path)) |
| 47 |
zip_info.external_attr = stat.S_IFLNK << 16 |
| 48 |
path.zip_file.writestr(zip_info, target.encode()) |
| 49 |
|
| 50 |
def create_hierarchy(self, p): |
| 51 |
# Add regular files |
| 52 |
self.create_file(p.joinpath('fileA'), b'this is file A\n') |
| 53 |
self.create_file(p.joinpath('dirB/fileB'), b'this is file B\n') |
| 54 |
self.create_file(p.joinpath('dirC/fileC'), b'this is file C\n') |
| 55 |
self.create_file(p.joinpath('dirC/dirD/fileD'), b'this is file D\n') |
| 56 |
self.create_file(p.joinpath('dirC/novel.txt'), b'this is a novel\n') |
| 57 |
# Add symlinks |
| 58 |
self.create_symlink(p.joinpath('linkA'), 'fileA') |
| 59 |
self.create_symlink(p.joinpath('linkB'), 'dirB') |
| 60 |
self.create_symlink(p.joinpath('dirA/linkC'), '../dirB') |
| 61 |
self.create_symlink(p.joinpath('brokenLink'), 'non-existing') |
| 62 |
self.create_symlink(p.joinpath('brokenLinkLoop'), 'brokenLinkLoop') |
| 63 |
|
| 64 |
def readtext(self, p): |
| 65 |
with p.zip_file.open(str(p), 'r') as f: |
| 66 |
f = io.TextIOWrapper(f, encoding='utf-8') |
| 67 |
return f.read() |
| 68 |
|
| 69 |
def readbytes(self, p): |
| 70 |
with p.zip_file.open(str(p), 'r') as f: |
| 71 |
return f.read() |
| 72 |
|
| 73 |
readlink = readtext |
| 74 |
|
| 75 |
def isdir(self, p): |
| 76 |
path_str = str(p) + "/" |
| 77 |
return path_str in p.zip_file.NameToInfo |
| 78 |
|
| 79 |
def isfile(self, p): |
| 80 |
info = p.zip_file.NameToInfo.get(str(p)) |
| 81 |
if info is None: |
| 82 |
return False |
| 83 |
return not stat.S_ISLNK(info.external_attr >> 16) |
| 84 |
|
| 85 |
def islink(self, p): |
| 86 |
info = p.zip_file.NameToInfo.get(str(p)) |
| 87 |
if info is None: |
| 88 |
return False |
| 89 |
return stat.S_ISLNK(info.external_attr >> 16) |
| 90 |
|
| 91 |
|
| 92 |
class MissingZipPathInfo(PathInfo): |
| 93 |
""" |
| 94 |
PathInfo implementation that is used when a zip file member is missing. |
| 95 |
""" |
| 96 |
__slots__ = () |
| 97 |
|
| 98 |
def exists(self, follow_symlinks=True): |
| 99 |
return False |
| 100 |
|
| 101 |
def is_dir(self, follow_symlinks=True): |
| 102 |
return False |
| 103 |
|
| 104 |
def is_file(self, follow_symlinks=True): |
| 105 |
return False |
| 106 |
|
| 107 |
def is_symlink(self): |
| 108 |
return False |
| 109 |
|
| 110 |
def resolve(self): |
| 111 |
return self |
| 112 |
|
| 113 |
|
| 114 |
missing_zip_path_info = MissingZipPathInfo() |
| 115 |
|
| 116 |
|
| 117 |
class ZipPathInfo(PathInfo): |
| 118 |
""" |
| 119 |
PathInfo implementation for an existing zip file member. |
| 120 |
""" |
| 121 |
__slots__ = ('zip_file', 'zip_info', 'parent', 'children') |
| 122 |
|
| 123 |
def __init__(self, zip_file, parent=None): |
| 124 |
self.zip_file = zip_file |
| 125 |
self.zip_info = None |
| 126 |
self.parent = parent or self |
| 127 |
self.children = {} |
| 128 |
|
| 129 |
def exists(self, follow_symlinks=True): |
| 130 |
if follow_symlinks and self.is_symlink(): |
| 131 |
return self.resolve().exists() |
| 132 |
return True |
| 133 |
|
| 134 |
def is_dir(self, follow_symlinks=True): |
| 135 |
if follow_symlinks and self.is_symlink(): |
| 136 |
return self.resolve().is_dir() |
| 137 |
elif self.zip_info is None: |
| 138 |
return True |
| 139 |
elif fmt := S_IFMT(self.zip_info.external_attr >> 16): |
| 140 |
return S_ISDIR(fmt) |
| 141 |
else: |
| 142 |
return self.zip_info.filename.endswith('/') |
| 143 |
|
| 144 |
def is_file(self, follow_symlinks=True): |
| 145 |
if follow_symlinks and self.is_symlink(): |
| 146 |
return self.resolve().is_file() |
| 147 |
elif self.zip_info is None: |
| 148 |
return False |
| 149 |
elif fmt := S_IFMT(self.zip_info.external_attr >> 16): |
| 150 |
return S_ISREG(fmt) |
| 151 |
else: |
| 152 |
return not self.zip_info.filename.endswith('/') |
| 153 |
|
| 154 |
def is_symlink(self): |
| 155 |
if self.zip_info is None: |
| 156 |
return False |
| 157 |
elif fmt := S_IFMT(self.zip_info.external_attr >> 16): |
| 158 |
return S_ISLNK(fmt) |
| 159 |
else: |
| 160 |
return False |
| 161 |
|
| 162 |
def resolve(self, path=None, create=False, follow_symlinks=True): |
| 163 |
""" |
| 164 |
Traverse zip hierarchy (parents, children and symlinks) starting |
| 165 |
from this PathInfo. This is called from three places: |
| 166 |
|
| 167 |
- When a zip file member is added to ZipFile.filelist, this method |
| 168 |
populates the ZipPathInfo tree (using create=True). |
| 169 |
- When ReadableZipPath.info is accessed, this method is finds a |
| 170 |
ZipPathInfo entry for the path without resolving any final symlink |
| 171 |
(using follow_symlinks=False) |
| 172 |
- When ZipPathInfo methods are called with follow_symlinks=True, this |
| 173 |
method resolves any symlink in the final path position. |
| 174 |
""" |
| 175 |
link_count = 0 |
| 176 |
stack = path.split('/')[::-1] if path else [] |
| 177 |
info = self |
| 178 |
while True: |
| 179 |
if info.is_symlink() and (follow_symlinks or stack): |
| 180 |
link_count += 1 |
| 181 |
if link_count >= 40: |
| 182 |
return missing_zip_path_info # Symlink loop! |
| 183 |
path = info.zip_file.read(info.zip_info).decode() |
| 184 |
stack += path.split('/')[::-1] if path else [] |
| 185 |
info = info.parent |
| 186 |
|
| 187 |
if stack: |
| 188 |
name = stack.pop() |
| 189 |
else: |
| 190 |
return info |
| 191 |
|
| 192 |
if name == '..': |
| 193 |
info = info.parent |
| 194 |
elif name and name != '.': |
| 195 |
if name not in info.children: |
| 196 |
if create: |
| 197 |
info.children[name] = ZipPathInfo(info.zip_file, info) |
| 198 |
else: |
| 199 |
return missing_zip_path_info # No such child! |
| 200 |
info = info.children[name] |
| 201 |
|
| 202 |
|
| 203 |
class ZipFileList: |
| 204 |
""" |
| 205 |
`list`-like object that we inject as `ZipFile.filelist`. We maintain a |
| 206 |
tree of `ZipPathInfo` objects representing the zip file members. |
| 207 |
""" |
| 208 |
|
| 209 |
__slots__ = ('tree', '_items') |
| 210 |
|
| 211 |
def __init__(self, zip_file): |
| 212 |
self.tree = ZipPathInfo(zip_file) |
| 213 |
self._items = [] |
| 214 |
for item in zip_file.filelist: |
| 215 |
self.append(item) |
| 216 |
|
| 217 |
def __len__(self): |
| 218 |
return len(self._items) |
| 219 |
|
| 220 |
def __iter__(self): |
| 221 |
return iter(self._items) |
| 222 |
|
| 223 |
def append(self, item): |
| 224 |
self._items.append(item) |
| 225 |
self.tree.resolve(item.filename, create=True).zip_info = item |
| 226 |
|
| 227 |
|
| 228 |
class ReadableZipPath(_ReadablePath): |
| 229 |
""" |
| 230 |
Simple implementation of a ReadablePath class for .zip files. |
| 231 |
""" |
| 232 |
|
| 233 |
__slots__ = ('_segments', 'zip_file') |
| 234 |
parser = posixpath |
| 235 |
|
| 236 |
def __init__(self, *pathsegments, zip_file): |
| 237 |
self._segments = pathsegments |
| 238 |
self.zip_file = zip_file |
| 239 |
if not isinstance(zip_file.filelist, ZipFileList): |
| 240 |
zip_file.filelist = ZipFileList(zip_file) |
| 241 |
|
| 242 |
def __hash__(self): |
| 243 |
return hash((str(self), self.zip_file)) |
| 244 |
|
| 245 |
def __eq__(self, other): |
| 246 |
if not isinstance(other, ReadableZipPath): |
| 247 |
return NotImplemented |
| 248 |
return str(self) == str(other) and self.zip_file is other.zip_file |
| 249 |
|
| 250 |
def __str__(self): |
| 251 |
if not self._segments: |
| 252 |
return '' |
| 253 |
return self.parser.join(*self._segments) |
| 254 |
|
| 255 |
def __repr__(self): |
| 256 |
return f'{type(self).__name__}({str(self)!r}, zip_file={self.zip_file!r})' |
| 257 |
|
| 258 |
def with_segments(self, *pathsegments): |
| 259 |
return type(self)(*pathsegments, zip_file=self.zip_file) |
| 260 |
|
| 261 |
@property |
| 262 |
def info(self): |
| 263 |
tree = self.zip_file.filelist.tree |
| 264 |
return tree.resolve(str(self), follow_symlinks=False) |
| 265 |
|
| 266 |
def __open_rb__(self, buffering=-1): |
| 267 |
info = self.info.resolve() |
| 268 |
if not info.exists(): |
| 269 |
raise FileNotFoundError(errno.ENOENT, "File not found", self) |
| 270 |
elif info.is_dir(): |
| 271 |
raise IsADirectoryError(errno.EISDIR, "Is a directory", self) |
| 272 |
return self.zip_file.open(info.zip_info, 'r') |
| 273 |
|
| 274 |
def iterdir(self): |
| 275 |
info = self.info.resolve() |
| 276 |
if not info.exists(): |
| 277 |
raise FileNotFoundError(errno.ENOENT, "File not found", self) |
| 278 |
elif not info.is_dir(): |
| 279 |
raise NotADirectoryError(errno.ENOTDIR, "Not a directory", self) |
| 280 |
return (self / name for name in info.children) |
| 281 |
|
| 282 |
def readlink(self): |
| 283 |
info = self.info |
| 284 |
if not info.exists(): |
| 285 |
raise FileNotFoundError(errno.ENOENT, "File not found", self) |
| 286 |
elif not info.is_symlink(): |
| 287 |
raise OSError(errno.EINVAL, "Not a symlink", self) |
| 288 |
return self.with_segments(self.zip_file.read(info.zip_info).decode()) |
| 289 |
|
| 290 |
|
| 291 |
class WritableZipPath(_WritablePath): |
| 292 |
""" |
| 293 |
Simple implementation of a WritablePath class for .zip files. |
| 294 |
""" |
| 295 |
|
| 296 |
__slots__ = ('_segments', 'zip_file') |
| 297 |
parser = posixpath |
| 298 |
|
| 299 |
def __init__(self, *pathsegments, zip_file): |
| 300 |
self._segments = pathsegments |
| 301 |
self.zip_file = zip_file |
| 302 |
|
| 303 |
def __hash__(self): |
| 304 |
return hash((str(self), self.zip_file)) |
| 305 |
|
| 306 |
def __eq__(self, other): |
| 307 |
if not isinstance(other, WritableZipPath): |
| 308 |
return NotImplemented |
| 309 |
return str(self) == str(other) and self.zip_file is other.zip_file |
| 310 |
|
| 311 |
def __str__(self): |
| 312 |
if not self._segments: |
| 313 |
return '' |
| 314 |
return self.parser.join(*self._segments) |
| 315 |
|
| 316 |
def __repr__(self): |
| 317 |
return f'{type(self).__name__}({str(self)!r}, zip_file={self.zip_file!r})' |
| 318 |
|
| 319 |
def with_segments(self, *pathsegments): |
| 320 |
return type(self)(*pathsegments, zip_file=self.zip_file) |
| 321 |
|
| 322 |
def __open_wb__(self, buffering=-1): |
| 323 |
return self.zip_file.open(str(self), 'w') |
| 324 |
|
| 325 |
def mkdir(self, mode=0o777): |
| 326 |
zinfo = zipfile.ZipInfo(str(self) + '/') |
| 327 |
zinfo.external_attr |= stat.S_IFDIR << 16 |
| 328 |
zinfo.external_attr |= stat.FILE_ATTRIBUTE_DIRECTORY |
| 329 |
self.zip_file.writestr(zinfo, '') |
| 330 |
|
| 331 |
def symlink_to(self, target, target_is_directory=False): |
| 332 |
zinfo = zipfile.ZipInfo(str(self)) |
| 333 |
zinfo.external_attr = stat.S_IFLNK << 16 |
| 334 |
if target_is_directory: |
| 335 |
zinfo.external_attr |= 0x10 |
| 336 |
self.zip_file.writestr(zinfo, str(target)) |