Skip to content

Instantly share code, notes, and snippets.

@tcely
Created April 2, 2025 09:50
Show Gist options
  • Save tcely/8a712fdce593a04bc4d2ebf8a2d01fd7 to your computer and use it in GitHub Desktop.
Save tcely/8a712fdce593a04bc4d2ebf8a2d01fd7 to your computer and use it in GitHub Desktop.
Creating a sums file from the GitHub workflow
#!/usr/bin/env python3
import hashlib
import io
import os
import pathlib
import sys
def sum_file(hasher, file_path):
assert callable(hasher), 'the hasher must be callable'
hasher = hasher()
file_path = pathlib.Path(file_path)
buffer_size = io.DEFAULT_BUFFER_SIZE
with file_path.open('rb') as f:
data = f.read(buffer_size)
while len(data) > 0:
hasher.update(data)
data = f.read(buffer_size)
return hasher.hexdigest()
def main(args):
sum_type = args[1]
if sum_type not in hashlib.algorithms_available:
raise ValueError('the hash algorithm is not available')
func = getattr(hashlib, sum_type)
assert callable(func), 'the sum_type attribute was not callable'
for arg in args[2:]:
path = pathlib.Path(arg)
if path.is_file():
file = path.resolve(strict=True)
digest = sum_file(func, file)
line = f'{digest} *{file.name}'
print(line, flush=True)
if '__main__' == __name__:
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment