From 88cd646c98e5ed91384f4aa887fad74d7d864e4b Mon Sep 17 00:00:00 2001 From: Alina Kladieva Date: Sun, 23 Feb 2025 13:01:07 +0100 Subject: [PATCH] [GHA] Store sha-256 checksums for artifacts Signed-off-by: Alina Kladieva --- .github/actions/create_manifest/test.py | 1 + .../store_artifacts/store_artifacts.py | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 .github/actions/create_manifest/test.py diff --git a/.github/actions/create_manifest/test.py b/.github/actions/create_manifest/test.py new file mode 100644 index 00000000000000..97e80857e3f48b --- /dev/null +++ b/.github/actions/create_manifest/test.py @@ -0,0 +1 @@ +from unittest.mock import patch, mock_open diff --git a/.github/actions/store_artifacts/store_artifacts.py b/.github/actions/store_artifacts/store_artifacts.py index 7b761d042b6aae..ff796bee4f5ccf 100644 --- a/.github/actions/store_artifacts/store_artifacts.py +++ b/.github/actions/store_artifacts/store_artifacts.py @@ -4,6 +4,7 @@ from __future__ import annotations import argparse +import hashlib import logging import os import sys @@ -62,6 +63,33 @@ def rotate_dir(directory: Path) -> bool: return True +def generate_sha256sum(file_path: str | Path) -> str: + """ + Generates the SHA-256 checksum for the given file. + + :param file_path: Path to the file + :return: SHA-256 checksum as a hexadecimal string + """ + sha256 = hashlib.sha256() + with open(file_path, 'rb') as file: + for chunk in iter(lambda: file.read(4096), b''): + sha256.update(chunk) + return sha256.hexdigest() + + +def store_checksums(artifact_path: Path) -> None: + """ + Generates SHA-256 checksums for a given artifact and stores them in separate files. + + :param artifact_path: Path to either a single artifact file or the directory with files to generate checksums for + """ + files = [path for path in artifact_path.rglob('*') if path.is_file] if artifact_path.is_dir() else [artifact_path] + for file in files: + hashsum_filepath = file.with_suffix('.sha256') + with open(hashsum_filepath, 'w') as hashsum_file: + hashsum_file.write(generate_sha256sum(file)) + + def main(): action_utils.init_logger() logger = logging.getLogger(__name__) @@ -79,6 +107,14 @@ def main(): error_found = False for artifact in args.artifacts.split(): artifact_path = Path(artifact) + + logger.debug(f"Calculating checksum for {artifact_path}") + try: + store_checksums(artifact_path) + except Exception as e: + logger.error(f'Failed to calculate checksum for {artifact}: {e}') + error_found = True + logger.debug(f"Copying {artifact_path} to {storage / artifact_path.name}") try: with preserve_stats_context(): @@ -87,6 +123,9 @@ def main(): else: storage.mkdir(parents=True, exist_ok=True) shutil.copy2(artifact_path, storage / artifact_path.name) + if artifact_path.with_suffix('.sha256').exists(): + shutil.copy2(artifact_path.with_suffix('.sha256'), + storage / artifact_path.with_suffix('.sha256').name) except Exception as e: logger.error(f'Failed to copy {artifact}: {e}') error_found = True