Debian Codegraph @

infra/bots/recipe_modules/flavor/resources/wait_for_device.py

raw
1 # Copyright 2025 Google LLC
2 #
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 import argparse
7 import subprocess
8 import time
9
10
11 def run(*cmd, capture_output=False):
12 print('==================================')
13 print(' '.join(cmd))
14 print('----------------------------------')
15 result = subprocess.run(cmd, check=True, capture_output=capture_output, timeout=180)
16 print('==================================')
17 return result
18
19
20 def adb_connect(adb, device):
21 while True:
22 result = run(adb, 'connect', device, capture_output=True)
23 if result.returncode == 0 and 'unable to connect' not in result.stdout.decode():
24 break
25 time.sleep(1)
26
27 def main():
28 parser = argparse.ArgumentParser(
29 prog='wait_for_device.py',
30 description='Wait for an Android device to connect.')
31 parser.add_argument('--adb', default='adb')
32 parser.add_argument('--connect-to')
33 args = parser.parse_args()
34
35 if args.connect_to:
36 adb_connect(args.adb, args.connect_to)
37
38 run(args.adb, 'wait-for-device', 'shell',
39 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done')
40
41
42 if __name__ == '__main__':
43 main()