Skip to content

Commit

Permalink
cylc: tidy exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-sanders committed Mar 12, 2019
1 parent 8c7fb82 commit d96d81d
Show file tree
Hide file tree
Showing 134 changed files with 854 additions and 855 deletions.
39 changes: 21 additions & 18 deletions bin/cylc-broadcast
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ if '--use-ssh' in sys.argv[1:]:
import re
from tempfile import NamedTemporaryFile

from cylc.exceptions import UserInputError
from cylc.terminal import cli_function
from cylc.broadcast_report import (
get_broadcast_change_report, get_broadcast_bad_options_report)
from cylc.cfgspec.suite import SPEC, upg
Expand Down Expand Up @@ -114,8 +116,8 @@ def get_rdict(left, right=None):
left can be key, [key], [key1]key2, [key1][key2], [key1][key2]key3, etc.
"""
if left == "inherit":
raise ValueError(
"ERROR: Inheritance cannot be changed by broadcast")
raise UserInputError(
"Inheritance cannot be changed by broadcast")
rdict = {}
cur_dict = rdict
tail = left
Expand Down Expand Up @@ -171,7 +173,7 @@ def files_to_settings(settings, setting_files, cancel_mode=False):
cur_setting[key] = item


def main():
def parse_args():
"""CLI for "cylc broadcast"."""
parser = COP(__doc__, comms=True)

Expand Down Expand Up @@ -245,7 +247,12 @@ def main():
"the broadcast config structure in raw Python form.",
action="store_true", default=False, dest="raw")

options, args = parser.parse_args()
return parser.parse_args()


@cli_function
def main():
options, args = parse_args()
suite = args[0]

pclient = SuiteRuntimeClient(
Expand Down Expand Up @@ -275,7 +282,7 @@ def main():
if modified_settings:
print(get_broadcast_change_report(
modified_settings, is_cancel=True))
sys.exit(get_broadcast_bad_options_report(bad_options))
raise UserInputError(get_broadcast_bad_options_report(bad_options))

if options.expire:
modified_settings, bad_options = pclient(
Expand All @@ -285,7 +292,7 @@ def main():
if modified_settings:
print(get_broadcast_change_report(
modified_settings, is_cancel=True))
sys.exit(get_broadcast_bad_options_report(bad_options))
raise UserInputError(get_broadcast_bad_options_report(bad_options))

# implement namespace and cycle point defaults here
namespaces = options.namespaces
Expand All @@ -299,8 +306,8 @@ def main():
settings = []
for option_item in options.cancel:
if "=" in option_item:
raise ValueError(
"ERROR: --cancel=[SEC]ITEM does not take a value")
raise UserInputError(
"--cancel=[SEC]ITEM does not take a value")
option_item = option_item.strip()
setting = get_rdict(option_item)
settings.append(setting)
Expand All @@ -314,14 +321,14 @@ def main():
if modified_settings:
print(get_broadcast_change_report(
modified_settings, is_cancel=True))
sys.exit(get_broadcast_bad_options_report(bad_options))
raise UserInputError(get_broadcast_bad_options_report(bad_options))

if options.settings or options.setting_files:
settings = []
for option_item in options.settings:
if "=" not in option_item:
raise ValueError(
"ERROR: --set=[SEC]ITEM=VALUE requires a value")
raise UserInputError(
"--set=[SEC]ITEM=VALUE requires a value")
lhs, rhs = [s.strip() for s in option_item.split("=", 1)]
setting = get_rdict(lhs, rhs)
settings.append(setting)
Expand All @@ -334,13 +341,9 @@ def main():
}
)
print(get_broadcast_change_report(modified_settings))
sys.exit(get_broadcast_bad_options_report(bad_options, is_set=True))
raise UserInputError(
get_broadcast_bad_options_report(bad_options, is_set=True))


if __name__ == "__main__":
try:
main()
except Exception as exc:
if cylc.flags.debug:
raise
sys.exit(str(exc))
main()
11 changes: 8 additions & 3 deletions bin/cylc-cat-log
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ from stat import S_IRUSR
from tempfile import mkstemp
from subprocess import Popen, PIPE

from cylc.exceptions import UserInputError
import cylc.flags
from cylc.option_parsers import CylcOptionParser as COP
from cylc.rundb import CylcSuiteDAO
Expand All @@ -64,6 +65,7 @@ from cylc.cfgspec.glbl_cfg import glbl_cfg
from cylc.task_id import TaskID
from cylc.task_job_logs import (
JOB_LOG_OUT, JOB_LOG_ERR, JOB_LOG_OPTS, NN, JOB_LOGS_LOCAL)
from cylc.terminal import cli_function


# Immortal tail-follow processes on job hosts can be cleaned up by killing
Expand Down Expand Up @@ -135,7 +137,7 @@ def view_log(logpath, mode, tailer_tmpl, batchview_cmd=None, remote=False):
elif not os.path.exists(logpath) and batchview_cmd is None:
# Note: batchview_cmd may not need to have access to logpath, so don't
# test for existence of path if it is set.
sys.stderr.write('ERROR: file not found: %s\n' % logpath)
sys.stderr.write('file not found: %s\n' % logpath)
return 1
elif mode == 'print-dir':
print(os.path.dirname(logpath))
Expand Down Expand Up @@ -277,6 +279,7 @@ def tmpfile_edit(tmpfile, geditor=False):
sys.stderr.write(err)


@cli_function
def main():
"""Implement cylc cat-log CLI.
Expand Down Expand Up @@ -321,7 +324,8 @@ def main():
try:
logpath = logs[int(options.rotation_num)]
except IndexError:
sys.exit("ERROR: max rotation %d" % (len(logs) - 1))
raise UserInputError(
"max rotation %d" % (len(logs) - 1))
tail_tmpl = str(glbl_cfg().get_host_item("tail command template"))
out = view_log(logpath, mode, tail_tmpl)
if out == 1:
Expand All @@ -333,7 +337,8 @@ def main():
if len(args) == 2:
# Cat task job logs, may be on suite or job host.
if options.rotation_num is not None:
sys.exit("ERROR: only suite (not job) logs get rotated")
raise UserInputError(
"only suite (not job) logs get rotated")
task_id = args[1]
try:
task, point = TaskID.split(task_id)
Expand Down
5 changes: 4 additions & 1 deletion bin/cylc-cat-state
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@ import traceback

from cylc.cfgspec.glbl_cfg import glbl_cfg
from cylc.dump import dump_to_stdout
from cylc.exceptions import CylcError
import cylc.flags
from cylc.option_parsers import CylcOptionParser as COP
from cylc.rundb import CylcSuiteDAO
from cylc.task_id import TaskID
from cylc.terminal import cli_function
from cylc.wallclock import get_unix_time_from_time_string


REC_BROADCAST_KEY_SECTIONS = re.compile(r"\[([^\]]+)\]")


@cli_function
def main():
"""CLI of "cylc cat-state"."""
parser = COP(__doc__, argdoc=[("REG", "Suite name")])
Expand All @@ -61,7 +64,7 @@ def main():
except sqlite3.Error:
if cylc.flags.debug:
traceback.print_exc()
sys.exit("ERROR: cannot print suite state for %s" % suite)
raise CylcError("cannot print suite state for %s" % suite)

if options.dumpform:
dump_to_stdout(extract_json(state))
Expand Down
52 changes: 30 additions & 22 deletions bin/cylc-check-triggering
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,40 @@ import os
import sys

from cylc.log_diagnosis import LogAnalyser
from cylc.terminal import cli_function

if len(sys.argv) == 2 and sys.argv[1] == '--help':
print(__doc__)
sys.exit(0)

print("\nThis is the cylc check-triggering shutdown event handler")
@cli_function
def main():
if len(sys.argv) == 2 and sys.argv[1] == '--help':
print(__doc__)
sys.exit(0)

event, suite = sys.argv[1], sys.argv[2]
print("\nThis is the cylc check-triggering shutdown event handler")

if event != 'shutdown':
raise SystemExit("ERROR: run this as a shutdown event handler")
event, suite = sys.argv[1], sys.argv[2]

try:
log_dir = os.path.expandvars(os.environ['CYLC_SUITE_LOG_DIR'])
suite_dir = os.path.expandvars(os.environ['CYLC_SUITE_DEF_PATH'])
except KeyError as exc:
raise SystemExit(exc)
if event != 'shutdown':
raise SystemExit("ERROR: run this as a shutdown event handler")

new_log = os.path.join(log_dir, 'log')
ref_log = os.path.join(suite_dir, 'reference.log')
try:
log_dir = os.path.expandvars(os.environ['CYLC_SUITE_LOG_DIR'])
suite_dir = os.path.expandvars(os.environ['CYLC_SUITE_DEF_PATH'])
except KeyError as exc:
raise SystemExit(exc)

try:
lanal = LogAnalyser(new_log, ref_log)
lanal.verify_triggering()
except Exception as exc:
print(exc, file=sys.stderr)
raise SystemExit("ERROR: Triggering check FAILED")
else:
print("Triggering check passed")
new_log = os.path.join(log_dir, 'log')
ref_log = os.path.join(suite_dir, 'reference.log')

try:
lanal = LogAnalyser(new_log, ref_log)
lanal.verify_triggering()
except Exception as exc:
print(exc, file=sys.stderr)
raise SystemExit("ERROR: Triggering check FAILED")
else:
print("Triggering check passed")


if __name__ == '__main__':
main()
9 changes: 3 additions & 6 deletions bin/cylc-check-versions
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ from cylc.subprocpool import SuiteProcPool
from cylc.suite_srv_files_mgr import SuiteSrvFilesManager
from cylc.task_remote_mgr import TaskRemoteMgr
from cylc.templatevars import load_template_vars
from cylc.terminal import cli_function


@cli_function
def main():
parser = COP(__doc__, prep=True, jset=True)

Expand Down Expand Up @@ -142,9 +144,4 @@ def main():


if __name__ == "__main__":
try:
main()
except Exception as exc:
if cylc.flags.debug:
raise
sys.exit(str(exc))
main()
9 changes: 3 additions & 6 deletions bin/cylc-checkpoint
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ if "--use-ssh" in sys.argv[1:]:
import cylc.flags
from cylc.option_parsers import CylcOptionParser as COP
from cylc.network.client import SuiteRuntimeClient
from cylc.terminal import cli_function


@cli_function
def main():
parser = COP(__doc__, comms=True, argdoc=[
("REG", "Suite name"),
Expand All @@ -48,9 +50,4 @@ def main():


if __name__ == "__main__":
try:
main()
except Exception as exc:
if cylc.flags.debug:
raise
sys.exit(str(exc))
main()
2 changes: 2 additions & 0 deletions bin/cylc-client
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ if '--use-ssh' in sys.argv[1:]:

from cylc.option_parsers import CylcOptionParser as COP
from cylc.network.client import SuiteRuntimeClient
from cylc.terminal import cli_function


@cli_function
def main():
parser = COP(__doc__, comms=True, argdoc=[
('REG', 'Suite name'),
Expand Down
3 changes: 3 additions & 0 deletions bin/cylc-cycle-point
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ import sys

import cylc.cycling.iso8601
from cylc.option_parsers import CylcOptionParser as COP
from cylc.terminal import cli_function

import isodatetime.data
import isodatetime.dumpers
import isodatetime.parsers


@cli_function
def main():
parser = COP(
__doc__,
Expand Down
9 changes: 3 additions & 6 deletions bin/cylc-diff
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ from cylc.option_parsers import CylcOptionParser as COP
from cylc.config import SuiteConfig
from cylc.suite_srv_files_mgr import SuiteSrvFilesManager
from cylc.templatevars import load_template_vars
from cylc.terminal import cli_function

n_oone = 0
n_otwo = 0
Expand Down Expand Up @@ -112,6 +113,7 @@ def prdict(dct, arrow='<', section='', level=0, diff=False, nested=False):
print(' ' + arrow + ' ', key, '=', dct[key])


@cli_function
def main():
parser = COP(
__doc__, jset=True, prep=True, icp=True,
Expand Down Expand Up @@ -174,9 +176,4 @@ def main():


if __name__ == "__main__":
try:
main()
except Exception as exc:
if cylc.flags.debug:
raise
sys.exit(str(exc))
main()
Loading

0 comments on commit d96d81d

Please sign in to comment.