Repository: DataBiosphere/toil

Issue:
cwltoil cann't support coresMax of which value is an expression
echo.cwl content:
```
#!/usr/bin/env cwl-runner
cwlVersion: v1.0
class: CommandLineTool
baseCommand: echo
requirements:
  - class: ResourceRequirement
    ramMax: 500
    coresMax: $(inputs.cpus)

stdout: output1.txt
inputs:
  message:
    type: string
    inputBinding:
      position: 1
  sample_id:
    type: string
  cpus: int
outputs:
  file1:
    type: File
    outputBinding:
      glob: "output1.txt"
```

The corresponding yaml file echo.yml is:
```
message: This is my firest cwl tool
sample_id: Sample1
cpus: 4
```
I got the right output when I use *cwltool* and  run `cwltool echo.cwl echo.yml` ,but error occurred when I use *cwltoil* like `cwltoil echo.cwl echo.yml`. The error is as below:
```
Traceback (most recent call last):
  File "/usr/bin/cwltoil", line 11, in <module>
    sys.exit(main())
  File "/usr/lib/python2.7/site-packages/toil/cwl/cwltoil.py", line 713, in main
    (wf1, wf2) = makeJob(t, {}, use_container=use_container, preserve_environment=options.preserve_environment, tmpdir=os.path.realpath(outdir))
  File "/usr/lib/python2.7/site-packages/toil/cwl/cwltoil.py", line 307, in makeJob
    job = CWLJob(tool, jobobj, **kwargs)
  File "/usr/lib/python2.7/site-packages/toil/cwl/cwltoil.py", line 243, in __init__
    req = tool.evalResources(builder, {})
  File "/usr/lib/python2.7/site-packages/cwltool/process.py", line 550, in evalResources
    mx = builder.do_eval(resourceReq[a+"Max"])
  File "/usr/lib/python2.7/site-packages/cwltool/builder.py", line 210, in do_eval
    debug=self.debug)
  File "/usr/lib/python2.7/site-packages/cwltool/expression.py", line 212, in do_eval
    raise WorkflowException("Expression evaluation error:\n%s" % e)
cwltool.errors.WorkflowException: Expression evaluation error:
inputs does not contain key 'cpus'
```


Discussion:
This seems to be the same issue I reported as #1540. It really would be great to get this fixed.

Resolving patch:
diff --git a/src/toil/cwl/cwltoil.py b/src/toil/cwl/cwltoil.py
--- a/src/toil/cwl/cwltoil.py
+++ b/src/toil/cwl/cwltoil.py
@@ -234,13 +234,16 @@ class CWLJob(Job):
     """Execute a CWL tool wrapper."""
 
     def __init__(self, tool, cwljob, **kwargs):
-        builder = cwltool.builder.Builder()
-        builder.job = {}
-        builder.requirements = []
-        builder.outdir = None
-        builder.tmpdir = None
-        builder.timeout = 0
-        builder.resources = {}
+        if 'builder' in kwargs:
+            builder = kwargs["builder"]
+        else:
+            builder = cwltool.builder.Builder()
+            builder.job = {}
+            builder.requirements = []
+            builder.outdir = None
+            builder.tmpdir = None
+            builder.timeout = 0
+            builder.resources = {}
         req = tool.evalResources(builder, {})
         self.cwltool = remove_pickle_problems(tool)
         # pass the default of None if basecommand is empty
@@ -714,7 +717,9 @@ def importDefault(tool):
         else:
             basedir = os.path.dirname(os.path.abspath(options.cwljob or options.cwltool))
             builder = t._init_job(job, basedir=basedir, use_container=use_container)
-            (wf1, wf2) = makeJob(t, {}, use_container=use_container, preserve_environment=options.preserve_environment, tmpdir=os.path.realpath(outdir))
+            (wf1, wf2) = makeJob(t, {}, use_container=use_container,
+                    preserve_environment=options.preserve_environment,
+                    tmpdir=os.path.realpath(outdir), builder=builder)
             try:
                 if isinstance(wf1, CWLWorkflow):
                     [unsupportedDefaultCheck(s.tool) for s in wf1.cwlwf.steps]


Test patch: