| 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 |
"""Cylc command argument validation logic.""" |
| 18 |
|
| 19 |
from typing import ( |
| 20 |
TYPE_CHECKING, |
| 21 |
Dict, |
| 22 |
Iterable, |
| 23 |
List, |
| 24 |
Optional, |
| 25 |
Set, |
| 26 |
cast, |
| 27 |
) |
| 28 |
|
| 29 |
from cylc.flow.cycling.loader import standardise_point_string |
| 30 |
from cylc.flow.exceptions import InputError, PointParsingError |
| 31 |
from cylc.flow.flow_mgr import ( |
| 32 |
FLOW_NEW, |
| 33 |
FLOW_NONE, |
| 34 |
) |
| 35 |
from cylc.flow.id import ( |
| 36 |
IDTokens, |
| 37 |
Tokens, |
| 38 |
) |
| 39 |
from cylc.flow.id_cli import contains_fnmatch |
| 40 |
from cylc.flow.scripts.set import XTRIGGER_PREREQ_PREFIX |
| 41 |
from cylc.flow.task_outputs import TASK_OUTPUT_SUCCEEDED |
| 42 |
|
| 43 |
|
| 44 |
if TYPE_CHECKING: |
| 45 |
from cylc.flow.id import TaskTokens |
| 46 |
|
| 47 |
|
| 48 |
ERR_OPT_FLOW_VAL_INT_NEW_NONE = ( # for set and trigger commands |
| 49 |
f"Flow values must be integers, or '{FLOW_NEW}', or '{FLOW_NONE}'" |
| 50 |
) |
| 51 |
ERR_OPT_FLOW_VAL_INT = "Flow values must be integers" # for remove command |
| 52 |
ERR_OPT_FLOW_COMBINE = "Cannot combine --flow={0} with other flow values" |
| 53 |
ERR_OPT_FLOW_WAIT = ( |
| 54 |
f"--wait is not compatible with --flow={FLOW_NEW} or --flow={FLOW_NONE}" |
| 55 |
) |
| 56 |
|
| 57 |
|
| 58 |
def flow_opts( |
| 59 |
flows: List[str], |
| 60 |
flow_wait: bool, |
| 61 |
allow_new_or_none: bool = True, |
| 62 |
) -> None: |
| 63 |
"""Check validity of flow-related CLI options. |
| 64 |
|
| 65 |
Note the schema defaults flows to []. |
| 66 |
|
| 67 |
Examples: |
| 68 |
Good: |
| 69 |
>>> flow_opts([], False) |
| 70 |
>>> flow_opts(["new"], False) |
| 71 |
>>> flow_opts(["1", "2"], False) |
| 72 |
>>> flow_opts(["1", "2"], True) |
| 73 |
|
| 74 |
Bad: |
| 75 |
>>> flow_opts(["none", "1"], False) |
| 76 |
Traceback (most recent call last): |
| 77 |
cylc.flow.exceptions.InputError: Cannot combine --flow=none with other |
| 78 |
flow values |
| 79 |
|
| 80 |
>>> flow_opts(["cheese", "2"], True) |
| 81 |
Traceback (most recent call last): |
| 82 |
cylc.flow.exceptions.InputError: ... or 'new', or 'none' |
| 83 |
|
| 84 |
>>> flow_opts(["new"], True) |
| 85 |
Traceback (most recent call last): |
| 86 |
cylc.flow.exceptions.InputError: --wait is not compatible with |
| 87 |
--flow=new or --flow=none |
| 88 |
|
| 89 |
>>> flow_opts(["new"], False, allow_new_or_none=False) |
| 90 |
Traceback (most recent call last): |
| 91 |
cylc.flow.exceptions.InputError: ... must be integers |
| 92 |
|
| 93 |
>>> flow_opts([''], False, allow_new_or_none=False) |
| 94 |
Traceback (most recent call last): |
| 95 |
cylc.flow.exceptions.InputError: ... must be integers |
| 96 |
|
| 97 |
""" |
| 98 |
if not flows: |
| 99 |
return |
| 100 |
|
| 101 |
flows = [val.strip() for val in flows] |
| 102 |
|
| 103 |
for val in flows: |
| 104 |
val = val.strip() |
| 105 |
if val in {FLOW_NONE, FLOW_NEW}: |
| 106 |
if len(flows) != 1: |
| 107 |
raise InputError(ERR_OPT_FLOW_COMBINE.format(val)) |
| 108 |
if not allow_new_or_none and val in {FLOW_NEW, FLOW_NONE}: |
| 109 |
raise InputError(ERR_OPT_FLOW_VAL_INT) |
| 110 |
else: |
| 111 |
try: |
| 112 |
int(val) |
| 113 |
except ValueError: |
| 114 |
if allow_new_or_none: |
| 115 |
raise InputError(ERR_OPT_FLOW_VAL_INT_NEW_NONE) from None |
| 116 |
raise InputError(ERR_OPT_FLOW_VAL_INT) from None |
| 117 |
|
| 118 |
if flow_wait and flows[0] in {FLOW_NEW, FLOW_NONE}: |
| 119 |
raise InputError(ERR_OPT_FLOW_WAIT) |
| 120 |
|
| 121 |
|
| 122 |
def prereqs(prereqs: Optional[List[str]]): |
| 123 |
"""Validate prerequisites, add implicit ":succeeded". |
| 124 |
|
| 125 |
Comma-separated lists should be split already, client-side. |
| 126 |
|
| 127 |
Examples: |
| 128 |
# Set multiple at once, prereq and xtriggers: |
| 129 |
>>> prereqs(['1/foo:bar', '2/foo:baz', 'xtrigger/x1']) |
| 130 |
['1/foo:bar', '2/foo:baz', 'xtrigger/x1:succeeded'] |
| 131 |
|
| 132 |
# --pre=all |
| 133 |
>>> prereqs(["all"]) |
| 134 |
['all'] |
| 135 |
|
| 136 |
# implicit ":succeeded" |
| 137 |
>>> prereqs(["1/foo"]) |
| 138 |
['1/foo:succeeded'] |
| 139 |
|
| 140 |
# implicit ":satisifed" |
| 141 |
>>> prereqs(["xtrigger/foo"]) |
| 142 |
['xtrigger/foo:succeeded'] |
| 143 |
|
| 144 |
# Error: invalid format: |
| 145 |
>>> prereqs(["fish", "dog"]) |
| 146 |
Traceback (most recent call last): |
| 147 |
cylc.flow.exceptions.InputError: ... |
| 148 |
* fish |
| 149 |
* dog |
| 150 |
|
| 151 |
# Error: invalid format: |
| 152 |
>>> prereqs(["1/foo::bar"]) |
| 153 |
Traceback (most recent call last): |
| 154 |
cylc.flow.exceptions.InputError: ... |
| 155 |
* 1/foo::bar |
| 156 |
|
| 157 |
# Error: invalid format: |
| 158 |
>>> prereqs(["xtrigger/x1::bar"]) |
| 159 |
Traceback (most recent call last): |
| 160 |
cylc.flow.exceptions.InputError: ... |
| 161 |
* xtrigger/x1::bar |
| 162 |
|
| 163 |
# Error: "all" must be used alone: |
| 164 |
>>> prereqs(["all", "2/foo:baz"]) |
| 165 |
Traceback (most recent call last): |
| 166 |
cylc.flow.exceptions.InputError: --pre=all must be used alone |
| 167 |
|
| 168 |
""" |
| 169 |
if prereqs is None: |
| 170 |
return [] |
| 171 |
|
| 172 |
prereqs2 = [] |
| 173 |
bad: List[str] = [] |
| 174 |
for pre in prereqs: |
| 175 |
p = prereq(pre) |
| 176 |
if p is not None: |
| 177 |
prereqs2.append(p) |
| 178 |
else: |
| 179 |
bad.append(pre) |
| 180 |
if bad: |
| 181 |
raise InputError( |
| 182 |
"Bad prerequisite format, see command help:\n * " |
| 183 |
+ "\n * ".join(bad) |
| 184 |
) |
| 185 |
if len(prereqs2) > 1: # noqa SIM102 (anticipates "cylc set --pre=cycle") |
| 186 |
if "all" in prereqs: |
| 187 |
raise InputError("--pre=all must be used alone") |
| 188 |
|
| 189 |
return prereqs2 |
| 190 |
|
| 191 |
|
| 192 |
def prereq(prereq: str) -> Optional[str]: |
| 193 |
"""Return standardised task and xtrigger prerequisites if valid, else None. |
| 194 |
|
| 195 |
Default to suffix ":succeeded" (task and xtrigger prerequisites). |
| 196 |
|
| 197 |
(Standardisation of "start" -> "started" etc. is done later). |
| 198 |
|
| 199 |
Format: cycle/task[:output] |
| 200 |
(xtriggers: cycle is "xtrigger", task is xtrigger label) |
| 201 |
|
| 202 |
Examples: |
| 203 |
>>> prereq('1/foo:succeeded') |
| 204 |
'1/foo:succeeded' |
| 205 |
|
| 206 |
>>> prereq('1/foo:succeed') |
| 207 |
'1/foo:succeed' |
| 208 |
|
| 209 |
>>> prereq('1/foo') |
| 210 |
'1/foo:succeeded' |
| 211 |
|
| 212 |
>>> prereq('1/foo:other_output') |
| 213 |
'1/foo:other_output' |
| 214 |
|
| 215 |
>>> prereq('all') |
| 216 |
'all' |
| 217 |
|
| 218 |
>>> prereq('xtrigger/wall_clock') |
| 219 |
'xtrigger/wall_clock:succeeded' |
| 220 |
|
| 221 |
>>> prereq('xtrigger/wall_clock:succeeded') |
| 222 |
'xtrigger/wall_clock:succeeded' |
| 223 |
|
| 224 |
>>> prereq('xtrigger/all') |
| 225 |
'xtrigger/all:succeeded' |
| 226 |
|
| 227 |
>>> prereq('xtrigger/all:succeeded') |
| 228 |
'xtrigger/all:succeeded' |
| 229 |
|
| 230 |
# Error, xtrigger state must be succeeded or waiting: |
| 231 |
>>> prereq('xtrigger/wall_clock:other') |
| 232 |
|
| 233 |
# Error, just a task name: |
| 234 |
>>> prereq('fish') |
| 235 |
|
| 236 |
""" |
| 237 |
try: |
| 238 |
tokens = Tokens(prereq, relative=True) |
| 239 |
except ValueError: |
| 240 |
return None |
| 241 |
|
| 242 |
if tokens["cycle"] == prereq and prereq != "all": |
| 243 |
# Error: --pre=<word> other than "all" |
| 244 |
return None |
| 245 |
|
| 246 |
if tokens["cycle"] == XTRIGGER_PREREQ_PREFIX: |
| 247 |
if tokens["task_sel"] not in {None, TASK_OUTPUT_SUCCEEDED}: |
| 248 |
# Error: xtrigger status must be default or succeeded. |
| 249 |
return None |
| 250 |
if tokens["task_sel"] is None: |
| 251 |
# Default to succeeded |
| 252 |
prereq += f":{TASK_OUTPUT_SUCCEEDED}" |
| 253 |
else: |
| 254 |
if prereq != "all" and tokens["task_sel"] is None: |
| 255 |
# Default to succeeded |
| 256 |
prereq += f":{TASK_OUTPUT_SUCCEEDED}" |
| 257 |
|
| 258 |
return prereq |
| 259 |
|
| 260 |
|
| 261 |
def outputs(outputs: Optional[List[str]]): |
| 262 |
"""Validate outputs. |
| 263 |
|
| 264 |
Comma-separated lists should be split already, client-side. |
| 265 |
|
| 266 |
Examples: |
| 267 |
Good: |
| 268 |
>>> outputs(['a', 'b']) |
| 269 |
['a', 'b'] |
| 270 |
|
| 271 |
>>> outputs(["required"]) # "required" is explicit default |
| 272 |
[] |
| 273 |
|
| 274 |
Bad: |
| 275 |
>>> outputs(["required", "a"]) |
| 276 |
Traceback (most recent call last): |
| 277 |
cylc.flow.exceptions.InputError: --out=required must be used alone |
| 278 |
|
| 279 |
>>> outputs(["waiting"]) |
| 280 |
Traceback (most recent call last): |
| 281 |
cylc.flow.exceptions.InputError: Tasks cannot be set to waiting... |
| 282 |
|
| 283 |
""" |
| 284 |
# If "required" is explicit just ditch it (same as the default) |
| 285 |
if not outputs or outputs == ["required"]: |
| 286 |
return [] |
| 287 |
|
| 288 |
if "required" in outputs: |
| 289 |
raise InputError("--out=required must be used alone") |
| 290 |
|
| 291 |
if "waiting" in outputs: |
| 292 |
raise InputError( |
| 293 |
"Tasks cannot be set to waiting. Use trigger to re-run tasks." |
| 294 |
) |
| 295 |
|
| 296 |
return outputs |
| 297 |
|
| 298 |
|
| 299 |
def consistency( |
| 300 |
outputs: Optional[List[str]], |
| 301 |
prereqs: Optional[List[str]], |
| 302 |
) -> None: |
| 303 |
"""Check global option consistency |
| 304 |
|
| 305 |
Examples: |
| 306 |
>>> consistency(["a"], None) # OK |
| 307 |
|
| 308 |
>>> consistency(None, ["1/a:failed"]) #OK |
| 309 |
|
| 310 |
>>> consistency(["a"], ["1/a:failed"]) |
| 311 |
Traceback (most recent call last): |
| 312 |
cylc.flow.exceptions.InputError: ... |
| 313 |
|
| 314 |
""" |
| 315 |
if outputs and prereqs: |
| 316 |
raise InputError("Use --prerequisite or --output, not both.") |
| 317 |
|
| 318 |
|
| 319 |
def is_tasks(ids: Iterable[str]) -> 'Set[TaskTokens]': |
| 320 |
"""Ensure all IDs are task IDs and standardise them. |
| 321 |
|
| 322 |
* Parses IDs. |
| 323 |
* Filters out job ids and ensures at least the cycle point is provided. |
| 324 |
* Standardises the cycle point format. |
| 325 |
* Defaults the namespace to "root" unless provided. |
| 326 |
|
| 327 |
Args: |
| 328 |
ids: The strings to parse. |
| 329 |
|
| 330 |
Returns: |
| 331 |
The parsed IDs as TaskTokens objects. |
| 332 |
|
| 333 |
Raises: |
| 334 |
InputError: If any of the IDs cannot be parsed or formatted. |
| 335 |
|
| 336 |
""" |
| 337 |
if not ids: |
| 338 |
raise InputError("No tasks specified") |
| 339 |
ret: 'Set[TaskTokens]' = set() |
| 340 |
errors: Dict[str, List[str]] = {} |
| 341 |
|
| 342 |
for id_ in ids: |
| 343 |
# parse id |
| 344 |
try: |
| 345 |
tokens = Tokens(id_, relative=True) |
| 346 |
except ValueError: |
| 347 |
errors.setdefault('Invalid ID', []).append(id_) |
| 348 |
continue |
| 349 |
|
| 350 |
# filter out job IDs |
| 351 |
if tokens.lowest_token == IDTokens.Job.value: |
| 352 |
errors.setdefault('This command does not take job IDs', []).append( |
| 353 |
id_ |
| 354 |
) |
| 355 |
continue |
| 356 |
|
| 357 |
# if the task is not specified, default to "root" |
| 358 |
if tokens['task'] is None: |
| 359 |
tokens = tokens.duplicate(task='root') |
| 360 |
|
| 361 |
# if the cycle is not a glob or reference, standardise it |
| 362 |
if ( |
| 363 |
# cycle point is a glob |
| 364 |
not contains_fnmatch(cast('str', tokens['cycle'])) |
| 365 |
# cycle point is a reference to the ICP/FCP |
| 366 |
and tokens['cycle'] not in {'^', '$'} |
| 367 |
): |
| 368 |
try: |
| 369 |
cycle = standardise_point_string(tokens['cycle']) |
| 370 |
except PointParsingError: |
| 371 |
errors.setdefault('Invalid cycle point', []).append(id_) |
| 372 |
continue |
| 373 |
else: |
| 374 |
if cycle != tokens['cycle']: |
| 375 |
tokens = tokens.duplicate(cycle=cycle) |
| 376 |
|
| 377 |
# we have confirmed that both cycle and task have been provided |
| 378 |
ret.add(cast('TaskTokens', tokens)) |
| 379 |
|
| 380 |
if errors: |
| 381 |
raise InputError( |
| 382 |
'\n'.join( |
| 383 |
f'{message}: {", ".join(sorted(_ids))}' |
| 384 |
for message, _ids in sorted(errors.items()) |
| 385 |
) |
| 386 |
) |
| 387 |
|
| 388 |
return ret |