This repository has been archived by the owner on Mar 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
/
update-dockerfile-includes
executable file
·69 lines (60 loc) · 2.01 KB
/
update-dockerfile-includes
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
#!/usr/bin/python
import os
import re
from subprocess import check_call as sh
import sys
ROOT = os.path.dirname(os.path.abspath(__file__))
STACKS = os.path.join(ROOT, 'docker-stacks')
STACK_URL = 'https://github.com/jupyter/docker-stacks'
DOCKERFILE = os.path.join(ROOT, 'Dockerfile')
def clone_stacks(ref):
if not os.path.exists(STACKS):
sh(['git', 'clone', STACK_URL, STACKS], cwd=ROOT)
else:
sh(['git', 'fetch', 'origin'], cwd=STACKS)
sh(['git', 'checkout', ref], cwd=STACKS)
def read_stack(name):
"""Yield lines from a docker-stack Dockerfile.
Comments-out FROM, MAINTAINER
"""
dockerfile = os.path.join(STACKS, name, 'Dockerfile')
with open(dockerfile) as f:
# read up to FROM
for line in f:
if line.startswith(('FROM', 'MAINTAINER')):
line = '# ' + line
yield line
begin_include_re = re.compile(r'\s*#\s*BEGININCLUDE\s+jupyter/(.*)')
def update_dockerfile(ref):
clone_stacks(ref)
lines = []
with open(DOCKERFILE) as f:
line = True
while line:
line = f.readline()
if line.startswith('FROM '):
break
lines.append(line)
# FROM:
from_, tag = line.split(':')
lines.append('%s:%s\n' % (from_, ref))
while line:
line = f.readline()
lines.append(line)
include_match = begin_include_re.match(line)
if include_match:
# read include
name = include_match.group(1)
print("including %s:%s" % (name, ref))
lines.extend(read_stack(name))
# strip between begin/end
endline = ''
while 'ENDINCLUDE' not in endline:
endline = f.readline()
lines.append(endline)
# write out result
with open(DOCKERFILE, 'w') as f:
for line in lines:
f.write(line)
if __name__ == '__main__':
update_dockerfile(sys.argv[1])