def slow_cwl_load(workflow, cwl_args=None, only_tool=None): """ Follows standard routine for loading CWL file the same way as cwltool does it. "workflow" should be an absolute path the cwl file to load. "cwl_args" can be used to update default arguments mostly used for loading and runtime contexts. If "only_tool" is True, return only tool (used for pickling, because the whole Workflow object later fails to be unpickled). If "workflow" was already parsed into CommentedMap, return it unchanged (in a form similar to what we can get if parsed "workflow" with "only_tool" set to True). If "workflow" was a gzip compressed content of a file, it needs to be uncompressed, then written to the temp file and loaded the same way as described above. After loading temp file will be removed automatically. First always try to uncompress, because it's faster. """ cwl_args = {} if cwl_args is None else cwl_args only_tool = False if only_tool is None else only_tool if isinstance(workflow, CommentedMap): return workflow default_cwl_args = get_default_cwl_args(cwl_args) def __load(location): return load_tool( location, setup_loadingContext(LoadingContext(default_cwl_args), RuntimeContext(default_cwl_args), argparse.Namespace(**default_cwl_args))) try: with NamedTemporaryFile( mode="w" ) as temp_stream: # guarantees that temp file will be removed json.dump(get_uncompressed(workflow, parse_as_yaml=True), temp_stream) temp_stream.flush() # otherwise it might be only partially written workflow_data = __load(temp_stream.name) except (zlib.error, binascii.Error): # file was real workflow_data = __load(workflow) return workflow_data.tool if only_tool else workflow_data
def test_get_uncompressed(compressed_data, control_data, parse_as_yaml): uncompressed_data = get_uncompressed(compressed_data, parse_as_yaml) assert control_data == uncompressed_data, \ "Failed to uncompress data"
def test_get_uncompressed_should_fail(compressed_data, control_data, parse_as_yaml): with pytest.raises((zlib.error, binascii.Error, ValueError, YAMLError)): uncompressed_data = get_uncompressed(compressed_data, parse_as_yaml)