Skip to content

Commit 4a0e0d2

Browse files
committed
Refactor toward allowing extensions required in #93.
1 parent 2352d16 commit 4a0e0d2

File tree

1 file changed

+69
-35
lines changed

1 file changed

+69
-35
lines changed

cwltool/job.py

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,6 @@ def run(self, dry_run=False, pull_image=True, rm_container=True,
151151

152152
stageFiles(self.pathmapper, os.symlink)
153153

154-
stdin = None # type: Union[IO[Any], int]
155-
stderr = None # type: IO[Any]
156-
stdout = None # type: IO[Any]
157-
158154
scr, _ = get_feature(self, "ShellCommandRequirement")
159155

160156
if scr:
@@ -191,51 +187,34 @@ def linkoutdir(src, tgt):
191187
break
192188
stageFiles(generatemapper, linkoutdir)
193189

190+
stdin_path = None
194191
if self.stdin:
195-
stdin = open(self.pathmapper.reversemap(self.stdin)[1], "rb")
196-
else:
197-
stdin = subprocess.PIPE
192+
stdin_path = self.pathmapper.reversemap(self.stdin)[1]
198193

194+
stderr_path = None
199195
if self.stderr:
200196
abserr = os.path.join(self.outdir, self.stderr)
201197
dnerr = os.path.dirname(abserr)
202198
if dnerr and not os.path.exists(dnerr):
203199
os.makedirs(dnerr)
204-
stderr = open(abserr, "wb")
205-
else:
206-
stderr = sys.stderr
200+
stderr_path = abserr
207201

202+
stdout_path = None
208203
if self.stdout:
209204
absout = os.path.join(self.outdir, self.stdout)
210205
dn = os.path.dirname(absout)
211206
if dn and not os.path.exists(dn):
212207
os.makedirs(dn)
213-
stdout = open(absout, "wb")
214-
else:
215-
stdout = sys.stderr
216-
217-
sp = subprocess.Popen([Text(x).encode('utf-8') for x in runtime + self.command_line],
218-
shell=False,
219-
close_fds=True,
220-
stdin=stdin,
221-
stderr=stderr,
222-
stdout=stdout,
223-
env=env,
224-
cwd=self.outdir)
225-
226-
if sp.stdin:
227-
sp.stdin.close()
208+
stdout_path = absout
228209

229-
rcode = sp.wait()
230-
231-
if isinstance(stdin, file):
232-
stdin.close()
233-
234-
if stderr is not sys.stderr:
235-
stderr.close()
236-
237-
if stdout is not sys.stderr:
238-
stdout.close()
210+
rcode = _job_popen(
211+
[Text(x).encode('utf-8') for x in runtime + self.command_line],
212+
stdin_path=stdin_path,
213+
stdout_path=stdout_path,
214+
stderr_path=stderr_path,
215+
env=env,
216+
cwd=self.outdir,
217+
)
239218

240219
if self.successCodes and rcode in self.successCodes:
241220
processStatus = "success"
@@ -294,3 +273,58 @@ def linkoutdir(src, tgt):
294273
if move_outputs == "move" and empty_subtree(self.outdir):
295274
_logger.debug(u"[job %s] Removing empty output directory %s", self.name, self.outdir)
296275
shutil.rmtree(self.outdir, True)
276+
277+
278+
def _job_popen(
279+
commands,
280+
stdin_path,
281+
stdout_path,
282+
stderr_path,
283+
env,
284+
cwd,
285+
):
286+
# type: (List[str], Text, Text, Text, Union[MutableMapping[Text, Text], MutableMapping[str, str]], Text) -> int
287+
288+
stdin = None # type: Union[IO[Any], int]
289+
stderr = None # type: IO[Any]
290+
stdout = None # type: IO[Any]
291+
292+
if stdin_path is not None:
293+
stdin = open(stdin_path, "rb")
294+
else:
295+
stdin = subprocess.PIPE
296+
297+
if stdout_path is not None:
298+
stdout = open(stdout_path, "wb")
299+
else:
300+
stdout = sys.stderr
301+
302+
if stderr_path is not None:
303+
stderr = open(stderr_path, "wb")
304+
else:
305+
stderr = sys.stderr
306+
307+
sp = subprocess.Popen(commands,
308+
shell=False,
309+
close_fds=True,
310+
stdin=stdin,
311+
stdout=stdout,
312+
stderr=stderr,
313+
env=env,
314+
cwd=cwd)
315+
316+
if sp.stdin:
317+
sp.stdin.close()
318+
319+
rcode = sp.wait()
320+
321+
if isinstance(stdin, file):
322+
stdin.close()
323+
324+
if stdout is not sys.stderr:
325+
stdout.close()
326+
327+
if stderr is not sys.stderr:
328+
stderr.close()
329+
330+
return rcode

0 commit comments

Comments
 (0)