| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 4 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
"""cylc list [OPTIONS] ARGS |
| 20 |
|
| 21 |
List tasks and families defined in a workflow. |
| 22 |
|
| 23 |
Print runtime namespace names (tasks and families), the first-parent |
| 24 |
inheritance graph, or actual tasks for a given cycle range. |
| 25 |
|
| 26 |
The first-parent inheritance graph determines the primary task family |
| 27 |
groupings that are collapsible in cylc visualisation tools. |
| 28 |
|
| 29 |
To visualize the full multiple inheritance hierarchy use: |
| 30 |
$ cylc graph -n |
| 31 |
""" |
| 32 |
|
| 33 |
import asyncio |
| 34 |
from copy import copy |
| 35 |
import os |
| 36 |
from pathlib import Path |
| 37 |
import sys |
| 38 |
from typing import TYPE_CHECKING |
| 39 |
|
| 40 |
from cylc.flow.config import WorkflowConfig |
| 41 |
from cylc.flow.exceptions import InputError |
| 42 |
from cylc.flow.id_cli import parse_id_async |
| 43 |
from cylc.flow.option_parsers import ( |
| 44 |
AGAINST_SOURCE_OPTION, |
| 45 |
WORKFLOW_ID_OR_PATH_ARG_DOC, |
| 46 |
CylcOptionParser as COP, |
| 47 |
icp_option, |
| 48 |
) |
| 49 |
from cylc.flow.print_tree import print_tree |
| 50 |
from cylc.flow.workflow_files import get_workflow_run_dir |
| 51 |
from cylc.flow.templatevars import get_template_vars |
| 52 |
from cylc.flow.terminal import cli_function |
| 53 |
|
| 54 |
if TYPE_CHECKING: |
| 55 |
from optparse import Values |
| 56 |
|
| 57 |
|
| 58 |
def get_option_parser(): |
| 59 |
parser = COP( |
| 60 |
__doc__, |
| 61 |
jset=True, |
| 62 |
argdoc=[WORKFLOW_ID_OR_PATH_ARG_DOC], |
| 63 |
) |
| 64 |
|
| 65 |
parser.add_option( |
| 66 |
"-a", "--all-tasks", |
| 67 |
help="Print all tasks, not just those used in the graph.", |
| 68 |
action="store_true", default=False, dest="all_tasks") |
| 69 |
|
| 70 |
parser.add_option( |
| 71 |
"-n", "--all-namespaces", |
| 72 |
help="Print all runtime namespaces, not just tasks.", |
| 73 |
action="store_true", default=False, dest="all_namespaces") |
| 74 |
|
| 75 |
parser.add_option( |
| 76 |
"-m", "--mro", |
| 77 |
help="Print the linear \"method resolution order\" for each namespace " |
| 78 |
"(the multiple-inheritance precedence order as determined by the " |
| 79 |
"C3 linearization algorithm).", |
| 80 |
action="store_true", default=False, dest="mro") |
| 81 |
|
| 82 |
parser.add_option( |
| 83 |
"-t", "--tree", |
| 84 |
help="Print the first-parent inheritance hierarchy in tree form.", |
| 85 |
action="store_true", default=False, dest="tree") |
| 86 |
|
| 87 |
parser.add_option( |
| 88 |
"-b", "--box", |
| 89 |
help="With -t/--tree, using unicode box characters. Your terminal " |
| 90 |
"must be able to display unicode characters.", |
| 91 |
action="store_true", default=False, dest="box") |
| 92 |
|
| 93 |
parser.add_option( |
| 94 |
"-w", "--with-titles", help="Print namespaces titles too.", |
| 95 |
action="store_true", default=False, dest="titles") |
| 96 |
|
| 97 |
parser.add_option( |
| 98 |
"-p", "--points", |
| 99 |
help="Print task IDs from [START] to [STOP] cycle points. Both bounds " |
| 100 |
"are optional and STOP can be an interval from START (or from the " |
| 101 |
"initial cycle point, by default). Use '-p , ' for the default range.", |
| 102 |
metavar="[START],[STOP]", action="store", default=None, dest="prange") |
| 103 |
|
| 104 |
parser.add_option( |
| 105 |
*AGAINST_SOURCE_OPTION.args, **AGAINST_SOURCE_OPTION.kwargs) |
| 106 |
|
| 107 |
parser.add_option(icp_option) |
| 108 |
|
| 109 |
parser.add_cylc_rose_options() |
| 110 |
return parser |
| 111 |
|
| 112 |
|
| 113 |
@cli_function(get_option_parser) |
| 114 |
def main(parser: COP, options: 'Values', workflow_id: str) -> None: |
| 115 |
asyncio.run(_main(options, workflow_id)) |
| 116 |
|
| 117 |
|
| 118 |
def define_inheritance_tree(tree, hierarchy): |
| 119 |
"""Combine inheritance hierarchies into a tree structure.""" |
| 120 |
for rt_ in hierarchy: |
| 121 |
hier = copy(hierarchy[rt_]) |
| 122 |
hier.reverse() |
| 123 |
cur_tree = tree |
| 124 |
for item in hier: |
| 125 |
if item not in cur_tree: |
| 126 |
cur_tree[item] = {} |
| 127 |
cur_tree = cur_tree[item] |
| 128 |
|
| 129 |
|
| 130 |
def print_first_parent_tree(config, pretty=False, titles=False): |
| 131 |
# find task namespaces (no descendants) |
| 132 |
tasks = [] |
| 133 |
for ns in config.cfg['runtime']: |
| 134 |
if ns not in config.runtime['descendants']: |
| 135 |
tasks.append(ns) |
| 136 |
|
| 137 |
pruned_ancestors = config.get_first_parent_ancestors(pruned=True) |
| 138 |
tree = {} |
| 139 |
define_inheritance_tree(tree, pruned_ancestors) |
| 140 |
padding = '' |
| 141 |
if titles: |
| 142 |
config.add_tree_titles(tree) |
| 143 |
# compute pre-title padding |
| 144 |
maxlen = 0 |
| 145 |
for namespace in pruned_ancestors: |
| 146 |
items = copy(pruned_ancestors[namespace]) |
| 147 |
items.reverse() |
| 148 |
for itt, item in enumerate(items): |
| 149 |
tmp = 2 * itt + 1 + len(item) |
| 150 |
if itt == 0: |
| 151 |
tmp -= 1 |
| 152 |
if tmp > maxlen: |
| 153 |
maxlen = tmp |
| 154 |
padding = maxlen * ' ' |
| 155 |
|
| 156 |
print_tree(tree, padding=padding, use_unicode=pretty) |
| 157 |
|
| 158 |
|
| 159 |
async def _main(options: 'Values', workflow_id: str) -> None: |
| 160 |
workflow_id, _, flow_file = await parse_id_async( |
| 161 |
workflow_id, |
| 162 |
src=True, |
| 163 |
constraint='workflows', |
| 164 |
) |
| 165 |
template_vars = get_template_vars(options) |
| 166 |
|
| 167 |
if options.all_tasks and options.all_namespaces: |
| 168 |
raise InputError("Choose either -a or -n") |
| 169 |
if (options.all_tasks or options.all_namespaces) and options.prange: |
| 170 |
raise InputError( |
| 171 |
'--points cannot be used with --all-tasks or --all-namespaces' |
| 172 |
) |
| 173 |
if options.box and not options.tree: |
| 174 |
options.tree = True |
| 175 |
|
| 176 |
if options.all_tasks: |
| 177 |
which = "all tasks" |
| 178 |
elif options.all_namespaces: |
| 179 |
which = "all namespaces" |
| 180 |
elif options.prange: |
| 181 |
which = "prange" |
| 182 |
if options.prange == ",": |
| 183 |
tr_start = None |
| 184 |
tr_stop = None |
| 185 |
elif options.prange.endswith(","): |
| 186 |
tr_start = options.prange[:-1] |
| 187 |
tr_stop = None |
| 188 |
elif options.prange.startswith(","): |
| 189 |
tr_start = None |
| 190 |
tr_stop = options.prange[1:] |
| 191 |
else: |
| 192 |
tr_start, tr_stop = options.prange.split(',') |
| 193 |
else: |
| 194 |
which = "graphed tasks" |
| 195 |
|
| 196 |
if options.tree and os.environ['LANG'] == 'C' and options.box: |
| 197 |
print("WARNING, ignoring -t/--tree: $LANG=C", file=sys.stderr) |
| 198 |
options.tree = False |
| 199 |
|
| 200 |
if options.titles and options.mro: |
| 201 |
raise InputError("Please choose --mro or --title, not both") |
| 202 |
|
| 203 |
if options.tree and any( |
| 204 |
[options.all_tasks, options.all_namespaces, options.mro]): |
| 205 |
print("WARNING: -t chosen, ignoring non-tree options.", |
| 206 |
file=sys.stderr) |
| 207 |
|
| 208 |
# Save the location of the existing workflow run dir in the |
| 209 |
# against source option: |
| 210 |
if options.against_source: |
| 211 |
options.against_source = Path(get_workflow_run_dir(workflow_id)) |
| 212 |
|
| 213 |
config = WorkflowConfig( |
| 214 |
workflow_id, |
| 215 |
flow_file, |
| 216 |
options, |
| 217 |
template_vars |
| 218 |
) |
| 219 |
if options.tree: |
| 220 |
print_first_parent_tree( |
| 221 |
config, |
| 222 |
pretty=options.box, |
| 223 |
titles=options.titles, |
| 224 |
) |
| 225 |
elif options.prange: |
| 226 |
for node in sorted(config.get_node_labels(tr_start, tr_stop)): |
| 227 |
print(node) |
| 228 |
else: |
| 229 |
result = config.get_namespace_list(which) |
| 230 |
namespaces = list(result) |
| 231 |
namespaces.sort() |
| 232 |
|
| 233 |
if (options.mro or options.titles): |
| 234 |
# compute padding |
| 235 |
maxlen = 0 |
| 236 |
for ns in namespaces: |
| 237 |
if len(ns) > maxlen: |
| 238 |
maxlen = len(ns) |
| 239 |
padding = maxlen * ' ' |
| 240 |
|
| 241 |
for ns in namespaces: |
| 242 |
if options.mro: |
| 243 |
print(ns, padding[0:len(padding) - len(ns)], end=' ') |
| 244 |
print(' '.join(config.get_mro(ns))) |
| 245 |
elif options.titles: |
| 246 |
print(ns, padding[0:len(padding) - len(ns)], end=' ') |
| 247 |
print(result[ns]) |
| 248 |
else: |
| 249 |
print(ns) |