-
Notifications
You must be signed in to change notification settings - Fork 6
/
docker_process.py
62 lines (49 loc) · 2.04 KB
/
docker_process.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
from qt_wrapper import QtCore
import random
import string
import contextlib
class DockerProcess(QtCore.QProcess):
def __init__(self):
super(DockerProcess, self).__init__()
self._name = get_random_name()
def start(self, working_dir, command):
with fixed_macos_path():
full_command = docker_command(working_dir, command, self._name)
super(DockerProcess, self).start(full_command)
def terminate(self):
kill_process = QtCore.QProcess()
kill_process.start("docker kill {}".format(self._name))
kill_process.waitForFinished()
super(DockerProcess, self).terminate()
def get_random_name():
return ''.join(random.choice(string.ascii_lowercase) for _ in range(10))
def docker_command(working_dir, command, name):
if os.name == 'nt':
if os.environ.get('DOCKER_TOOLBOX_INSTALL_PATH'):
working_dir = parse_win_path(working_dir)
return "docker run --name {name} --rm -v \"{dir}\":/tmp " \
"kzinovjev/enlighten2 " \
"/bin/bash -lc \"{command}\"".format(name=name,
dir=working_dir,
command=command)
return "docker run --name {name} --rm -v \"{dir}\":/tmp -u {uid}:{gid} " \
"kzinovjev/enlighten2 " \
"/bin/bash -lc \"{command}\"".format(name=name,
dir=working_dir,
uid=os.geteuid(),
gid=os.getegid(),
command=command)
def parse_win_path(path):
drive = path[0]
return path.replace('{}:/'.format(drive),
'//{}/'.format(drive.lower()))
@contextlib.contextmanager
def fixed_macos_path():
if os.name == 'nt':
yield
return
path = os.environ['PATH']
os.environ['PATH'] = path + ':/usr/local/bin:/opt/local/bin'
yield
os.environ['PATH'] = path