@@ -93,7 +93,7 @@ from pathlib import Path
9393import pytask
9494
9595
96- def run_on (ctx ): ...
96+ def run_on (task ): ...
9797
9898
9999@pytask.task (
@@ -104,11 +104,65 @@ def task_copy(depends_on: Path, produces: Path) -> None:
104104 produces.write_text(depends_on.read_text())
105105```
106106
107- The callable receives a context object as its first positional argument.
107+ The callable receives the current task as its first positional argument.
108108
109- The exact shape of ` ctx ` is still to be determined. The goal is to keep enough
110- information available for advanced decisions without making the API harder to understand
111- than necessary.
109+ A public helper can be used to read recorded state from the lockfile:
110+
111+ - ` pytask.get_recorded_state(task) ` for the task itself
112+ - ` pytask.get_recorded_state(task, node) ` for a dependency or product node
113+
114+ For example:
115+
116+ ``` python
117+ from optree import tree_leaves
118+
119+ import pytask
120+
121+
122+ def run_on (task ) -> bool :
123+ for node in tree_leaves(task.depends_on):
124+ if node.state() != pytask.get_recorded_state(task, node):
125+ return True
126+ return task.state() != pytask.get_recorded_state(task)
127+ ```
128+
129+ An expiration rule after seven days could also inspect file modification times for
130+ path-based tasks and nodes. For example:
131+
132+ ``` python
133+ from datetime import datetime, timedelta, timezone
134+
135+ from optree import tree_leaves
136+
137+ import pytask
138+
139+
140+ def run_on (task ) -> bool :
141+ deadline = datetime.now(timezone.utc) - timedelta(days = 7 )
142+
143+ if isinstance (task, pytask.PTaskWithPath):
144+ modified = datetime.fromtimestamp(task.path.stat().st_mtime, tz = timezone.utc)
145+ if modified < deadline:
146+ return True
147+
148+ for node in tree_leaves(task.depends_on):
149+ if isinstance (node, pytask.PPathNode):
150+ modified = datetime.fromtimestamp(
151+ node.path.stat().st_mtime, tz = timezone.utc
152+ )
153+ if modified < deadline:
154+ return True
155+
156+ for node in tree_leaves(task.produces):
157+ if isinstance (node, pytask.PPathNode):
158+ modified = datetime.fromtimestamp(
159+ node.path.stat().st_mtime, tz = timezone.utc
160+ )
161+ if modified < deadline:
162+ return True
163+
164+ return False
165+ ```
112166
113167The relevant use cases for a callable are currently:
114168
0 commit comments