-
Notifications
You must be signed in to change notification settings - Fork 21
/
run.py
executable file
·124 lines (111 loc) · 4.11 KB
/
run.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
"""
Usage: run.py [options]
run.py [options] [--] execute <commands>...
Options:
-b --base DISTRO Use DISTRO as base system [Default: all]
--no-cache Do not use cache when building the image [Default: False]
--rm Automatically remove the container when it exits [Default: True]
--display DISPLAY Change the DISPLAY environment variable passed to the container [Default: :0]
--vnc Enable port forwarding and disable X11 socket sharing
--noninteractive Disable TTY allocation and stdin (useful for CI test)
--qt PATH Set the PATH to your a specified Qt installation (mounted as /qt) [Default: False]
-h --help Display this message
"""
from docopt import docopt
import os
import sys
import re
import subprocess
MNT_DIR = os.path.expanduser('~') + '/kdebuild'
KDESRC_BUILD_DATA_FILE_NAME = '.kdesrc-build-data'
__SCRIPT_CUR_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
def check_templates(sel_distro):
regexp_str = ''
avail_templates = []
if sel_distro == 'all':
regexp_str = '^Dockerfile-(.+)$'
else:
regexp_str = '^Dockerfile-({})$'.format(sel_distro)
reg = re.compile(regexp_str)
for i in os.listdir(__SCRIPT_CUR_DIR):
if reg.match(i):
avail_templates.append( reg.match(i).group(1) )
return avail_templates
def check_mnt_point(template):
print("Checking mount point for {}".format(template))
path = '{}/{}'.format(MNT_DIR, template)
os.makedirs(path, exist_ok=True)
host_kdesrc_build_data_file = '{}/{}'.format(path, KDESRC_BUILD_DATA_FILE_NAME)
if not os.path.exists(host_kdesrc_build_data_file):
with open(host_kdesrc_build_data_file, 'w'):
pass
def update_image(template, cache_enabled):
print("Updating image for " + template)
subprocess.call(['docker',
'build',
'--no-cache={}'.format(str(cache_enabled)),
'-t',
'{}-kdedev'.format(template),
'-f', 'Dockerfile-{}'.format(template),
'.'
], cwd=__SCRIPT_CUR_DIR)
def run_kdesrc_build(template, auto_rm_enabled, display, vnc_enabled, qt_dir,
noninteractive, commands):
host_mnt_dir = '{}/{}'.format(MNT_DIR, template)
# vnc vs x11socket
xsocket = ''
vnc = ''
if vnc_enabled:
vnc = ['-p', '127.0.0.1:5900:5900']
else:
xsocket = [ '-v', '/tmp/.X11-unix:/tmp/.X11-unix:ro' ]
# qt volume
qt_mount = ''
if qt_dir != 'False':
qt_mount = [ '-v', '{}:/qt'.format(qt_dir) ]
interactive = '-it'
if noninteractive:
interactive = ''
# create subp_cmd
subp_cmd = [
'docker',
'run',
interactive,
'--privileged',
'--rm={}'.format(str(auto_rm_enabled)),
'-e', 'DISPLAY={}'.format(display),
]
subp_cmd.extend([
'-v', '{}:/work'.format(host_mnt_dir),
'-v', __SCRIPT_CUR_DIR + '/kdesrc-buildrc:/home/kdedev/.kdesrc-buildrc',
'-v', host_mnt_dir + '/' + KDESRC_BUILD_DATA_FILE_NAME + ':/home/kdedev/' + KDESRC_BUILD_DATA_FILE_NAME,
])
if 'fedora' == template:
subp_cmd.extend(['-v', __SCRIPT_CUR_DIR + '/bash_profile-fedora:/home/kdedev/.bash_profile'])
else:
subp_cmd.extend(['-v', __SCRIPT_CUR_DIR + '/bashrc:/home/kdedev/.bashrc'])
subp_cmd.extend(xsocket)
subp_cmd.extend(vnc)
subp_cmd.extend(qt_mount)
subp_cmd.extend([
'{}-kdedev'.format(template),
])
# commands
if commands:
subp_cmd.extend(commands)
# run
return subprocess.call(subp_cmd)
if __name__ == '__main__':
args = docopt(__doc__)
templates = check_templates(args['--base'])
os.makedirs(MNT_DIR, exist_ok=True)
commands = args['<commands>'] if args['<commands>'] else None
for i in templates:
print(i)
check_mnt_point(i)
update_image(i, args['--no-cache'])
exit_code = run_kdesrc_build(i, args['--rm'], args['--display'], args['--vnc'], args['--qt'],
args['--noninteractive'], commands)
if exit_code != 0:
sys.exit(exit_code)