@@ -151,10 +151,6 @@ def run(self, dry_run=False, pull_image=True, rm_container=True,
151
151
152
152
stageFiles (self .pathmapper , os .symlink )
153
153
154
- stdin = None # type: Union[IO[Any], int]
155
- stderr = None # type: IO[Any]
156
- stdout = None # type: IO[Any]
157
-
158
154
scr , _ = get_feature (self , "ShellCommandRequirement" )
159
155
160
156
if scr :
@@ -191,51 +187,34 @@ def linkoutdir(src, tgt):
191
187
break
192
188
stageFiles (generatemapper , linkoutdir )
193
189
190
+ stdin_path = None
194
191
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 ]
198
193
194
+ stderr_path = None
199
195
if self .stderr :
200
196
abserr = os .path .join (self .outdir , self .stderr )
201
197
dnerr = os .path .dirname (abserr )
202
198
if dnerr and not os .path .exists (dnerr ):
203
199
os .makedirs (dnerr )
204
- stderr = open (abserr , "wb" )
205
- else :
206
- stderr = sys .stderr
200
+ stderr_path = abserr
207
201
202
+ stdout_path = None
208
203
if self .stdout :
209
204
absout = os .path .join (self .outdir , self .stdout )
210
205
dn = os .path .dirname (absout )
211
206
if dn and not os .path .exists (dn ):
212
207
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
228
209
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
+ )
239
218
240
219
if self .successCodes and rcode in self .successCodes :
241
220
processStatus = "success"
@@ -294,3 +273,58 @@ def linkoutdir(src, tgt):
294
273
if move_outputs == "move" and empty_subtree (self .outdir ):
295
274
_logger .debug (u"[job %s] Removing empty output directory %s" , self .name , self .outdir )
296
275
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