blob: bdd9d62d414708d1bb257f9bab97fb6aa89c35e9 [file] [log] [blame]
Isabella Gottardief2b9dd2022-02-16 14:24:03 +00001#!/usr/bin/env python3
Alex Tawsedaba3cf2023-09-29 15:55:38 +01002# SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
Isabella Gottardief2b9dd2022-02-16 14:24:03 +00003# 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.
Alex Tawsedaba3cf2023-09-29 15:55:38 +010016"""
17Contains methods to check if the downloaded resources need to be refreshed
18"""
19import hashlib
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000020import json
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000021import sys
Alex Tawsedaba3cf2023-09-29 15:55:38 +010022import typing
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000023from argparse import ArgumentParser
Richard Burton17069622022-03-17 10:54:26 +000024from pathlib import Path
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000025
26
Alex Tawsedaba3cf2023-09-29 15:55:38 +010027def get_md5sum_for_file(
28 filepath: typing.Union[str, Path]
29) -> str:
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000030 """
31 Function to calculate md5sum for contents of a given file.
32
33 Parameters:
34 ----------
35 filepath (string): Path to the required file.
36
37 Returns:
38 -------
39 Hex digest represented as string.
40 """
41 md5_sum = hashlib.md5()
42 with open(filepath, mode='rb') as f:
43 buf = f.read()
44 md5_sum.update(buf)
45 return md5_sum.hexdigest()
46
47
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000048def check_update_resources_downloaded(
Alex Tawsedaba3cf2023-09-29 15:55:38 +010049 resource_downloaded_dir: str, set_up_script_path: str
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000050):
51 """
52 Function that check if the resources downloaded need to be refreshed.
53
54 Parameters:
55 ----------
56 resource_downloaded_dir (string): Specifies the path to resources_downloaded folder.
57 set_up_script_path (string): Specifies the path to set_up_default_resources.py file.
58 """
59
Richard Burton17069622022-03-17 10:54:26 +000060 metadata_file_path = Path(resource_downloaded_dir) / "resources_downloaded_metadata.json"
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000061
Richard Burton17069622022-03-17 10:54:26 +000062 if metadata_file_path.is_file():
Alex Tawsedaba3cf2023-09-29 15:55:38 +010063 with open(metadata_file_path, encoding="utf8") as metadata_json:
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000064 metadata_dict = json.load(metadata_json)
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000065
Alex Tawsedaba3cf2023-09-29 15:55:38 +010066 md5_key = 'set_up_script_md5sum'
67 set_up_script_md5sum_metadata = ''
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000068
Alex Tawsedaba3cf2023-09-29 15:55:38 +010069 if md5_key in metadata_dict.keys():
70 set_up_script_md5sum_metadata = metadata_dict["set_up_script_md5sum"]
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000071
Alex Tawsedaba3cf2023-09-29 15:55:38 +010072 set_up_script_md5sum_current = get_md5sum_for_file(set_up_script_path)
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000073
Alex Tawsedaba3cf2023-09-29 15:55:38 +010074 if set_up_script_md5sum_current == set_up_script_md5sum_metadata:
75 return 0
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000076
Alex Tawsedaba3cf2023-09-29 15:55:38 +010077 # Return code 1 if the resources need to be refreshed.
78 print('Error: hash mismatch!')
79 print(f'Metadata: {set_up_script_md5sum_metadata}')
80 print(f'Current : {set_up_script_md5sum_current}')
81 return 1
82
83 # Return error code 2 if the file doesn't exist.
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +000084 print(f'Error: could not find {metadata_file_path}')
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000085 return 2
86
87
88if __name__ == "__main__":
89 parser = ArgumentParser()
90 parser.add_argument(
Kshitij Sisodia6b9adad2022-03-01 13:42:18 +000091 "--resource_downloaded_dir",
92 help="Resources downloaded directory.",
93 type=str,
94 required=True)
Isabella Gottardief2b9dd2022-02-16 14:24:03 +000095 parser.add_argument(
Kshitij Sisodia6b9adad2022-03-01 13:42:18 +000096 "--setup_script_path",
97 help="Path to set_up_default_resources.py.",
98 type=str,
99 required=True)
Isabella Gottardief2b9dd2022-02-16 14:24:03 +0000100 args = parser.parse_args()
101
Richard Burton17069622022-03-17 10:54:26 +0000102 # Check validity of script path.
103 if not Path(args.setup_script_path).is_file():
Kshitij Sisodia6b9adad2022-03-01 13:42:18 +0000104 raise ValueError(f'Invalid script path: {args.setup_script_path}')
105
Kshitij Sisodia6a2ac462022-03-01 17:36:06 +0000106 # Check the resources are downloaded as expected
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100107 STATUS = check_update_resources_downloaded(
108 args.resource_downloaded_dir,
109 args.setup_script_path
110 )
111 sys.exit(STATUS)