blob: 021f1b17f8ab3d039dc28b2c01dd93cc1f153dce [file] [log] [blame]
Isabella Gottardief2b9dd2022-02-16 14:24:03 +00001#!/usr/bin/env python3
2# Copyright (c) 2022 Arm Limited. All rights reserved.
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Richard Burton17069622022-03-17 10:54:26 +000016
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000017import json
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000018import sys
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000019import hashlib
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000020from argparse import ArgumentParser
Richard Burton17069622022-03-17 10:54:26 +000021from pathlib import Path
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000022
23
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000024def get_md5sum_for_file(filepath: str) -> str:
25 """
26 Function to calculate md5sum for contents of a given file.
27
28 Parameters:
29 ----------
30 filepath (string): Path to the required file.
31
32 Returns:
33 -------
34 Hex digest represented as string.
35 """
36 md5_sum = hashlib.md5()
37 with open(filepath, mode='rb') as f:
38 buf = f.read()
39 md5_sum.update(buf)
40 return md5_sum.hexdigest()
41
42
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000043def check_update_resources_downloaded(
44 resource_downloaded_dir: str, set_up_script_path: str
45):
46 """
47 Function that check if the resources downloaded need to be refreshed.
48
49 Parameters:
50 ----------
51 resource_downloaded_dir (string): Specifies the path to resources_downloaded folder.
52 set_up_script_path (string): Specifies the path to set_up_default_resources.py file.
53 """
54
Richard Burton17069622022-03-17 10:54:26 +000055 metadata_file_path = Path(resource_downloaded_dir) / "resources_downloaded_metadata.json"
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000056
Richard Burton17069622022-03-17 10:54:26 +000057 if metadata_file_path.is_file():
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000058 with open(metadata_file_path) as metadata_json:
59
60 metadata_dict = json.load(metadata_json)
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000061 md5_key = 'set_up_script_md5sum'
62 set_up_script_md5sum_metadata = ''
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000063
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000064 if md5_key in metadata_dict.keys():
65 set_up_script_md5sum_metadata = metadata_dict["set_up_script_md5sum"]
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000066
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000067 set_up_script_md5sum_current = get_md5sum_for_file(set_up_script_path)
68
69 if set_up_script_md5sum_current == set_up_script_md5sum_metadata:
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000070 return 0
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000071
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000072 # Return code 1 if the resources need to be refreshed.
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000073 print('Error: hash mismatch!')
74 print(f'Metadata: {set_up_script_md5sum_metadata}')
75 print(f'Current : {set_up_script_md5sum_current}')
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000076 return 1
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000077
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000078 # Return error code 2 if the file doesn't exists.
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000079 print(f'Error: could not find {metadata_file_path}')
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000080 return 2
81
82
83if __name__ == "__main__":
84 parser = ArgumentParser()
85 parser.add_argument(
Kshitij Sisodia6b9adad2022-03-01 13:42:18 +000086 "--resource_downloaded_dir",
87 help="Resources downloaded directory.",
88 type=str,
89 required=True)
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000090 parser.add_argument(
Kshitij Sisodia6b9adad2022-03-01 13:42:18 +000091 "--setup_script_path",
92 help="Path to set_up_default_resources.py.",
93 type=str,
94 required=True)
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000095 args = parser.parse_args()
96
Richard Burton17069622022-03-17 10:54:26 +000097 # Check validity of script path.
98 if not Path(args.setup_script_path).is_file():
Kshitij Sisodia6b9adad2022-03-01 13:42:18 +000099 raise ValueError(f'Invalid script path: {args.setup_script_path}')
100
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +0000101 # Check the resources are downloaded as expected
102 status = check_update_resources_downloaded(
103 args.resource_downloaded_dir,
104 args.setup_script_path)
Isabella Gottardief2b9dd2022-02-16 14:24:03 +0000105 sys.exit(status)