Commit d4bf4f3a authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Cache built Docker images

parent 06aaaa8d
Loading
Loading
Loading
Loading
Loading
+16 −2
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ Commands for managing Docker for fixtures
from __future__ import annotations

import codecs
import hashlib
import ipaddress
import json
import logging
@@ -137,6 +138,14 @@ def _get_docker_host_ip() -> IPAddress:
	return ipaddress.ip_address(purl.hostname)


def _hash_cmd(cmd: Arguments) -> str:
	hash = hashlib.sha256()
	for arg in cmd:
		arg = fspath(arg)
		hash.update(arg.encode("utf-8") if isinstance(arg, str) else arg)
	return f"@cmd:{hash.hexdigest()}"


class IPv4Address(ipaddress.IPv4Address):
	"""
	Subclass of IPv4Address that handle's docker idiosyncratic tendency to add a mask suffix
@@ -197,8 +206,13 @@ class Image:
			b"build", context, f"--target={target}",
			*(f"--build-arg={arg}={val}" for arg, val in build_args.items() if val is not None),
		]
		key = _hash_cmd(cmd)
		try:
			iid = cls._cache[key]
		except KeyError:
			docker(*cmd, DOCKER_BUILDKIT='1')
			iid = ShaID(docker_output(*cmd, '-q', DOCKER_BUILDKIT='1'))
			cls._cache[key] = iid
		return cls(iid)

	@classmethod