blob: 44e9bd9ee247d5e3c8fd45a9ac44839a5ea65406 [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.
16import json
17import os
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
21
22
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000023def get_md5sum_for_file(filepath: str) -> str:
24 """
25 Function to calculate md5sum for contents of a given file.
26
27 Parameters:
28 ----------
29 filepath (string): Path to the required file.
30
31 Returns:
32 -------
33 Hex digest represented as string.
34 """
35 md5_sum = hashlib.md5()
36 with open(filepath, mode='rb') as f:
37 buf = f.read()
38 md5_sum.update(buf)
39 return md5_sum.hexdigest()
40
41
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000042def check_update_resources_downloaded(
43 resource_downloaded_dir: str, set_up_script_path: str
44):
45 """
46 Function that check if the resources downloaded need to be refreshed.
47
48 Parameters:
49 ----------
50 resource_downloaded_dir (string): Specifies the path to resources_downloaded folder.
51 set_up_script_path (string): Specifies the path to set_up_default_resources.py file.
52 """
53
54 metadata_file_path = os.path.join(
55 resource_downloaded_dir, "resources_downloaded_metadata.json"
56 )
57
58 if os.path.isfile(metadata_file_path):
59 with open(metadata_file_path) as metadata_json:
60
61 metadata_dict = json.load(metadata_json)
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000062 md5_key = 'set_up_script_md5sum'
63 set_up_script_md5sum_metadata = ''
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000064
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000065 if md5_key in metadata_dict.keys():
66 set_up_script_md5sum_metadata = metadata_dict["set_up_script_md5sum"]
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000067
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000068 set_up_script_md5sum_current = get_md5sum_for_file(set_up_script_path)
69
70 if set_up_script_md5sum_current == set_up_script_md5sum_metadata:
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000071 return 0
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000072
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000073 # Return code 1 if the resources need to be refreshed.
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000074 print('Error: hash mismatch!')
75 print(f'Metadata: {set_up_script_md5sum_metadata}')
76 print(f'Current : {set_up_script_md5sum_current}')
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000077 return 1
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000078
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000079 # Return error code 2 if the file doesn't exists.
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000080 print(f'Error: could not find {metadata_file_path}')
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000081 return 2
82
83
84if __name__ == "__main__":
85 parser = ArgumentParser()
86 parser.add_argument(
Kshitij Sisodia6b9adad2022-03-01 13:42:18 +000087 "--resource_downloaded_dir",
88 help="Resources downloaded directory.",
89 type=str,
90 required=True)
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000091 parser.add_argument(
Kshitij Sisodia6b9adad2022-03-01 13:42:18 +000092 "--setup_script_path",
93 help="Path to set_up_default_resources.py.",
94 type=str,
95 required=True)
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000096 args = parser.parse_args()
97
Kshitij Sisodia6b9adad2022-03-01 13:42:18 +000098 # Check validity of script path
99 if not os.path.isfile(args.setup_script_path):
100 raise ValueError(f'Invalid script path: {args.setup_script_path}')
101
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +0000102 # Check the resources are downloaded as expected
103 status = check_update_resources_downloaded(
104 args.resource_downloaded_dir,
105 args.setup_script_path)
Isabella Gottardief2b9dd2022-02-16 14:24:03 +0000106 sys.exit(status)