hafnia 0.1.26__py3-none-any.whl → 0.1.27__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import json
2
2
  import os
3
+ import re
3
4
  import subprocess
4
- import tempfile
5
5
  import zipfile
6
6
  from hashlib import sha256
7
7
  from pathlib import Path
@@ -58,7 +58,7 @@ def buildx_available() -> bool:
58
58
  return False
59
59
 
60
60
 
61
- def build_dockerfile(dockerfile: str, docker_context: str, docker_tag: str, meta_file: str) -> None:
61
+ def build_dockerfile(dockerfile: str, docker_context: str, docker_tag: str) -> None:
62
62
  """
63
63
  Build a Docker image using the provided Dockerfile.
64
64
 
@@ -73,12 +73,12 @@ def build_dockerfile(dockerfile: str, docker_context: str, docker_tag: str, meta
73
73
 
74
74
  cmd = ["docker", "build", "--platform", "linux/amd64", "-t", docker_tag, "-f", dockerfile]
75
75
 
76
- remote_cache = os.getenv("REMOTE_CACHE_REPO")
76
+ remote_cache = os.getenv("EXPERIMENT_CACHE_ECR")
77
77
  cloud_mode = os.getenv("HAFNIA_CLOUD", "false").lower() in ["true", "1", "yes"]
78
78
 
79
79
  if buildx_available():
80
80
  cmd.insert(1, "buildx")
81
- cmd += ["--build-arg", "BUILDKIT_INLINE_CACHE=1", "--metadata-file", meta_file]
81
+ cmd += ["--build-arg", "BUILDKIT_INLINE_CACHE=1"]
82
82
  if cloud_mode:
83
83
  cmd += ["--push"]
84
84
  if remote_cache:
@@ -91,11 +91,27 @@ def build_dockerfile(dockerfile: str, docker_context: str, docker_tag: str, meta
91
91
  cmd.append(docker_context)
92
92
  sys_logger.debug("Build cmd: `{}`".format(" ".join(cmd)))
93
93
  sys_logger.info(f"Building and pushing Docker image with BuildKit (buildx); cache repo: {remote_cache or 'none'}")
94
+ result = None
95
+ output = ""
96
+ errors = []
94
97
  try:
95
- subprocess.run(cmd, check=True)
98
+ result = subprocess.run(cmd, check=True, capture_output=True, text=True)
99
+ output = (result.stdout or "") + (result.stderr or "")
96
100
  except subprocess.CalledProcessError as e:
97
- sys_logger.error(f"Docker build failed: {e}")
98
- raise RuntimeError(f"Docker build failed: {e}")
101
+ output = (e.stdout or "") + (e.stderr or "")
102
+ error_pattern = r"ERROR: (.+?)(?:\n|$)"
103
+ errors = re.findall(error_pattern, output)
104
+ if not errors:
105
+ raise RuntimeError(f"Docker build failed: {output}")
106
+ if re.search(r"image tag '([^']+)' already exists", errors[-1]):
107
+ sys_logger.warning("Image {} already exists in the registry.".format(docker_tag.rsplit("/")[-1]))
108
+ return
109
+ raise RuntimeError(f"Docker build failed: {output}")
110
+ finally:
111
+ stage_pattern = r"^.*\[\d+/\d+\][^\n]*"
112
+ stages = re.findall(stage_pattern, output, re.MULTILINE)
113
+ user_logger.info("\n".join(stages))
114
+ sys_logger.debug(output)
99
115
 
100
116
 
101
117
  def check_registry(docker_image: str) -> Optional[str]:
@@ -127,18 +143,8 @@ def build_image(metadata: Dict, registry_repo: str, state_file: str = "state.jso
127
143
  docker_image = f"{registry_repo}:{metadata['digest']}"
128
144
  image_exists = check_registry(docker_image) is not None
129
145
  if image_exists:
130
- sys_logger.info(f"Tag already in ECR – skipping build of {docker_image}.")
146
+ sys_logger.info("Image {} already exists in the registry.".format(docker_image.rsplit("/")[-1]))
131
147
  else:
132
- with tempfile.NamedTemporaryFile() as meta_tmp:
133
- meta_file = meta_tmp.name
134
- build_dockerfile(
135
- metadata["dockerfile"], Path(metadata["dockerfile"]).parent.as_posix(), docker_image, meta_file
136
- )
137
- with open(meta_file) as m:
138
- try:
139
- build_meta = json.load(m)
140
- metadata["local_digest"] = build_meta["containerimage.digest"]
141
- except Exception:
142
- metadata["local_digest"] = ""
148
+ build_dockerfile(metadata["dockerfile"], Path(metadata["dockerfile"]).parent.as_posix(), docker_image)
143
149
  metadata.update({"image_tag": docker_image, "image_exists": image_exists})
144
150
  Path(state_file).write_text(json.dumps(metadata, indent=2))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hafnia
3
- Version: 0.1.26
3
+ Version: 0.1.27
4
4
  Summary: Python SDK for communication with Hafnia platform.
5
5
  Author-email: Milestone Systems <hafniaplatform@milestone.dk>
6
6
  License-File: LICENSE
@@ -17,11 +17,11 @@ hafnia/data/factory.py,sha256=4fZDkWNyOK1QNCmsxsXfSztPJkJW_HBIa_PTdGCYHCM,2551
17
17
  hafnia/experiment/__init__.py,sha256=OEFE6HqhO5zcTCLZcPcPVjIg7wMFFnvZ1uOtAVhRz7M,85
18
18
  hafnia/experiment/hafnia_logger.py,sha256=usL5pl7XLJP-g1vZrwvbky5YiD6Bg-rOODYYAX5z43I,6830
19
19
  hafnia/platform/__init__.py,sha256=I-VIVXDxwBAUzxx8Zx0g_wykyDdFGTsjb_mYLmvxk2Y,443
20
- hafnia/platform/builder.py,sha256=OFPnOjE3bAbWjUgYErXtffDKTiW_9ol95eVzKqL27WM,5433
20
+ hafnia/platform/builder.py,sha256=_g8ykQWETz5Y4Np9QU1a6wIzbbJwXCkbiOCA6JcF5Rc,5742
21
21
  hafnia/platform/download.py,sha256=t055axPNHlXTYCQgZHOS2YMQt1I2_bc4G8dltsOKttY,4760
22
22
  hafnia/platform/experiment.py,sha256=-nAfTmn1c8sE6pHDCTNZvWDTopkXndarJAPIGvsnk60,2389
23
- hafnia-0.1.26.dist-info/METADATA,sha256=Lds8gx_ffd8_l9kByvK_e-HPFehSSUv8E_85d8ZelSE,14990
24
- hafnia-0.1.26.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
25
- hafnia-0.1.26.dist-info/entry_points.txt,sha256=FCJVIQ8GP2VE9I3eeGVF5eLxVDNW_01pOJCpG_CGnMM,45
26
- hafnia-0.1.26.dist-info/licenses/LICENSE,sha256=wLZw1B7_mod_CO1H8LXqQgfqlWD6QceJR8--LJYRZGE,1078
27
- hafnia-0.1.26.dist-info/RECORD,,
23
+ hafnia-0.1.27.dist-info/METADATA,sha256=zClGBhUHMs5pW19BX5avM5tBWvF5KAQxmAvEwRKNeSw,14990
24
+ hafnia-0.1.27.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
25
+ hafnia-0.1.27.dist-info/entry_points.txt,sha256=FCJVIQ8GP2VE9I3eeGVF5eLxVDNW_01pOJCpG_CGnMM,45
26
+ hafnia-0.1.27.dist-info/licenses/LICENSE,sha256=wLZw1B7_mod_CO1H8LXqQgfqlWD6QceJR8--LJYRZGE,1078
27
+ hafnia-0.1.27.dist-info/RECORD,,