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. |
| 16 | import json |
| 17 | import os |
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 |
| 21 | |
| 22 | |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame^] | 23 | def 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 Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 42 | def 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 Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame^] | 62 | md5_key = 'set_up_script_md5sum' |
| 63 | set_up_script_md5sum_metadata = '' |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 64 | |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame^] | 65 | if md5_key in metadata_dict.keys(): |
| 66 | set_up_script_md5sum_metadata = metadata_dict["set_up_script_md5sum"] |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 67 | |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame^] | 68 | 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 Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 71 | return 0 |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame^] | 72 | |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 73 | # Return code 1 if the resources need to be refreshed. |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame^] | 74 | print('Error: hash mismatch!') |
| 75 | print(f'Metadata: {set_up_script_md5sum_metadata}') |
| 76 | print(f'Current : {set_up_script_md5sum_current}') |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 77 | return 1 |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame^] | 78 | |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 79 | # Return error code 2 if the file doesn't exists. |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame^] | 80 | print(f'Error: could not find {metadata_file_path}') |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 81 | return 2 |
| 82 | |
| 83 | |
| 84 | if __name__ == "__main__": |
| 85 | parser = ArgumentParser() |
| 86 | parser.add_argument( |
Kshitij Sisodia | 6b9adad | 2022-03-01 13:42:18 +0000 | [diff] [blame] | 87 | "--resource_downloaded_dir", |
| 88 | help="Resources downloaded directory.", |
| 89 | type=str, |
| 90 | required=True) |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 91 | parser.add_argument( |
Kshitij Sisodia | 6b9adad | 2022-03-01 13:42:18 +0000 | [diff] [blame] | 92 | "--setup_script_path", |
| 93 | help="Path to set_up_default_resources.py.", |
| 94 | type=str, |
| 95 | required=True) |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 96 | args = parser.parse_args() |
| 97 | |
Kshitij Sisodia | 6b9adad | 2022-03-01 13:42:18 +0000 | [diff] [blame] | 98 | # 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 Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame^] | 102 | # Check the resources are downloaded as expected |
| 103 | status = check_update_resources_downloaded( |
| 104 | args.resource_downloaded_dir, |
| 105 | args.setup_script_path) |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 106 | sys.exit(status) |