|
5 | 5 | import hashlib
|
6 | 6 | import inspect
|
7 | 7 | import pickle
|
| 8 | +from contextlib import suppress |
8 | 9 | from os import stat_result
|
9 | 10 | from pathlib import Path # noqa: TCH003
|
10 | 11 | from typing import TYPE_CHECKING
|
@@ -83,12 +84,10 @@ def signature(self) -> str:
|
83 | 84 |
|
84 | 85 | def state(self) -> str | None:
|
85 | 86 | """Return the state of the node."""
|
86 |
| - try: |
| 87 | + with suppress(OSError): |
87 | 88 | source = inspect.getsource(self.function)
|
88 |
| - except OSError: |
89 |
| - return None |
90 |
| - else: |
91 | 89 | return hashlib.sha256(source.encode()).hexdigest()
|
| 90 | + return None |
92 | 91 |
|
93 | 92 | def execute(self, **kwargs: Any) -> Any:
|
94 | 93 | """Execute the task."""
|
@@ -145,10 +144,7 @@ def signature(self) -> str:
|
145 | 144 |
|
146 | 145 | def state(self) -> str | None:
|
147 | 146 | """Return the state of the node."""
|
148 |
| - if self.path.exists(): |
149 |
| - modification_time = self.path.stat().st_mtime |
150 |
| - return hash_path(self.path, modification_time) |
151 |
| - return None |
| 147 | + return _get_state(self.path) |
152 | 148 |
|
153 | 149 | def execute(self, **kwargs: Any) -> Any:
|
154 | 150 | """Execute the task."""
|
@@ -188,9 +184,7 @@ def state(self) -> str | None:
|
188 | 184 | The state is given by the modification timestamp.
|
189 | 185 |
|
190 | 186 | """
|
191 |
| - if self.path.exists(): |
192 |
| - return _get_state(self.path) |
193 |
| - return None |
| 187 | + return _get_state(self.path) |
194 | 188 |
|
195 | 189 | def load(self, is_product: bool = False) -> Path: # noqa: ARG002
|
196 | 190 | """Load the value."""
|
@@ -324,9 +318,7 @@ def from_path(cls, path: Path) -> PickleNode:
|
324 | 318 | return cls(name=path.as_posix(), path=path)
|
325 | 319 |
|
326 | 320 | def state(self) -> str | None:
|
327 |
| - if self.path.exists(): |
328 |
| - return _get_state(self.path) |
329 |
| - return None |
| 321 | + return _get_state(self.path) |
330 | 322 |
|
331 | 323 | def load(self, is_product: bool = False) -> Any:
|
332 | 324 | if is_product:
|
@@ -377,15 +369,19 @@ def collect(self) -> list[Path]:
|
377 | 369 | return list(self.root_dir.glob(self.pattern)) # type: ignore[union-attr]
|
378 | 370 |
|
379 | 371 |
|
380 |
| -def _get_state(path: Path) -> str: |
| 372 | +def _get_state(path: Path) -> str | None: |
381 | 373 | """Get state of a path.
|
382 | 374 |
|
383 | 375 | A simple function to handle local and remote files.
|
384 | 376 |
|
385 | 377 | """
|
386 |
| - stat = path.stat() |
| 378 | + try: |
| 379 | + stat = path.stat() |
| 380 | + except FileNotFoundError: |
| 381 | + return None |
| 382 | + |
387 | 383 | if isinstance(stat, stat_result):
|
388 |
| - modification_time = path.stat().st_mtime |
| 384 | + modification_time = stat.st_mtime |
389 | 385 | return hash_path(path, modification_time)
|
390 | 386 | if isinstance(stat, UPathStatResult):
|
391 | 387 | return stat.as_info().get("ETag", "0")
|
|
0 commit comments