|
| 1 | +""" |
| 2 | +""" |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +import copy |
| 6 | +import os |
| 7 | + |
| 8 | +import click |
| 9 | + |
| 10 | +import schema_salad |
| 11 | +import cwltool |
| 12 | +from cwltool.main import load_tool |
| 13 | +from cwltool.process import ( |
| 14 | + checkRequirements, |
| 15 | + shortname, |
| 16 | +) |
| 17 | +from cwl2script.cwl2script import ( |
| 18 | + supportedProcessRequirements, |
| 19 | + generateScriptForWorkflow, |
| 20 | + generateScriptForTool, |
| 21 | +) |
| 22 | + |
| 23 | +from planemo.cli import pass_context |
| 24 | +from planemo import options |
| 25 | + |
| 26 | + |
| 27 | +@click.command("cwl_script") |
| 28 | +@options.required_tool_arg() |
| 29 | +@options.required_job_arg() |
| 30 | +@click.option('--no_container', is_flag=True, default=False) |
| 31 | +@click.option('outdir', '--output_dir', type=click.Path()) |
| 32 | +@click.option('basedir', '--base_dir', type=click.Path(), default=".") |
| 33 | +@pass_context |
| 34 | +def cli(ctx, path, job_path, **kwds): |
| 35 | + """This compiles simple common workflow language workflows to a shell |
| 36 | + script. |
| 37 | + """ |
| 38 | + uri = "file://" + os.path.abspath(job_path) |
| 39 | + |
| 40 | + loader = schema_salad.ref_resolver.Loader({ |
| 41 | + "@base": uri, |
| 42 | + "path": { |
| 43 | + "@type": "@id" |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + job, _ = loader.resolve_ref(uri) |
| 48 | + |
| 49 | + t = load_tool(path, False, False, cwltool.workflow.defaultMakeTool, True) |
| 50 | + |
| 51 | + if type(t) == int: |
| 52 | + return t |
| 53 | + |
| 54 | + checkRequirements(t.tool, supportedProcessRequirements) |
| 55 | + |
| 56 | + for inp in t.tool["inputs"]: |
| 57 | + if shortname(inp["id"]) in job: |
| 58 | + pass |
| 59 | + elif shortname(inp["id"]) not in job and "default" in inp: |
| 60 | + job[shortname(inp["id"])] = copy.copy(inp["default"]) |
| 61 | + elif shortname(inp["id"]) not in job and inp["type"][0] == "null": |
| 62 | + pass |
| 63 | + else: |
| 64 | + raise Exception("Missing inputs `%s`" % shortname(inp["id"])) |
| 65 | + |
| 66 | + if not kwds.get("basedir", None): |
| 67 | + kwds["basedir"] = os.path.dirname(os.path.abspath(job_path)) |
| 68 | + |
| 69 | + outdir = kwds.get("outdir") |
| 70 | + |
| 71 | + if t.tool["class"] == "Workflow": |
| 72 | + print(generateScriptForWorkflow(t, job, outdir)) |
| 73 | + elif t.tool["class"] == "CommandLineTool": |
| 74 | + print(generateScriptForTool(t, job, outdir)) |
| 75 | + |
| 76 | + return 0 |
0 commit comments