]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Move hash_file to utils
authorSeptatrix <24257556+Septatrix@users.noreply.github.com>
Wed, 27 Mar 2024 19:35:07 +0000 (20:35 +0100)
committerSeptatrix <24257556+Septatrix@users.noreply.github.com>
Fri, 29 Mar 2024 18:14:43 +0000 (19:14 +0100)
mkosi/__init__.py
mkosi/util.py

index b747d4bcfcb6697bc4ad14f05fa099c6699e7322..590123ea3d8473d31a8c066f5ea928f51af91859 100644 (file)
@@ -20,7 +20,7 @@ import textwrap
 import uuid
 from collections.abc import Iterator, Mapping, Sequence
 from pathlib import Path
-from typing import Optional, TextIO, Union, cast
+from typing import Optional, Union, cast
 
 from mkosi.archive import extract_tar, make_cpio, make_tar
 from mkosi.burn import run_burn
@@ -75,6 +75,7 @@ from mkosi.util import (
     flock,
     flock_or_die,
     format_rlimit,
+    hash_file,
     make_executable,
     one_zero,
     parents_below,
@@ -2457,17 +2458,6 @@ def copy_initrd(context: Context) -> None:
         break
 
 
-def hash_file(of: TextIO, path: Path) -> None:
-    bs = 16 * 1024**2
-    h = hashlib.sha256()
-
-    with path.open("rb") as sf:
-        while (buf := sf.read(bs)):
-            h.update(buf)
-
-    of.write(h.hexdigest() + " *" + path.name + "\n")
-
-
 def calculate_sha256sum(context: Context) -> None:
     if not context.config.checksum:
         return
@@ -2478,7 +2468,7 @@ def calculate_sha256sum(context: Context) -> None:
     with complete_step("Calculating SHA256SUMS…"):
         with open(context.workspace / context.config.output_checksum, "w") as f:
             for p in context.staging.iterdir():
-                hash_file(f, p)
+                print(hash_file(p) + " *" + p.name, file=f)
 
         (context.workspace / context.config.output_checksum).rename(context.staging / context.config.output_checksum)
 
index 05c6c91d54d629cf7e46846c8936b2b9c75f22d8..b08c9221fe1f0657e54f1f770fd2925d7b16080f 100644 (file)
@@ -7,6 +7,7 @@ import enum
 import errno
 import fcntl
 import functools
+import hashlib
 import importlib
 import importlib.resources
 import itertools
@@ -298,3 +299,15 @@ def resource_path(mod: ModuleType) -> Iterator[Path]:
             p.parent.chmod(0o755)
 
         yield p
+
+
+def hash_file(path: Path) -> str:
+    # TODO Replace with hashlib.file_digest after dropping support for Python 3.10.
+    bs = 16 * 1024**2
+    h = hashlib.sha256()
+
+    with path.open("rb") as sf:
+        while (buf := sf.read(bs)):
+            h.update(buf)
+
+    return h.hexdigest()