Skip to content

Commit 1e440ce

Browse files
committed
feat: generate Makefile with log --format Makefile
1 parent 746b222 commit 1e440ce

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

renku/cli/_format.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,33 @@ def color(p):
278278
stream.write('}\n')
279279

280280

281+
def makefile(graph):
282+
"""Format graph as Makefile."""
283+
from renku._compat import Path
284+
from renku.models.provenance.activities import ProcessRun, WorkflowRun
285+
286+
for activity in graph.activities.values():
287+
if not isinstance(activity, ProcessRun):
288+
continue
289+
elif isinstance(activity, WorkflowRun):
290+
steps = activity.subprocesses.values()
291+
else:
292+
steps = [activity]
293+
294+
for step in steps:
295+
click.echo(' '.join(step.outputs) + ': ' + ' '.join(step.inputs))
296+
297+
tool = step.process
298+
basedir = Path(step.path).parent
299+
300+
click.echo(
301+
'\t@' + ' '.join(tool.to_argv()) + ' ' + ' '.join(
302+
tool.STD_STREAMS_REPR[key] + ' ' + str(path)
303+
for key, path in tool._std_streams(basedir=basedir).items()
304+
)
305+
)
306+
307+
281308
def jsonld(graph):
282309
"""Format graph as JSON-LD file."""
283310
click.echo(_jsonld(graph, 'expand'))
@@ -327,6 +354,7 @@ def rdf(graph):
327354
'dot-debug': dot_debug,
328355
'json-ld': jsonld,
329356
'json-ld-graph': jsonld_graph,
357+
'Makefile': makefile,
330358
'nt': nt,
331359
'rdf': rdf,
332360
}

renku/models/cwl/command_line_tool.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""Represent a ``CommandLineTool`` from the Common Workflow Language."""
1919

2020
import fnmatch
21+
import os
2122
import re
2223
import shlex
2324
from contextlib import contextmanager
@@ -50,6 +51,13 @@ def convert_arguments(value):
5051
class CommandLineTool(Process, CWLClass):
5152
"""Represent a command line tool."""
5253

54+
STD_STREAMS_REPR = {
55+
'stdin': '<',
56+
'stdout': '>',
57+
'stderr': '2>',
58+
}
59+
"""Format streams for a shell command representation."""
60+
5361
# specialize inputs and outputs with Command{Input,Output}Parameter
5462

5563
baseCommand = attr.ib(
@@ -72,21 +80,32 @@ class CommandLineTool(Process, CWLClass):
7280
temporaryFailCodes = attr.ib(default=attr.Factory(list)) # list(int)
7381
permanentFailCodes = attr.ib(default=attr.Factory(list)) # list(int)
7482

75-
def __str__(self):
76-
"""Generate a simple representation."""
77-
argv = ' '.join(self.to_argv())
83+
def _std_streams(self, basedir=None):
84+
"""Return mapped standard streams."""
85+
streams = {}
86+
7887
if self.stdin:
7988
assert self.stdin.startswith('$(inputs.')
8089
input_id = self.stdin.split('.')[1]
8190
for input_ in self.inputs:
8291
if input_id == input_.id:
83-
argv += ' < ' + str(input_.default)
92+
streams['stdin'] = os.path.relpath(
93+
str(Path(basedir or '.') / str(input_.default))
94+
)
8495
break
8596
if self.stdout:
86-
argv += ' > ' + self.stdout
97+
streams['stdout'] = self.stdout
8798
if self.stderr:
88-
argv += ' 2> ' + self.stderr
89-
return argv
99+
streams['stderr'] = self.stderr
100+
101+
return streams
102+
103+
def __str__(self):
104+
"""Generate a simple representation."""
105+
return ' '.join(self.to_argv()) + ' ' + ' '.join(
106+
self.STD_STREAMS_REPR[key] + ' ' + str(path)
107+
for key, path in self._std_streams().items()
108+
)
90109

91110
def create_run(self, **kwargs):
92111
"""Return an instance of process run."""

tests/test_cwl.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,10 @@ def test_stdin_and_stdout(argv, instance_path):
251251

252252
tool = factory.generate_tool()
253253
assert tool.to_argv() == argv
254-
std_streams = ' < input.txt > output.txt 2> error.log'
255-
assert str(tool) == ' '.join(argv) + std_streams
254+
std_streams = (' < input.txt', ' > output.txt', ' 2> error.log')
255+
tool_str = str(tool)
256+
assert tool_str.startswith(' '.join(argv))
257+
assert all(stream in tool_str for stream in std_streams)
256258

257259

258260
def test_input_directory(instance_path):

0 commit comments

Comments
 (0)