• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
21from .handle_input import HandleInput
22
23TORQ_TEMP_DIR = "/tmp/.torq"
24TEMP_CACHE_BUILDER_SCRIPT = TORQ_TEMP_DIR + "/binary_cache_builder.py"
25SIMPLEPERF_SCRIPTS_DIR = "/system/extras/simpleperf/scripts"
26BUILDER_SCRIPT = SIMPLEPERF_SCRIPTS_DIR + "/binary_cache_builder.py"
27
28def verify_simpleperf_args(args):
29  args.scripts_path = TORQ_TEMP_DIR
30  if ("ANDROID_BUILD_TOP" in os.environ
31      and path_exists(os.environ["ANDROID_BUILD_TOP"] + BUILDER_SCRIPT)):
32    args.scripts_path = (os.environ["ANDROID_BUILD_TOP"]
33                         + SIMPLEPERF_SCRIPTS_DIR)
34
35  if args.symbols is None or not dir_exists(args.symbols):
36    if args.symbols is not None:
37      return None, ValidationError(
38          ("%s is not a valid path." % args.symbols),
39          "Set --symbols to a valid symbols lib path or set "
40          "$ANDROID_PRODUCT_OUT to your android product out directory "
41          "(<ANDROID_BUILD_TOP>/out/target/product/<TARGET>).")
42    if "ANDROID_PRODUCT_OUT" not in os.environ:
43      return None, ValidationError(
44          "ANDROID_PRODUCT_OUT is not set.",
45          "Set --symbols to a valid symbols lib path or set "
46          "$ANDROID_PRODUCT_OUT to your android product out directory "
47          "(<ANDROID_BUILD_TOP>/out/target/product/<TARGET>).")
48    if not dir_exists(os.environ["ANDROID_PRODUCT_OUT"]):
49      return None, ValidationError(
50          ("%s is not a valid $ANDROID_PRODUCT_OUT."
51           % (os.environ["ANDROID_PRODUCT_OUT"])),
52          "Set --symbols to a valid symbols lib path or set "
53          "$ANDROID_PRODUCT_OUT to your android product out directory "
54          "(<ANDROID_BUILD_TOP>/out/target/product/<TARGET>).")
55    args.symbols = os.environ["ANDROID_PRODUCT_OUT"]
56
57  if (args.scripts_path != TORQ_TEMP_DIR or
58      path_exists(TEMP_CACHE_BUILDER_SCRIPT)):
59    return args, None
60
61  error = download_simpleperf_scripts()
62
63  if error is not None:
64    return None, error
65
66  return args, None
67
68def download_simpleperf_scripts():
69  fail_suggestion = ("Set $ANDROID_BUILD_TOP to your android root "
70                     "path and make sure you have $ANDROID_BUILD_TOP"
71                     "/system/extras/simpleperf/scripts "
72                     "downloaded.")
73
74  def download_accepted_callback():
75    subprocess.run(("mkdir -p %s && wget -P %s "
76                    "https://android.googlesource.com/platform/system/extras"
77                    "/+archive/refs/heads/main/simpleperf/scripts.tar.gz "
78                    "&& tar -xvzf %s/scripts.tar.gz -C %s"
79                    % (TORQ_TEMP_DIR, TORQ_TEMP_DIR, TORQ_TEMP_DIR,
80                       TORQ_TEMP_DIR)),
81                   shell=True)
82
83    if not path_exists(TEMP_CACHE_BUILDER_SCRIPT):
84      raise Exception("Error while downloading simpleperf scripts. Try again "
85                      "or set $ANDROID_BUILD_TOP to your android root path and "
86                      "make sure you have $ANDROID_BUILD_TOP/system/extras/"
87                      "simpleperf/scripts downloaded.")
88    return None
89
90  def rejected_callback():
91    return ValidationError("Did not download simpleperf scripts.",
92                           fail_suggestion)
93
94  return (HandleInput(("You do not have an Android Root configured "
95                      "with the simpleperf directory. To use "
96                      "simpleperf, torq will download simpleperf "
97                      "scripts to '%s'. Are you ok with this download?"
98                      " [Y/N]: " % TORQ_TEMP_DIR), fail_suggestion,
99                     {"y": download_accepted_callback,
100                      "n": rejected_callback})
101          .handle_input())
102