Implements full path for hash checks of binaries
[releng-anteater.git] / utils / generate-sha256.py
1 ##############################################################################
2 # Copyright (c) 2017 Luke Hinds <lhinds@redhat.com>, Red Hat
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 # python generate-sha256.py --project /home/user/opnfv/infra
11 # output made to working directory, file `output.yaml`
12
13 import os
14 import sys
15 import hashlib
16 import argparse
17 from binaryornot.check import is_binary
18
19 hasher = hashlib.sha256()
20 parser = argparse.ArgumentParser()
21
22 parser.add_argument('--project', help="Full path to project folder",
23                     required=True)
24 args = parser.parse_args()
25 ignore_dirs = ['.git']
26 sys.stdout = open('output.yaml', 'w')
27
28 print("binaries:")
29 for root, dirs, files in os.walk(args.project):
30     dirs[:] = [d for d in dirs if d not in ignore_dirs]
31     for file in files:
32         full_path = os.path.join(root, file)
33         if is_binary(full_path):
34             with open(full_path, 'rb') as afile:
35                 buf = afile.read()
36                 hasher.update(buf)
37                 split_path = full_path.split(args.project + '/', 1)[-1]
38                 print("  {}:".format(split_path))
39                 sum = hasher.hexdigest()
40                 print("    - {}".format(sum))