Skip to content

Commit 3e4989c

Browse files
committed
unix: write out and depend on expanded Dockerfile
Previously, if we changed an included Dockerfile, derived Dockerfile and their images wouldn't be invalidated by make dependencies. We refactor the code to always write out the expanded Dockerfile so any content changes result in image invalidation.
1 parent 59926c5 commit 3e4989c

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

cpython-unix/Makefile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ OUTDIR := $(ROOT)/build
55
BUILD := $(HERE)/build.py
66
NULL :=
77

8-
COMMON_DEPENDS := \
9-
# $(BUILD) \
10-
$(NULL)
11-
128
ifndef PYBUILD_TARGET_TRIPLE
139
$(error PYBUILD_TARGET_TRIPLE not defined)
1410
endif
@@ -49,6 +45,9 @@ $(shell $(RUN_BUILD) placeholder_archive makefiles)
4945
include $(OUTDIR)/Makefile.$(HOST_PLATFORM).$(TARGET_TRIPLE).$(PYBUILD_PYTHON_MAJOR_VERSION)
5046
include $(OUTDIR)/versions/VERSION.*
5147

48+
# Always write out expanded Dockerfiles.
49+
$(shell $(RUN_BUILD) placeholder_archive dockerfiles)
50+
5251
BASE_TOOLCHAIN_DEPENDS := \
5352
$(if $(NEED_BINUTILS),$(OUTDIR)/binutils-$(BINUTILS_VERSION)-$(HOST_PLATFORM).tar) \
5453
$(if $(NEED_GCC),$(OUTDIR)/gcc-$(GCC_VERSION)-$(HOST_PLATFORM).tar) \
@@ -68,7 +67,7 @@ PYTHON_DEP_DEPENDS := \
6867
default: $(OUTDIR)/cpython-$(PYBUILD_PYTHON_VERSION)-$(PACKAGE_SUFFIX).tar
6968

7069
ifndef PYBUILD_NO_DOCKER
71-
$(OUTDIR)/image-%.tar: $(HERE)/%.Dockerfile $(COMMON_DEPENDS)
70+
$(OUTDIR)/image-%.tar: $(OUTDIR)/%.Dockerfile
7271
$(RUN_BUILD) --toolchain image-$*
7372
endif
7473

cpython-unix/build.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
parse_setup_line,
2323
STDLIB_TEST_PACKAGES,
2424
)
25-
from pythonbuild.docker import build_docker_image, get_image
25+
from pythonbuild.docker import build_docker_image, get_image, write_dockerfiles
2626
from pythonbuild.downloads import DOWNLOADS
2727
from pythonbuild.logging import log, set_logger
2828
from pythonbuild.utils import (
@@ -959,7 +959,9 @@ def main():
959959

960960
settings = get_target_settings(TARGETS_CONFIG, target_triple)
961961

962-
if args.action == "makefiles":
962+
if args.action == "dockerfiles":
963+
log_name = "dockerfiles"
964+
elif args.action == "makefiles":
963965
log_name = "makefiles"
964966
elif args.action.startswith("image-"):
965967
log_name = "image-%s" % action
@@ -978,12 +980,19 @@ def main():
978980

979981
with log_path.open("wb") as log_fh:
980982
set_logger(action, log_fh)
981-
if action == "makefiles":
983+
if action == "dockerfiles":
984+
write_dockerfiles(SUPPORT, BUILD)
985+
elif action == "makefiles":
982986
write_triples_makefiles(get_targets(TARGETS_CONFIG), BUILD, SUPPORT)
983987
write_package_versions(BUILD / "versions")
984988

985989
elif action.startswith("image-"):
986-
build_docker_image(client, ROOT, BUILD, action[6:])
990+
image_name = action[6:]
991+
image_path = BUILD / ("%s.Dockerfile" % image_name)
992+
with image_path.open("rb") as fh:
993+
image_data = fh.read()
994+
995+
build_docker_image(client, image_data, BUILD, image_name)
987996

988997
elif action == "binutils":
989998
build_binutils(client, get_image(client, ROOT, BUILD, "gcc"), host_platform)

pythonbuild/docker.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,26 @@
1313
import jinja2
1414

1515
from .logging import log, log_raw
16+
from .utils import write_if_different
1617

1718

18-
def build_docker_image(client, source_dir: pathlib.Path, image_dir: pathlib.Path, name):
19-
image_path = image_dir / ("image-%s" % name)
19+
def write_dockerfiles(source_dir: pathlib.Path, dest_dir: pathlib.Path):
20+
env = jinja2.Environment(loader=jinja2.FileSystemLoader(str(source_dir)))
2021

21-
env = jinja2.Environment(
22-
loader=jinja2.FileSystemLoader(str(source_dir / "cpython-unix"))
23-
)
22+
for f in os.listdir(source_dir):
23+
if not f.endswith(".Dockerfile"):
24+
continue
2425

25-
tmpl = env.get_template("%s.Dockerfile" % name)
26-
data = tmpl.render()
26+
tmpl = env.get_template(f)
27+
data = tmpl.render()
2728

28-
return ensure_docker_image(
29-
client, io.BytesIO(data.encode("utf")), image_path=image_path
30-
)
29+
write_if_different(dest_dir / f, data.encode("utf-8"))
30+
31+
32+
def build_docker_image(client, image_data: bytes, image_dir: pathlib.Path, name):
33+
image_path = image_dir / ("image-%s" % name)
34+
35+
return ensure_docker_image(client, io.BytesIO(image_data), image_path=image_path)
3136

3237

3338
def ensure_docker_image(client, fh, image_path=None):

0 commit comments

Comments
 (0)