|
5 | 5 | from custom_ci import custom_input, custom_print |
6 | 6 |
|
7 | 7 |
|
8 | | -def init(mode, tcp_ip='', tcp_port=''): |
| 8 | +def init(mode, tcp_ip="", tcp_port=""): |
9 | 9 | custom_print( |
10 | | - f'>>> I am in device_serial_id.init({mode=!s}, {tcp_ip=!s}, {tcp_port=!s})', is_print=False) |
| 10 | + f">>> I am in device_serial_id.init(mode={mode}, tcp_ip{tcp_ip}, tcp_port={tcp_port})", |
| 11 | + is_print=False, |
| 12 | + ) |
11 | 13 | # Detect OS |
12 | 14 | is_windows = False |
13 | | - if platform.system() == 'Windows': |
| 15 | + if platform.system() == "Windows": |
14 | 16 | is_windows = True |
15 | 17 |
|
16 | 18 | # Global command line helpers |
17 | 19 | curr_dir = os.path.dirname(os.path.realpath(__file__)) |
18 | | - root_dir = os.path.abspath(os.path.join(curr_dir, '..')) |
| 20 | + root_dir = os.path.abspath(os.path.join(curr_dir, "..")) |
19 | 21 |
|
20 | | - if(is_windows): |
21 | | - adb = f'{root_dir}\\bin\\adb.exe' |
| 22 | + if is_windows: |
| 23 | + adb = f"{root_dir}\\bin\\adb.exe" |
22 | 24 | else: |
23 | | - adb = 'adb' |
| 25 | + adb = "adb" |
24 | 26 |
|
25 | 27 | # Kill server before getting list to avoid daemon texts. |
26 | | - os.system(f'{adb} kill-server') |
27 | | - os.system(f'{adb} start-server') |
28 | | - |
29 | | - combo = f'{tcp_ip}:{tcp_port}' |
30 | | - cmd = '' |
31 | | - if mode == 'USB': |
32 | | - cmd = f'{adb} devices' |
33 | | - elif mode == 'TCP': |
34 | | - cmd = f'{adb} connect {combo}' |
| 28 | + os.system(f"{adb} kill-server") |
| 29 | + os.system(f"{adb} start-server") |
| 30 | + |
| 31 | + combo = f"{tcp_ip}:{tcp_port}" |
| 32 | + cmd = "" |
| 33 | + if mode == "USB": |
| 34 | + cmd = f"{adb} devices" |
| 35 | + elif mode == "TCP": |
| 36 | + cmd = f"{adb} connect {combo}" |
35 | 37 | else: |
36 | 38 | pass |
37 | 39 | # FIXME: Wrong choice. |
38 | | - proc = sp.Popen(cmd.split(), stdin=sp.PIPE, stdout=sp.PIPE, |
39 | | - stderr=sp.PIPE, shell=False) |
| 40 | + proc = sp.Popen( |
| 41 | + cmd.split(), stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, shell=False |
| 42 | + ) |
40 | 43 | output, error = proc.communicate() |
41 | | - output = output.decode('utf-8') |
42 | | - error = error.decode('utf-8') |
| 44 | + output = output.decode("utf-8") |
| 45 | + error = error.decode("utf-8") |
43 | 46 |
|
44 | 47 | if len(output) == 0 or error: |
45 | 48 | output = None |
46 | | - custom_print(error, 'red') |
| 49 | + custom_print(error, "red") |
47 | 50 | kill_me() |
48 | 51 |
|
49 | | - if mode == 'USB': |
50 | | - output = [x.strip() for x in output.split('\n') if len(x.strip()) > 0] |
| 52 | + if mode == "USB": |
| 53 | + output = [x.strip() for x in output.split("\n") if len(x.strip()) > 0] |
51 | 54 |
|
52 | | - if(len(output) == 1): |
| 55 | + if len(output) == 1: |
53 | 56 | custom_print( |
54 | | - 'Could not find any connected device. Is USB Debugging on?', 'red') |
55 | | - return '' |
| 57 | + "Could not find any connected device. Is USB Debugging on?", "red" |
| 58 | + ) |
| 59 | + return "" |
56 | 60 |
|
57 | 61 | device_to_connect = None |
58 | | - if(len(output) == 2): |
59 | | - if(output[1].split()[1] == 'offline'): |
| 62 | + if len(output) == 2: |
| 63 | + if output[1].split()[1] == "offline": |
60 | 64 | custom_print( |
61 | | - 'Device is offline, try turning off USB debugging and turn on again.', 'yellow') |
| 65 | + "Device is offline, try turning off USB debugging and turn on again.", |
| 66 | + "yellow", |
| 67 | + ) |
62 | 68 | kill_me() |
63 | | - if(output[1].split()[1] == 'unauthorized'): |
| 69 | + if output[1].split()[1] == "unauthorized": |
64 | 70 | custom_print( |
65 | | - 'Device unauthorized. Please check the confirmation dialog on your device.', 'red') |
| 71 | + "Device unauthorized. Please check the confirmation dialog on your device.", |
| 72 | + "red", |
| 73 | + ) |
66 | 74 | kill_me() |
67 | 75 | return output[1].split()[0] |
68 | 76 |
|
69 | 77 | custom_print(output[0]) |
70 | | - custom_print('\n', is_get_time=False) |
| 78 | + custom_print("\n", is_get_time=False) |
71 | 79 | if device_to_connect is None: |
72 | 80 | padding = f' {" " * 25}' |
73 | 81 | for index, device in enumerate(output[1:]): |
74 | 82 | serial = device.split()[0] |
75 | 83 | state = device.split()[1] |
76 | | - name = 'Unknown' if state == 'unauthorized' else sp.getoutput( |
77 | | - f'{adb} -s {device.split()[0]} shell getprop ro.product.model').strip() |
78 | | - custom_print('{}. {:.15s} {:.15s} {}' |
79 | | - .format(index + 1, serial + padding, state + padding, name)) |
| 84 | + name = ( |
| 85 | + "Unknown" |
| 86 | + if state == "unauthorized" |
| 87 | + else sp.getoutput( |
| 88 | + f"{adb} -s {device.split()[0]} shell getprop ro.product.model" |
| 89 | + ).strip() |
| 90 | + ) |
| 91 | + custom_print( |
| 92 | + "{}. {:.15s} {:.15s} {}".format( |
| 93 | + index + 1, serial + padding, state + padding, name |
| 94 | + ) |
| 95 | + ) |
80 | 96 |
|
81 | 97 | while device_to_connect is None: |
82 | | - device_index = int(custom_input( |
83 | | - 'Enter device number (for ex: 2): ')) |
| 98 | + device_index = int(custom_input("Enter device number (for ex: 2): ")) |
84 | 99 | if device_index <= 0 or device_index + 1 > len(output): |
85 | 100 | continue |
86 | 101 | device_to_connect = output[device_index] |
87 | 102 |
|
88 | | - if(device_to_connect.split()[1] == 'offline'): |
| 103 | + if device_to_connect.split()[1] == "offline": |
89 | 104 | custom_print( |
90 | | - 'Device is offline, try turning off USB debugging and turn on again.', 'yellow') |
| 105 | + "Device is offline, try turning off USB debugging and turn on again.", |
| 106 | + "yellow", |
| 107 | + ) |
91 | 108 | kill_me() |
92 | | - if(device_to_connect.split()[1] == 'unauthorized'): |
| 109 | + if device_to_connect.split()[1] == "unauthorized": |
93 | 110 | custom_print( |
94 | | - 'Device unauthorized. Please check the confirmation dialog on your device.', 'red') |
| 111 | + "Device unauthorized. Please check the confirmation dialog on your device.", |
| 112 | + "red", |
| 113 | + ) |
95 | 114 | kill_me() |
96 | 115 | return device_to_connect.split()[0] |
97 | 116 |
|
98 | | - elif mode == 'TCP': |
| 117 | + elif mode == "TCP": |
99 | 118 | output = [x.strip() for x in output.split() if len(x.strip()) > 0] |
100 | | - if('connected' in (x.lower() for x in output)): |
| 119 | + if "connected" in (x.lower() for x in output): |
101 | 120 | return combo |
102 | | - if('authenticate' in (x.lower() for x in output)): |
| 121 | + if "authenticate" in (x.lower() for x in output): |
103 | 122 | custom_print( |
104 | | - 'Device unauthorized. Please check the confirmation dialog on your device.', 'red') |
| 123 | + "Device unauthorized. Please check the confirmation dialog on your device.", |
| 124 | + "red", |
| 125 | + ) |
105 | 126 | kill_me() |
106 | | - if('refused' in (x.lower() for x in output)): |
| 127 | + if "refused" in (x.lower() for x in output): |
107 | 128 | custom_print( |
108 | | - 'Could not find any connected device. Either USB Debugging is off or device is not running ADB over TCP', 'red') |
109 | | - return '' |
110 | | - ''' Possible outputs |
| 129 | + "Could not find any connected device. Either USB Debugging is off or device is not running ADB over TCP", |
| 130 | + "red", |
| 131 | + ) |
| 132 | + return "" |
| 133 | + """ Possible outputs |
111 | 134 | ['connected', 'to', '192.168.43.130:5555'] |
112 | 135 | ['failed', 'to', 'authenticate', 'to', '192.168.43.130:5555'] |
113 | 136 | ['cannot', 'connect', 'to', '192.168.43.130:5555:', 'No', 'connection', 'could', 'be', 'made', 'because', 'the', 'target', 'machine', 'actively', 'refused', 'it.', '(10061)'] |
114 | | - ''' |
| 137 | + """ |
115 | 138 | else: |
116 | 139 | pass |
117 | 140 | # FIXME: Wrong choice. |
118 | 141 |
|
119 | 142 |
|
120 | 143 | def kill_me(): |
| 144 | + custom_print(">>> I am in device_serial_id.kill_me()", is_print=False) |
| 145 | + custom_print("\n", is_get_time=False) |
| 146 | + custom_print("Exiting...") |
121 | 147 | custom_print( |
122 | | - '>>> I am in device_serial_id.kill_me()', is_print=False) |
123 | | - custom_print('\n', is_get_time=False) |
124 | | - custom_print('Exiting...') |
125 | | - custom_print( |
126 | | - 'Turn off USB debugging [and USB debugging (Security Settings)] if you\'re done.', 'cyan') |
127 | | - custom_input('Hit \"Enter\" key to continue....', 'cyan') |
| 148 | + "Turn off USB debugging [and USB debugging (Security Settings)] if you're done.", |
| 149 | + "cyan", |
| 150 | + ) |
| 151 | + custom_input('Hit "Enter" key to continue....', "cyan") |
128 | 152 | quit() |
0 commit comments