Lalit Maganti | d7031a9 | 2023-09-26 12:50:01 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright (C) 2023 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 | import argparse |
| 17 | import os |
| 18 | import re |
| 19 | import sys |
| 20 | import tempfile |
| 21 | import time |
| 22 | |
| 23 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 24 | sys.path.append(os.path.join(ROOT_DIR)) |
| 25 | |
| 26 | from python.perfetto.prebuilts.perfetto_prebuilts import * |
| 27 | |
| 28 | PERMSISION_REGEX = re.compile(r'''uses-permission: name='(.*)'.*''') |
| 29 | NAME_REGEX = re.compile(r'''package: name='(.*?)' .*''') |
| 30 | |
| 31 | |
| 32 | def cmd(args: list[str]): |
| 33 | print('Running command ' + ' '.join(args)) |
| 34 | return subprocess.check_output(args) |
| 35 | |
| 36 | |
| 37 | def main(): |
| 38 | parser = argparse.ArgumentParser() |
| 39 | parser.add_argument('--apk', help='Local APK to use instead of builtin') |
| 40 | |
| 41 | args = parser.parse_args() |
| 42 | |
| 43 | if args.apk: |
| 44 | apk = args.apk |
| 45 | else: |
| 46 | apk = download_or_get_cached( |
| 47 | 'CtsPerfettoReporterApp.apk', |
| 48 | 'https://storage.googleapis.com/perfetto/CtsPerfettoReporterApp.apk', |
| 49 | 'f21dda36668c368793500b13724ab2a6231d12ded05746f7cfaaba4adedd7d46') |
| 50 | |
| 51 | # Figure out the package name and the permissions we need |
| 52 | aapt = subprocess.check_output(['aapt', 'dump', 'badging', apk]).decode() |
| 53 | permission_names = [] |
| 54 | name = '' |
| 55 | for l in aapt.splitlines(): |
| 56 | name_match = NAME_REGEX.match(l) |
| 57 | if name_match: |
| 58 | name = name_match[1] |
| 59 | continue |
| 60 | |
| 61 | permission_match = PERMSISION_REGEX.match(l) |
| 62 | if permission_match: |
| 63 | permission_names.append(permission_match[1]) |
| 64 | continue |
| 65 | |
| 66 | # Root and remount the device. |
| 67 | cmd(['adb', 'root']) |
| 68 | cmd(['adb', 'wait-for-device']) |
| 69 | cmd(['adb', 'remount', '-R']) |
| 70 | input('The device might now reboot. If so, please unlock the device then ' |
| 71 | 'press enter to continue') |
| 72 | cmd(['adb', 'wait-for-device']) |
| 73 | cmd(['adb', 'root']) |
| 74 | cmd(['adb', 'wait-for-device']) |
| 75 | cmd(['adb', 'remount', '-R']) |
| 76 | |
| 77 | # Write out the permission file on device. |
| 78 | permissions = '\n'.join( |
| 79 | f'''<permission name='{p}' />''' for p in permission_names) |
| 80 | permission_file_contents = f''' |
| 81 | <permissions> |
| 82 | <privapp-permissions package="{name}"> |
| 83 | {permissions} |
| 84 | </privapp-permissions> |
| 85 | </permissions> |
| 86 | ''' |
| 87 | with tempfile.NamedTemporaryFile() as f: |
| 88 | f.write(permission_file_contents.encode()) |
| 89 | f.flush() |
| 90 | |
| 91 | cmd([ |
| 92 | 'adb', 'push', f.name, |
| 93 | f'/system/etc/permissions/privapp-permissions-{name}.xml' |
| 94 | ]) |
| 95 | |
| 96 | # Stop system_server, push the apk on device and restart system_server |
| 97 | priv_app_path = f'/system/priv-app/{name}/{name}.apk' |
| 98 | cmd(['adb', 'shell', 'stop']) |
| 99 | cmd(['adb', 'push', apk, priv_app_path]) |
| 100 | cmd(['adb', 'shell', 'start']) |
| 101 | cmd(['adb', 'wait-for-device']) |
| 102 | time.sleep(10) |
| 103 | |
| 104 | # Wait for system_server and package manager to come up. |
| 105 | while 'system_server' not in cmd(['adb', 'shell', 'ps']).decode(): |
| 106 | time.sleep(1) |
| 107 | while True: |
| 108 | ps = set([ |
| 109 | l.strip() |
| 110 | for l in cmd(['adb', 'shell', 'dumpsys', '-l']).decode().splitlines() |
| 111 | ]) |
| 112 | if 'storaged' in ps and 'settings' in ps and 'package' in ps: |
| 113 | break |
| 114 | time.sleep(1) |
| 115 | |
| 116 | # Install the actual APK. |
| 117 | cmd(['adb', 'shell', 'pm', 'install', '-r', '-d', '-g', '-t', priv_app_path]) |
| 118 | |
| 119 | return 0 |
| 120 | |
| 121 | |
| 122 | sys.exit(main()) |