| 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 config [OPTIONS] ARGS |
| 20 |
|
| 21 |
Parse and print Cylc configuration files. |
| 22 |
|
| 23 |
Print parsed configuration, after runtime inheritance. If WORKFLOW is |
| 24 |
specified, print the workflow configuration, otherwise print the global |
| 25 |
configuration. |
| 26 |
|
| 27 |
Note: |
| 28 |
This is different to `cylc view` which doesn't parse the configuration, |
| 29 |
so is useful for debugging Jinja2. |
| 30 |
|
| 31 |
By default, unset values are printed as an empty string, or (for |
| 32 |
historical reasons) as "None" with -o/--one-line. These defaults |
| 33 |
can be changed with the -n/--null-value option. |
| 34 |
|
| 35 |
Examples: |
| 36 |
# print global configuration |
| 37 |
$ cylc config |
| 38 |
|
| 39 |
# print workflow configuration |
| 40 |
$ cylc config myflow |
| 41 |
|
| 42 |
# print specific setting from the global config |
| 43 |
$ cylc config -i '[platforms][myplatform]hosts |
| 44 |
|
| 45 |
# print specific section from workflow config |
| 46 |
$ cylc config -i '[scheduling][graph]' myflow |
| 47 |
|
| 48 |
# print workflow config, first setting the initial cycle point |
| 49 |
$ cylc config --initial-cycle-point=now myflow |
| 50 |
""" |
| 51 |
|
| 52 |
import asyncio |
| 53 |
|
| 54 |
import os.path |
| 55 |
from pathlib import Path |
| 56 |
from typing import List, Optional, TYPE_CHECKING |
| 57 |
|
| 58 |
from cylc.flow.cfgspec.glbl_cfg import glbl_cfg |
| 59 |
from cylc.flow.config import WorkflowConfig |
| 60 |
from cylc.flow.id_cli import parse_id_async |
| 61 |
from cylc.flow.exceptions import InputError |
| 62 |
from cylc.flow.option_parsers import ( |
| 63 |
AGAINST_SOURCE_OPTION, |
| 64 |
WORKFLOW_ID_OR_PATH_ARG_DOC, |
| 65 |
CylcOptionParser as COP, |
| 66 |
icp_option, |
| 67 |
) |
| 68 |
from cylc.flow.pathutil import get_workflow_run_dir |
| 69 |
from cylc.flow.templatevars import get_template_vars |
| 70 |
from cylc.flow.terminal import cli_function |
| 71 |
from cylc.flow.workflow_files import WorkflowFiles |
| 72 |
|
| 73 |
if TYPE_CHECKING: |
| 74 |
from optparse import Values |
| 75 |
|
| 76 |
|
| 77 |
def get_option_parser() -> COP: |
| 78 |
parser = COP( |
| 79 |
__doc__, |
| 80 |
argdoc=[COP.optional(WORKFLOW_ID_OR_PATH_ARG_DOC)], |
| 81 |
jset=True, |
| 82 |
) |
| 83 |
|
| 84 |
parser.add_option( |
| 85 |
"-i", "--item", metavar="[SEC...]ITEM", |
| 86 |
help="Item or section to print (multiple use allowed).", |
| 87 |
action="append", dest="item", default=[]) |
| 88 |
|
| 89 |
parser.add_option( |
| 90 |
'-d', '--defaults', |
| 91 |
help='Include the hard-coded Cylc default values in the output.', |
| 92 |
action='store_true', |
| 93 |
default=False |
| 94 |
) |
| 95 |
|
| 96 |
parser.add_option( |
| 97 |
"-n", "--null-value", |
| 98 |
help="The string to print for unset values (default nothing).", |
| 99 |
metavar="STRING", action="store", default='', dest="none_str") |
| 100 |
|
| 101 |
parser.add_option( |
| 102 |
"-o", "--one-line", |
| 103 |
help="Print multiple single-value items at once.", |
| 104 |
action="store_true", default=False, dest="oneline") |
| 105 |
|
| 106 |
parser.add_option( |
| 107 |
"--print-hierarchy", "--print-filenames", "--hierarchy", |
| 108 |
help=( |
| 109 |
"Print the list of locations in which configuration files are " |
| 110 |
"looked for. An existing configuration file lower down the list " |
| 111 |
"overrides any settings it shares with those higher up."), |
| 112 |
action="store_true", default=False, dest="print_hierarchy") |
| 113 |
|
| 114 |
parser.add_option(icp_option) |
| 115 |
|
| 116 |
platform_listing_options_group = parser.add_option_group( |
| 117 |
'Platform printing options') |
| 118 |
platform_listing_options_group.add_option( |
| 119 |
'--platform-names', |
| 120 |
help=( |
| 121 |
'Print a list of platforms and platform group names from the ' |
| 122 |
'configuration.' |
| 123 |
), |
| 124 |
action='store_true', default=False, dest='print_platform_names' |
| 125 |
) |
| 126 |
platform_listing_options_group.add_option( |
| 127 |
'--platforms', |
| 128 |
help=( |
| 129 |
'Print platform and platform group configurations, ' |
| 130 |
'including metadata.' |
| 131 |
), |
| 132 |
action='store_true', default=False, dest='print_platforms' |
| 133 |
) |
| 134 |
|
| 135 |
parser.add_option( |
| 136 |
*AGAINST_SOURCE_OPTION.args, **AGAINST_SOURCE_OPTION.kwargs) |
| 137 |
|
| 138 |
parser.add_cylc_rose_options() |
| 139 |
|
| 140 |
return parser |
| 141 |
|
| 142 |
|
| 143 |
def get_config_file_hierarchy(workflow_id: Optional[str] = None) -> List[str]: |
| 144 |
filepaths = [os.path.join(path, glbl_cfg().CONF_BASENAME) |
| 145 |
for _, path in glbl_cfg().conf_dir_hierarchy] |
| 146 |
if workflow_id is not None: |
| 147 |
filepaths.append( |
| 148 |
get_workflow_run_dir(workflow_id, WorkflowFiles.FLOW_FILE) |
| 149 |
) |
| 150 |
return filepaths |
| 151 |
|
| 152 |
|
| 153 |
@cli_function(get_option_parser) |
| 154 |
def main( |
| 155 |
parser: COP, |
| 156 |
options: 'Values', |
| 157 |
*ids, |
| 158 |
) -> None: |
| 159 |
asyncio.run(_main(parser, options, *ids)) |
| 160 |
|
| 161 |
|
| 162 |
async def _main( |
| 163 |
parser: COP, |
| 164 |
options: 'Values', |
| 165 |
*ids, |
| 166 |
) -> None: |
| 167 |
|
| 168 |
if options.print_platform_names and options.print_platforms: |
| 169 |
options.print_platform_names = False |
| 170 |
|
| 171 |
if options.print_platform_names or options.print_platforms: |
| 172 |
# Get platform information: |
| 173 |
if ids: |
| 174 |
raise InputError( |
| 175 |
"Workflow IDs are incompatible with --platform options." |
| 176 |
) |
| 177 |
glbl_cfg().platform_dump( |
| 178 |
options.print_platform_names, |
| 179 |
options.print_platforms |
| 180 |
) |
| 181 |
return |
| 182 |
|
| 183 |
if not ids: |
| 184 |
if options.print_hierarchy: |
| 185 |
print("\n".join(get_config_file_hierarchy())) |
| 186 |
return |
| 187 |
|
| 188 |
glbl_cfg().idump( |
| 189 |
options.item, |
| 190 |
not options.defaults, |
| 191 |
oneline=options.oneline, |
| 192 |
none_str=options.none_str |
| 193 |
) |
| 194 |
return |
| 195 |
|
| 196 |
workflow_id, _, flow_file = await parse_id_async( |
| 197 |
*ids, |
| 198 |
src=True, |
| 199 |
constraint='workflows', |
| 200 |
) |
| 201 |
|
| 202 |
if options.print_hierarchy: |
| 203 |
print("\n".join(get_config_file_hierarchy(workflow_id))) |
| 204 |
return |
| 205 |
|
| 206 |
# Save the location of the existing workflow run dir in the |
| 207 |
# against source option: |
| 208 |
if options.against_source: |
| 209 |
options.against_source = Path(get_workflow_run_dir(workflow_id)) |
| 210 |
|
| 211 |
config = WorkflowConfig( |
| 212 |
workflow_id, |
| 213 |
flow_file, |
| 214 |
options, |
| 215 |
get_template_vars(options) |
| 216 |
) |
| 217 |
|
| 218 |
config.pcfg.idump( |
| 219 |
options.item, |
| 220 |
not options.defaults, |
| 221 |
oneline=options.oneline, |
| 222 |
none_str=options.none_str |
| 223 |
) |