1# Lint as: python2, python3
2# pylint: disable=missing-docstring
3
4from __future__ import absolute_import
5from __future__ import division
6from __future__ import print_function
7import sys, re, traceback
8
9# these statuses are ordered such that a status earlier in the list will
10# override a status later in a list (e.g. ERROR during a test will override
11# prior GOOD results, but WARN will not override a FAIL)
12job_statuses = ["TEST_NA", "ABORT", "ERROR", "FAIL", "WARN", "GOOD", "ALERT",
13                "RUNNING", "NOSTATUS"]
14
15def is_valid_status(status):
16    if not re.match(r'(START|INFO|(END )?(' + '|'.join(job_statuses) + '))$',
17                    status):
18        return False
19    else:
20        return True
21
22
23def log_and_ignore_errors(msg):
24    """ A decorator for wrapping functions in a 'log exception and ignore'
25    try-except block. """
26    def decorator(fn):
27        def decorated_func(*args, **dargs):
28            try:
29                fn(*args, **dargs)
30            except Exception:
31                print(msg, file=sys.stderr)
32                traceback.print_exc(file=sys.stderr)
33        return decorated_func
34    return decorator
35