Skip to content

Commit

Permalink
[GHA] Store sha-256 checksums for artifacts
Browse files Browse the repository at this point in the history
Signed-off-by: Alina Kladieva <alina.kladieva@intel.com>
  • Loading branch information
akladiev committed Feb 23, 2025
1 parent f77ef0f commit 53bca78
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .github/actions/store_artifacts/store_artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import annotations

import argparse
import hashlib
import logging
import os
import sys
Expand Down Expand Up @@ -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__)
Expand All @@ -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():
Expand All @@ -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
Expand Down

0 comments on commit 53bca78

Please sign in to comment.