1# 2# Copyright (C) 2024 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# 16 17import os 18import subprocess 19from utils import path_exists, dir_exists 20from validation_error import ValidationError 21 22TORQ_TEMP_DIR = "/tmp/.torq" 23TEMP_CACHE_BUILDER_SCRIPT = TORQ_TEMP_DIR + "/binary_cache_builder.py" 24SIMPLEPERF_SCRIPTS_DIR = "/system/extras/simpleperf/scripts" 25BUILDER_SCRIPT = SIMPLEPERF_SCRIPTS_DIR + "/binary_cache_builder.py" 26 27def verify_simpleperf_args(args): 28 args.scripts_path = TORQ_TEMP_DIR 29 if ("ANDROID_BUILD_TOP" in os.environ 30 and path_exists(os.environ["ANDROID_BUILD_TOP"] + BUILDER_SCRIPT)): 31 args.scripts_path = (os.environ["ANDROID_BUILD_TOP"] 32 + SIMPLEPERF_SCRIPTS_DIR) 33 34 if args.symbols is None or not dir_exists(args.symbols): 35 if args.symbols is not None: 36 return None, ValidationError( 37 ("%s is not a valid path." % args.symbols), 38 "Set --symbols to a valid symbols lib path or set " 39 "$ANDROID_PRODUCT_OUT to your android product out directory " 40 "(<ANDROID_BUILD_TOP>/out/target/product/<TARGET>).") 41 if "ANDROID_PRODUCT_OUT" not in os.environ: 42 return None, ValidationError( 43 "ANDROID_PRODUCT_OUT is not set.", 44 "Set --symbols to a valid symbols lib path or set " 45 "$ANDROID_PRODUCT_OUT to your android product out directory " 46 "(<ANDROID_BUILD_TOP>/out/target/product/<TARGET>).") 47 if not dir_exists(os.environ["ANDROID_PRODUCT_OUT"]): 48 return None, ValidationError( 49 ("%s is not a valid $ANDROID_PRODUCT_OUT." 50 % (os.environ["ANDROID_PRODUCT_OUT"])), 51 "Set --symbols to a valid symbols lib path or set " 52 "$ANDROID_PRODUCT_OUT to your android product out directory " 53 "(<ANDROID_BUILD_TOP>/out/target/product/<TARGET>).") 54 args.symbols = os.environ["ANDROID_PRODUCT_OUT"] 55 56 if (args.scripts_path != TORQ_TEMP_DIR or 57 path_exists(TEMP_CACHE_BUILDER_SCRIPT)): 58 return args, None 59 60 error = download_simpleperf_scripts() 61 62 if error is not None: 63 return None, error 64 65 return args, None 66 67def download_simpleperf_scripts(): 68 i = 0 69 while i <= 3: 70 i += 1 71 confirmation = input("You do not have an Android Root configured with " 72 "the simpleperf directory. To use simpleperf, torq " 73 "will download simpleperf scripts to '%s'. " 74 "Are you ok with this download? [Y/N]: " 75 % TORQ_TEMP_DIR) 76 77 if confirmation.lower() == "y": 78 break 79 elif confirmation.lower() == "n": 80 return ValidationError("Did not download simpleperf scripts.", 81 "Set $ANDROID_BUILD_TOP to your android root " 82 "path and make sure you have $ANDROID_BUILD_TOP" 83 "/system/extras/simpleperf/scripts " 84 "downloaded.") 85 if i == 3: 86 return ValidationError("Invalid inputs.", 87 "Set $ANDROID_BUILD_TOP to your android root " 88 "path and make sure you have $ANDROID_BUILD_TOP" 89 "/system/extras/simpleperf/scripts " 90 "downloaded.") 91 92 subprocess.run(("mkdir -p %s && wget -P %s " 93 "https://android.googlesource.com/platform/system/extras" 94 "/+archive/refs/heads/main/simpleperf/scripts.tar.gz " 95 "&& tar -xvzf %s/scripts.tar.gz -C %s" 96 % (TORQ_TEMP_DIR, TORQ_TEMP_DIR, TORQ_TEMP_DIR, 97 TORQ_TEMP_DIR)), 98 shell=True) 99 100 if not path_exists(TEMP_CACHE_BUILDER_SCRIPT): 101 raise Exception("Error while downloading simpleperf scripts. Try again " 102 "or set $ANDROID_BUILD_TOP to your android root path and " 103 "make sure you have $ANDROID_BUILD_TOP/system/extras/" 104 "simpleperf/scripts downloaded.") 105 106 return None 107