Debian Codegraph @

cylc/flow/scripts/get_workflow_version.py

raw
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 get-workflow-version [OPTIONS] ARGS
20
21 Find out what version of Cylc a running workflow is using.
22
23 To find the version you've invoked at the command line see "cylc version".
24 """
25
26 from functools import partial
27 from typing import TYPE_CHECKING
28 import sys
29
30 from cylc.flow.network.client_factory import get_client
31 from cylc.flow.network.multi import call_multi
32 from cylc.flow.option_parsers import (
33 WORKFLOW_ID_MULTI_ARG_DOC,
34 CylcOptionParser as COP,
35 )
36 from cylc.flow.terminal import cli_function
37
38 if TYPE_CHECKING:
39 from optparse import Values
40
41
42 QUERY = '''
43 query ($wFlows: [ID]) {
44 workflows(ids: $wFlows) {
45 id
46 name
47 owner
48 cylcVersion
49 }
50 }
51 '''
52
53
54 def get_option_parser():
55 parser = COP(
56 __doc__,
57 comms=True,
58 multiworkflow=True,
59 argdoc=[WORKFLOW_ID_MULTI_ARG_DOC],
60 )
61 return parser
62
63
64 async def run(options, workflow_id, *_):
65 pclient = get_client(workflow_id, timeout=options.comms_timeout)
66
67 query_kwargs = {
68 'request_string': QUERY,
69 'variables': {'wFlows': [workflow_id]}
70 }
71
72 result = await pclient.async_request('graphql', query_kwargs)
73
74 for workflow in result['workflows']:
75 return workflow['cylcVersion']
76
77
78 @cli_function(get_option_parser)
79 def main(parser: COP, options: 'Values', workflow_id: str) -> None:
80 rets = call_multi(
81 partial(run, options),
82 workflow_id,
83 report=lambda x: (x, None, True),
84 # we need the mixed format for call_multi but don't want any tasks
85 constraint='mixed',
86 max_tasks=0,
87 )
88 sys.exit(all(rets.values()) is False)