Skip to content

Commit 031fc05

Browse files
wxtimMetRonnie
andauthored
Feat.lint obsolete vars (#5879)
* lint.warn about deprecated templating * Update changes.d/5879.feat.md Co-authored-by: Ronnie Dutta <[email protected]> * Update cylc/flow/scripts/lint.py Co-authored-by: Ronnie Dutta <[email protected]> * Clarify templated vars which have to be set in metadata sections. Fix an rst bug. * fix broken code --------- Co-authored-by: Ronnie Dutta <[email protected]>
1 parent 3320c05 commit 031fc05

File tree

3 files changed

+81
-7
lines changed

3 files changed

+81
-7
lines changed

changes.d/5879.feat.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
`cylc lint` now warns of use of old templated items such as `%(suite)s`

cylc/flow/scripts/lint.py

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
TOMLDecodeError,
6868
)
6969
from typing import (
70-
TYPE_CHECKING, Any, Callable, Dict, Iterator, List, Union)
70+
TYPE_CHECKING, Any, Callable, Dict, Iterator, List, Optional, Union)
7171

7272
from cylc.flow import LOG
7373
from cylc.flow.exceptions import CylcError
@@ -106,6 +106,31 @@
106106
}
107107

108108

109+
DEPRECATED_STRING_TEMPLATES = {
110+
'suite': 'workflow',
111+
'suite_uuid': 'uuid',
112+
'batch_sys_name': 'job_runner_name',
113+
'batch_sys_job_id': 'job_id',
114+
'user@host': 'platform_name',
115+
'task_url': '``URL`` (if set in :cylc:conf:`[meta]URL`)',
116+
'workflow_url': (
117+
'``workflow_URL`` (if set in '
118+
':cylc:conf:`[runtime][<namespace>][meta]URL`)'),
119+
}
120+
121+
122+
LIST_ITEM = ' * '
123+
124+
125+
deprecated_string_templates = {
126+
key: (
127+
re.compile(r'%\(' + key + r'\)s'),
128+
value
129+
)
130+
for key, value in DEPRECATED_STRING_TEMPLATES.items()
131+
}
132+
133+
109134
def check_jinja2_no_shebang(
110135
line: str,
111136
file: Path,
@@ -233,6 +258,36 @@ def check_for_obsolete_environment_variables(line: str) -> List[str]:
233258
return [i for i in OBSOLETE_ENV_VARS if i in line]
234259

235260

261+
def check_for_deprecated_task_event_template_vars(
262+
line: str
263+
) -> Optional[Dict[str, str]]:
264+
"""Look for string variables which are no longer supported
265+
266+
Examples:
267+
>>> this = check_for_deprecated_task_event_template_vars
268+
269+
>>> this('hello = "My name is %(suite)s"')
270+
{'list': ' * %(suite)s ⇒ %(workflow)s'}
271+
272+
>>> expect = {'list': (
273+
... ' * %(suite)s ⇒ %(workflow)s * %(task_url)s'
274+
... ' - get ``URL`` (if set in :cylc:conf:`[meta]URL`)')}
275+
>>> this('hello = "My name is %(suite)s, %(task_url)s"') == expect
276+
True
277+
"""
278+
result = []
279+
for key, (regex, replacement) in deprecated_string_templates.items():
280+
search_outcome = regex.findall(line)
281+
if search_outcome and ' ' in replacement:
282+
result.append(f'%({key})s - get {replacement}')
283+
elif search_outcome:
284+
result.append(f'%({key})s ⇒ %({replacement})s')
285+
286+
if result:
287+
return {'list': LIST_ITEM + LIST_ITEM.join(result)}
288+
return None
289+
290+
236291
INDENTATION = re.compile(r'^(\s*)(.*)')
237292

238293

@@ -584,6 +639,23 @@ def check_indentation(line: str) -> bool:
584639
),
585640
FUNCTION: re.compile(r'rose +date').findall,
586641
},
642+
'U015': {
643+
'short': (
644+
'Deprecated template variables.'),
645+
'rst': (
646+
'The following template variables, mostly used in event handlers,'
647+
'are deprecated, and should be replaced:'
648+
+ ''.join([
649+
f'\n * ``{old}`` ⇒ {new}'
650+
for old, new in DEPRECATED_STRING_TEMPLATES.items()
651+
])
652+
),
653+
'url': (
654+
'https://cylc.github.io/cylc-doc/stable/html/user-guide/'
655+
'writing-workflows/runtime.html#task-event-template-variables'
656+
),
657+
FUNCTION: check_for_deprecated_task_event_template_vars,
658+
}
587659
}
588660
RULESETS = ['728', 'style', 'all']
589661
EXTRA_TOML_VALIDATION = {
@@ -1077,7 +1149,7 @@ def get_cylc_files(
10771149
'section heading': '\n{title}\n{underline}\n',
10781150
'issue heading': {
10791151
'text': '\n{check}:\n {summary}\n {url}\n\n',
1080-
'rst': '\n`{check} <{url}>`_\n{underline}\n{summary}\n\n',
1152+
'rst': '\n{url}_\n{underline}\n{summary}\n\n',
10811153
},
10821154
'auto gen message': (
10831155
'U998 and U999 represent automatically generated'
@@ -1122,16 +1194,17 @@ def get_reference(linter, output_type):
11221194
output += '\n'
11231195
output += '\n* ' + summary
11241196
else:
1197+
check = get_index_str(meta, index)
11251198
template = issue_heading_template
11261199
url = get_url(meta)
1200+
if output_type == 'rst':
1201+
url = f'`{check} <{url}>`' if url else f'{check}'
11271202
msg = template.format(
11281203
title=index,
1129-
check=get_index_str(meta, index),
1204+
check=check,
11301205
summary=summary,
11311206
url=url,
1132-
underline=(
1133-
len(get_index_str(meta, index)) + len(url) + 6
1134-
) * '^'
1207+
underline=(len(url) + 1) * '^'
11351208
)
11361209
output += msg
11371210
output += '\n'

tests/unit/scripts/test_lint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
submission failed handler = giaSEHFUIHJ
120120
failed handler = woo
121121
execution timeout handler = sdfghjkl
122-
expired handler = dafuhj
122+
expired handler = %(suite_uuid)s %(user@host)s
123123
late handler = dafuhj
124124
submitted handler = dafuhj
125125
started handler = dafuhj

0 commit comments

Comments
 (0)