Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 2 | # SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 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. |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 16 | """ |
| 17 | Contains methods to check if the downloaded resources need to be refreshed |
| 18 | """ |
| 19 | import hashlib |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 20 | import json |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 21 | import sys |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 22 | import typing |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 23 | from argparse import ArgumentParser |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 24 | from pathlib import Path |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 25 | |
| 26 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 27 | def get_md5sum_for_file( |
| 28 | filepath: typing.Union[str, Path] |
| 29 | ) -> str: |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 30 | """ |
| 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 Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 48 | def check_update_resources_downloaded( |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 49 | resource_downloaded_dir: str, set_up_script_path: str |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 50 | ): |
| 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 Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 60 | metadata_file_path = Path(resource_downloaded_dir) / "resources_downloaded_metadata.json" |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 61 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 62 | if metadata_file_path.is_file(): |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 63 | with open(metadata_file_path, encoding="utf8") as metadata_json: |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 64 | metadata_dict = json.load(metadata_json) |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 65 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 66 | md5_key = 'set_up_script_md5sum' |
| 67 | set_up_script_md5sum_metadata = '' |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 68 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 69 | if md5_key in metadata_dict.keys(): |
| 70 | set_up_script_md5sum_metadata = metadata_dict["set_up_script_md5sum"] |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 71 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 72 | set_up_script_md5sum_current = get_md5sum_for_file(set_up_script_path) |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 73 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 74 | if set_up_script_md5sum_current == set_up_script_md5sum_metadata: |
| 75 | return 0 |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 76 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 77 | # 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 Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 84 | print(f'Error: could not find {metadata_file_path}') |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 85 | return 2 |
| 86 | |
| 87 | |
| 88 | if __name__ == "__main__": |
| 89 | parser = ArgumentParser() |
| 90 | parser.add_argument( |
Kshitij Sisodia | 6b9adad | 2022-03-01 13:42:18 +0000 | [diff] [blame] | 91 | "--resource_downloaded_dir", |
| 92 | help="Resources downloaded directory.", |
| 93 | type=str, |
| 94 | required=True) |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 95 | parser.add_argument( |
Kshitij Sisodia | 6b9adad | 2022-03-01 13:42:18 +0000 | [diff] [blame] | 96 | "--setup_script_path", |
| 97 | help="Path to set_up_default_resources.py.", |
| 98 | type=str, |
| 99 | required=True) |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 100 | args = parser.parse_args() |
| 101 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 102 | # Check validity of script path. |
| 103 | if not Path(args.setup_script_path).is_file(): |
Kshitij Sisodia | 6b9adad | 2022-03-01 13:42:18 +0000 | [diff] [blame] | 104 | raise ValueError(f'Invalid script path: {args.setup_script_path}') |
| 105 | |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 106 | # Check the resources are downloaded as expected |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 107 | STATUS = check_update_resources_downloaded( |
| 108 | args.resource_downloaded_dir, |
| 109 | args.setup_script_path |
| 110 | ) |
| 111 | sys.exit(STATUS) |