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 |
| 18 | import subprocess |
| 19 | import sys |
| 20 | from argparse import ArgumentParser |
| 21 | |
| 22 | |
| 23 | def check_update_resources_downloaded( |
| 24 | resource_downloaded_dir: str, set_up_script_path: str |
| 25 | ): |
| 26 | """ |
| 27 | Function that check if the resources downloaded need to be refreshed. |
| 28 | |
| 29 | Parameters: |
| 30 | ---------- |
| 31 | resource_downloaded_dir (string): Specifies the path to resources_downloaded folder. |
| 32 | set_up_script_path (string): Specifies the path to set_up_default_resources.py file. |
| 33 | """ |
| 34 | |
| 35 | metadata_file_path = os.path.join( |
| 36 | resource_downloaded_dir, "resources_downloaded_metadata.json" |
| 37 | ) |
| 38 | |
| 39 | if os.path.isfile(metadata_file_path): |
| 40 | with open(metadata_file_path) as metadata_json: |
| 41 | |
| 42 | metadata_dict = json.load(metadata_json) |
| 43 | set_up_script_hash = metadata_dict["set_up_script_hash"] |
| 44 | command = f"git log -1 --pretty=tformat:%H {set_up_script_path}" |
| 45 | |
| 46 | proc = subprocess.run( |
| 47 | command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True |
| 48 | ) |
| 49 | git_commit_hash = proc.stdout.decode("utf-8").strip("\n") |
| 50 | proc.check_returncode() |
| 51 | |
| 52 | if set_up_script_hash == git_commit_hash: |
| 53 | return 0 |
| 54 | # Return code 1 if the resources need to be refreshed. |
| 55 | return 1 |
| 56 | # Return error code 2 if the file doesn't exists. |
| 57 | return 2 |
| 58 | |
| 59 | |
| 60 | if __name__ == "__main__": |
| 61 | parser = ArgumentParser() |
| 62 | parser.add_argument( |
Kshitij Sisodia | 6b9adad | 2022-03-01 13:42:18 +0000 | [diff] [blame^] | 63 | "--resource_downloaded_dir", |
| 64 | help="Resources downloaded directory.", |
| 65 | type=str, |
| 66 | required=True) |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 67 | parser.add_argument( |
Kshitij Sisodia | 6b9adad | 2022-03-01 13:42:18 +0000 | [diff] [blame^] | 68 | "--setup_script_path", |
| 69 | help="Path to set_up_default_resources.py.", |
| 70 | type=str, |
| 71 | required=True) |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 72 | args = parser.parse_args() |
| 73 | |
Kshitij Sisodia | 6b9adad | 2022-03-01 13:42:18 +0000 | [diff] [blame^] | 74 | # Check validity of script path |
| 75 | if not os.path.isfile(args.setup_script_path): |
| 76 | raise ValueError(f'Invalid script path: {args.setup_script_path}') |
| 77 | |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 78 | # Check if the repo root directory is a git repository |
| 79 | root_file_dir = os.path.dirname(os.path.abspath(args.setup_script_path)) |
| 80 | is_git_repo = os.path.exists(os.path.join(root_file_dir, ".git")) |
| 81 | |
| 82 | # if we have a git repo then check the resources are downloaded, |
| 83 | # otherwise it's considered a prerequisite to have run |
| 84 | # the set_up_default_resources.py |
| 85 | status = ( |
| 86 | check_update_resources_downloaded( |
| 87 | args.resource_downloaded_dir, args.setup_script_path |
| 88 | ) |
| 89 | if is_git_repo |
| 90 | else 0 |
| 91 | ) |
| 92 | sys.exit(status) |