Skip to content

Commit

Permalink
modified traceback printout, renamed conflicting ImportError -> Impor…
Browse files Browse the repository at this point in the history
…terError, deliberate helpText calling no longer raise error.
  • Loading branch information
DarrinFong committed Aug 25, 2020
1 parent 64b0f1d commit e1f9595
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 51 deletions.
22 changes: 15 additions & 7 deletions boutiques/bosh.py
Expand Up @@ -5,6 +5,8 @@
import sys
import os.path as op
import pytest
import argparse
import traceback
from jsonschema import ValidationError
from boutiques.boshParsers import *
from boutiques.dataHandler import DataHandlerError
Expand All @@ -15,13 +17,12 @@
from boutiques.localExec import ExecutorOutput
from boutiques.localExec import ExecutorError
from boutiques.exporter import ExportError
from boutiques.importer import ImportError
from boutiques.importer import ImporterError
from boutiques.localExec import addDefaultValues
from boutiques.util.utils import loadJson, customSortInvocationByInput
from boutiques.util.utils import formatSphinxUsage
from boutiques.logger import raise_error, print_error, print_info
from tabulate import tabulate
import argparse


def pprint(*params):
Expand Down Expand Up @@ -433,6 +434,11 @@ def bosh_return(val, code=0, hide=False, formatted=None):
return code # everything went well
return val # calling function wants this value

def print_traceback():
exc_type, exc_value, exc_traceback = sys.exc_info()
print("".join(
traceback.format_exception(exc_type, exc_value, exc_traceback)))

# Params are set depending on where bosh is called from
if runs_as_cli():
func = sys.argv[1] if len(sys.argv) >= 2 else None
Expand Down Expand Up @@ -494,28 +500,30 @@ def bosh_return(val, code=0, hide=False, formatted=None):
elif func == "deprecate":
out = deprecate(*params)
return bosh_return(out)
elif func == "-h" or func == "--help":
return bosh_return(parser_bosh().format_help())
else:
print(parser_bosh().format_help())
raise_error(ExecutorError,
"Incorrect bosh mode \'{}\'".format(func))
raise_error(SystemExit,
"Incorrect bosh command \'{}\'".format(func))

except (ZenodoError,
NexusError,
DescriptorValidationError,
InvocationValidationError,
ValidationError,
ExportError,
ImportError,
ImporterError,
ExecutorError) as e:
# We don't want to raise an exception when function is called
# from CLI.'
if runs_as_cli():
print(e)
print_traceback()
return 99 # Note: this conflicts with tool error codes.
raise e
except SystemExit as e:
if runs_as_cli():
print(e)
print_traceback()
return 99 # Note: this conflicts with tool error codes.
raise_error(BoutiquesError,
"Unable to parse arguments resulting in SystemExit.")
Expand Down
30 changes: 15 additions & 15 deletions boutiques/importer.py
Expand Up @@ -21,7 +21,7 @@
import collections


class ImportError(Exception):
class ImporterError(Exception):
pass


Expand Down Expand Up @@ -68,7 +68,7 @@ def upgrade_04(self):
descriptor = loadJson(self.input_descriptor)

if descriptor["schema-version"] != "0.4":
raise_error(ImportError, "The input descriptor must have "
raise_error(ImporterError, "The input descriptor must have "
"'schema-version'=0.4")
descriptor["schema-version"] = "0.5"

Expand Down Expand Up @@ -187,7 +187,7 @@ def import_cwl(self):
bout_desc = {}
# Command line
if cwl_desc.get('baseCommand') is None:
raise_error(ImportError, 'Cannot find baseCommand attribute, '
raise_error(ImporterError, 'Cannot find baseCommand attribute, '
'perhaps you passed a workflow document, '
'this is not supported')
if type(cwl_desc['baseCommand']) is list:
Expand All @@ -199,9 +199,9 @@ def import_cwl(self):
if cwl_desc.get('arguments'):
for i in cwl_desc['arguments']:
if type(i) is dict:
raise_error(ImportError, 'Dict arguments not supported.')
raise_error(ImporterError, 'Dict arguments not supported.')
if "$(runtime." in i:
raise_error(ImportError, 'Runtime parameters '
raise_error(ImporterError, 'Runtime parameters '
' are not supported:'
" "+i)
command_line += i+" "
Expand Down Expand Up @@ -247,19 +247,19 @@ def position(x):
cwl_type = 'string'
if type(cwl_type) is dict: # It must be an array
if cwl_type['type'] != "array":
raise_error(ImportError, "Only 1-level nested "
raise_error(ImporterError, "Only 1-level nested "
"types of type"
" 'array' are supported (CWL input: {0})".
format(cwl_input))
if cwl_type.get('inputBinding') is not None:
raise_error(ImportError, "Input bindings of "
raise_error(ImporterError, "Input bindings of "
"array elements "
"are not supported (CWL input: {0})".
format(cwl_input))
cwl_type = cwl_type['items']
bout_input['list'] = True
if type(cwl_type) != str:
raise_error(ImportError, "Unknown type:"
raise_error(ImporterError, "Unknown type:"
" {0}".format(str(cwl_type)))
boutiques_type = boutiques_types[cwl_type.replace("[]", "")
.replace("?", "")]
Expand All @@ -286,7 +286,7 @@ def position(x):
bout_input['list'] = True
if cwl_input_binding.get("itemSeparator"):
if cwl_input_binding['itemSeparator'] != ' ':
raise_error(ImportError, 'Array separators wont be '
raise_error(ImporterError, 'Array separators wont be '
'supported until #76 is implemented')

# Outputs
Expand All @@ -295,12 +295,12 @@ def resolve_glob(glob, boutiques_inputs):
if not glob.startswith("$"):
return glob
if not glob.startswith("$(inputs."):
raise_error(ImportError, "Unsupported reference: "+glob)
raise_error(ImporterError, "Unsupported reference: "+glob)
input_id = glob.replace("$(inputs.", "").replace(")", "")
for i in boutiques_inputs:
if i['id'] == input_id:
return i['value-key']
raise_error(ImportError, "Unresolved reference"
raise_error(ImporterError, "Unresolved reference"
" in glob: " + glob)

boutiques_outputs = []
Expand All @@ -326,7 +326,7 @@ def resolve_glob(glob, boutiques_inputs):
cwl_out_obj['type']['type'] == 'array'):
bout_output['list'] = True
else:
raise_error(ImportError, 'Unsupported output type: ' +
raise_error(ImporterError, 'Unsupported output type: ' +
cwl_output['type'])
boutiques_outputs.append(bout_output)
# Boutiques descriptors have to have at least 1 output file
Expand Down Expand Up @@ -405,7 +405,7 @@ def parse_req(req, req_type, bout_desc):
'file-template': template
})
return
raise_error(ImportError, 'Unsupported requirement: '+str(req))
raise_error(ImporterError, 'Unsupported requirement: '+str(req))

for key in ['requirements', 'hints']:
if(cwl_desc.get(key)):
Expand Down Expand Up @@ -622,7 +622,7 @@ def __init__(self, docopt_str, base_descriptor):
matched, left, collected = self.pattern.fix().match(argv)
except Exception:
os.remove(base_descriptor)
raise_error(ImportError, "Invalid docopt script")
raise_error(ImporterError, "Invalid docopt script")

self.loadDocoptDescription()
self.loadDescriptionAndType()
Expand Down Expand Up @@ -737,7 +737,7 @@ def _loadInputsFromUsage(self, usage):
ancestors.append(arg.name)
else:
raise_error(
ImportError,
ImporterError,
"Non implemented docopt arg.type: {0}".format(arg_type))

def _addArgumentToDependencies(self, node, ancestors=None,
Expand Down
10 changes: 1 addition & 9 deletions boutiques/puller.py
Expand Up @@ -4,15 +4,7 @@
from boutiques.logger import raise_error, print_info
from boutiques.searcher import Searcher
from boutiques.zenodoHelper import ZenodoError, ZenodoHelper

try:
# Python 3
from urllib.request import urlopen
from urllib.request import urlretrieve
except ImportError:
# Python 2
from urllib2 import urlopen
from urllib import urlretrieve
from urllib.request import urlretrieve


class Puller():
Expand Down
27 changes: 16 additions & 11 deletions boutiques/tests/test_bosh.py
Expand Up @@ -9,20 +9,25 @@
class TestBosh(BaseTest):

def test_help(self):
self.assertRaises(ExecutorError, bosh, [])
self.assertRaises(ExecutorError, bosh, ["--help"])
self.assertRaises(ExecutorError, bosh, ["exec", "--help"])
self.assertRaises(ExecutorError, bosh, ["exec", "launch", "--help"])
self.assertRaises(ExecutorError, bosh, ["exec", "simulate", "--help"])
self.assertRaises(ExecutorError, bosh, ["exec", "prepare", "--help"])
self.assertRaises(BoutiquesError, bosh, [])
self.assertRaises(BoutiquesError, bosh, ["create", "--help"])
self.assertRaises(DataHandlerError, bosh, ["data", "--help"])
self.assertRaises(DataHandlerError, bosh, ["data", "delete", "--help"])
self.assertRaises(DataHandlerError, bosh, ["data", "inspect", "--help"])
self.assertRaises(DataHandlerError, bosh, ["data", "publish", "--help"])
self.assertRaises(BoutiquesError, bosh, ["publish", "--help"])
self.assertRaises(BoutiquesError, bosh, ["import", "--help"])
self.assertRaises(BoutiquesError, bosh, ["validate", "--help"])
self.assertRaises(BoutiquesError, bosh, ["invocation", "--help"])
self.assertRaises(BoutiquesError, bosh, ["deprecate", "--help"])
self.assertRaises(BoutiquesError, bosh, ["evaluate", "--help"])
self.assertRaises(BoutiquesError, bosh, ["create", "--help"])
self.assertRaises(BoutiquesError, bosh, ["example", "--help"])
self.assertRaises(ExecutorError, bosh, ["exec", "--help"])
self.assertRaises(ExecutorError, bosh, ["exec", "launch", "--help"])
self.assertRaises(ExecutorError, bosh, ["exec", "simulate", "--help"])
self.assertRaises(ExecutorError, bosh, ["exec", "prepare", "--help"])
self.assertRaises(BoutiquesError, bosh, ["export", "--help"])
self.assertRaises(BoutiquesError, bosh, ["import", "--help"])
self.assertRaises(BoutiquesError, bosh, ["invocation", "--help"])
self.assertRaises(BoutiquesError, bosh, ["pprint", "--help"])
self.assertRaises(BoutiquesError, bosh, ["publish", "--help"])
self.assertRaises(BoutiquesError, bosh, ["pull", "--help"])
self.assertRaises(BoutiquesError, bosh, ["search", "--help"])
self.assertRaises(BoutiquesError, bosh, ["test", "--help"])
self.assertRaises(BoutiquesError, bosh, ["validate", "--help"])
8 changes: 4 additions & 4 deletions boutiques/tests/test_import.py
Expand Up @@ -6,7 +6,7 @@
import os.path as op
from os.path import join as opj
import pytest
from boutiques.importer import ImportError
from boutiques.importer import ImporterError
import boutiques
import tarfile
import simplejson as json
Expand Down Expand Up @@ -106,7 +106,7 @@ def test_import_cwl_valid(self):
out_desc,
cwl_descriptor]
if d in bad_dirs:
with pytest.raises(ImportError):
with pytest.raises(ImporterError):
bosh(args)
else:
self.assertFalse(bosh(args), cwl_descriptor)
Expand Down Expand Up @@ -164,9 +164,9 @@ def test_docopt_import_invalid(self):

args = ["import", "docopt", output_descriptor, pydocopt_input]

with pytest.raises(ImportError, match="Invalid docopt script"):
with pytest.raises(ImporterError, match="Invalid docopt script"):
bosh(args)
self.fail("Did not raise ImportError or" +
self.fail("Did not raise ImporterError or" +
" message did not match Invalid docopt script")

if op.isfile(output_descriptor):
Expand Down
9 changes: 4 additions & 5 deletions boutiques/tests/test_validator.py
Expand Up @@ -71,8 +71,7 @@ def test_invalid_container_index(self):
process = subprocess.Popen(command, shell=True,
stdout=subprocess.PIPE)
stdout = process.stdout.read().decode("utf-8").strip()
self.assertEqual(stdout,
('ContainerError: container image'
' \"docker://index.docker.io\" is prepended by index'
' that doesn\'t match container index value'
' \"shub://\"'))
self.assertIn('ContainerError: container image'
' \"docker://index.docker.io\" is prepended by index'
' that doesn\'t match container index value'
' \"shub://\"', stdout)

0 comments on commit e1f9595

Please sign in to comment.