Skip to content

Commit 209a1aa

Browse files
author
Yuvraj Raghuvanshi
committed
FIxes: #110 #99
1 parent ab3d123 commit 209a1aa

File tree

6 files changed

+818
-587
lines changed

6 files changed

+818
-587
lines changed

helpers/device_serial_id.py

Lines changed: 81 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,124 +5,148 @@
55
from custom_ci import custom_input, custom_print
66

77

8-
def init(mode, tcp_ip='', tcp_port=''):
8+
def init(mode, tcp_ip="", tcp_port=""):
99
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+
)
1113
# Detect OS
1214
is_windows = False
13-
if platform.system() == 'Windows':
15+
if platform.system() == "Windows":
1416
is_windows = True
1517

1618
# Global command line helpers
1719
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, ".."))
1921

20-
if(is_windows):
21-
adb = f'{root_dir}\\bin\\adb.exe'
22+
if is_windows:
23+
adb = f"{root_dir}\\bin\\adb.exe"
2224
else:
23-
adb = 'adb'
25+
adb = "adb"
2426

2527
# 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}"
3537
else:
3638
pass
3739
# 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+
)
4043
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")
4346

4447
if len(output) == 0 or error:
4548
output = None
46-
custom_print(error, 'red')
49+
custom_print(error, "red")
4750
kill_me()
4851

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]
5154

52-
if(len(output) == 1):
55+
if len(output) == 1:
5356
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 ""
5660

5761
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":
6064
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+
)
6268
kill_me()
63-
if(output[1].split()[1] == 'unauthorized'):
69+
if output[1].split()[1] == "unauthorized":
6470
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+
)
6674
kill_me()
6775
return output[1].split()[0]
6876

6977
custom_print(output[0])
70-
custom_print('\n', is_get_time=False)
78+
custom_print("\n", is_get_time=False)
7179
if device_to_connect is None:
7280
padding = f' {" " * 25}'
7381
for index, device in enumerate(output[1:]):
7482
serial = device.split()[0]
7583
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+
)
8096

8197
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): "))
8499
if device_index <= 0 or device_index + 1 > len(output):
85100
continue
86101
device_to_connect = output[device_index]
87102

88-
if(device_to_connect.split()[1] == 'offline'):
103+
if device_to_connect.split()[1] == "offline":
89104
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+
)
91108
kill_me()
92-
if(device_to_connect.split()[1] == 'unauthorized'):
109+
if device_to_connect.split()[1] == "unauthorized":
93110
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+
)
95114
kill_me()
96115
return device_to_connect.split()[0]
97116

98-
elif mode == 'TCP':
117+
elif mode == "TCP":
99118
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):
101120
return combo
102-
if('authenticate' in (x.lower() for x in output)):
121+
if "authenticate" in (x.lower() for x in output):
103122
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+
)
105126
kill_me()
106-
if('refused' in (x.lower() for x in output)):
127+
if "refused" in (x.lower() for x in output):
107128
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
111134
['connected', 'to', '192.168.43.130:5555']
112135
['failed', 'to', 'authenticate', 'to', '192.168.43.130:5555']
113136
['cannot', 'connect', 'to', '192.168.43.130:5555:', 'No', 'connection', 'could', 'be', 'made', 'because', 'the', 'target', 'machine', 'actively', 'refused', 'it.', '(10061)']
114-
'''
137+
"""
115138
else:
116139
pass
117140
# FIXME: Wrong choice.
118141

119142

120143
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...")
121147
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")
128152
quit()

0 commit comments

Comments
 (0)