-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyadb.py
147 lines (140 loc) · 5.52 KB
/
pyadb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import urllib.request
import zipfile
import os
import json
import re
with open('config.json', 'r') as f:
config = json.load(f)
url = 'https://dl.google.com/android/repository/platform-tools_r34.0.3-windows.zip?hl=zh-cn'
def download_adb():
# 检查adb目录是否存在,如果不存在则创建它
if not os.path.exists('adb'):
os.makedirs('adb')
# 下载文件
urllib.request.urlretrieve(url, 'platform-tools.zip')
# 解压缩文件
with zipfile.ZipFile('platform-tools.zip', 'r') as zip_ref:
zip_ref.extractall('adb')
# 删除zip文件
os.remove('platform-tools.zip')
project_path = os.path.abspath(os.path.dirname(__file__))
adb_path = os.path.join(project_path, 'adb\\platform-tools\\adb.exe')
config['adb_path'] = adb_path
with open('config.json', 'w') as f:
json.dump(config, f)
print('ADB已安装完成并写入config')
return adb_path
def get_bluestacks_adb_port():
# 读取bluestacks.conf文件
bluestacks_adb_port_keys = config.get('bluestacks_adb_port_keys', [])
bluestacks_conf_path = config.get('bluestacks_conf_path', 'C:/ProgramData/BlueStacks_nxt_cn/bluestacks.conf')
if not os.path.exists(bluestacks_conf_path):
print(f'Error: 文件"{bluestacks_conf_path}"不存在')
else:
with open(bluestacks_conf_path, 'r') as f:
conf = f.read()
# 使用正则表达式匹配adb端口号
for key in bluestacks_adb_port_keys:
match = re.search(rf'{key}="(\d+)"', conf)
if match:
adb_port = match.group(1)
return adb_port
return None
def check_device_connection():
with open('config.json', 'r') as f:
config = json.load(f)
adb_path = config['adb_path']
# 检查是否已连接设备
device = None # 初始化device变量为None
output = os.popen(f'{adb_path} devices').read().strip().split('\n')
if len(output) <= 1 or output[0] != 'List of devices attached':
print('Error: 无设备,尝试连接adb_address')
if 'adb_address' in config and config['adb_address']:
device = config['adb_address']
os.system(f'{adb_path} connect {device}')
output = os.popen(f'{adb_path} devices').read().strip().split('\n')
if len(output) <= 1 or output[0] != 'List of devices attached':
print('Error: 无法连接adb_address,尝试连接蓝叠')
blurestack=connect_bluestack()
if not blurestack:
return None
else:
return blurestack
else:
print(f'已连接设备:{device}')
device_id = device
config['device_id'] = device_id
return device
else:
print('Error:adb_address为空,尝试连接蓝叠')
blurestack=connect_bluestack()
if not blurestack:
return None
else:
return blurestack
else:
for line in output[1:]:
if not line.endswith('\tdevice'):
continue
elif line.endswith('\tdevice'):
device =line.split('\t')[0]
break
if not device:
print('Error: 无设备,尝试连接adb_address')
if 'adb_address' in config and config['adb_address']:
device = config['adb_address']
os.system(f'{adb_path} connect {device}')
output = os.popen(f'{adb_path} devices').read().strip().split('\n')
if len(output) <= 1 or output[0] != 'List of devices attached':
print('Error: 无法连接adb_address,尝试连接蓝叠')
blurestack=connect_bluestack()
if not blurestack:
return None
else:
return blurestack
else:
print(f'已连接设备:{device}')
device_id = device
config['device_id'] = device_id
return device
else:
print('Error:adb_address为空,尝试连接蓝叠')
blurestack=connect_bluestack()
if not blurestack:
return None
else:
return blurestack
else:
print(f'已连接设备:{device}')
return device
def connect_bluestack():
adb_path = config['adb_path']
device = '127.0.0.1:' + str(get_bluestacks_adb_port())
os.system(f'{adb_path} connect {device}')
output = os.popen(f'{adb_path} devices').read().strip().split('\n')
if len(output) <= 1 or output[0] != 'List of devices attached':
print('Error: 无法连接蓝叠(列表为空))')
return None
else:
config['adb_address'] = device
print(f'已连接设备:{device}')
device_id = device
config['device_id'] = device_id
return device
def is_adb_connected():
# 检查adb路径是否存在
if 'adb_path' in config and os.path.exists(config['adb_path']):
adb_path = config['adb_path']
else:
result = os.system('adb version')
if result == 0:
adb_path = 'adb'
else:
adb_path = download_adb()
# 检查adb是否存在
result = os.system(f'{adb_path} version')
if result != 0:
print('Error: adb不在PATH中或未安装')
else:
device = check_device_connection()
return device