blob: 6408f1495642a44cbcde7758f95fa6fe1cf64f33 [file] [log] [blame]
Isabella Gottardief2b9dd2022-02-16 14:24:03 +00001#!/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.
16import json
17import os
18import subprocess
19import sys
20from argparse import ArgumentParser
21
22
23def 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
60if __name__ == "__main__":
61 parser = ArgumentParser()
62 parser.add_argument(
63 "--resource_downloaded_dir", help="Resources downloaded directory.", type=str
64 )
65 parser.add_argument(
66 "--setup_script_path", help="Path to set_up_default_resources.py.", type=str
67 )
68 args = parser.parse_args()
69
70 # Check if the repo root directory is a git repository
71 root_file_dir = os.path.dirname(os.path.abspath(args.setup_script_path))
72 is_git_repo = os.path.exists(os.path.join(root_file_dir, ".git"))
73
74 # if we have a git repo then check the resources are downloaded,
75 # otherwise it's considered a prerequisite to have run
76 # the set_up_default_resources.py
77 status = (
78 check_update_resources_downloaded(
79 args.resource_downloaded_dir, args.setup_script_path
80 )
81 if is_git_repo
82 else 0
83 )
84 sys.exit(status)