-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_dockerfiles_for_docker_hub.py
101 lines (80 loc) · 2.94 KB
/
gen_dockerfiles_for_docker_hub.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
import os
dockerhubTargets = ['dev', 'runtime', 'example']
class BuildTarget:
def __init__(self, name, depends):
self.name = name
self.depends = [depends]
self.block = list()
def get_dependencies(targets_available, depends):
col = depends
for d in depends:
tad = []
for ta in targets_available:
if d == ta.name:
tad.extend(ta.depends)
break
if tad:
col.extend(get_dependencies(targets_available, tad))
col = set(col)
return col
with open('Dockerfile') as f:
dockerfileLines = f.readlines()
dockerfileLines = map(lambda l: l.rstrip(), dockerfileLines)
# extract common head from Dockerfile
head = []
buildTarget = BuildTarget(None, None)
for line in dockerfileLines:
if 'NPROC' in line:
line = line.split('=')[0] + '=1'
if '###' in line:
break
head.append(line)
# extract targets from Dockerfile
targets = []
for line in dockerfileLines:
if 'FROM' in line:
if buildTarget.name is not None:
targets.append(buildTarget)
elements = line.split()
buildTarget = BuildTarget(elements[3], elements[1])
if '--from=' in line:
tmp = line.strip()
tmp = tmp.split()
for t in tmp:
if '--from=' in t:
buildTarget.depends.append(t.split('=')[1])
buildTarget.block.append(line)
targets.append(buildTarget)
# for target in targets:
# print('Target: %s' % target.name)
# target.depends = set(target.depends)
# target.depends = list(filter(lambda s: '$' not in s, target.depends))
# print('depends on: %s' % target.depends)
# print(target.block)
# print('------------')
def wlines(file, listoflines):
for line in listoflines:
if '#####' in line:
continue
file.write('%s\n' % line)
if not os.path.exists('./dockerhub'):
os.makedirs('./dockerhub')
for target in dockerhubTargets:
dep_list = get_dependencies(targets, [target])
print('Target: %s' % target)
print('Dependencies:\n\t%s' % dep_list)
with open(os.path.join('./dockerhub', "Dockerfile.%s" % target), "w") as f:
f.writelines([
'################################################################################\n',
'## This Dockerfile is an autogenerated extraction of ../Dockerfile. ##\n',
'################################################################################\n',
'## DO NOT MODIFY. ##\n',
'################################################################################\n',
'\n'
])
wlines(f, head)
for tgt in targets:
if tgt.name in dep_list:
f.write('########################################'
'########################################\n')
wlines(f, tgt.block)