Skip to content

Commit 4049927

Browse files
committed
drop kwargs from Factory()
1 parent e4a5268 commit 4049927

File tree

6 files changed

+121
-133
lines changed

6 files changed

+121
-133
lines changed

cwltool/factory.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from __future__ import absolute_import
22

33
import os
4-
from typing import Callable as tCallable # pylint: disable=unused-import
5-
from typing import (Any, # pylint: disable=unused-import
4+
from typing import Callable as tCallable # pylint: disable=unused-import
5+
from typing import (Any, # pylint: disable=unused-import
66
Dict, Optional, Text, Tuple, Union)
77

88
from . import load_tool
@@ -29,44 +29,33 @@ def __init__(self, t, factory): # type: (Process, Factory) -> None
2929

3030
def __call__(self, **kwargs):
3131
# type: (**Any) -> Union[Text, Dict[Text, Text]]
32-
runtimeContext = self.factory.runtimeContext.copy()
33-
runtimeContext.basedir = os.getcwd()
34-
out, status = self.factory.executor(self.t, kwargs, runtimeContext)
32+
runtime_context = self.factory.runtime_context.copy()
33+
runtime_context.basedir = os.getcwd()
34+
out, status = self.factory.executor(self.t, kwargs, runtime_context)
3535
if status != "success":
3636
raise WorkflowStatus(out, status)
3737
else:
3838
return out
3939

4040
class Factory(object):
4141
def __init__(self,
42-
executor=None, # type: tCallable[...,Tuple[Dict[Text,Any], Text]]
43-
loadingContext=None, # type: LoadingContext
44-
runtimeContext=None, # type: RuntimeContext
45-
**kwargs
42+
executor=None, # type: tCallable[...,Tuple[Dict[Text,Any], Text]]
43+
loading_context=None, # type: LoadingContext
44+
runtime_context=None # type: RuntimeContext
4645
): # type: (...) -> None
4746
if executor is None:
4847
executor = SingleJobExecutor()
4948
self.executor = executor
50-
51-
new_exec_kwargs = get_default_args()
52-
new_exec_kwargs.update(kwargs)
53-
new_exec_kwargs.pop("job_order")
54-
new_exec_kwargs.pop("workflow")
55-
new_exec_kwargs.pop("outdir")
56-
57-
if loadingContext is None:
58-
self.loadingContext = LoadingContext(new_exec_kwargs)
59-
else:
60-
self.loadingContext = loadingContext
61-
62-
if runtimeContext is None:
63-
self.runtimeContext = RuntimeContext(new_exec_kwargs)
64-
else:
65-
self.runtimeContext = runtimeContext
49+
self.loading_context = loading_context
50+
if loading_context is None:
51+
self.loading_context = LoadingContext()
52+
self.runtime_context = runtime_context
53+
if runtime_context is None:
54+
self.runtime_context = RuntimeContext()
6655

6756
def make(self, cwl):
6857
"""Instantiate a CWL object from a CWl document."""
69-
load = load_tool.load_tool(cwl, self.loadingContext)
58+
load = load_tool.load_tool(cwl, self.loading_context)
7059
if isinstance(load, int):
7160
raise Exception("Error loading tool")
7261
return Callable(load, self)

tests/test_examples.py

Lines changed: 74 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from cwltool.errors import WorkflowException
2020
from cwltool.main import main
2121
from cwltool.utils import onWindows, subprocess
22+
from cwltool.context import RuntimeContext
2223

2324
from .util import (get_data, get_windows_safe_factory, needs_docker,
2425
needs_singularity, windows_needs_docker)
@@ -152,16 +153,21 @@ def test_factory_bad_outputs(self):
152153

153154
def test_default_args(self):
154155
f = cwltool.factory.Factory()
155-
assert f.runtimeContext.use_container is True
156-
assert f.runtimeContext.on_error == "stop"
156+
assert f.runtime_context.use_container is True
157+
assert f.runtime_context.on_error == "stop"
157158

158159
def test_redefined_args(self):
159-
f = cwltool.factory.Factory(use_container=False, on_error="continue")
160-
assert f.runtimeContext.use_container is False
161-
assert f.runtimeContext.on_error == "continue"
160+
runtime_context = RuntimeContext()
161+
runtime_context.use_container = False
162+
runtime_context.on_error = "continue"
163+
f = cwltool.factory.Factory(runtime_context=runtime_context)
164+
assert f.runtime_context.use_container is False
165+
assert f.runtime_context.on_error == "continue"
162166

163167
def test_partial_scatter(self):
164-
f = cwltool.factory.Factory(on_error="continue")
168+
runtime_context = RuntimeContext()
169+
runtime_context.on_error = "continue"
170+
f = cwltool.factory.Factory(runtime_context=runtime_context)
165171
fail = f.make(get_data("tests/wf/scatterfail.cwl"))
166172
try:
167173
fail()
@@ -173,7 +179,9 @@ def test_partial_scatter(self):
173179
self.fail("Should have raised WorkflowStatus")
174180

175181
def test_partial_output(self):
176-
f = cwltool.factory.Factory(on_error="continue")
182+
runtime_context = RuntimeContext()
183+
runtime_context.on_error = "continue"
184+
f = cwltool.factory.Factory(runtime_context=runtime_context)
177185
fail = f.make(get_data("tests/wf/wffail.cwl"))
178186
try:
179187
fail()
@@ -248,36 +256,32 @@ def loadref(base, p):
248256

249257
sc.sort(key=lambda k: k["basename"])
250258

251-
self.assertEquals([{
252-
"basename": "bar.cwl",
253-
"nameroot": "bar",
254-
"class": "File",
255-
"nameext": ".cwl",
256-
"location": "file:///example/bar.cwl"
257-
},
258-
{
259-
"basename": "data.txt",
260-
"nameroot": "data",
261-
"class": "File",
262-
"nameext": ".txt",
263-
"location": "file:///example/data.txt"
264-
},
265-
{
266-
"basename": "data2",
267-
"class": "Directory",
268-
"location": "file:///example/data2",
269-
"listing": [{
270-
"basename": "data3.txt",
271-
"nameroot": "data3",
272-
"class": "File",
273-
"nameext": ".txt",
274-
"location": "file:///example/data3.txt",
275-
"secondaryFiles": [{
276-
"class": "File",
277-
"basename": "data5.txt",
278-
"location": "file:///example/data5.txt",
279-
"nameext": ".txt",
280-
"nameroot": "data5"
259+
self.assertEquals([
260+
{"basename": "bar.cwl",
261+
"nameroot": "bar",
262+
"class": "File",
263+
"nameext": ".cwl",
264+
"location": "file:///example/bar.cwl"},
265+
{"basename": "data.txt",
266+
"nameroot": "data",
267+
"class": "File",
268+
"nameext": ".txt",
269+
"location": "file:///example/data.txt"},
270+
{"basename": "data2",
271+
"class": "Directory",
272+
"location": "file:///example/data2",
273+
"listing": [
274+
{"basename": "data3.txt",
275+
"nameroot": "data3",
276+
"class": "File",
277+
"nameext": ".txt",
278+
"location": "file:///example/data3.txt",
279+
"secondaryFiles": [
280+
{"class": "File",
281+
"basename": "data5.txt",
282+
"location": "file:///example/data5.txt",
283+
"nameext": ".txt",
284+
"nameroot": "data5"
281285
}]
282286
}]
283287
}, {
@@ -313,39 +317,27 @@ def test_trick_scandeps(self):
313317

314318
class TestDedup(unittest.TestCase):
315319
def test_dedup(self):
316-
ex = [{
317-
"class": "File",
318-
"location": "file:///example/a"
319-
},
320-
{
321-
"class": "File",
322-
"location": "file:///example/a"
323-
},
324-
{
325-
"class": "File",
326-
"location": "file:///example/d"
327-
},
328-
{
329-
"class": "Directory",
330-
"location": "file:///example/c",
331-
"listing": [{
332-
"class": "File",
333-
"location": "file:///example/d"
334-
}]
335-
}]
336-
337-
self.assertEquals([{
338-
"class": "File",
339-
"location": "file:///example/a"
340-
},
341-
{
342-
"class": "Directory",
343-
"location": "file:///example/c",
344-
"listing": [{
345-
"class": "File",
346-
"location": "file:///example/d"
347-
}]
348-
}], cwltool.pathmapper.dedup(ex))
320+
ex = [{"class": "File",
321+
"location": "file:///example/a"},
322+
{"class": "File",
323+
"location": "file:///example/a"},
324+
{"class": "File",
325+
"location": "file:///example/d"},
326+
{"class": "Directory",
327+
"location": "file:///example/c",
328+
"listing": [
329+
{"class": "File",
330+
"location": "file:///example/d"}]}]
331+
332+
self.assertEquals([
333+
{"class": "File",
334+
"location": "file:///example/a"},
335+
{"class": "Directory",
336+
"location": "file:///example/c",
337+
"listing": [
338+
{"class": "File",
339+
"location": "file:///example/d"}]}],
340+
cwltool.pathmapper.dedup(ex))
349341

350342

351343
class TestTypeCompare(unittest.TestCase):
@@ -623,11 +615,9 @@ def test_print_dot(self):
623615

624616
class TestCmdLine(unittest.TestCase):
625617
def get_main_output(self, new_args):
626-
process = subprocess.Popen([
627-
sys.executable,
628-
"-m",
629-
"cwltool"
630-
] + new_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
618+
process = subprocess.Popen(
619+
[sys.executable, "-m", "cwltool"] + new_args,
620+
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
631621

632622
stdout, stderr = process.communicate()
633623
return process.returncode, stdout.decode(), stderr.decode()
@@ -684,15 +674,15 @@ def test_issue_740_fixed(self):
684674
class TestChecksum(TestCmdLine):
685675

686676
def test_compute_checksum(self):
687-
f = cwltool.factory.Factory(compute_checksum=True,
688-
use_container=onWindows())
677+
runtime_context = RuntimeContext()
678+
runtime_context.compute_checksum = True
679+
runtime_context.use_container = onWindows()
680+
f = cwltool.factory.Factory(runtime_context=runtime_context)
689681
echo = f.make(get_data("tests/wf/cat-tool.cwl"))
690-
output = echo(file1={
691-
"class": "File",
692-
"location": get_data("tests/wf/whale.txt")
693-
},
694-
reverse=False
695-
)
682+
output = echo(
683+
file1={"class": "File",
684+
"location": get_data("tests/wf/whale.txt")},
685+
reverse=False)
696686
self.assertEquals(output['output']["checksum"], "sha1$327fc7aedf4f6b69a42a7c8b808dc5a7aff61376")
697687

698688
def test_no_compute_checksum(self):
@@ -721,5 +711,6 @@ def test_singularity_workflow(self):
721711
self.assertIn("completed success", stderr)
722712
self.assertEquals(error_code, 0)
723713

714+
724715
if __name__ == '__main__':
725716
unittest.main()

tests/test_parallel.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import json
22
import unittest
33

4-
import pytest
5-
6-
import cwltool
7-
import cwltool.factory
84
from cwltool.executors import MultithreadedJobExecutor
9-
from cwltool.utils import onWindows
5+
from cwltool.context import RuntimeContext
106

117
from .util import get_data, get_windows_safe_factory, windows_needs_docker
128

@@ -15,12 +11,15 @@ class TestParallel(unittest.TestCase):
1511
@windows_needs_docker
1612
def test_sequential_workflow(self):
1713
test_file = "tests/wf/count-lines1-wf.cwl"
18-
f = get_windows_safe_factory(executor=MultithreadedJobExecutor())
19-
echo = f.make(get_data(test_file))
20-
self.assertEqual(echo(file1= {
21-
"class": "File",
22-
"location": get_data("tests/wf/whale.txt")
23-
}),
14+
executor = MultithreadedJobExecutor()
15+
runtime_context = RuntimeContext()
16+
runtime_context.select_resources = executor.select_resources
17+
factory = get_windows_safe_factory(
18+
executor=executor, runtime_context=runtime_context)
19+
echo = factory.make(get_data(test_file))
20+
self.assertEqual(
21+
echo(file1={"class": "File",
22+
"location": get_data("tests/wf/whale.txt")}),
2423
{"count_output": 16})
2524

2625
@windows_needs_docker

tests/util.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,23 @@
1010

1111
from cwltool.factory import Factory
1212
from cwltool.utils import onWindows, windows_default_container_id
13+
from cwltool.context import RuntimeContext, LoadingContext
1314

14-
15-
def get_windows_safe_factory(**execkwargs):
15+
def get_windows_safe_factory(runtime_context=None, # type: RuntimeContext
16+
loading_context=None, # type: LoadingContext
17+
executor=None # type: Any
18+
): # type: (...) -> Factory
1619
if onWindows():
17-
makekwargs = {'find_default_container': functools.partial(
18-
force_default_container, windows_default_container_id),
19-
'use_container': True}
20-
execkwargs['default_container'] = windows_default_container_id
21-
else:
22-
makekwargs = {}
23-
return Factory(makekwargs=makekwargs, **execkwargs)
20+
if not runtime_context:
21+
runtime_context = RuntimeContext()
22+
runtime_context.find_default_container = functools.partial(
23+
force_default_container, windows_default_container_id)
24+
runtime_context.use_container = True
25+
runtime_context.default_container = windows_default_container_id
26+
return Factory(executor, loading_context, runtime_context)
2427

2528
def force_default_container(default_container_id, builder):
26-
return default_container_id
29+
return default_container_id
2730

2831
def get_data(filename):
2932
filename = os.path.normpath(
@@ -51,6 +54,6 @@ def get_data(filename):
5154
"system path.")
5255

5356
windows_needs_docker = pytest.mark.skipif(
54-
onWindows() and not bool(distutils.spawn.find_executable('docker')),
55-
reason="Running this test on MS Windows requires the docker executable "
56-
"on the system path.")
57+
onWindows() and not bool(distutils.spawn.find_executable('docker')),
58+
reason="Running this test on MS Windows requires the docker executable "
59+
"on the system path.")

tests/wf/count-lines1-wf.cwl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
class: Workflow
22
cwlVersion: v1.0
3+
requirements:
4+
ResourceRequirement:
5+
ramMin: 100
36

47
inputs:
58
file1:

tests/wf/scatter-wf4.cwl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ cwlVersion: v1.0
33
$graph:
44
- id: echo
55
class: CommandLineTool
6+
requirements:
7+
ResourceRequirement:
8+
ramMin: 10
69
inputs:
710
echo_in1:
811
type: string

0 commit comments

Comments
 (0)