Skip to content

Commit 5a94866

Browse files
Merge pull request #5904 from cylc/8.2.x-sync
🤖 Merge 8.2.x-sync into master
2 parents dd8700e + deafd6c commit 5a94866

File tree

2 files changed

+117
-10
lines changed

2 files changed

+117
-10
lines changed

cylc/flow/option_parsers.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,99 @@ def format_help_headings(string):
182182
)
183183

184184

185+
def verbosity_to_log_level(verb: int) -> int:
186+
"""Convert Cylc verbosity to log severity level."""
187+
if verb < 0:
188+
return logging.WARNING
189+
if verb > 0:
190+
return logging.DEBUG
191+
return logging.INFO
192+
193+
194+
def log_level_to_verbosity(lvl: int) -> int:
195+
"""Convert log severity level to Cylc verbosity.
196+
197+
Examples:
198+
>>> log_level_to_verbosity(logging.NOTSET)
199+
2
200+
>>> log_level_to_verbosity(logging.DEBUG)
201+
2
202+
>>> log_level_to_verbosity(logging.INFO)
203+
0
204+
>>> log_level_to_verbosity(logging.WARNING)
205+
-1
206+
>>> log_level_to_verbosity(logging.ERROR)
207+
-1
208+
"""
209+
if lvl <= logging.DEBUG:
210+
return 2
211+
if lvl < logging.INFO:
212+
return 1
213+
if lvl == logging.INFO:
214+
return 0
215+
return -1
216+
217+
218+
def verbosity_to_opts(verb: int) -> List[str]:
219+
"""Convert Cylc verbosity to the CLI opts required to replicate it.
220+
221+
Examples:
222+
>>> verbosity_to_opts(0)
223+
[]
224+
>>> verbosity_to_opts(-2)
225+
['-q', '-q']
226+
>>> verbosity_to_opts(2)
227+
['-v', '-v']
228+
229+
"""
230+
return [
231+
'-q'
232+
for _ in range(verb, 0)
233+
] + [
234+
'-v'
235+
for _ in range(0, verb)
236+
]
237+
238+
239+
def verbosity_to_env(verb: int) -> Dict[str, str]:
240+
"""Convert Cylc verbosity to the env vars required to replicate it.
241+
242+
Examples:
243+
>>> verbosity_to_env(0)
244+
{'CYLC_VERBOSE': 'false', 'CYLC_DEBUG': 'false'}
245+
>>> verbosity_to_env(1)
246+
{'CYLC_VERBOSE': 'true', 'CYLC_DEBUG': 'false'}
247+
>>> verbosity_to_env(2)
248+
{'CYLC_VERBOSE': 'true', 'CYLC_DEBUG': 'true'}
249+
250+
"""
251+
return {
252+
'CYLC_VERBOSE': str((verb > 0)).lower(),
253+
'CYLC_DEBUG': str((verb > 1)).lower(),
254+
}
255+
256+
257+
def env_to_verbosity(env: Union[Dict, os._Environ]) -> int:
258+
"""Extract verbosity from environment variables.
259+
260+
Examples:
261+
>>> env_to_verbosity({})
262+
0
263+
>>> env_to_verbosity({'CYLC_VERBOSE': 'true'})
264+
1
265+
>>> env_to_verbosity({'CYLC_DEBUG': 'true'})
266+
2
267+
>>> env_to_verbosity({'CYLC_DEBUG': 'TRUE'})
268+
2
269+
270+
"""
271+
return (
272+
2 if env.get('CYLC_DEBUG', '').lower() == 'true'
273+
else 1 if env.get('CYLC_VERBOSE', '').lower() == 'true'
274+
else 0
275+
)
276+
277+
185278
class CylcOption(Option):
186279
"""Optparse option which adds a decrement action."""
187280

tests/functional/cli/03-set-verbosity.t

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#-------------------------------------------------------------------------------
1818
# Test "cylc set-verbosity"
1919
. "$(dirname "$0")/test_header"
20-
set_test_number 6
20+
set_test_number 4
2121

2222
# Test illegal log level
2323
TEST_NAME="${TEST_NAME_BASE}-bad"
@@ -28,18 +28,32 @@ grep_ok 'InputError: Illegal logging level, duck' "${TEST_NAME}.stderr"
2828
TEST_NAME="${TEST_NAME_BASE}-good"
2929
init_workflow "${TEST_NAME_BASE}" << '__FLOW__'
3030
[scheduler]
31-
allow implicit tasks = True
31+
[[events]]
32+
abort on stall timeout = True
33+
stall timeout = PT0S
3234
[scheduling]
3335
[[graph]]
34-
R1 = andor
36+
R1 = setter => getter
37+
[runtime]
38+
[[setter]]
39+
script = """
40+
echo "CYLC_VERBOSE: $CYLC_VERBOSE"
41+
[[ "$CYLC_VERBOSE" != 'true' ]]
42+
echo "CYLC_DEBUG: $CYLC_DEBUG"
43+
[[ "$CYLC_DEBUG" != 'true' ]]
44+
45+
cylc set-verbosity DEBUG "$CYLC_WORKFLOW_ID"
46+
cylc__job__poll_grep_workflow_log 'Command actioned: set_verbosity'
47+
"""
48+
[[getter]]
49+
script = """
50+
echo "CYLC_VERBOSE: $CYLC_VERBOSE"
51+
[[ "$CYLC_VERBOSE" == 'true' ]]
52+
echo "CYLC_DEBUG: $CYLC_DEBUG"
53+
[[ "$CYLC_DEBUG" == 'true' ]]
54+
"""
3555
__FLOW__
3656

3757
run_ok "${TEST_NAME}-validate" cylc validate "$WORKFLOW_NAME"
38-
workflow_run_ok "${TEST_NAME}-run" cylc play --pause "$WORKFLOW_NAME"
39-
40-
run_ok "$TEST_NAME" cylc set-verbosity DEBUG "$WORKFLOW_NAME"
41-
log_scan "${TEST_NAME}-grep" "${WORKFLOW_RUN_DIR}/log/scheduler/log" 5 1 \
42-
'Command actioned: set_verbosity'
43-
44-
cylc stop "$WORKFLOW_NAME"
58+
workflow_run_ok "${TEST_NAME}-run" cylc play --no-detach "$WORKFLOW_NAME"
4559
purge

0 commit comments

Comments
 (0)