Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 1 | #!/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 Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 16 | |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 17 | import json |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 18 | import sys |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 19 | import hashlib |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 20 | from argparse import ArgumentParser |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 21 | from pathlib import Path |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 22 | |
| 23 | |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 24 | def 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 Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 43 | def 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 Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 55 | metadata_file_path = Path(resource_downloaded_dir) / "resources_downloaded_metadata.json" |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 56 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 57 | if metadata_file_path.is_file(): |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 58 | with open(metadata_file_path) as metadata_json: |
| 59 | |
| 60 | metadata_dict = json.load(metadata_json) |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 61 | md5_key = 'set_up_script_md5sum' |
| 62 | set_up_script_md5sum_metadata = '' |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 63 | |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 64 | if md5_key in metadata_dict.keys(): |
| 65 | set_up_script_md5sum_metadata = metadata_dict["set_up_script_md5sum"] |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 66 | |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 67 | 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 Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 70 | return 0 |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 71 | |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 72 | # Return code 1 if the resources need to be refreshed. |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 73 | print('Error: hash mismatch!') |
| 74 | print(f'Metadata: {set_up_script_md5sum_metadata}') |
| 75 | print(f'Current : {set_up_script_md5sum_current}') |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 76 | return 1 |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 77 | |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 78 | # Return error code 2 if the file doesn't exists. |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 79 | print(f'Error: could not find {metadata_file_path}') |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 80 | return 2 |
| 81 | |
| 82 | |
| 83 | if __name__ == "__main__": |
| 84 | parser = ArgumentParser() |
| 85 | parser.add_argument( |
Kshitij Sisodia | 6b9adad | 2022-03-01 13:42:18 +0000 | [diff] [blame] | 86 | "--resource_downloaded_dir", |
| 87 | help="Resources downloaded directory.", |
| 88 | type=str, |
| 89 | required=True) |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 90 | parser.add_argument( |
Kshitij Sisodia | 6b9adad | 2022-03-01 13:42:18 +0000 | [diff] [blame] | 91 | "--setup_script_path", |
| 92 | help="Path to set_up_default_resources.py.", |
| 93 | type=str, |
| 94 | required=True) |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 95 | args = parser.parse_args() |
| 96 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 97 | # Check validity of script path. |
| 98 | if not Path(args.setup_script_path).is_file(): |
Kshitij Sisodia | 6b9adad | 2022-03-01 13:42:18 +0000 | [diff] [blame] | 99 | raise ValueError(f'Invalid script path: {args.setup_script_path}') |
| 100 | |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 101 | # Check the resources are downloaded as expected |
| 102 | status = check_update_resources_downloaded( |
| 103 | args.resource_downloaded_dir, |
| 104 | args.setup_script_path) |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 105 | sys.exit(status) |