16
16
17
17
import shellescape
18
18
19
+ from .utils import copytree_with_merge , docker_windows_path_adjust , onWindows
19
20
from . import docker
20
21
from .builder import Builder
21
22
from .docker_uid import docker_vm_uid
@@ -106,8 +107,14 @@ def relink_initialworkdir(pathmapper, inplace_update=False):
106
107
if os .path .islink (vol .target ) or os .path .isfile (vol .target ):
107
108
os .remove (vol .target )
108
109
elif os .path .isdir (vol .target ):
109
- os .rmdir (vol .target )
110
- os .symlink (vol .resolved , vol .target )
110
+ shutil .rmtree (vol .target )
111
+ if onWindows ():
112
+ if vol .type in ("File" , "WritableFile" ):
113
+ shutil .copy (vol .resolved ,vol .target )
114
+ elif vol .type in ("Directory" , "WritableDirectory" ):
115
+ copytree_with_merge (vol .resolved , vol .target )
116
+ else :
117
+ os .symlink (vol .resolved , vol .target )
111
118
112
119
class JobBase (object ):
113
120
def __init__ (self ): # type: () -> None
@@ -278,13 +285,16 @@ def run(self, pull_image=True, rm_container=True,
278
285
if vars_to_preserve is not None :
279
286
for key , value in os .environ .items ():
280
287
if key in vars_to_preserve and key not in env :
281
- env [key ] = value
282
- env ["HOME" ] = self .outdir
283
- env ["TMPDIR" ] = self .tmpdir
284
-
285
- stageFiles (self .pathmapper , os .symlink , ignoreWritable = True )
288
+ # On Windows, subprocess env can't handle unicode.
289
+ env [key ] = str (value ) if onWindows () else value
290
+ env ["HOME" ] = str (self .outdir ) if onWindows () else self .outdir
291
+ env ["TMPDIR" ] = str (self .tmpdir ) if onWindows () else self .tmpdir
292
+ if "PATH" not in env :
293
+ env ["PATH" ] = str (os .environ ["PATH" ]) if onWindows () else os .environ ["PATH" ]
294
+
295
+ stageFiles (self .pathmapper , ignoreWritable = True , symLink = True )
286
296
if self .generatemapper :
287
- stageFiles (self .generatemapper , os . symlink , ignoreWritable = self .inplace_update )
297
+ stageFiles (self .generatemapper , ignoreWritable = self .inplace_update , symLink = True )
288
298
relink_initialworkdir (self .generatemapper , inplace_update = self .inplace_update )
289
299
290
300
self ._execute ([], env , rm_tmpdir = rm_tmpdir , move_outputs = move_outputs )
@@ -306,25 +316,26 @@ def add_volumes(self, pathmapper, runtime, stage_output):
306
316
containertgt = vol .target
307
317
if vol .type in ("File" , "Directory" ):
308
318
if not vol .resolved .startswith ("_:" ):
309
- runtime .append (u"--volume=%s:%s:ro" % (vol .resolved , containertgt ))
319
+ runtime .append (u"--volume=%s:%s:ro" % (docker_windows_path_adjust ( vol .resolved ), docker_windows_path_adjust ( containertgt ) ))
310
320
elif vol .type == "WritableFile" :
311
321
if self .inplace_update :
312
- runtime .append (u"--volume=%s:%s:rw" % (vol .resolved , containertgt ))
322
+ runtime .append (u"--volume=%s:%s:rw" % (docker_windows_path_adjust ( vol .resolved ), docker_windows_path_adjust ( containertgt ) ))
313
323
else :
314
324
shutil .copy (vol .resolved , vol .target )
315
325
elif vol .type == "WritableDirectory" :
316
326
if vol .resolved .startswith ("_:" ):
317
327
os .makedirs (vol .target , 0o0755 )
318
328
else :
319
329
if self .inplace_update :
320
- runtime .append (u"--volume=%s:%s:rw" % (vol .resolved , containertgt ))
330
+ runtime .append (u"--volume=%s:%s:rw" % (docker_windows_path_adjust ( vol .resolved ), docker_windows_path_adjust ( containertgt ) ))
321
331
else :
322
332
shutil .copytree (vol .resolved , vol .target )
323
333
elif vol .type == "CreateFile" :
324
334
createtmp = os .path .join (host_outdir , os .path .basename (vol .target ))
325
335
with open (createtmp , "wb" ) as f :
326
336
f .write (vol .resolved .encode ("utf-8" ))
327
- runtime .append (u"--volume=%s:%s:ro" % (createtmp , vol .target ))
337
+ runtime .append (u"--volume=%s:%s:ro" % (docker_windows_path_adjust (createtmp ), docker_windows_path_adjust (vol .target )))
338
+
328
339
329
340
def run (self , pull_image = True , rm_container = True ,
330
341
rm_tmpdir = True , move_outputs = "move" , ** kwargs ):
@@ -361,14 +372,14 @@ def run(self, pull_image=True, rm_container=True,
361
372
362
373
runtime = [u"docker" , u"run" , u"-i" ]
363
374
364
- runtime .append (u"--volume=%s:%s:rw" % (os .path .realpath (self .outdir ), self .builder .outdir ))
365
- runtime .append (u"--volume=%s:%s:rw" % (os .path .realpath (self .tmpdir ), "/tmp" ))
375
+ runtime .append (u"--volume=%s:%s:rw" % (docker_windows_path_adjust ( os .path .realpath (self .outdir ) ), self .builder .outdir ))
376
+ runtime .append (u"--volume=%s:%s:rw" % (docker_windows_path_adjust ( os .path .realpath (self .tmpdir ) ), "/tmp" ))
366
377
367
378
self .add_volumes (self .pathmapper , runtime , False )
368
379
if self .generatemapper :
369
380
self .add_volumes (self .generatemapper , runtime , True )
370
381
371
- runtime .append (u"--workdir=%s" % (self .builder .outdir ))
382
+ runtime .append (u"--workdir=%s" % (docker_windows_path_adjust ( self .builder .outdir ) ))
372
383
runtime .append (u"--read-only=true" )
373
384
374
385
if kwargs .get ("custom_net" , None ) is not None :
@@ -379,9 +390,12 @@ def run(self, pull_image=True, rm_container=True,
379
390
if self .stdout :
380
391
runtime .append ("--log-driver=none" )
381
392
382
- euid = docker_vm_uid () or os .geteuid ()
393
+ if onWindows (): # windows os dont have getuid or geteuid functions
394
+ euid = docker_vm_uid ()
395
+ else :
396
+ euid = docker_vm_uid () or os .geteuid ()
383
397
384
- if kwargs .get ("no_match_user" , None ) is False :
398
+ if kwargs .get ("no_match_user" , None ) is False and euid is not None :
385
399
runtime .append (u"--user=%s" % (euid ))
386
400
387
401
if rm_container :
@@ -436,7 +450,7 @@ def _job_popen(
436
450
437
451
sp = subprocess .Popen (commands ,
438
452
shell = False ,
439
- close_fds = True ,
453
+ close_fds = not onWindows () ,
440
454
stdin = stdin ,
441
455
stdout = stdout ,
442
456
stderr = stderr ,
@@ -478,14 +492,14 @@ def _job_popen(
478
492
stderr_path = stderr_path ,
479
493
stdin_path = stdin_path ,
480
494
)
481
- with open (os .path .join (job_dir , "job.json" ), "w " ) as f :
495
+ with open (os .path .join (job_dir , "job.json" ), "wb " ) as f :
482
496
json .dump (job_description , f )
483
497
try :
484
498
job_script = os .path .join (job_dir , "run_job.bash" )
485
499
with open (job_script , "wb" ) as f :
486
500
f .write (job_script_contents .encode ('utf-8' ))
487
501
job_run = os .path .join (job_dir , "run_job.py" )
488
- with open (job_run , "w " ) as f :
502
+ with open (job_run , "wb " ) as f :
489
503
f .write (PYTHON_RUN_SCRIPT )
490
504
sp = subprocess .Popen (
491
505
["bash" , job_script .encode ("utf-8" )],
0 commit comments