mirror of
https://gitee.com/Vancouver2017/luban-lite-t3e-pro.git
synced 2025-12-16 19:38:56 +00:00
v1.0.3
This commit is contained in:
243
packages/third-party/adbd/tools/script/adb_sync.py
vendored
Normal file
243
packages/third-party/adbd/tools/script/adb_sync.py
vendored
Normal file
@@ -0,0 +1,243 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import json
|
||||
import copy
|
||||
import time
|
||||
import hashlib
|
||||
import datetime
|
||||
import subprocess
|
||||
from adb_transfer import adb_push_file
|
||||
from adb_transfer import adb_read_data
|
||||
from adb_transfer import adb_write_data
|
||||
|
||||
loacl_path = ''
|
||||
remote_path = ''
|
||||
adb_exec_path = ''
|
||||
adb_serial_number = ''
|
||||
|
||||
adb_sync_mod_name = 'sync_mod'
|
||||
adb_sync_key = 'path'
|
||||
name = 'name'
|
||||
md5 = 'md5'
|
||||
buff_size = 512
|
||||
|
||||
def get_pc_dir_info(path):
|
||||
result = []
|
||||
paths = os.listdir(path)
|
||||
for i, item in enumerate(paths):
|
||||
sub_path = os.path.join(path, item)
|
||||
if os.path.isdir(sub_path):
|
||||
file_info = {}
|
||||
file_info[name] = path[len(loacl_path) :] + item + '/'
|
||||
file_info[md5] = ''
|
||||
result.append(file_info)
|
||||
result += get_pc_dir_info(sub_path + '/')
|
||||
else:
|
||||
myhash = hashlib.md5()
|
||||
f = open(sub_path,'rb')
|
||||
while True:
|
||||
b = f.read(8096)
|
||||
if not b :
|
||||
break
|
||||
myhash.update(b)
|
||||
f.close()
|
||||
file_info = {}
|
||||
file_info[name] = path[len(loacl_path) :] + item
|
||||
file_info[md5] = str(myhash.hexdigest())
|
||||
result.append(file_info)
|
||||
return result
|
||||
|
||||
def get_dev_dir_info(path):
|
||||
param = {}
|
||||
result = None
|
||||
param[adb_sync_key] = path
|
||||
data = adb_read_data(adb_sync_mod_name, param, adb_path = adb_exec_path, serial_number = adb_serial_number)
|
||||
if data:
|
||||
result = json.loads(data)
|
||||
return result
|
||||
|
||||
def get_sync_info(pc_info, dev_info):
|
||||
sync_info = {}
|
||||
delete_list = []
|
||||
sync_list = []
|
||||
temp = {}
|
||||
|
||||
for item in json_pc:
|
||||
temp[item[name]] = item[md5]
|
||||
for item in dev_info:
|
||||
if not item[name] in list(temp.keys()):
|
||||
delete_list.append(item[name])
|
||||
elif item[md5] == temp[item[name]]:
|
||||
del temp[item[name]]
|
||||
|
||||
for item in list(temp.keys()):
|
||||
sync_list.append(item)
|
||||
|
||||
sync_info['delete'] = delete_list
|
||||
sync_info['sync'] = sync_list
|
||||
|
||||
return sync_info
|
||||
|
||||
def dev_file_delete(path, delete_list):
|
||||
buff = ''
|
||||
curr_len = 0
|
||||
for item in delete_list:
|
||||
if curr_len + len(item) < buff_size - 4:
|
||||
if curr_len == 0:
|
||||
buff = '['
|
||||
buff += '\"' + item + '\"'
|
||||
else:
|
||||
buff += ',' + '\"' + item + '\"'
|
||||
curr_len = len(buff)
|
||||
else:
|
||||
buff += ']'
|
||||
buff += b'\x00' * (buff_size-curr_len)
|
||||
curr_len = 0
|
||||
if curr_len != 0:
|
||||
buff += ']'
|
||||
buff += b'\x00' * (buff_size-curr_len)
|
||||
curr_len = 0
|
||||
param = {}
|
||||
param[adb_sync_key] = path
|
||||
adb_write_data(adb_sync_mod_name, param, buff, adb_path = adb_exec_path, serial_number = adb_serial_number)
|
||||
|
||||
def dev_file_delete_(path, delete_list):
|
||||
success_list = []
|
||||
failure_list = []
|
||||
result = {}
|
||||
for item in delete_list:
|
||||
buff = []
|
||||
buff.append(item)
|
||||
json_str = json.dumps(buff).encode('utf-8') + b'\x00'
|
||||
if len(json_str) > buff_size:
|
||||
print('err! file name is to long')
|
||||
else:
|
||||
param = {}
|
||||
param[adb_sync_key] = path
|
||||
print('delete ' + item)
|
||||
if adb_write_data(adb_sync_mod_name, param, json_str, adb_path = adb_exec_path, serial_number = adb_serial_number) == 'OK':
|
||||
success_list.append(item)
|
||||
else:
|
||||
failure_list.append(item)
|
||||
result['success'] = success_list
|
||||
result['failure'] = failure_list
|
||||
return result
|
||||
|
||||
def dev_file_sync(sync_list):
|
||||
return adb_push_file(loacl_path, remote_path, sync_list, adb_path = adb_exec_path, serial_number = adb_serial_number)
|
||||
|
||||
def list_merge_path(list):
|
||||
i = 0
|
||||
j = 0
|
||||
sort_res = sorted(list,key = lambda i:len(i),reverse=False)
|
||||
while i < len(sort_res):
|
||||
j = i + 1
|
||||
while j < len(sort_res):
|
||||
t1 = sort_res[i]
|
||||
t2 = sort_res[j]
|
||||
if t2.count(t1, 0, len(t1)) > 0:
|
||||
sort_res.pop(j)
|
||||
else:
|
||||
j += 1
|
||||
i += 1
|
||||
return sort_res
|
||||
|
||||
def list_filtration_folders(list):
|
||||
res = []
|
||||
for item in list:
|
||||
if item[-1] != '/':
|
||||
res.append(item)
|
||||
return res
|
||||
|
||||
def file_path_check():
|
||||
global loacl_path
|
||||
global remote_path
|
||||
global adb_exec_path
|
||||
|
||||
loacl_path = loacl_path.replace('\\', '/')
|
||||
remote_path = remote_path.replace('\\', '/')
|
||||
if adb_exec_path != '':
|
||||
adb_exec_path = adb_exec_path.replace('\\', '/')
|
||||
if not os.path.exists(adb_exec_path):
|
||||
print(adb_exec_path + ' adb exec path not exist')
|
||||
return False
|
||||
else:
|
||||
if os.path.isdir(adb_exec_path):
|
||||
if adb_exec_path[-1] != '/':
|
||||
adb_exec_path += '/'
|
||||
if not os.path.exists(adb_exec_path + 'adb.exe'):
|
||||
print(adb_exec_path + 'adb.exe' + ' not exist')
|
||||
return False
|
||||
if loacl_path[-1] != '/':
|
||||
loacl_path = loacl_path + '/'
|
||||
if remote_path[-1] != '/':
|
||||
remote_path = remote_path + '/'
|
||||
|
||||
if not os.path.exists(loacl_path):
|
||||
print(loacl_path + ' folder does not exist')
|
||||
return False
|
||||
return True
|
||||
|
||||
def string_insensitive_sort(str_list):
|
||||
listtemp = [(x.lower(),x) for x in str_list]
|
||||
listtemp.sort()
|
||||
return [x[1] for x in listtemp]
|
||||
|
||||
def user_parameter_parsing(args):
|
||||
global adb_exec_path
|
||||
global adb_serial_number
|
||||
|
||||
for i in range(0, len(args)):
|
||||
if args[i] == '-s':
|
||||
adb_serial_number = args[i+1]
|
||||
elif args[i] == '-p':
|
||||
adb_exec_path = args[i+1]
|
||||
|
||||
if __name__=='__main__':
|
||||
loacl_path = sys.argv[1] if len(sys.argv) > 1 else '.'
|
||||
remote_path = sys.argv[2] if len(sys.argv) > 2 else '/'
|
||||
if len(sys.argv) > 3:
|
||||
user_parameter_parsing(sys.argv[3:])
|
||||
if file_path_check() == False:
|
||||
exit(0)
|
||||
starttime = time.time()
|
||||
print('loacl:' + loacl_path)
|
||||
print('remote:' + remote_path)
|
||||
print('---------------- get dev info ----------------')
|
||||
json_dev = get_dev_dir_info(remote_path)
|
||||
if not isinstance(json_dev, list):
|
||||
exit(0)
|
||||
print('---------------- get pc info ----------------')
|
||||
json_pc = get_pc_dir_info(loacl_path)
|
||||
print('---------------- sync check ----------------')
|
||||
sync_info = get_sync_info(json_pc, json_dev)
|
||||
sync_list = list_filtration_folders(sync_info['sync'])
|
||||
delete_list = list_merge_path(sync_info['delete'])
|
||||
print('---------------- sync file ----------------')
|
||||
sync_list_sort = string_insensitive_sort(sync_list)
|
||||
sync_res = dev_file_sync(sync_list_sort)
|
||||
print('---------------- delete file ----------------')
|
||||
delete_list_sort = string_insensitive_sort(delete_list)
|
||||
delete_res = dev_file_delete_(remote_path, delete_list_sort)
|
||||
sync_try_res = {}
|
||||
if len(sync_res['failure']) > 0:
|
||||
print('---------------- sync retry ----------------')
|
||||
sync_try_res = dev_file_sync(sync_res['failure'])
|
||||
delete_try_res = {}
|
||||
if len(delete_res['failure']) > 0:
|
||||
print('---------------- delete retry ----------------')
|
||||
delete_try_res = dev_file_delete_(delete_res['failure'])
|
||||
print('---------------- sync end ----------------')
|
||||
pc_list = []
|
||||
for item in json_pc:
|
||||
pc_list.append(item[name])
|
||||
all_count = len(list_filtration_folders(sync_info['sync']))
|
||||
delt_count = len(list_filtration_folders(sync_info['delete']))
|
||||
fail_count = len(sync_try_res['failure']) if sync_try_res else 0
|
||||
sync_count = all_count - fail_count
|
||||
skip_count = len(list_filtration_folders(pc_list)) - sync_count
|
||||
endtime = time.time()
|
||||
print('sync:' + str(sync_count) + ' fail:' + str(fail_count) + ' skip:' + str(skip_count) + ' delete:' + str(delt_count) + ' time:' + str(round(endtime - starttime, 2)) + 's')
|
||||
170
packages/third-party/adbd/tools/script/adb_transfer.py
vendored
Normal file
170
packages/third-party/adbd/tools/script/adb_transfer.py
vendored
Normal file
@@ -0,0 +1,170 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
#adb push ./push_test.t "#sync_mod#<208>*path=/adb_sync/*/adb_sync/adb_sync.adb"
|
||||
#adb pull "#sync_mod#<208>*path=/adb_sync/*/adb_sync/adb_sync.adb" ./pull_test.t
|
||||
|
||||
def execute_command(cmdstring, cwd=None, shell=True):
|
||||
"""Execute the system command at the specified address."""
|
||||
|
||||
if shell:
|
||||
cmdstring_list = cmdstring
|
||||
|
||||
sub = subprocess.Popen(cmdstring_list, cwd=cwd, stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE, shell=shell, bufsize=8192)
|
||||
|
||||
stdout_str = ""
|
||||
while sub.poll() is None:
|
||||
stdout_str += str(sub.stdout.read())
|
||||
time.sleep(0.1)
|
||||
|
||||
return stdout_str
|
||||
|
||||
def get_dir_size(dir):
|
||||
size = 0
|
||||
for root, dirs, files in os.walk(dir):
|
||||
size += sum([os.path.getsize(os.path.join(root, name)) for name in files])
|
||||
return size
|
||||
|
||||
def get_transfer_err(adb_out):
|
||||
adb_out = adb_out.split('\n')
|
||||
err_index = -1
|
||||
out_str = None
|
||||
for i in adb_out:
|
||||
err_index = i.find('adb: error:')
|
||||
if err_index >= 0:
|
||||
out_str = i[err_index + len('adb: error:') : ]
|
||||
break
|
||||
return out_str
|
||||
|
||||
def get_cmd(cmd, adb_path, serial_number):
|
||||
if serial_number != '':
|
||||
cmd = '-s ' + serial_number + ' ' + cmd
|
||||
if adb_path == '':
|
||||
return 'adb ' + cmd + ' '
|
||||
else:
|
||||
(adb_file_path, adb_file_name) = os.path.split(adb_path)
|
||||
if adb_file_name == '':
|
||||
return adb_path + 'adb.exe ' + cmd + ' '
|
||||
else:
|
||||
return adb_path + ' ' + cmd + ' '
|
||||
|
||||
def get_push_cmd(adb_path = '', serial_number = ''):
|
||||
return get_cmd('push', adb_path, serial_number)
|
||||
|
||||
def get_pull_cmd(adb_path = '', serial_number = ''):
|
||||
return get_cmd('pull', adb_path, serial_number)
|
||||
|
||||
def param_to_str(modname, parameter):
|
||||
cmdparam = ''
|
||||
if not isinstance(parameter, dict):
|
||||
return None
|
||||
for key in list(parameter.keys()):
|
||||
value = parameter[key]
|
||||
if isinstance(value, dict):
|
||||
continue
|
||||
else:
|
||||
if len(cmdparam) == 0:
|
||||
cmdparam = '#' + modname + '#' + '<' + '>'
|
||||
cmdparam += '*' + str(key) + '=' + str(value)
|
||||
else:
|
||||
cmdparam += ',' + str(key) + '=' + str(value)
|
||||
cmdparam += '*'
|
||||
return cmdparam
|
||||
|
||||
def adb_read_data(modname, parameter, path='/adb_pull.sync', adb_path = '', serial_number = ''):
|
||||
cmdhead = get_pull_cmd(adb_path, serial_number)
|
||||
cmdparam = param_to_str(modname, parameter)
|
||||
filename = tempfile.gettempdir() + '/' + 'abd_temp.sync'
|
||||
if not cmdparam:
|
||||
return None
|
||||
cmdparam = "\"" + cmdparam + path + "\""
|
||||
cmd = cmdhead + ' ' + cmdparam + ' ' + filename
|
||||
#recv data by adb
|
||||
adb_out = str(execute_command(cmd))
|
||||
adb_err = get_transfer_err(adb_out)
|
||||
if adb_err:
|
||||
print('adb read data err:' + adb_err)
|
||||
return None
|
||||
with open(filename, 'r') as fp:
|
||||
data = fp.read()
|
||||
if os.path.exists(filename):
|
||||
os.remove(filename)
|
||||
# print('filename' + filename)
|
||||
# print('cmd' + cmd)
|
||||
# print('adb_out' + adb_out)
|
||||
# print('data:' + data)
|
||||
return data
|
||||
|
||||
def adb_write_data(modname, parameter, data, path='/adb_push.sync', adb_path = '', serial_number = ''):
|
||||
cmdhead = get_push_cmd(adb_path, serial_number)
|
||||
filename = ''
|
||||
cmdparam = param_to_str(modname, parameter)
|
||||
if not cmdparam:
|
||||
return ''
|
||||
cmdparam = "\"" + cmdparam + path + "\""
|
||||
f_temp = tempfile.NamedTemporaryFile(delete=False)
|
||||
filename = f_temp.name
|
||||
f_temp.write(data)
|
||||
f_temp.close()
|
||||
cmd = cmdhead + ' ' + filename + ' ' + cmdparam
|
||||
#send data by adb
|
||||
adb_out = str(execute_command(cmd))
|
||||
adb_err = get_transfer_err(adb_out)
|
||||
if os.path.exists(filename):
|
||||
os.remove(filename)
|
||||
if adb_err:
|
||||
print('adb write data err:' + adb_err)
|
||||
return adb_err
|
||||
else:
|
||||
return 'OK'
|
||||
# print('adb_out' + adb_out)
|
||||
|
||||
def adb_push_file(loacl_path, remote_path, file_list, adb_path = '', serial_number = ''):
|
||||
cmdhead = get_push_cmd(adb_path, serial_number)
|
||||
adb_err = None
|
||||
name_max = 0
|
||||
success_list = []
|
||||
failure_list = []
|
||||
result = {}
|
||||
|
||||
for item in file_list:
|
||||
if len(item) > name_max:
|
||||
name_max = len(item)
|
||||
name_max += 9
|
||||
for item in file_list:
|
||||
starttime = time.time()
|
||||
if item.count(loacl_path, 0, len(loacl_path)):
|
||||
pathname = item[len(loacl_path) : ]
|
||||
else:
|
||||
pathname = item
|
||||
[dirname,filename]=os.path.split(pathname)
|
||||
cmd = cmdhead + ' ' + "\"" + loacl_path + dirname + '/' + filename + "\"" + ' ' + "\"" + remote_path + dirname + '/' + filename + "\""
|
||||
cmd = cmd.replace('\\', '/')
|
||||
cmd = cmd.replace('//', '/')
|
||||
out_str = 'sync ' + dirname + '/' + filename
|
||||
sys.stdout.write(out_str)
|
||||
sys.stdout.flush()
|
||||
adb_out = str(execute_command(cmd))
|
||||
adb_err = get_transfer_err(adb_out)
|
||||
endtime = time.time()
|
||||
if adb_err:
|
||||
out_str = '\r' + 'err! ' + dirname + '/' + filename + adb_err
|
||||
failure_list.append(item)
|
||||
else:
|
||||
fsize = 0
|
||||
if os.path.isdir(loacl_path + dirname + '/' + filename):
|
||||
fsize = get_dir_size(loacl_path + dirname + '/' + filename)
|
||||
else:
|
||||
fsize = os.path.getsize(loacl_path + dirname + '/' + filename)
|
||||
out_str = '\r' + out_str + ' ' * (name_max - len(out_str)) + str(round( fsize / 1024 / ((endtime - starttime)), 1)) + 'KB/s'
|
||||
success_list.append(item)
|
||||
print(out_str)
|
||||
result['success'] = success_list
|
||||
result['failure'] = failure_list
|
||||
return result
|
||||
Reference in New Issue
Block a user