| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
"""Utilities for matching IDs which may reference families or include globs.""" |
| 18 |
|
| 19 |
from copy import deepcopy |
| 20 |
from fnmatch import fnmatchcase |
| 21 |
from typing import ( |
| 22 |
TYPE_CHECKING, |
| 23 |
Any, |
| 24 |
Dict, |
| 25 |
Iterable, |
| 26 |
Set, |
| 27 |
Tuple, |
| 28 |
) |
| 29 |
|
| 30 |
from cylc.flow import LOG |
| 31 |
from cylc.flow.cycling.loader import get_point |
| 32 |
from cylc.flow.id import TaskTokens |
| 33 |
from cylc.flow.id_cli import contains_fnmatch |
| 34 |
|
| 35 |
|
| 36 |
if TYPE_CHECKING: |
| 37 |
from cylc.flow.config import WorkflowConfig |
| 38 |
|
| 39 |
|
| 40 |
def id_match( |
| 41 |
config: 'WorkflowConfig', |
| 42 |
pool: Set[TaskTokens], |
| 43 |
ids: Set[TaskTokens], |
| 44 |
only_match_pool: bool = False, |
| 45 |
) -> Tuple[Set[TaskTokens], Set[TaskTokens]]: |
| 46 |
"""New Cylc 8.6.0 task matching interface. |
| 47 |
|
| 48 |
Args: |
| 49 |
config: |
| 50 |
The workflow config. |
| 51 |
pool: |
| 52 |
The IDs in the pool of active tasks to match as a set of tokens. |
| 53 |
The Tokens should include the task state using `task_sel` to |
| 54 |
support task state matching. |
| 55 |
ids: |
| 56 |
The provided IDs to match. |
| 57 |
only_match_pool: |
| 58 |
If True, tasks outside of the provided pool will not be matched. |
| 59 |
|
| 60 |
Returns: |
| 61 |
(matched, unmatched) |
| 62 |
|
| 63 |
""" |
| 64 |
unmatched: Set[TaskTokens] = set() |
| 65 |
|
| 66 |
# mapping of family name to all contained tasks |
| 67 |
family_lookup: Dict[str, Set[str]] = _get_family_lookup(config) |
| 68 |
|
| 69 |
# set of all active cycles |
| 70 |
all_cycles: Set[str] = {tokens['cycle'] for tokens in pool} |
| 71 |
|
| 72 |
# set of all possible namespaces (tasks + families) |
| 73 |
all_namespaces: Dict[str, Any] = config.get_namespace_list( |
| 74 |
'all namespaces' |
| 75 |
) |
| 76 |
|
| 77 |
# separate IDs targeting active tasks ONLY from the remainder |
| 78 |
active_only_ids = { |
| 79 |
id_ |
| 80 |
for id_ in ids |
| 81 |
if id_.get('task_sel') or id_.get('cycle_sel') |
| 82 |
} |
| 83 |
plain_ids = ids - active_only_ids |
| 84 |
|
| 85 |
# match active-only IDs |
| 86 |
active_only_ids, _unmatched = _match( |
| 87 |
config, |
| 88 |
pool, |
| 89 |
active_only_ids, |
| 90 |
family_lookup, |
| 91 |
all_cycles, |
| 92 |
all_namespaces, |
| 93 |
only_match_pool=True, |
| 94 |
match_selectors=True, |
| 95 |
) |
| 96 |
unmatched.update(_unmatched) |
| 97 |
|
| 98 |
# match IDs |
| 99 |
plain_ids, _unmatched = _match( |
| 100 |
config, |
| 101 |
pool, |
| 102 |
plain_ids, |
| 103 |
family_lookup, |
| 104 |
all_cycles, |
| 105 |
all_namespaces, |
| 106 |
only_match_pool=only_match_pool, |
| 107 |
) |
| 108 |
unmatched.update(_unmatched) |
| 109 |
|
| 110 |
return {*active_only_ids, *plain_ids}, unmatched |
| 111 |
|
| 112 |
|
| 113 |
def _match( |
| 114 |
config: 'WorkflowConfig', |
| 115 |
pool: Set[TaskTokens], |
| 116 |
ids: Set[TaskTokens], |
| 117 |
family_lookup: Dict[str, Set[str]], |
| 118 |
all_cycles: Set[str], |
| 119 |
all_namespaces: Dict[str, Any], |
| 120 |
only_match_pool: bool = False, |
| 121 |
match_selectors: bool = False, |
| 122 |
) -> Tuple[Set[TaskTokens], Set[TaskTokens]]: |
| 123 |
# results |
| 124 |
unmatched: Set[TaskTokens] = set() |
| 125 |
matched: Set[TaskTokens] = set() |
| 126 |
|
| 127 |
for id_ in ids: |
| 128 |
# replace the "^" token with the initial cycle point |
| 129 |
if id_['cycle'] == '^': |
| 130 |
id_ = id_.duplicate(cycle=str(config.initial_point)) |
| 131 |
|
| 132 |
# replace the "$" token with the final cycle point |
| 133 |
elif id_['cycle'] == '$': |
| 134 |
if config.final_point: |
| 135 |
id_ = id_.duplicate(cycle=str(config.final_point)) |
| 136 |
else: |
| 137 |
LOG.warning( |
| 138 |
'ID references final cycle point, but none is set:' |
| 139 |
f' {id_.relative_id}' |
| 140 |
) |
| 141 |
unmatched.add(id_) |
| 142 |
continue |
| 143 |
|
| 144 |
# match cycles |
| 145 |
if contains_fnmatch(id_['cycle']): |
| 146 |
_cycles = _fnmatchcase_glob(id_['cycle'], all_cycles) |
| 147 |
else: |
| 148 |
_cycles = {id_['cycle']} |
| 149 |
|
| 150 |
# match tasks |
| 151 |
_namespace = id_.get('task', '*') or 'root' |
| 152 |
_tasks = { |
| 153 |
task |
| 154 |
for namespace in _fnmatchcase_glob(_namespace, all_namespaces) |
| 155 |
for task in family_lookup.get(namespace, {namespace}) |
| 156 |
} |
| 157 |
|
| 158 |
# expand matched IDs |
| 159 |
_matched = { |
| 160 |
TaskTokens( |
| 161 |
cycle=_cycle, |
| 162 |
task=_task, |
| 163 |
task_sel=id_.get('task_sel') or id_.get('cycle_sel'), |
| 164 |
) |
| 165 |
for _cycle in _cycles |
| 166 |
for _task in _tasks |
| 167 |
} |
| 168 |
|
| 169 |
if only_match_pool and match_selectors: |
| 170 |
# filter against active tasks |
| 171 |
_matched = _matched.intersection(pool) |
| 172 |
elif only_match_pool: |
| 173 |
_matched = _matched.intersection({ |
| 174 |
pool_task_id.duplicate(task_sel=None) |
| 175 |
for pool_task_id in pool |
| 176 |
}) |
| 177 |
else: |
| 178 |
# filter for on-sequence task instances |
| 179 |
# exclude pool IDs from filtering (they're already validated) |
| 180 |
_invalid = set() |
| 181 |
for id__ in _matched.difference({ |
| 182 |
pool_task_id.duplicate(task_sel=None) |
| 183 |
for pool_task_id in pool |
| 184 |
}): |
| 185 |
try: |
| 186 |
taskdef = config.taskdefs[id__['task']] |
| 187 |
if not taskdef.is_valid_point(get_point(id__['cycle'])): |
| 188 |
_matched.remove(id__) |
| 189 |
if ( |
| 190 |
# don't warn if the task was provided as a pattern |
| 191 |
not contains_fnmatch(id_['task']) |
| 192 |
# don't warn if the cycle was provided as a pattern |
| 193 |
and not contains_fnmatch(id_['cycle']) |
| 194 |
): |
| 195 |
# the cycle point is not valid for this task |
| 196 |
# NOTE: only log this if the user asked for a |
| 197 |
# specific cycle/task |
| 198 |
_invalid.add(id__) |
| 199 |
|
| 200 |
except (ValueError, KeyError): |
| 201 |
_matched.remove(id__) |
| 202 |
|
| 203 |
if not _matched and _invalid: |
| 204 |
# warn if there were no matches for the provided id |
| 205 |
for id__ in sorted(_invalid): |
| 206 |
LOG.warning( |
| 207 |
'Invalid cycle point for task:' |
| 208 |
f' {id__["task"]}, {id__["cycle"]}' |
| 209 |
) |
| 210 |
|
| 211 |
if _matched: |
| 212 |
matched = matched.union(_matched) |
| 213 |
else: |
| 214 |
unmatched.add(id_) |
| 215 |
|
| 216 |
return matched, unmatched |
| 217 |
|
| 218 |
|
| 219 |
def _fnmatchcase_glob(pattern: str, values: Iterable[str]) -> Set[str]: |
| 220 |
"""Convenience function for globbing over a list of values. |
| 221 |
|
| 222 |
This uses the "fnmatchcase" function which is shell glob like. |
| 223 |
|
| 224 |
Args: |
| 225 |
Pattern: The glob. |
| 226 |
Values: The things to evaluate the glob over. |
| 227 |
|
| 228 |
Examples: |
| 229 |
>>> sorted(_fnmatchcase_glob('*', {'a', 'b', 'c'})) |
| 230 |
['a', 'b', 'c'] |
| 231 |
|
| 232 |
>>> sorted(_fnmatchcase_glob('a*', {'a1', 'a2', 'b1'})) |
| 233 |
['a1', 'a2'] |
| 234 |
|
| 235 |
""" |
| 236 |
return { |
| 237 |
value |
| 238 |
for value in values |
| 239 |
if fnmatchcase(value, pattern) |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
def _get_family_lookup(config: 'WorkflowConfig') -> Dict[str, Set[str]]: |
| 244 |
"""Return a dict mapping families to all contained tasks. |
| 245 |
|
| 246 |
This recursively expands families avoiding the need to do so later. |
| 247 |
""" |
| 248 |
lookup = deepcopy(config.runtime['descendants']) |
| 249 |
|
| 250 |
def _iter(): |
| 251 |
ret = False |
| 252 |
for namespaces in lookup.values(): |
| 253 |
for namespace in list(namespaces): |
| 254 |
if namespace in config.runtime['descendants']: |
| 255 |
ret = True |
| 256 |
namespaces.remove(namespace) |
| 257 |
namespaces.update(config.runtime['descendants'][namespace]) |
| 258 |
return ret |
| 259 |
|
| 260 |
while _iter(): |
| 261 |
pass |
| 262 |
|
| 263 |
return lookup |