Skip to content

Commit

Permalink
minor improvements for validating simulators
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrkarr committed Dec 21, 2020
1 parent 9a86882 commit 9132cd4
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion biosimulators_utils/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.11'
__version__ = '0.1.12'
11 changes: 5 additions & 6 deletions biosimulators_utils/gh_action/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,26 @@ class GitHubActionCaughtError(Exception):
class GitHubActionErrorHandling(object):
""" Methods for handing errors in the execution of GitHu actions """
@classmethod
def catch_errors(cls, issue_number,
uncaught_exception_msg_func=None,
def catch_errors(cls, uncaught_exception_msg_func=None,
caught_error_labels=None,
uncaught_error_labels=None):
""" Generator for a decorator for CI actions that catches errors and reports them as comments to an issue
Args:
issue_number (:obj:`str`): issue number
uncaught_exception_msg_func (:obj:`types.FunctionType`, optional): function to calculate error message to display to users
caught_error_labels (:obj:`list` of :obj:`str`, optional): labels to apply to caught errors
uncaught_error_labels (:obj:`list` of :obj:`str`, optional): labels to apply to uncaught errors
"""
if uncaught_exception_msg_func is None:
uncaught_exception_msg_func = cls.get_uncaught_exception_msg
return functools.partial(cls._catch_errors, issue_number, uncaught_exception_msg_func,
return functools.partial(cls._catch_errors, uncaught_exception_msg_func,
caught_error_labels or [], uncaught_error_labels or [])

@staticmethod
def _catch_errors(issue_number, uncaught_exception_msg_func, caught_error_labels, uncaught_error_labels, func):
def _catch_errors(uncaught_exception_msg_func, caught_error_labels, uncaught_error_labels, func):
""" Decorator for CI actions that catches errors and reports them as comments to an issue
Args:
issue_number (:obj:`str`): issue number
uncaught_exception_msg_func (:obj:`types.FunctionType`, optional): function to calculate error message to display to users
caught_error_labels (:obj:`list` of :obj:`str`): labels to apply to caught errors
uncaught_error_labels (:obj:`list` of :obj:`str`): labels to apply to uncaught errors
Expand All @@ -62,9 +59,11 @@ def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
except GitHubActionCaughtError:
issue_number = os.getenv('GH_ISSUE_NUMBER')
GitHubAction.add_labels_to_issue(issue_number, caught_error_labels)
raise
except Exception as exception:
issue_number = os.getenv('GH_ISSUE_NUMBER')
GitHubAction.add_labels_to_issue(issue_number, uncaught_error_labels)
GitHubAction.add_error_comment_to_issue(issue_number,
uncaught_exception_msg_func(exception),
Expand Down
4 changes: 2 additions & 2 deletions biosimulators_utils/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ def pull_docker_image(url):
url, str(error).replace('\n', '\n ')))


def tag_and_push_docker_image(docker_client, image, tag):
def tag_and_push_docker_image(image, tag):
""" Tag and push Docker image
Args:
docker_client (:obj:`docker.client.DockerClient`): Docker client
image (:obj:`docker.models.images.Image`): Docker image
tag (:obj:`str`): tag
"""
assert image.tag(tag)
docker_client = docker.from_env()
response = docker_client.images.push(tag)
response = json.loads(response.rstrip().split('\n')[-1])
if 'error' in response:
Expand Down
4 changes: 2 additions & 2 deletions biosimulators_utils/simulator/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def read_simulator_specs(url):
specs = response.json()
except simplejson.errors.JSONDecodeError as error:
raise ValueError(''.join([
'Simulator specifications from {} could not be parsed.'.format(url),
'Simulator specifications from {} could not be parsed. '.format(url),
'Specifications must be encoded into JSON.\n\n {}'.format(str(error).replace('\n', '\n ')),
]))

Expand All @@ -50,7 +50,7 @@ def read_simulator_specs(url):
response.raise_for_status()
except requests.RequestException as error:
raise ValueError(''.join([
'Simulator specifications from {} are not valid.'.format(url),
'Simulator specifications from {} are not valid. '.format(url),
'Specifications must be adhere to the BioSimulators schema. Documentation is available at {}.\n\n {}'.format(
api_endpoint, str(error).replace('\n', '\n ')),
]))
Expand Down
6 changes: 2 additions & 4 deletions tests/gh_action/test_gh_action_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ def requests_delete_1(url, auth):

# error handling: caught error
class Action(GitHubAction):
@GitHubActionErrorHandling.catch_errors(GitHubAction.get_issue_number(),
caught_error_labels=['invalid'],
@GitHubActionErrorHandling.catch_errors(caught_error_labels=['invalid'],
uncaught_error_labels=['action_error'])
def run(self):
raise GitHubActionCaughtError()
Expand All @@ -128,8 +127,7 @@ def requests_labels_method(url, headers, auth, json):

# error handling: uncaught error
class Action(GitHubAction):
@GitHubActionErrorHandling.catch_errors(GitHubAction.get_issue_number(),
caught_error_labels=['invalid'],
@GitHubActionErrorHandling.catch_errors(caught_error_labels=['invalid'],
uncaught_error_labels=['action_error'])
def run(self):
raise ValueError('Details about error')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_tag_and_push_docker_image(self):
response = '{"error": "x"}'
docker_client = mock.Mock(images=mock.Mock(push=lambda tag: response))
with mock.patch('docker.from_env', return_value=docker_client):
with self.assertRaises(Exception):
with self.assertRaisesRegex(Exception, 'Unable to push image to'):
image.tag_and_push_docker_image(img, 'hello-world-2')

def test_convert_docker_image_to_singularity(self):
Expand Down

0 comments on commit 9132cd4

Please sign in to comment.