From 6a2ac465896ce6f980f0f3d374d6d401b8acb900 Mon Sep 17 00:00:00 2001 From: Kshitij Sisodia Date: Tue, 1 Mar 2022 17:36:06 +0000 Subject: MLECO-2881: Using MD5 sum for downloaded resources check. Removing git as a requirement for the checks performed for downloaded resources. Another minor change is renaming of RTE_components.h to RTE_Components.h as per the convention. Change-Id: If93f80f2f5dfa6a3f143259904c33b3b6d3a6e7c Signed-off-by: Kshitij Sisodia --- scripts/py/check_update_resources_downloaded.py | 60 +++++++++++++++---------- 1 file changed, 37 insertions(+), 23 deletions(-) (limited to 'scripts/py/check_update_resources_downloaded.py') diff --git a/scripts/py/check_update_resources_downloaded.py b/scripts/py/check_update_resources_downloaded.py index c016665..44e9bd9 100644 --- a/scripts/py/check_update_resources_downloaded.py +++ b/scripts/py/check_update_resources_downloaded.py @@ -15,11 +15,30 @@ # limitations under the License. import json import os -import subprocess import sys +import hashlib from argparse import ArgumentParser +def get_md5sum_for_file(filepath: str) -> str: + """ + Function to calculate md5sum for contents of a given file. + + Parameters: + ---------- + filepath (string): Path to the required file. + + Returns: + ------- + Hex digest represented as string. + """ + md5_sum = hashlib.md5() + with open(filepath, mode='rb') as f: + buf = f.read() + md5_sum.update(buf) + return md5_sum.hexdigest() + + def check_update_resources_downloaded( resource_downloaded_dir: str, set_up_script_path: str ): @@ -40,20 +59,25 @@ def check_update_resources_downloaded( with open(metadata_file_path) as metadata_json: metadata_dict = json.load(metadata_json) - set_up_script_hash = metadata_dict["set_up_script_hash"] - command = f"git log -1 --pretty=tformat:%H {set_up_script_path}" + md5_key = 'set_up_script_md5sum' + set_up_script_md5sum_metadata = '' + + if md5_key in metadata_dict.keys(): + set_up_script_md5sum_metadata = metadata_dict["set_up_script_md5sum"] - proc = subprocess.run( - command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True - ) - git_commit_hash = proc.stdout.decode("utf-8").strip("\n") - proc.check_returncode() + set_up_script_md5sum_current = get_md5sum_for_file(set_up_script_path) - if set_up_script_hash == git_commit_hash: + if set_up_script_md5sum_current == set_up_script_md5sum_metadata: return 0 + # Return code 1 if the resources need to be refreshed. + print('Error: hash mismatch!') + print(f'Metadata: {set_up_script_md5sum_metadata}') + print(f'Current : {set_up_script_md5sum_current}') return 1 + # Return error code 2 if the file doesn't exists. + print(f'Error: could not find {metadata_file_path}') return 2 @@ -75,18 +99,8 @@ if __name__ == "__main__": if not os.path.isfile(args.setup_script_path): raise ValueError(f'Invalid script path: {args.setup_script_path}') - # Check if the repo root directory is a git repository - root_file_dir = os.path.dirname(os.path.abspath(args.setup_script_path)) - is_git_repo = os.path.exists(os.path.join(root_file_dir, ".git")) - - # if we have a git repo then check the resources are downloaded, - # otherwise it's considered a prerequisite to have run - # the set_up_default_resources.py - status = ( - check_update_resources_downloaded( - args.resource_downloaded_dir, args.setup_script_path - ) - if is_git_repo - else 0 - ) + # Check the resources are downloaded as expected + status = check_update_resources_downloaded( + args.resource_downloaded_dir, + args.setup_script_path) sys.exit(status) -- cgit v1.2.1