From b6c858f9711fbd0134314fcdba2a46e9ef2852a3 Mon Sep 17 00:00:00 2001 From: Dom Sekotill Date: Thu, 19 May 2022 19:48:28 +0100 Subject: [PATCH] Add an Image tag cache --- behave_utils/docker.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/behave_utils/docker.py b/behave_utils/docker.py index 5873d50..72eb7a1 100644 --- a/behave_utils/docker.py +++ b/behave_utils/docker.py @@ -174,6 +174,8 @@ class Image(Item): Docker image items """ + _cache = dict[str, str]() + T = TypeVar('T', bound='Image') def __init__(self, iid: str): @@ -200,10 +202,23 @@ class Image(Item): """ Pull an image from a registry """ - docker(b"pull", repository) - iid = Item(repository).inspect().path('$.Id', str) + try: + iid = cls._cache[repository] + except KeyError: + docker(b"pull", repository) + iid = cls._process_image(repository) return cls(iid) + @classmethod + def _process_image(cls, reference: str) -> str: + report = Item(reference).inspect() + iid = report.path("$.Id", str) + cls._cache.update( + ((tag, iid) for tag in report.path("$.RepoTags", list[str])), + reference=iid, + ) + return iid + def get_id(self) -> str: """ Return an identifier for the Docker Image -- GitLab