/
docker_cli.py
executable file
·36 lines (27 loc) · 1.51 KB
/
docker_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
from subprocess import call
from deepneuro.utilities.util import quotes
def nvidia_docker_wrapper(command, cli_args=None, filename_args=None, interactive=False, docker_container='deepneuro'):
if filename_args is not None:
filename_args = [arg for arg in filename_args if cli_args[arg] is not None]
mounted_dir = os.path.abspath(os.path.dirname(os.path.commonprefix([cli_args[arg] for arg in filename_args])))
for arg in filename_args:
after_mounted = os.path.abspath(cli_args[arg]).split(mounted_dir, 1)[1].lstrip(os.sep)
cli_args[arg] = quotes(os.path.join('/INPUT_DATA', after_mounted))
else:
pass # TODO: Default behavior when mounted directory not needed.
if interactive:
docker_command = ['nvidia-docker', 'run', '-it', '-v', mounted_dir + ':/INPUT_DATA', docker_container, 'bash']
else:
docker_command = ['nvidia-docker', 'run', '--rm', '-v', mounted_dir + ':/INPUT_DATA', docker_container] + command
# This presumes everything is an optional arg, which is wrong.
# Rewrite this to accept command line strings, instead of Python variables.
# Would solve problem..
for arg in cli_args:
if cli_args[arg] is True:
docker_command += ['-' + str(arg)]
elif not cli_args[arg] or cli_args[arg] is None:
continue
else:
docker_command += ['-' + str(arg) + ' ' + cli_args[arg]]
call(' '.join(docker_command), shell=True)