Mercurial > hgrepos > Python > apps > py-cutils
comparison cutils/util/walk.py @ 273:c02a57df2a29
treesum: - WalkDirEntry tests with a FIFO special file - WalkDirEntry.is_reg
Also all to be handled special files in the notes document.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Wed, 19 Feb 2025 09:12:30 +0100 |
| parents | 0add8276e6b8 |
| children | 224725fd9f2f |
comparison
equal
deleted
inserted
replaced
| 272:b4137ebd8e79 | 273:c02a57df2a29 |
|---|---|
| 19 except ImportError: | 19 except ImportError: |
| 20 try: | 20 try: |
| 21 from scandir import scandir | 21 from scandir import scandir |
| 22 except ImportError: | 22 except ImportError: |
| 23 scandir = None | 23 scandir = None |
| 24 import stat | |
| 24 import sys | 25 import sys |
| 25 | 26 |
| 26 from . import PY2 | 27 from . import PY2 |
| 27 | 28 |
| 28 | 29 |
| 51 its results. | 52 its results. |
| 52 | 53 |
| 53 """ | 54 """ |
| 54 | 55 |
| 55 __slots__ = ("_name", "_path", # encoded as given in the ctor | 56 __slots__ = ("_name", "_path", # encoded as given in the ctor |
| 56 "_is_symlink", "_is_dir", "_stat_result", "_stat_errno", | 57 "_is_symlink", "_is_reg", "_is_dir", "_stat_result", |
| 57 "_stat_errstr", | 58 "_stat_errno", "_stat_errstr", |
| 58 "_alt_fsname", "_alt_u8name") | 59 "_alt_fsname", "_alt_u8name") |
| 59 | 60 |
| 60 def __init__(self, name, path): | 61 def __init__(self, name, path): |
| 61 self._name = name # the name as given in the constructor | 62 self._name = name # the name as given in the constructor |
| 62 """The name exactly as given in the ctor""" | 63 """The name exactly as given in the ctor""" |
| 63 self._path = _unix_path(path) | 64 self._path = _unix_path(path) |
| 64 """The path as given in the ctor -- but normalized to have slashes""" | 65 """The path as given in the ctor -- but normalized to have slashes""" |
| 65 self._is_symlink = self._is_dir = self._stat_result = \ | 66 self._is_symlink = self._is_reg = self._is_dir = self._stat_result = \ |
| 66 self._stat_errno = self._stat_errstr = None | 67 self._stat_errno = self._stat_errstr = None |
| 67 self._alt_fsname = self._alt_u8name = _notset | 68 self._alt_fsname = self._alt_u8name = _notset |
| 68 | 69 |
| 69 @property | 70 @property |
| 70 def name(self): | 71 def name(self): |
| 225 @property | 226 @property |
| 226 def is_symlink(self): | 227 def is_symlink(self): |
| 227 return self._is_symlink | 228 return self._is_symlink |
| 228 | 229 |
| 229 @property | 230 @property |
| 231 def is_reg(self): | |
| 232 return self._is_reg | |
| 233 | |
| 234 @property | |
| 230 def is_dir(self): | 235 def is_dir(self): |
| 231 return self._is_dir | 236 return self._is_dir |
| 232 | 237 |
| 233 @property | 238 @property |
| 234 def stat(self): | 239 def stat(self): |
| 276 w._stat_result = entry.stat(follow_symlinks=True) | 281 w._stat_result = entry.stat(follow_symlinks=True) |
| 277 except OSError as e: | 282 except OSError as e: |
| 278 w._stat_result = None | 283 w._stat_result = None |
| 279 w._stat_errno = e.errno | 284 w._stat_errno = e.errno |
| 280 w._stat_errstr = e.strerror | 285 w._stat_errstr = e.strerror |
| 286 w._is_reg = False | |
| 287 else: | |
| 288 w._is_reg = stat.S_ISREG(w._stat_result.st_mode) | |
| 281 return w | 289 return w |
| 282 | 290 |
| 283 @classmethod | 291 @classmethod |
| 284 def from_path_name(cls_, path, name, _do_stat=True): | 292 def from_path_name(cls_, path, name, _do_stat=True): |
| 285 """`_do_stat` is to be used only for testing purposes""" | 293 """`_do_stat` is to be used only for testing purposes""" |
| 305 w._stat_result = os.stat(w._path) | 313 w._stat_result = os.stat(w._path) |
| 306 except OSError as e: | 314 except OSError as e: |
| 307 w._stat_result = None | 315 w._stat_result = None |
| 308 w._stat_errno = e.errno | 316 w._stat_errno = e.errno |
| 309 w._stat_errstr = e.strerror | 317 w._stat_errstr = e.strerror |
| 318 w._is_reg = False | |
| 319 else: | |
| 320 w._is_reg = stat.S_ISREG(w._stat_result.st_mode) | |
| 310 return w | 321 return w |
| 311 | 322 |
| 312 @classmethod | 323 @classmethod |
| 313 def from_readlink(cls_, path): | 324 def from_readlink(cls_, path): |
| 314 w = cls_(os.path.basename(path), path) | 325 w = cls_(os.path.basename(path), path) |
