-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull_file.py
284 lines (231 loc) · 8.64 KB
/
pull_file.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# -*- coding:UTF-8 -*- # 标识为
import creditutils.exec_cmd as exec_cmd
import sys, re
import os
import subprocess
import shlex
class FilePulling:
_INVALID_FILE_RE_OBJ = re.compile('^\s*$')
_INSTALLED_RE_OBJ = re.compile(u'Success')
_UNINSTALLED_RE_OBJ = re.compile(u'Success')
_INSTALL_FAILED_ALREADY_EXISTS_RE = re.compile(u'INSTALL_FAILED_ALREADY_EXISTS')
_RUN_ADB_FAILED_RE_OBJ = re.compile('^adb server is out of date')
_VALID_DEVICE_LINE = re.compile('^\s*([^\s]+)\s+device')
_ABNORMAL_DEVICE_LINE = re.compile('^\s*([^\s]+)\s+offline')
def __init__(self, src_dir, dst_dir):
self.src_dir = src_dir
self.dst_dir = dst_dir
self.dev_name_list = []
self.is_empty = True
def get_device_info(self):
cmd_str = 'adb devices'
result = exec_cmd.run_cmd_for_output_in_specified_dir(sys.path[0], cmd_str)
lines = result.split('\r\n')[1:]
# 如果出现异常的设备信息,则先处理然后再运行
isAbnormal = self._check_device_lines_abnormality(lines)
if isAbnormal:
self._run_adb_kill_server_cmd()
result = self._run_check_environment_cmd_for_result()
lines = result.split('\r\n')[1:]
self.dev_name_list = []
for line in lines:
isValid, dev_name = self._check_device_line_vality(line)
if isValid:
if dev_name in self.dev_name_list:
msg_info = u'设备存在重复的序列号'
raise ValueError(msg_info)
else:
self.dev_name_list.append(dev_name)
if self.dev_name_list:
return True
else:
msg_info = u'没有可连接的手机设备'
raise ValueError(msg_info)
def check_environment(self):
isReady = False
try:
self.get_device_info()
if len(self.dev_name_list) == 1:
isReady = True
except Exception as e:
isReady = False
print(str(e))
if not isReady:
print('has no device or more than one device')
return isReady
def _check_device_line_abnormality(self, line):
obj_ = FilePulling._ABNORMAL_DEVICE_LINE.match(line)
if obj_:
return True
else:
return False
def _check_device_lines_abnormality(self, lines):
is_abnormal = False
for line in lines:
result = self._check_device_line_abnormality(line)
if result:
is_abnormal = True
break
return is_abnormal
def _check_device_line_vality(self, line):
obj_ = FilePulling._VALID_DEVICE_LINE.match(line)
if obj_:
return True, obj_.group(1)
else:
return False, None
def _check_file_vality(self, src_file):
match_obj = FilePulling._INVALID_FILE_RE_OBJ.match(src_file)
if not match_obj:
return True
else:
return False
def _run_adb_kill_server_cmd(self):
try:
cmd_str = 'adb kill-server'
result = exec_cmd.run_cmd_for_stdout_ignores_exit_code(cmd_str)
info_format = 'run {0}, result: {1}'
info = info_format.format(cmd_str, result)
print(info)
except Exception as e: # 其它可能的出错情况,当前暂不清楚会在什么条件下出现
print(str(e))
def __call__(self, src_file, print_flag=True):
if not self._check_file_vality(src_file):
raise Exception('file name "' + src_file + '" is invalid')
if self.check_environment():
cmd_info = 'adb pull {0}/{1} {2}'.format(self.src_dir, src_file, self.dst_dir)
if print_flag:
print(cmd_info)
try:
result = subprocess.check_output(cmd_info, shell=True, universal_newlines=True)
if print_flag:
print(result)
except subprocess.CalledProcessError as e:
print((str(e)))
return False
return True
else:
return False
def get_all_src_files(self):
args = list()
args.append('adb')
args.append('shell')
ls_cmd_str = 'cd {0} && ls'.format(self.src_dir)
args.append(ls_cmd_str)
try:
result = subprocess.check_output(args, shell=True, universal_newlines=True)
except subprocess.CalledProcessError as e:
print((str(e)))
return []
return re.split('[\r\n]+', result)
def pull_all_files(self, print_flag=True):
self.is_empty = True
content = self.get_all_src_files()
# print(content)
for line in content:
src_file = line.strip()
if len(src_file) == 0:
break
self.is_empty = False
self.__call__(src_file, print_flag)
if self.is_empty:
print('no file to pull')
def remove_src_file(self, src_file, print_flag=True):
if not self._check_file_vality(src_file):
raise Exception('file name "' + src_file + '" is invalid')
# cmd_info = 'adb shell "cd {0} && rm {1}"'.format(self.src_dir, src_file)
args = []
args.append('adb')
args.append('shell')
rm_cmd_str = 'cd {0} && rm {1}'.format(self.src_dir, src_file)
args.append(rm_cmd_str)
if print_flag:
print(args)
try:
result = subprocess.check_output(args, shell=True, universal_newlines=True)
if print_flag:
print(result)
except subprocess.CalledProcessError as e:
print((str(e)))
def remove_all_src_files(self, print_flag=True):
# cmd_info = 'adb shell "cd {0} && rm *"'.format(src_dir)
if self.is_empty:
return
args = []
args.append('adb')
args.append('shell')
ls_cmd_str = 'cd {0} && rm *'.format(self.src_dir)
args.append(ls_cmd_str)
if print_flag:
print(args)
try:
result = subprocess.check_output(args, shell=True, universal_newlines=True)
if print_flag:
print(result)
except subprocess.CalledProcessError as e:
print((str(e)))
def cmd_exec_test():
args = []
args.append('adb')
args.append('shell')
# src_dir = '/sdcard/tiny/crash'
src_dir = '/sdcard/crash'
ls_cmd_str = 'cd {0} && ls'.format(src_dir)
args.append(ls_cmd_str)
try:
result = subprocess.check_output(args, shell=True, universal_newlines=True)
# print(args)
print(result)
except subprocess.CalledProcessError as e:
print((str(e)))
return []
return result.split('\n')
if __name__ == '__main__':
req_cnt_1 = 3
req_cnt_2 = 4
del_flag = 1
cnt = len(sys.argv)
if cnt != req_cnt_1 and cnt != req_cnt_2:
str_format = 'argument count should be {} or {}, but current count is {}'
print(str_format.format(req_cnt_1, req_cnt_2, cnt))
exit(1)
# else:
# print(sys.argv)
src_path = sys.argv[1]
dst_dir = os.path.abspath(sys.argv[2])
to_del = False
if cnt == req_cnt_2:
del_para = int(sys.argv[3])
if del_para == del_flag:
to_del = True
else:
to_del = False
file_flag = False
if src_path[-1] == '*':
file_flag = False
src_dir = os.path.dirname(src_path)
else:
file_flag = True
src_dir = os.path.dirname(src_path)
to_pull = os.path.basename(src_path)
file_pulling = FilePulling(src_dir, dst_dir)
if not file_pulling.check_environment():
exit(1)
if file_flag:
# 该部分代码用于从文件中读取文件列表
# content = myfile.read_file_lines(to_pull)
# for line in content:
# src_file = line.strip()
# if len(src_file) == 0:
# continue
# file_pulling(src_file)
# file_pulling.remove_src_file(src_file)
# 直接拉取单个文件
file_pulling(to_pull)
if to_del:
file_pulling.remove_src_file(to_pull)
else:
file_pulling.pull_all_files()
if to_del:
file_pulling.remove_all_src_files()
# cmd_exec_test()
exit(0)