Skip to content

Commit

Permalink
added subprocess failure management
Browse files Browse the repository at this point in the history
  • Loading branch information
wxtim committed Aug 1, 2024
1 parent ce49c92 commit a2fc0a2
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cylc/sphinx_ext/metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ def append(self, text, underline=None):
super().append('', '', 1)


class DependencyError(Exception):
...


class CylcMetadata(SphinxDirective):
"""Represent a Cylc Config.
"""
Expand Down Expand Up @@ -125,6 +129,9 @@ def load_global_cfg(conf_path=None):
)
else:
sub = run(['cylc', 'config', '--json'], capture_output=True)

CylcMetadata.check_subproc_output(sub)

return json.loads(sub.stdout)

@staticmethod
Expand Down Expand Up @@ -211,6 +218,7 @@ def load_workflow_cfg(conf_path):
['cylc', 'config', '--json', conf_path],
capture_output=True,
)
CylcMetadata.check_subproc_output(sub)
return json.loads(sub.stdout)

@staticmethod
Expand Down Expand Up @@ -294,3 +302,22 @@ def write_section(rst, section, title_level='^'):

# Description
rst.append(section.get('description', ''))

@staticmethod
def check_subproc_output(sub):
"""Check subprocess outputs - catch failure.
"""
if sub.returncode:
# Very specifically handle the case where the correct
# version of Cylc isn't installed:
if 'no such option: --json' in sub.stderr.decode():
msg = (
'Requires cylc config --json, not available'
' for this version of Cylc')
raise DependencyError(msg)
# Handle any and all other errors in the subprocess:
else:
msg = 'Cylc config metadata failed with: \n'
msg += '\n'.join(
i.strip("\n") for i in sub.stderr.decode().split('\n'))
raise Exception(msg)

0 comments on commit a2fc0a2

Please sign in to comment.